Merge pull request #67 from strongloop/feature/memory-persistence

Add an option for the memory connector to persist model instances
This commit is contained in:
Raymond Feng 2014-01-29 17:18:00 -08:00
commit e65d21dcdb
4 changed files with 174 additions and 15 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ docs/html
docs/man docs/man
npm-debug.log npm-debug.log
.project .project
test/memory.json

View File

@ -98,6 +98,7 @@ Connector.prototype.defineProperty = function (model, propertyName, propertyDefi
*/ */
Connector.prototype.disconnect = function disconnect(cb) { Connector.prototype.disconnect = function disconnect(cb) {
// NO-OP // NO-OP
cb && process.nextTick(cb);
}; };
/** /**

View File

@ -2,6 +2,8 @@ var util = require('util');
var Connector = require('../connector'); var Connector = require('../connector');
var geo = require('../geo'); var geo = require('../geo');
var utils = require('../utils'); var utils = require('../utils');
var fs = require('fs');
var async = require('async');
/** /**
* Initialize the Oracle connector against the given data source * Initialize the Oracle connector against the given data source
@ -10,24 +12,24 @@ var utils = require('../utils');
* @param {Function} [callback] The callback function * @param {Function} [callback] The callback function
*/ */
exports.initialize = function initializeDataSource(dataSource, callback) { exports.initialize = function initializeDataSource(dataSource, callback) {
dataSource.connector = new Memory(); dataSource.connector = new Memory(null, dataSource.settings);
dataSource.connector.connect(callback); dataSource.connector.connect(callback);
}; };
exports.Memory = Memory; exports.Memory = Memory;
function Memory(m) { function Memory(m, settings) {
if (m) { if (m instanceof Memory) {
this.isTransaction = true; this.isTransaction = true;
this.cache = m.cache; this.cache = m.cache;
this.ids = m.ids; this.ids = m.ids;
this.constructor.super_.call(this, 'memory'); this.constructor.super_.call(this, 'memory', settings);
this._models = m._models; this._models = m._models;
} else { } else {
this.isTransaction = false; this.isTransaction = false;
this.cache = {}; this.cache = {};
this.ids = {}; this.ids = {};
this.constructor.super_.call(this, 'memory'); this.constructor.super_.call(this, 'memory', settings);
} }
} }
@ -36,16 +38,77 @@ util.inherits(Memory, Connector);
Memory.prototype.connect = function (callback) { Memory.prototype.connect = function (callback) {
if (this.isTransaction) { if (this.isTransaction) {
this.onTransactionExec = callback; this.onTransactionExec = callback;
} else {
this.loadFromFile(callback);
}
};
Memory.prototype.loadFromFile = function(callback) {
var self = this;
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 {
if (data) {
data = JSON.parse(data.toString());
self.ids = data.ids || {};
self.cache = data.models || {};
} else {
if(!self.cache) {
self.ids = {};
self.cache = {};
}
}
callback && callback();
}
});
} else { } else {
process.nextTick(callback); process.nextTick(callback);
} }
}; };
/*!
* Flush the cache into the json file if necessary
* @param {Function} callback
*/
Memory.prototype.saveToFile = function (result, callback) {
var self = this;
if (this.settings.file) {
if(!self.writeQueue) {
// Create a queue for writes
self.writeQueue = async.queue(function (task, cb) {
// Flush out the models/ids
var data = JSON.stringify({
ids: self.ids,
models: self.cache
}, null, ' ');
fs.writeFile(self.settings.file, data, function (err) {
cb(err);
task.callback && task.callback(err, task.data);
});
}, 1);
}
// Enqueue the write
self.writeQueue.push({
data: result,
callback: callback
});
} else {
process.nextTick(function () {
callback && callback(null, result);
});
}
};
Memory.prototype.define = function defineModel(definition) { Memory.prototype.define = function defineModel(definition) {
this.constructor.super_.prototype.define.apply(this, [].slice.call(arguments)); this.constructor.super_.prototype.define.apply(this, [].slice.call(arguments));
var m = definition.model.modelName; var m = definition.model.modelName;
this.cache[m] = {}; if(!this.cache[m]) {
this.ids[m] = 1; this.cache[m] = {};
this.ids[m] = 1;
}
}; };
Memory.prototype.create = function create(model, data, callback) { Memory.prototype.create = function create(model, data, callback) {
@ -68,10 +131,11 @@ Memory.prototype.create = function create(model, data, callback) {
var idName = this.idName(model); var idName = this.idName(model);
id = (props[idName] && props[idName].type && props[idName].type(id)) || id; id = (props[idName] && props[idName].type && props[idName].type(id)) || id;
this.setIdValue(model, data, id); this.setIdValue(model, data, id);
if(!this.cache[model]) {
this.cache[model] = {};
}
this.cache[model][id] = JSON.stringify(data); this.cache[model][id] = JSON.stringify(data);
process.nextTick(function () { this.saveToFile(id, callback);
callback(null, id);
});
}; };
Memory.prototype.updateOrCreate = function (model, data, callback) { Memory.prototype.updateOrCreate = function (model, data, callback) {
@ -90,9 +154,7 @@ Memory.prototype.updateOrCreate = function (model, data, callback) {
Memory.prototype.save = function save(model, data, callback) { Memory.prototype.save = function save(model, data, callback) {
this.cache[model][this.getIdValue(model, data)] = JSON.stringify(data); this.cache[model][this.getIdValue(model, data)] = JSON.stringify(data);
process.nextTick(function () { this.saveToFile(data, callback);
callback(null, data);
});
}; };
Memory.prototype.exists = function exists(model, id, callback) { Memory.prototype.exists = function exists(model, id, callback) {
@ -110,7 +172,7 @@ Memory.prototype.find = function find(model, id, callback) {
Memory.prototype.destroy = function destroy(model, id, callback) { Memory.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id]; delete this.cache[model][id];
process.nextTick(callback); this.saveToFile(null, callback);
}; };
Memory.prototype.fromDb = function (model, data) { Memory.prototype.fromDb = function (model, data) {
@ -273,7 +335,7 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
if (!where) { if (!where) {
this.cache[model] = {}; this.cache[model] = {};
} }
process.nextTick(callback); this.saveToFile(null, callback);
}; };
Memory.prototype.count = function count(model, callback, where) { Memory.prototype.count = function count(model, callback, where) {

95
test/memory.test.js Normal file
View File

@ -0,0 +1,95 @@
var jdb = require('../');
var DataSource = jdb.DataSource;
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var async = require('async');
describe('Memory connector', function () {
var file = path.join(__dirname, 'memory.json');
function readModels(done) {
fs.readFile(file, function (err, data) {
var json = JSON.parse(data.toString());
assert(json.models);
assert(json.ids.User);
done(err, json);
});
}
before(function (done) {
fs.unlink(file, function (err) {
if (!err || err.code === 'ENOENT') {
done();
}
});
});
it('should save to a json file', function (done) {
var ds = new DataSource({
connector: 'memory',
file: file
});
var User = ds.createModel('User', {
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
var count = 0;
var ids = [];
async.eachSeries(['John1', 'John2', 'John3'], function (item, cb) {
User.create({name: item}, function (err, result) {
ids.push(result.id);
count++;
readModels(function (err, json) {
assert.equal(Object.keys(json.models.User).length, count);
cb(err);
});
});
}, function (err, results) {
// Now try to delete one
User.deleteById(ids[0], function (err) {
readModels(function (err, json) {
assert.equal(Object.keys(json.models.User).length, 2);
User.upsert({id: ids[1], name: 'John'}, function(err, result) {
readModels(function (err, json) {
assert.equal(Object.keys(json.models.User).length, 2);
var user = JSON.parse(json.models.User[ids[1]]);
assert.equal(user.name, 'John');
done();
});
});
});
});
});
});
// The saved memory.json from previous test should be loaded
it('should load from the json file', function (done) {
var ds = new DataSource({
connector: 'memory',
file: file
});
var User = ds.createModel('User', {
name: String,
bio: String,
approved: Boolean,
joinedAt: Date,
age: Number
});
User.find(function (err, users) {
// There should be 2 records
assert.equal(users.length, 2);
done(err);
});
});
});