Merge pull request #107 from strongloop/feature/local-storage
Local storage support for memory connector
This commit is contained in:
commit
db33808502
|
@ -71,13 +71,33 @@ function deserialize(dbObj) {
|
||||||
|
|
||||||
Memory.prototype.loadFromFile = function(callback) {
|
Memory.prototype.loadFromFile = function(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
|
||||||
|
var localStorage = hasLocalStorage && this.settings.localStorage;
|
||||||
|
|
||||||
if (self.settings.file) {
|
if (self.settings.file) {
|
||||||
fs.readFile(self.settings.file, {encoding: 'utf8', flag: 'r'}, function (err, data) {
|
fs.readFile(self.settings.file, {encoding: 'utf8', flag: 'r'}, function (err, data) {
|
||||||
if (err && err.code !== 'ENOENT') {
|
if (err && err.code !== 'ENOENT') {
|
||||||
callback && callback(err);
|
callback && callback(err);
|
||||||
} else {
|
} 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) {
|
if (data) {
|
||||||
|
try {
|
||||||
data = JSON.parse(data.toString());
|
data = JSON.parse(data.toString());
|
||||||
|
} catch(e) {
|
||||||
|
return callback(e);
|
||||||
|
}
|
||||||
|
|
||||||
self.ids = data.ids || {};
|
self.ids = data.ids || {};
|
||||||
self.cache = data.models || {};
|
self.cache = data.models || {};
|
||||||
} else {
|
} else {
|
||||||
|
@ -88,10 +108,6 @@ Memory.prototype.loadFromFile = function(callback) {
|
||||||
}
|
}
|
||||||
callback && callback();
|
callback && callback();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} else {
|
|
||||||
process.nextTick(callback);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -100,7 +116,10 @@ Memory.prototype.loadFromFile = function(callback) {
|
||||||
*/
|
*/
|
||||||
Memory.prototype.saveToFile = function (result, callback) {
|
Memory.prototype.saveToFile = function (result, callback) {
|
||||||
var self = this;
|
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) {
|
if(!self.writeQueue) {
|
||||||
// Create a queue for writes
|
// Create a queue for writes
|
||||||
self.writeQueue = async.queue(function (task, cb) {
|
self.writeQueue = async.queue(function (task, cb) {
|
||||||
|
@ -121,6 +140,16 @@ Memory.prototype.saveToFile = function (result, callback) {
|
||||||
data: result,
|
data: result,
|
||||||
callback: callback
|
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 {
|
} else {
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
callback && callback(null, result);
|
callback && callback(null, result);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Module exports class Model
|
* Module exports class Model
|
||||||
*/
|
*/
|
||||||
|
@ -240,6 +241,9 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
|
||||||
obj = data;
|
obj = data;
|
||||||
}
|
}
|
||||||
callback(err, obj);
|
callback(err, obj);
|
||||||
|
if(!err) {
|
||||||
|
Model.emit('changed', inst);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.findById(getIdValue(this, data), function (err, inst) {
|
this.findById(getIdValue(this, data), function (err, inst) {
|
||||||
|
|
|
@ -246,6 +246,9 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var Model = this.constructor;
|
var Model = this.constructor;
|
||||||
|
|
||||||
|
// if it is already an Object
|
||||||
|
if(Model === Object) return self;
|
||||||
|
|
||||||
var strict = this.__strict;
|
var strict = this.__strict;
|
||||||
var schemaLess = (strict === false) || !onlySchema;
|
var schemaLess = (strict === false) || !onlySchema;
|
||||||
|
|
||||||
|
@ -256,6 +259,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
||||||
if (typeof self[propertyName] === 'function') {
|
if (typeof self[propertyName] === 'function') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self[propertyName] instanceof List) {
|
if (self[propertyName] instanceof List) {
|
||||||
data[propertyName] = self[propertyName].toObject(!schemaLess, removeHidden);
|
data[propertyName] = self[propertyName].toObject(!schemaLess, removeHidden);
|
||||||
} else if (self.__data.hasOwnProperty(propertyName)) {
|
} else if (self.__data.hasOwnProperty(propertyName)) {
|
||||||
|
|
Loading…
Reference in New Issue