loopback-datasource-juggler/lib/adapters/memory.js

175 lines
4.7 KiB
JavaScript
Raw Normal View History

2011-10-03 13:36:43 +00:00
exports.initialize = function initializeSchema(schema, callback) {
schema.adapter = new Memory();
2011-11-13 08:05:50 +00:00
process.nextTick(callback);
2011-10-03 13:36:43 +00:00
};
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) {
2012-03-22 19:46:16 +00:00
var id = data.id || this.ids[model]++;
2011-10-03 13:36:43 +00:00
data.id = id;
this.cache[model][id] = data;
callback(null, id);
};
2012-03-22 19:46:16 +00:00
Memory.prototype.updateOrCreate = function (model, data, callback) {
var mem = this;
this.exists(model, data.id, function (err, exists) {
if (exists) {
mem.save(model, data, callback);
} else {
mem.create(model, data, function (err, id) {
data.id = id;
callback(err, data);
});
}
});
};
2011-10-03 13:36:43 +00:00
Memory.prototype.save = function save(model, data, callback) {
this.cache[model][data.id] = data;
2012-03-22 19:46:16 +00:00
callback(null, data);
2011-10-03 13:36:43 +00:00
};
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));
if (filter) {
// do we need some filtration?
if (filter.where) {
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
}
// do we need some sorting?
if (filter.order) {
var props = this._models[model].properties;
var allNumeric = true;
var orders = filter.order;
var reverse = false;
if (typeof filter.order === "string") {
orders = [filter.order];
}
orders.forEach(function (key, i) {
var m = key.match(/\s+(A|DE)SC$/i);
if (m) {
key = key.replace(/\s+(A|DE)SC/i, '');
if (m[1] === 'DE') reverse = true;
}
orders[i] = key;
if (props[key].type.name !== 'Number' && props[key].type.name !== 'Date') {
allNumeric = false;
}
});
if (allNumeric) {
nodes = nodes.sort(numerically.bind(orders));
} else {
nodes = nodes.sort(literally.bind(orders));
}
if (reverse) nodes = nodes.reverse();
}
}
2011-10-03 13:36:43 +00:00
process.nextTick(function () {
callback(null, nodes);
2011-10-03 13:36:43 +00:00
});
function numerically(a, b) {
return a[this[0]] - b[this[0]];
}
function literally(a, b) {
return a[this[0]] > b[this[0]];
}
2011-10-03 13:36:43 +00:00
};
function applyFilter(filter) {
if (typeof filter.where === 'function') {
return filter.where;
2011-10-03 13:36:43 +00:00
}
var keys = Object.keys(filter.where);
2011-10-03 13:36:43 +00:00
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter.where[key], obj[key])) {
2011-10-03 13:36:43 +00:00
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, where) {
var cache = this.cache[model];
var data = Object.keys(cache)
if (where) {
data = data.filter(function (id) {
var ok = true;
Object.keys(where).forEach(function (key) {
if (cache[id][key] != where[key]) {
ok = false;
}
});
return ok;
});
}
callback(null, data.length);
2011-10-03 13:36:43 +00:00
};
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) {
2012-03-22 19:46:16 +00:00
if (!base) return update;
2011-10-03 13:36:43 +00:00
Object.keys(update).forEach(function (key) {
base[key] = update[key];
});
return base;
}