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

406 lines
11 KiB
JavaScript
Raw Normal View History

var util = require('util');
var Connector = require('../connector');
2013-06-24 22:21:59 +00:00
var geo = require('../geo');
var utils = require('../utils');
var fs = require('fs');
var async = require('async');
2013-06-24 22:21:59 +00:00
/**
* Initialize the Oracle connector against the given data source
*
* @param {DataSource} dataSource The loopback-datasource-juggler dataSource
* @param {Function} [callback] The callback function
*/
exports.initialize = function initializeDataSource(dataSource, callback) {
dataSource.connector = new Memory(null, dataSource.settings);
2014-01-24 17:09:53 +00:00
dataSource.connector.connect(callback);
2011-10-03 13:36:43 +00:00
};
2013-06-26 03:31:00 +00:00
exports.Memory = Memory;
function Memory(m, settings) {
if (m instanceof Memory) {
2014-01-24 17:09:53 +00:00
this.isTransaction = true;
this.cache = m.cache;
this.ids = m.ids;
this.constructor.super_.call(this, 'memory', settings);
2014-01-24 17:09:53 +00:00
this._models = m._models;
} else {
this.isTransaction = false;
this.cache = {};
this.ids = {};
this.constructor.super_.call(this, 'memory', settings);
2014-01-24 17:09:53 +00:00
}
2011-10-03 13:36:43 +00:00
}
util.inherits(Memory, Connector);
Memory.prototype.getDefaultIdType = function() {
return Number;
};
Memory.prototype.getType = function() {
return 'db/nosql/memory';
};
2014-01-24 17:09:53 +00:00
Memory.prototype.connect = function (callback) {
if (this.isTransaction) {
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();
}
});
2014-01-24 17:09:53 +00:00
} else {
process.nextTick(callback);
}
2013-04-01 13:49:12 +00:00
};
/*!
* 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);
});
}
};
2013-10-03 16:14:24 +00:00
Memory.prototype.define = function defineModel(definition) {
2014-01-24 17:09:53 +00:00
this.constructor.super_.prototype.define.apply(this, [].slice.call(arguments));
var m = definition.model.modelName;
if(!this.cache[m]) {
this.cache[m] = {};
this.ids[m] = 1;
}
2011-10-03 13:36:43 +00:00
};
Memory.prototype.create = function create(model, data, callback) {
2014-01-24 17:09:53 +00:00
// FIXME: [rfeng] We need to generate unique ids based on the id type
// FIXME: [rfeng] We don't support composite ids yet
var currentId = this.ids[model];
if (currentId === undefined) {
// First time
this.ids[model] = 1;
currentId = 1;
}
var id = this.getIdValue(model, data) || currentId;
if (id > currentId) {
// If the id is passed in and the value is greater than the current id
currentId = id;
}
this.ids[model] = Number(currentId) + 1;
var props = this._models[model].properties;
var idName = this.idName(model);
id = (props[idName] && props[idName].type && props[idName].type(id)) || id;
this.setIdValue(model, data, id);
if(!this.cache[model]) {
this.cache[model] = {};
}
2014-01-24 17:09:53 +00:00
this.cache[model][id] = JSON.stringify(data);
this.saveToFile(id, callback);
2011-10-03 13:36:43 +00:00
};
2012-03-22 19:46:16 +00:00
Memory.prototype.updateOrCreate = function (model, data, callback) {
2014-01-24 17:09:53 +00:00
var self = this;
this.exists(model, self.getIdValue(model, data), function (err, exists) {
if (exists) {
self.save(model, data, callback);
} else {
self.create(model, data, function (err, id) {
self.setIdValue(model, data, id);
callback(err, data);
});
}
});
2012-03-22 19:46:16 +00:00
};
2011-10-03 13:36:43 +00:00
Memory.prototype.save = function save(model, data, callback) {
2014-01-24 17:09:53 +00:00
this.cache[model][this.getIdValue(model, data)] = JSON.stringify(data);
this.saveToFile(data, callback);
2011-10-03 13:36:43 +00:00
};
Memory.prototype.exists = function exists(model, id, callback) {
2014-01-24 17:09:53 +00:00
process.nextTick(function () {
callback(null, this.cache[model] && this.cache[model].hasOwnProperty(id));
}.bind(this));
2011-10-03 13:36:43 +00:00
};
Memory.prototype.find = function find(model, id, callback) {
2014-01-24 17:09:53 +00:00
var self = this;
process.nextTick(function () {
callback(null, id in this.cache[model] && this.fromDb(model, this.cache[model][id]));
}.bind(this));
2011-10-03 13:36:43 +00:00
};
Memory.prototype.destroy = function destroy(model, id, callback) {
2014-01-24 17:09:53 +00:00
delete this.cache[model][id];
this.saveToFile(null, callback);
2011-10-03 13:36:43 +00:00
};
2014-01-24 17:09:53 +00:00
Memory.prototype.fromDb = function (model, data) {
if (!data) return null;
data = JSON.parse(data);
var props = this._models[model].properties;
for (var key in data) {
var val = data[key];
if (val === undefined || val === null) {
continue;
}
if (props[key]) {
switch (props[key].type.name) {
case 'Date':
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
break;
case 'Boolean':
val = Boolean(val);
break;
case 'Number':
val = Number(val);
break;
}
}
2014-01-24 17:09:53 +00:00
data[key] = val;
}
return data;
2013-04-06 10:50:23 +00:00
};
2011-10-03 13:36:43 +00:00
Memory.prototype.all = function all(model, filter, callback) {
2014-01-24 17:09:53 +00:00
var self = this;
var nodes = Object.keys(this.cache[model]).map(function (key) {
return this.fromDb(model, this.cache[model][key]);
}.bind(this));
2014-01-24 17:09:53 +00:00
if (filter) {
// do we need some sorting?
if (filter.order) {
var orders = filter.order;
if (typeof filter.order === "string") {
orders = [filter.order];
}
orders.forEach(function (key, i) {
var reverse = 1;
var m = key.match(/\s+(A|DE)SC$/i);
if (m) {
key = key.replace(/\s+(A|DE)SC/i, '');
if (m[1].toLowerCase() === 'de') reverse = -1;
}
2014-01-24 17:09:53 +00:00
orders[i] = {"key": key, "reverse": reverse};
});
nodes = nodes.sort(sorting.bind(orders));
}
var nearFilter = geo.nearFilter(filter.where);
2013-03-26 20:50:13 +00:00
2014-01-24 17:09:53 +00:00
// geo sorting
if (nearFilter) {
nodes = geo.filter(nodes, nearFilter);
}
2014-01-24 17:09:53 +00:00
// do we need some filtration?
if (filter.where) {
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
}
2014-01-24 17:09:53 +00:00
// field selection
if (filter.fields) {
nodes = nodes.map(utils.selectFields(filter.fields));
}
// limit/skip
filter.skip = filter.skip || 0;
filter.limit = filter.limit || nodes.length;
nodes = nodes.slice(filter.skip, filter.skip + filter.limit);
}
process.nextTick(function () {
if (filter && filter.include) {
self._models[model].model.include(nodes, filter.include, callback);
} else {
callback(null, nodes);
}
});
function sorting(a, b) {
for (var i = 0, l = this.length; i < l; i++) {
if (a[this[i].key] > b[this[i].key]) {
return 1 * this[i].reverse;
} else if (a[this[i].key] < b[this[i].key]) {
return -1 * this[i].reverse;
}
2013-03-26 20:50:13 +00:00
}
2014-01-24 17:09:53 +00:00
return 0;
}
2011-10-03 13:36:43 +00:00
};
function applyFilter(filter) {
2014-01-24 17:09:53 +00:00
if (typeof filter.where === 'function') {
return filter.where;
}
var keys = Object.keys(filter.where);
return function (obj) {
var pass = true;
keys.forEach(function (key) {
if (!test(filter.where[key], obj && obj[key])) {
pass = false;
}
});
return pass;
}
function test(example, value) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
return value.match(example);
2011-10-03 13:36:43 +00:00
}
2014-01-24 17:09:53 +00:00
if (typeof example === 'undefined') return undefined;
if (typeof value === 'undefined') return undefined;
if (typeof example === 'object') {
// ignore geo near filter
if (example.near) return true;
2011-10-03 13:36:43 +00:00
2014-01-24 17:09:53 +00:00
if (example.inq) {
if (!value) return false;
for (var i = 0; i < example.inq.length; i += 1) {
if (example.inq[i] == value) return true;
2011-10-03 13:36:43 +00:00
}
2014-01-24 17:09:53 +00:00
return false;
}
if (isNum(example.gt) && example.gt < value) return true;
if (isNum(example.gte) && example.gte <= value) return true;
if (isNum(example.lt) && example.lt > value) return true;
if (isNum(example.lte) && example.lte >= value) return true;
}
2014-01-24 17:09:53 +00:00
// not strict equality
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
}
function isNum(n) {
return typeof n === 'number';
}
2011-10-03 13:36:43 +00:00
}
2013-08-18 17:58:53 +00:00
Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
2014-01-24 17:09:53 +00:00
if (!callback && 'function' === typeof where) {
callback = where;
where = undefined;
}
var cache = this.cache[model];
var filter = null;
if (where) {
filter = applyFilter({where: where});
}
Object.keys(cache).forEach(function (id) {
if (!filter || filter(this.fromDb(model, cache[id]))) {
delete cache[id];
2013-08-18 17:58:53 +00:00
}
2014-01-24 17:09:53 +00:00
}.bind(this));
if (!where) {
this.cache[model] = {};
}
this.saveToFile(null, callback);
2011-10-03 13:36:43 +00:00
};
Memory.prototype.count = function count(model, callback, where) {
2014-01-24 17:09:53 +00:00
var cache = this.cache[model];
var data = Object.keys(cache);
if (where) {
var filter = {where: where};
data = data.map(function (id) {
return this.fromDb(model, cache[id]);
}.bind(this));
data = data.filter(applyFilter(filter));
}
process.nextTick(function () {
callback(null, data.length);
});
2011-10-03 13:36:43 +00:00
};
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
2014-01-24 17:09:53 +00:00
if (!id) {
var err = new Error('You must provide an id when updating attributes!');
if (cb) {
return cb(err);
} else {
2014-01-24 17:09:53 +00:00
throw err;
}
2014-01-24 17:09:53 +00:00
}
this.setIdValue(model, data, id);
var cachedModels = this.cache[model];
var modelAsString = cachedModels && this.cache[model][id];
var modelData = modelAsString && JSON.parse(modelAsString);
if (modelData) {
this.save(model, merge(modelData, data), cb);
} else {
cb(new Error('Could not update attributes. Object with id ' + id + ' does not exist!'));
}
2011-10-03 13:36:43 +00:00
};
2013-04-01 13:49:12 +00:00
Memory.prototype.transaction = function () {
2014-01-24 17:09:53 +00:00
return new Memory(this);
2013-04-01 13:49:12 +00:00
};
2014-01-24 17:09:53 +00:00
Memory.prototype.exec = function (callback) {
this.onTransactionExec();
setTimeout(callback, 50);
2013-04-01 13:49:12 +00:00
};
2013-06-26 03:31:00 +00:00
Memory.prototype.buildNearFilter = function (filter) {
// noop
}
2011-10-03 13:36:43 +00:00
function merge(base, update) {
2014-01-24 17:09:53 +00:00
if (!base) return update;
Object.keys(update).forEach(function (key) {
base[key] = update[key];
});
return base;
2013-06-26 03:31:00 +00:00
}