Memory storage

This commit is contained in:
Anatoliy Chakkaev 2011-10-03 17:36:43 +04:00
parent d865b77aea
commit b773763cbe
2 changed files with 104 additions and 2 deletions

View File

@ -264,7 +264,9 @@ AbstractClass.prototype.save = function (callback) {
var data = this.toObject();
if (this.id) {
this._adapter().save(modelName, data, function (err) {
if (!err) {
if (err) {
console.log(err);
} else {
this.constructor.call(this, data);
}
if (callback) {
@ -296,7 +298,7 @@ AbstractClass.prototype.toObject = function () {
AbstractClass.prototype.destroy = function (cb) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
delete this.constructor.cache[this.id];
cb(err);
cb && cb(err);
}.bind(this));
};

100
lib/memory.js Normal file
View File

@ -0,0 +1,100 @@
exports.initialize = function initializeSchema(schema, callback) {
schema.adapter = new Memory();
};
function Memory() {
this._models = {};
this.cache = {};
this.ids = {};
}
Memory.prototype.define = function defineModel(descr) {
var m = descr.model.modelName;
this._models[m] = descr;
this.cache[m] = {};
this.ids[m] = 1;
};
Memory.prototype.create = function create(model, data, callback) {
var id = this.ids[model]++;
data.id = id;
this.cache[model][id] = data;
callback(null, id);
};
Memory.prototype.save = function save(model, data, callback) {
this.cache[model][data.id] = data;
callback();
};
Memory.prototype.exists = function exists(model, id, callback) {
callback(null, this.cache[model].hasOwnProperty(id));
};
Memory.prototype.find = function find(model, id, callback) {
callback(null, this.cache[model][id]);
};
Memory.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id];
callback();
};
Memory.prototype.all = function all(model, filter, callback) {
var nodes = Object.keys(this.cache[model]).map(function (key) {
return this.cache[model][key];
}.bind(this));
process.nextTick(function () {
callback(null, filter && nodes ? nodes.filter(applyFilter(filter)) : nodes);
});
};
function applyFilter(filter) {
if (typeof filter === 'function') {
return filter;
}
var keys = Object.keys(filter);
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter[key], obj[key])) {
pass = false;
}
});
return pass;
}
function test(example, value) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
return value.match(example);
}
// not strict equality
return example == value;
}
}
Memory.prototype.destroyAll = function destroyAll(model, callback) {
Object.keys(this.cache[model]).forEach(function (id) {
delete this.cache[model][id];
}.bind(this));
this.cache[model] = {};
callback();
};
Memory.prototype.count = function count(model, callback) {
callback(null, Object.keys(this.cache[model]).length);
};
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
data.id = id;
var base = this.cache[model][id];
this.save(model, merge(base, data), cb);
};
function merge(base, update) {
Object.keys(update).forEach(function (key) {
base[key] = update[key];
});
return base;
}