Implemented collection setting for Memory connector

This commit is contained in:
Fabien Franzen 2014-09-06 14:09:30 +02:00
parent 610866bd7c
commit 8352ed3afc
2 changed files with 86 additions and 26 deletions

View File

@ -70,6 +70,26 @@ function deserialize(dbObj) {
} }
} }
Memory.prototype.getCollection = function(model) {
var modelClass = this._models[model];
if (modelClass.settings.memory) {
model = modelClass.settings.memory.collection || model;
}
return model;
}
Memory.prototype.collection = function(model, val) {
model = this.getCollection(model);
if (arguments.length > 1) this.cache[model] = val;
return this.cache[model];
};
Memory.prototype.collectionSeq = function(model, val) {
model = this.getCollection(model);
if (arguments.length > 1) this.ids[model] = val;
return this.ids[model];
};
Memory.prototype.loadFromFile = function(callback) { Memory.prototype.loadFromFile = function(callback) {
var self = this; var self = this;
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage; var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
@ -161,36 +181,34 @@ Memory.prototype.saveToFile = function (result, callback) {
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;
if(!this.cache[m]) { if(!this.collection(m)) {
this.cache[m] = {}; this.collection(m, {});
this.ids[m] = 1; this.collectionSeq(m, 1);
} }
}; };
Memory.prototype.create = function create(model, data, callback) { Memory.prototype.create = function create(model, data, callback) {
// FIXME: [rfeng] We need to generate unique ids based on the id type // FIXME: [rfeng] We need to generate unique ids based on the id type
// FIXME: [rfeng] We don't support composite ids yet // FIXME: [rfeng] We don't support composite ids yet
var currentId = this.ids[model]; var currentId = this.collectionSeq(model);
if (currentId === undefined) { if (currentId === undefined) { // First time
// First time currentId = this.collectionSeq(model, 1);
this.ids[model] = 1;
currentId = 1;
} }
var id = this.getIdValue(model, data) || currentId; var id = this.getIdValue(model, data) || currentId;
if (id > currentId) { if (id > currentId) {
// If the id is passed in and the value is greater than the current id // If the id is passed in and the value is greater than the current id
currentId = id; currentId = id;
} }
this.ids[model] = Number(currentId) + 1; this.collectionSeq(model, Number(currentId) + 1);
var props = this._models[model].properties; var props = this._models[model].properties;
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]) { if(!this.collection(model)) {
this.cache[model] = {}; this.collection(model, {});
} }
this.cache[model][id] = serialize(data); this.collection(model)[id] = serialize(data);
this.saveToFile(id, callback); this.saveToFile(id, callback);
}; };
@ -210,30 +228,30 @@ Memory.prototype.updateOrCreate = function (model, data, callback) {
Memory.prototype.save = function save(model, data, callback) { Memory.prototype.save = function save(model, data, callback) {
var id = this.getIdValue(model, data); var id = this.getIdValue(model, data);
var cachedModels = this.cache[model]; var cachedModels = this.collection(model);
var modelData = cachedModels && this.cache[model][id]; var modelData = cachedModels && this.collection(model)[id];
modelData = modelData && deserialize(modelData); modelData = modelData && deserialize(modelData);
if (modelData) { if (modelData) {
data = merge(modelData, data); data = merge(modelData, data);
} }
this.cache[model][id] = serialize(data); this.collection(model)[id] = serialize(data);
this.saveToFile(data, callback); this.saveToFile(data, callback);
}; };
Memory.prototype.exists = function exists(model, id, callback) { Memory.prototype.exists = function exists(model, id, callback) {
process.nextTick(function () { process.nextTick(function () {
callback(null, this.cache[model] && this.cache[model].hasOwnProperty(id)); callback(null, this.collection(model) && this.collection(model).hasOwnProperty(id));
}.bind(this)); }.bind(this));
}; };
Memory.prototype.find = function find(model, id, callback) { Memory.prototype.find = function find(model, id, callback) {
process.nextTick(function () { process.nextTick(function () {
callback(null, id in this.cache[model] && this.fromDb(model, this.cache[model][id])); callback(null, id in this.collection(model) && this.fromDb(model, this.collection(model)[id]));
}.bind(this)); }.bind(this));
}; };
Memory.prototype.destroy = function destroy(model, id, callback) { Memory.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id]; delete this.collection(model)[id];
this.saveToFile(null, callback); this.saveToFile(null, callback);
}; };
@ -266,8 +284,8 @@ Memory.prototype.fromDb = function (model, data) {
Memory.prototype.all = function all(model, filter, callback) { Memory.prototype.all = function all(model, filter, callback) {
var self = this; var self = this;
var nodes = Object.keys(this.cache[model]).map(function (key) { var nodes = Object.keys(this.collection(model)).map(function (key) {
return this.fromDb(model, this.cache[model][key]); return this.fromDb(model, this.collection(model)[key]);
}.bind(this)); }.bind(this));
if (filter) { if (filter) {
@ -505,7 +523,7 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
callback = where; callback = where;
where = undefined; where = undefined;
} }
var cache = this.cache[model]; var cache = this.collection(model);
var filter = null; var filter = null;
if (where) { if (where) {
filter = applyFilter({where: where}); filter = applyFilter({where: where});
@ -516,13 +534,13 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
} }
}.bind(this)); }.bind(this));
if (!where) { if (!where) {
this.cache[model] = {}; this.collection(model, {});
} }
this.saveToFile(null, callback); this.saveToFile(null, callback);
}; };
Memory.prototype.count = function count(model, callback, where) { Memory.prototype.count = function count(model, callback, where) {
var cache = this.cache[model]; var cache = this.collection(model);
var data = Object.keys(cache); var data = Object.keys(cache);
if (where) { if (where) {
var filter = {where: where}; var filter = {where: where};
@ -539,7 +557,7 @@ Memory.prototype.count = function count(model, callback, where) {
Memory.prototype.update = Memory.prototype.update =
Memory.prototype.updateAll = function updateAll(model, where, data, cb) { Memory.prototype.updateAll = function updateAll(model, where, data, cb) {
var self = this; var self = this;
var cache = this.cache[model]; var cache = this.collection(model);
var filter = null; var filter = null;
where = where || {}; where = where || {};
filter = applyFilter({where: where}); filter = applyFilter({where: where});
@ -571,8 +589,8 @@ Memory.prototype.updateAttributes = function updateAttributes(model, id, data, c
this.setIdValue(model, data, id); this.setIdValue(model, data, id);
var cachedModels = this.cache[model]; var cachedModels = this.collection(model);
var modelData = cachedModels && this.cache[model][id]; var modelData = cachedModels && this.collection(model)[id];
if (modelData) { if (modelData) {
this.save(model, data, cb); this.save(model, data, cb);

View File

@ -270,6 +270,48 @@ describe('Memory connector', function () {
}); });
it('should use collection setting', function (done) {
var ds = new DataSource({
connector: 'memory'
});
var Product = ds.createModel('Product', {
name: String
});
var Tool = ds.createModel('Tool', {
name: String
}, {memory: {collection: 'Product'}});
var Widget = ds.createModel('Widget', {
name: String
}, {memory: {collection: 'Product'}});
ds.connector.getCollection('Tool').should.equal('Product');
ds.connector.getCollection('Widget').should.equal('Product');
async.series([
function(next) {
Tool.create({ name: 'Tool A' }, next);
},
function(next) {
Tool.create({ name: 'Tool B' }, next);
},
function(next) {
Widget.create({ name: 'Widget A' }, next);
}
], function(err) {
Product.find(function(err, products) {
should.not.exist(err);
products.should.have.length(3);
products[0].toObject().should.eql({ name: 'Tool A', id: 1 });
products[1].toObject().should.eql({ name: 'Tool B', id: 2 });
products[2].toObject().should.eql({ name: 'Widget A', id: 3 });
done();
});
});
});
}); });