Merge pull request #107 from strongloop/feature/local-storage

Local storage support for memory connector
This commit is contained in:
Ritchie Martori 2014-05-14 13:44:12 -07:00
commit db33808502
3 changed files with 50 additions and 13 deletions

View File

@ -71,13 +71,33 @@ function deserialize(dbObj) {
Memory.prototype.loadFromFile = function(callback) {
var self = this;
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
var localStorage = hasLocalStorage && this.settings.localStorage;
if (self.settings.file) {
fs.readFile(self.settings.file, {encoding: 'utf8', flag: 'r'}, function (err, data) {
if (err && err.code !== 'ENOENT') {
callback && callback(err);
} else {
parseAndLoad(data);
}
});
} else if(localStorage) {
var data = window.localStorage.getItem(localStorage);
data = data || '{}';
parseAndLoad(data);
} else {
process.nextTick(callback);
}
function parseAndLoad(data) {
if (data) {
try {
data = JSON.parse(data.toString());
} catch(e) {
return callback(e);
}
self.ids = data.ids || {};
self.cache = data.models || {};
} else {
@ -88,10 +108,6 @@ Memory.prototype.loadFromFile = function(callback) {
}
callback && callback();
}
});
} else {
process.nextTick(callback);
}
};
/*!
@ -100,7 +116,10 @@ Memory.prototype.loadFromFile = function(callback) {
*/
Memory.prototype.saveToFile = function (result, callback) {
var self = this;
if (this.settings.file) {
var file = this.settings.file;
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
var localStorage = hasLocalStorage && this.settings.localStorage;
if (file) {
if(!self.writeQueue) {
// Create a queue for writes
self.writeQueue = async.queue(function (task, cb) {
@ -121,6 +140,16 @@ Memory.prototype.saveToFile = function (result, callback) {
data: result,
callback: callback
});
} else if (localStorage) {
// Flush out the models/ids
var data = JSON.stringify({
ids: self.ids,
models: self.cache
}, null, ' ');
window.localStorage.setItem(localStorage, data);
process.nextTick(function () {
callback && callback(null, result);
});
} else {
process.nextTick(function () {
callback && callback(null, result);

View File

@ -1,3 +1,4 @@
/*!
* Module exports class Model
*/
@ -240,6 +241,9 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
obj = data;
}
callback(err, obj);
if(!err) {
Model.emit('changed', inst);
}
});
} else {
this.findById(getIdValue(this, data), function (err, inst) {

View File

@ -246,6 +246,9 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
var self = this;
var Model = this.constructor;
// if it is already an Object
if(Model === Object) return self;
var strict = this.__strict;
var schemaLess = (strict === false) || !onlySchema;
@ -256,6 +259,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
if (typeof self[propertyName] === 'function') {
return;
}
if (self[propertyName] instanceof List) {
data[propertyName] = self[propertyName].toObject(!schemaLess, removeHidden);
} else if (self.__data.hasOwnProperty(propertyName)) {