Skip to content

Commit

Permalink
Add Model assignment to LocalDbs, fixes #126
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed May 28, 2015
1 parent 7ed12b3 commit 111a694
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
17 changes: 12 additions & 5 deletions db/IndexedDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ define([
// The identity to use to lookup the object
// options: Object?
// returns: dojo//Deferred

return this._callOnStore('get',[id]);
var store = this;
return this._callOnStore('get',[id]).then(function (object) {
return store._restore(object);
});
},

getIdentity: function (object) {
Expand All @@ -254,7 +256,11 @@ define([

options = options || {};
this.cachedCount = {}; // clear the count cache
return this._callOnStore(options.overwrite === false ? 'add' : 'put',[object]);
var store = this;
return this._callOnStore(options.overwrite === false ? 'add' : 'put',[object])
.then(function (object) {
return store._restore(object);
});
},

add: function (object, options) {
Expand Down Expand Up @@ -580,15 +586,16 @@ define([
_fetch: function (callback, fetchOptions) {
var query = this._normalizeQuery();
var filterQuery = query.filter;
var store = this;
if (filterQuery instanceof Array || filterQuery.then) {
return this._union(query, function (object) {
callback(object);
callback(store._restore(object));
// keep going
return true;
}, fetchOptions);
}
return this._query(query, function (object) {
callback(object);
callback(store._restore(object));
// keep going
return true;
}, fetchOptions);
Expand Down
3 changes: 2 additions & 1 deletion db/LocalStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ function(declare, Memory) {
// load all the data from the local storage
var data = [];
var prefix = this.prefix = this.dbPrefix + '-' + this.storeName + '-';
var store = this;
for (var i = 0, l = localStorage.length; i < l; i++) {
var key = localStorage.key(i);
if (key.slice(0, prefix.length) === prefix) {
data.push(JSON.parse(localStorage.getItem(key)));
data.push(store._restore(JSON.parse(localStorage.getItem(key))));
}
}
this.setData(data);
Expand Down
6 changes: 4 additions & 2 deletions db/SQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ define([
selectColumns: ['*'],
get: function (id) {
// basic get() operation, query by id property
var store = this;
return when(this.executeSql('SELECT ' + this.selectColumns.join(',') + ' FROM ' +
this.table + ' WHERE ' + this.idProperty + '=?', [id]), function (result) {
return result.rows.length > 0 ? convertExtra(result.rows.item(0)) : undefined;
return result.rows.length > 0 ? store._restore(convertExtra(result.rows.item(0))) : undefined;
});
},
getIdentity: function (object) {
Expand Down Expand Up @@ -388,11 +389,12 @@ define([
if (options.start) {
limitedCondition += ' OFFSET ' + options.start;
}
var store = this;
var results = lang.delegate(this.executeSql(limitedCondition, params).then(function(sqlResults) {
// get the results back and do any conversions on it
var results = [];
for (var i = 0; i < sqlResults.rows.length; i++) {
results.push(convertExtra(sqlResults.rows.item(i)));
results.push(store._restore(convertExtra(sqlResults.rows.item(i))));
}
if (querier) {
results = querier(results);
Expand Down
11 changes: 10 additions & 1 deletion tests/LocalDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,21 @@ define([
'reload db': function () {
// reload the DB store and make sure the data is still there
dbConfig.openRequest = null;
numberStore = new DB({dbConfig: dbConfig, storeName: 'test'});
function Model() {}
Model.prototype.method = function () {
return true;
};
numberStore = new DB({
dbConfig: dbConfig,
storeName: 'test',
Model: Model
});
return numberStore.get(1).then(function(one) {
assert.strictEqual(one.id, 1);
assert.strictEqual(one.name, 'one');
assert.strictEqual(one.prime, false);
assert.strictEqual(one.mappedTo, 'E');
assert.isTrue(one.method());
return all([
testQuery(new Filter().gte('name', 's').lte('name', 'u'), {sort:[{property: 'id'}]}, [3, 6])(),
testQuery(new Filter().contains('words', [new Filter().match('words', /^orange/)]), {multi: true}, [3, 6])()
Expand Down

0 comments on commit 111a694

Please sign in to comment.