2016-04-06 14:51:49 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
var util = require('util');
|
2014-05-23 09:03:24 +00:00
|
|
|
var Connector = require('loopback-connector').Connector;
|
2013-06-24 22:21:59 +00:00
|
|
|
var geo = require('../geo');
|
2013-07-17 00:53:52 +00:00
|
|
|
var utils = require('../utils');
|
2014-01-29 20:04:09 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var async = require('async');
|
2013-06-24 22:21:59 +00:00
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
/**
|
2014-08-30 08:53:10 +00:00
|
|
|
* Initialize the Memory connector against the given data source
|
2013-10-02 05:14:21 +00:00
|
|
|
*
|
|
|
|
* @param {DataSource} dataSource The loopback-datasource-juggler dataSource
|
|
|
|
* @param {Function} [callback] The callback function
|
|
|
|
*/
|
|
|
|
exports.initialize = function initializeDataSource(dataSource, callback) {
|
2014-01-29 20:04:09 +00:00
|
|
|
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;
|
2014-07-27 14:30:45 +00:00
|
|
|
exports.applyFilter = applyFilter;
|
2013-06-26 03:31:00 +00:00
|
|
|
|
2014-01-29 20:04:09 +00:00
|
|
|
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;
|
2014-01-29 20:04:09 +00:00
|
|
|
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 = {};
|
2014-01-29 20:04:09 +00:00
|
|
|
this.constructor.super_.call(this, 'memory', settings);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2011-10-03 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
util.inherits(Memory, Connector);
|
|
|
|
|
2014-01-24 22:51:01 +00:00
|
|
|
Memory.prototype.getDefaultIdType = function() {
|
|
|
|
return Number;
|
|
|
|
};
|
|
|
|
|
2014-01-28 22:23:48 +00:00
|
|
|
Memory.prototype.getTypes = function() {
|
|
|
|
return ['db', 'nosql', 'memory'];
|
2014-01-24 22:51:01 +00:00
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.connect = function(callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (this.isTransaction) {
|
|
|
|
this.onTransactionExec = callback;
|
2014-01-29 20:04:09 +00:00
|
|
|
} else {
|
|
|
|
this.loadFromFile(callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-11 22:37:28 +00:00
|
|
|
function serialize(obj) {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (obj === null || obj === undefined) {
|
2014-03-11 22:37:28 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
return JSON.stringify(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deserialize(dbObj) {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (dbObj === null || dbObj === undefined) {
|
2014-03-11 22:37:28 +00:00
|
|
|
return dbObj;
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
if (typeof dbObj === 'string') {
|
2014-03-11 22:37:28 +00:00
|
|
|
return JSON.parse(dbObj);
|
|
|
|
} else {
|
|
|
|
return dbObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
Memory.prototype.getCollection = function(model) {
|
|
|
|
var modelClass = this._models[model];
|
2014-10-16 19:04:16 +00:00
|
|
|
if (modelClass && modelClass.settings.memory) {
|
2014-09-06 12:09:30 +00:00
|
|
|
model = modelClass.settings.memory.collection || model;
|
|
|
|
}
|
|
|
|
return model;
|
2016-04-14 14:41:19 +00:00
|
|
|
};
|
2014-09-06 12:09:30 +00:00
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
Memory.prototype.initCollection = function(model) {
|
|
|
|
this.collection(model, {});
|
|
|
|
this.collectionSeq(model, 1);
|
2016-04-14 14:41:19 +00:00
|
|
|
};
|
2014-09-06 12:38:57 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
2014-01-29 20:04:09 +00:00
|
|
|
Memory.prototype.loadFromFile = function(callback) {
|
|
|
|
var self = this;
|
2014-05-07 14:47:12 +00:00
|
|
|
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
|
|
|
|
var localStorage = hasLocalStorage && this.settings.localStorage;
|
|
|
|
|
2014-01-29 20:04:09 +00:00
|
|
|
if (self.settings.file) {
|
2016-04-14 14:41:19 +00:00
|
|
|
fs.readFile(self.settings.file, { encoding: 'utf8', flag: 'r' }, function(err, data) {
|
2014-01-29 20:04:09 +00:00
|
|
|
if (err && err.code !== 'ENOENT') {
|
|
|
|
callback && callback(err);
|
|
|
|
} else {
|
2014-05-07 14:47:12 +00:00
|
|
|
parseAndLoad(data);
|
2014-01-29 20:04:09 +00:00
|
|
|
}
|
|
|
|
});
|
2016-04-14 14:41:19 +00:00
|
|
|
} else if (localStorage) {
|
2014-05-07 14:47:12 +00:00
|
|
|
var data = window.localStorage.getItem(localStorage);
|
|
|
|
data = data || '{}';
|
|
|
|
parseAndLoad(data);
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick(callback);
|
|
|
|
}
|
2014-05-07 14:47:12 +00:00
|
|
|
|
|
|
|
function parseAndLoad(data) {
|
|
|
|
if (data) {
|
|
|
|
try {
|
|
|
|
data = JSON.parse(data.toString());
|
2016-04-14 14:41:19 +00:00
|
|
|
} catch (e) {
|
2014-05-07 14:47:12 +00:00
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ids = data.ids || {};
|
|
|
|
self.cache = data.models || {};
|
|
|
|
} else {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (!self.cache) {
|
2014-05-07 14:47:12 +00:00
|
|
|
self.ids = {};
|
|
|
|
self.cache = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback && callback();
|
|
|
|
}
|
2013-04-01 13:49:12 +00:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:04:09 +00:00
|
|
|
/*!
|
|
|
|
* Flush the cache into the json file if necessary
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.saveToFile = function(result, callback) {
|
2014-01-29 20:04:09 +00:00
|
|
|
var self = this;
|
2014-05-07 14:47:12 +00:00
|
|
|
var file = this.settings.file;
|
|
|
|
var hasLocalStorage = typeof window !== 'undefined' && window.localStorage;
|
|
|
|
var localStorage = hasLocalStorage && this.settings.localStorage;
|
|
|
|
if (file) {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (!self.writeQueue) {
|
2014-01-29 20:04:09 +00:00
|
|
|
// Create a queue for writes
|
2016-04-14 14:41:19 +00:00
|
|
|
self.writeQueue = async.queue(function(task, cb) {
|
2014-01-29 20:04:09 +00:00
|
|
|
// Flush out the models/ids
|
|
|
|
var data = JSON.stringify({
|
|
|
|
ids: self.ids,
|
2016-04-14 14:41:19 +00:00
|
|
|
models: self.cache,
|
2014-01-29 20:04:09 +00:00
|
|
|
}, null, ' ');
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
fs.writeFile(self.settings.file, data, function(err) {
|
2014-01-29 21:41:42 +00:00
|
|
|
cb(err);
|
|
|
|
task.callback && task.callback(err, task.data);
|
2014-01-29 20:04:09 +00:00
|
|
|
});
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
// Enqueue the write
|
2014-01-29 21:41:42 +00:00
|
|
|
self.writeQueue.push({
|
|
|
|
data: result,
|
2016-04-14 14:41:19 +00:00
|
|
|
callback: callback,
|
2014-01-29 21:41:42 +00:00
|
|
|
});
|
2014-05-07 14:47:12 +00:00
|
|
|
} else if (localStorage) {
|
|
|
|
// Flush out the models/ids
|
|
|
|
var data = JSON.stringify({
|
|
|
|
ids: self.ids,
|
2016-04-14 14:41:19 +00:00
|
|
|
models: self.cache,
|
2014-05-07 14:47:12 +00:00
|
|
|
}, null, ' ');
|
|
|
|
window.localStorage.setItem(localStorage, data);
|
2016-04-14 14:41:19 +00:00
|
|
|
process.nextTick(function() {
|
2014-05-07 14:47:12 +00:00
|
|
|
callback && callback(null, result);
|
|
|
|
});
|
2014-01-29 20:04:09 +00:00
|
|
|
} else {
|
2016-04-14 14:41:19 +00:00
|
|
|
process.nextTick(function() {
|
2014-01-29 20:04:09 +00:00
|
|
|
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;
|
2016-04-14 14:41:19 +00:00
|
|
|
if (!this.collection(m)) this.initCollection(m);
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
Memory.prototype._createSync = function(model, data, fn) {
|
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
|
2014-09-06 12:09:30 +00:00
|
|
|
var currentId = this.collectionSeq(model);
|
|
|
|
if (currentId === undefined) { // First time
|
|
|
|
currentId = this.collectionSeq(model, 1);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2014-09-06 12:09:30 +00:00
|
|
|
this.collectionSeq(model, Number(currentId) + 1);
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
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);
|
2016-01-05 20:26:09 +00:00
|
|
|
if (!this.collection(model)) {
|
2014-09-06 12:09:30 +00:00
|
|
|
this.collection(model, {});
|
2014-01-29 21:41:42 +00:00
|
|
|
}
|
2015-03-16 10:26:57 +00:00
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
if (this.collection(model)[id])
|
|
|
|
return fn(new Error('Duplicate entry for ' + model + '.' + idName));
|
2015-03-16 10:26:57 +00:00
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
this.collection(model)[id] = serialize(data);
|
2016-01-05 20:26:09 +00:00
|
|
|
fn(null, id);
|
|
|
|
};
|
|
|
|
|
|
|
|
Memory.prototype.create = function create(model, data, options, callback) {
|
|
|
|
var self = this;
|
|
|
|
this._createSync(model, data, function(err, id) {
|
|
|
|
if (err) {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
self.saveToFile(id, callback);
|
|
|
|
});
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.updateOrCreate = function(model, data, options, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var self = this;
|
2016-04-14 14:41:19 +00:00
|
|
|
this.exists(model, self.getIdValue(model, data), options, function(err, exists) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (exists) {
|
2015-05-13 16:36:29 +00:00
|
|
|
self.save(model, data, options, function(err, data) {
|
2015-03-19 12:25:03 +00:00
|
|
|
callback(err, data, { isNewInstance: false });
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2016-04-14 14:41:19 +00:00
|
|
|
self.create(model, data, options, function(err, id) {
|
2014-01-24 17:09:53 +00:00
|
|
|
self.setIdValue(model, data, id);
|
2015-03-19 12:25:03 +00:00
|
|
|
callback(err, data, { isNewInstance: true });
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-03-22 19:46:16 +00:00
|
|
|
};
|
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
Memory.prototype.findOrCreate = function(model, filter, data, callback) {
|
|
|
|
var self = this;
|
|
|
|
var nodes = self._findAllSkippingIncludes(model, filter);
|
|
|
|
var found = nodes[0];
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
if (!found) {
|
2016-01-05 20:26:09 +00:00
|
|
|
// Calling _createSync to update the collection in a sync way and to guarantee to create it in the same turn of even loop
|
|
|
|
return self._createSync(model, data, function(err, id) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
self.saveToFile(id, function(err, id) {
|
|
|
|
self.setIdValue(model, data, id);
|
|
|
|
callback(err, data, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!filter || !filter.include) {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
callback(null, found, false);
|
|
|
|
});
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
self._models[model].model.include(nodes[0], filter.include, {}, function(err, nodes) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
if (err) return callback(err);
|
|
|
|
callback(null, nodes[0], false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.save = function save(model, data, options, callback) {
|
2015-06-18 22:18:44 +00:00
|
|
|
var self = this;
|
2014-05-09 22:27:45 +00:00
|
|
|
var id = this.getIdValue(model, data);
|
2014-09-06 12:09:30 +00:00
|
|
|
var cachedModels = this.collection(model);
|
|
|
|
var modelData = cachedModels && this.collection(model)[id];
|
2014-05-09 22:27:45 +00:00
|
|
|
modelData = modelData && deserialize(modelData);
|
|
|
|
if (modelData) {
|
|
|
|
data = merge(modelData, data);
|
|
|
|
}
|
2014-09-06 12:09:30 +00:00
|
|
|
this.collection(model)[id] = serialize(data);
|
2015-03-19 12:25:03 +00:00
|
|
|
this.saveToFile(data, function(err) {
|
2015-06-18 22:18:44 +00:00
|
|
|
callback(err, self.fromDb(model, data), { isNewInstance: !modelData });
|
2015-03-19 12:25:03 +00:00
|
|
|
});
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.exists = function exists(model, id, options, callback) {
|
2016-04-14 14:41:19 +00:00
|
|
|
process.nextTick(function() {
|
2014-09-06 12:09:30 +00:00
|
|
|
callback(null, this.collection(model) && this.collection(model).hasOwnProperty(id));
|
2014-01-24 17:09:53 +00:00
|
|
|
}.bind(this));
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.find = function find(model, id, options, callback) {
|
2016-04-14 14:41:19 +00:00
|
|
|
process.nextTick(function() {
|
2014-09-06 12:09:30 +00:00
|
|
|
callback(null, id in this.collection(model) && this.fromDb(model, this.collection(model)[id]));
|
2014-01-24 17:09:53 +00:00
|
|
|
}.bind(this));
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.destroy = function destroy(model, id, options, callback) {
|
2015-03-20 16:49:32 +00:00
|
|
|
var exists = this.collection(model)[id];
|
2014-09-06 12:09:30 +00:00
|
|
|
delete this.collection(model)[id];
|
2015-03-20 16:49:32 +00:00
|
|
|
this.saveToFile({ count: exists ? 1 : 0 }, callback);
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.fromDb = function(model, data) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!data) return null;
|
2014-03-11 22:37:28 +00:00
|
|
|
data = deserialize(data);
|
2014-01-24 17:09:53 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
data[key] = val;
|
|
|
|
}
|
|
|
|
return data;
|
2013-04-06 10:50:23 +00:00
|
|
|
};
|
|
|
|
|
2015-03-27 23:05:12 +00:00
|
|
|
function getValue(obj, path) {
|
|
|
|
if (obj == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
var keys = path.split('.');
|
|
|
|
var val = obj;
|
|
|
|
for (var i = 0, n = keys.length; i < n; i++) {
|
|
|
|
val = val[keys[i]];
|
|
|
|
if (val == null) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
Memory.prototype._findAllSkippingIncludes = function(model, filter) {
|
2016-04-14 14:41:19 +00:00
|
|
|
var nodes = Object.keys(this.collection(model)).map(function(key) {
|
2014-09-06 12:09:30 +00:00
|
|
|
return this.fromDb(model, this.collection(model)[key]);
|
2014-01-24 17:09:53 +00:00
|
|
|
}.bind(this));
|
2012-01-19 13:44:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (filter) {
|
2014-06-27 06:40:20 +00:00
|
|
|
if (!filter.order) {
|
|
|
|
var idNames = this.idNames(model);
|
|
|
|
if (idNames && idNames.length) {
|
|
|
|
filter.order = idNames;
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
// do we need some sorting?
|
|
|
|
if (filter.order) {
|
|
|
|
var orders = filter.order;
|
2016-04-14 14:41:19 +00:00
|
|
|
if (typeof filter.order === 'string') {
|
2014-01-24 17:09:53 +00:00
|
|
|
orders = [filter.order];
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
orders.forEach(function(key, i) {
|
2014-01-24 17:09:53 +00:00
|
|
|
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;
|
2013-07-17 00:53:52 +00:00
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
orders[i] = { 'key': key, 'reverse': reverse };
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
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);
|
2012-01-19 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// do we need some filtration?
|
2015-07-24 19:56:31 +00:00
|
|
|
if (filter.where && nodes)
|
|
|
|
nodes = nodes.filter(applyFilter(filter));
|
2012-01-19 13:44:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// field selection
|
|
|
|
if (filter.fields) {
|
|
|
|
nodes = nodes.map(utils.selectFields(filter.fields));
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit/skip
|
2014-06-17 16:07:55 +00:00
|
|
|
var skip = filter.skip || filter.offset || 0;
|
|
|
|
var limit = filter.limit || nodes.length;
|
|
|
|
nodes = nodes.slice(skip, skip + limit);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2016-01-05 20:26:09 +00:00
|
|
|
return nodes;
|
2016-04-14 14:41:19 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function sorting(a, b) {
|
2014-12-22 20:06:01 +00:00
|
|
|
var undefinedA, undefinedB;
|
2014-12-22 17:45:39 +00:00
|
|
|
|
2014-12-22 20:06:01 +00:00
|
|
|
for (var i = 0, l = this.length; i < l; i++) {
|
2015-03-27 23:05:12 +00:00
|
|
|
var aVal = getValue(a, this[i].key);
|
|
|
|
var bVal = getValue(b, this[i].key);
|
|
|
|
undefinedB = bVal === undefined && aVal !== undefined;
|
|
|
|
undefinedA = aVal === undefined && bVal !== undefined;
|
2014-12-22 17:45:39 +00:00
|
|
|
|
2015-03-27 23:05:12 +00:00
|
|
|
if (undefinedB || aVal > bVal) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return 1 * this[i].reverse;
|
2015-03-27 23:05:12 +00:00
|
|
|
} else if (undefinedA || aVal < bVal) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return -1 * this[i].reverse;
|
|
|
|
}
|
2013-03-26 20:50:13 +00:00
|
|
|
}
|
2014-12-22 20:06:01 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2016-01-05 20:26:09 +00:00
|
|
|
Memory.prototype.all = function all(model, filter, options, callback) {
|
|
|
|
var self = this;
|
|
|
|
var nodes = self._findAllSkippingIncludes(model, filter);
|
|
|
|
|
|
|
|
process.nextTick(function() {
|
|
|
|
if (filter && filter.include) {
|
|
|
|
self._models[model].model.include(nodes, filter.include, options, callback);
|
|
|
|
} else {
|
|
|
|
callback(null, nodes);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-10-03 13:36:43 +00:00
|
|
|
function applyFilter(filter) {
|
2014-05-15 15:56:00 +00:00
|
|
|
var where = filter.where;
|
|
|
|
if (typeof where === 'function') {
|
|
|
|
return where;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-05-15 15:56:00 +00:00
|
|
|
var keys = Object.keys(where);
|
2016-04-14 14:41:19 +00:00
|
|
|
return function(obj) {
|
2015-08-18 19:56:36 +00:00
|
|
|
return keys.every(function(key) {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (key === 'and' || key === 'or') {
|
|
|
|
if (Array.isArray(where[key])) {
|
|
|
|
if (key === 'and') {
|
2015-08-18 19:56:36 +00:00
|
|
|
return where[key].every(function(cond) {
|
2016-04-14 14:41:19 +00:00
|
|
|
return applyFilter({ where: cond })(obj);
|
2014-05-15 15:56:00 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
if (key === 'or') {
|
2015-08-18 19:56:36 +00:00
|
|
|
return where[key].some(function(cond) {
|
2016-04-14 14:41:19 +00:00
|
|
|
return applyFilter({ where: cond })(obj);
|
2014-05-15 15:56:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 19:56:36 +00:00
|
|
|
|
2015-08-28 03:32:53 +00:00
|
|
|
var value = getValue(obj, key);
|
|
|
|
// Support referencesMany and other embedded relations
|
|
|
|
// Also support array types. Mongo, possibly PostgreSQL
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
var matcher = where[key];
|
2015-11-23 16:09:24 +00:00
|
|
|
// The following condition is for the case where we are querying with
|
|
|
|
// a neq filter, and when the value is an empty array ([]).
|
|
|
|
if (matcher.neq !== undefined && value.length <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
return value.some(function(v, i) {
|
|
|
|
var filter = { where: {}};
|
2015-08-28 03:32:53 +00:00
|
|
|
filter.where[i] = matcher;
|
|
|
|
return applyFilter(filter)(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test(where[key], value)) {
|
2015-08-18 19:56:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a composed key a.b and b would resolve to a property of an object inside an array
|
|
|
|
// then, we attempt to emulate mongo db matching. Helps for embedded relations
|
|
|
|
var dotIndex = key.indexOf('.');
|
|
|
|
var subValue = obj[key.substring(0, dotIndex)];
|
|
|
|
if (dotIndex !== -1 && Array.isArray(subValue)) {
|
2016-04-14 14:41:19 +00:00
|
|
|
var subFilter = { where: {}};
|
|
|
|
var subKey = key.substring(dotIndex + 1);
|
2015-08-18 19:56:36 +00:00
|
|
|
subFilter.where[subKey] = where[key];
|
2015-08-28 03:32:53 +00:00
|
|
|
return subValue.some(applyFilter(subFilter));
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-08-18 19:56:36 +00:00
|
|
|
|
|
|
|
return false;
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2016-04-14 14:41:19 +00:00
|
|
|
};
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-06-18 19:37:49 +00:00
|
|
|
function toRegExp(pattern) {
|
|
|
|
if (pattern instanceof RegExp) {
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
var regex = '';
|
2014-06-20 19:05:32 +00:00
|
|
|
// Escaping user input to be treated as a literal string within a regular expression
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Writing_a_Regular_Expression_Pattern
|
2016-04-14 14:41:19 +00:00
|
|
|
pattern = pattern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
|
2014-06-18 19:37:49 +00:00
|
|
|
for (var i = 0, n = pattern.length; i < n; i++) {
|
|
|
|
var char = pattern.charAt(i);
|
|
|
|
if (char === '\\') {
|
|
|
|
i++; // Skip to next char
|
|
|
|
if (i < n) {
|
|
|
|
regex += pattern.charAt(i);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (char === '%') {
|
|
|
|
regex += '.*';
|
|
|
|
} else if (char === '_') {
|
|
|
|
regex += '.';
|
|
|
|
} else if (char === '.') {
|
|
|
|
regex += '\\.';
|
|
|
|
} else if (char === '*') {
|
|
|
|
regex += '\\*';
|
2016-04-18 13:56:45 +00:00
|
|
|
} else {
|
2014-06-18 19:37:49 +00:00
|
|
|
regex += char;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return regex;
|
2014-06-18 06:19:28 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function test(example, value) {
|
2014-06-18 06:19:28 +00:00
|
|
|
if (typeof value === 'string' && (example instanceof RegExp)) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return value.match(example);
|
2011-10-03 13:36:43 +00:00
|
|
|
}
|
2015-07-24 19:56:31 +00:00
|
|
|
|
2014-08-29 15:45:45 +00:00
|
|
|
if (example === undefined) {
|
2014-05-15 15:56:00 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2014-09-02 15:36:37 +00:00
|
|
|
|
2015-01-12 23:38:21 +00:00
|
|
|
if (typeof example === 'object' && example !== null) {
|
2015-08-18 19:56:36 +00:00
|
|
|
if (example.regexp) {
|
|
|
|
return value ? value.match(example.regexp) : false;
|
|
|
|
}
|
2015-07-30 15:46:49 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// ignore geo near filter
|
2014-05-15 15:56:00 +00:00
|
|
|
if (example.near) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-03 13:36:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (example.inq) {
|
2014-06-06 15:19:41 +00:00
|
|
|
// if (!value) return false;
|
|
|
|
for (var i = 0; i < example.inq.length; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:36:37 +00:00
|
|
|
if ('neq' in example) {
|
2014-08-29 15:45:45 +00:00
|
|
|
return compare(example.neq, value) !== 0;
|
|
|
|
}
|
2015-03-19 12:25:03 +00:00
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
if ('between' in example) {
|
2016-04-18 13:56:45 +00:00
|
|
|
return (testInEquality({ gte: example.between[0] }, value) &&
|
|
|
|
testInEquality({ lte: example.between[1] }, value));
|
2015-03-16 10:21:04 +00:00
|
|
|
}
|
2015-03-19 12:25:03 +00:00
|
|
|
|
2014-06-18 06:19:28 +00:00
|
|
|
if (example.like || example.nlike) {
|
|
|
|
var like = example.like || example.nlike;
|
|
|
|
if (typeof like === 'string') {
|
2014-06-18 19:37:49 +00:00
|
|
|
like = toRegExp(like);
|
2014-06-18 06:19:28 +00:00
|
|
|
}
|
|
|
|
if (example.like) {
|
|
|
|
return !!new RegExp(like).test(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (example.nlike) {
|
|
|
|
return !new RegExp(like).test(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 15:19:41 +00:00
|
|
|
if (testInEquality(example, value)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-12 22:45:31 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
// not strict equality
|
2016-04-18 13:56:45 +00:00
|
|
|
return (example !== null ? example.toString() : example) ==
|
|
|
|
(value != null ? value.toString() : value);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:19:41 +00:00
|
|
|
/**
|
|
|
|
* Compare two values
|
|
|
|
* @param {*} val1 The 1st value
|
|
|
|
* @param {*} val2 The 2nd value
|
2014-06-06 16:10:47 +00:00
|
|
|
* @returns {number} 0: =, positive: >, negative <
|
2014-06-06 15:19:41 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function compare(val1, val2) {
|
2016-04-14 14:41:19 +00:00
|
|
|
if (val1 == null || val2 == null) {
|
2014-06-10 23:11:50 +00:00
|
|
|
// Either val1 or val2 is null or undefined
|
|
|
|
return val1 == val2 ? 0 : NaN;
|
|
|
|
}
|
2014-06-06 15:19:41 +00:00
|
|
|
if (typeof val1 === 'number') {
|
|
|
|
return val1 - val2;
|
|
|
|
}
|
|
|
|
if (typeof val1 === 'string') {
|
2014-06-10 23:11:50 +00:00
|
|
|
return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : (val1 == val2) ? 0 : NaN);
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
if (typeof val1 === 'boolean') {
|
|
|
|
return val1 - val2;
|
|
|
|
}
|
|
|
|
if (val1 instanceof Date) {
|
2014-06-10 23:11:50 +00:00
|
|
|
var result = val1 - val2;
|
2014-06-06 15:48:05 +00:00
|
|
|
return result;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
// Return NaN if we don't know how to compare
|
2014-06-10 23:11:50 +00:00
|
|
|
return (val1 == val2) ? 0 : NaN;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testInEquality(example, val) {
|
|
|
|
if ('gt' in example) {
|
2014-06-06 15:48:05 +00:00
|
|
|
return compare(val, example.gt) > 0;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
if ('gte' in example) {
|
2014-06-06 15:48:05 +00:00
|
|
|
return compare(val, example.gte) >= 0;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
if ('lt' in example) {
|
2014-06-06 15:48:05 +00:00
|
|
|
return compare(val, example.lt) < 0;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
if ('lte' in example) {
|
2014-06-06 15:48:05 +00:00
|
|
|
return compare(val, example.lte) <= 0;
|
2014-06-06 15:19:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2011-10-03 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.destroyAll = function destroyAll(model, where, options, callback) {
|
2014-09-06 12:09:30 +00:00
|
|
|
var cache = this.collection(model);
|
2014-01-24 17:09:53 +00:00
|
|
|
var filter = null;
|
2015-03-18 09:18:03 +00:00
|
|
|
var count = 0;
|
2014-01-24 17:09:53 +00:00
|
|
|
if (where) {
|
2016-04-14 14:41:19 +00:00
|
|
|
filter = applyFilter({ where: where });
|
|
|
|
Object.keys(cache).forEach(function(id) {
|
2014-09-06 17:24:30 +00:00
|
|
|
if (!filter || filter(this.fromDb(model, cache[id]))) {
|
2015-03-18 09:18:03 +00:00
|
|
|
count++;
|
2014-09-06 17:24:30 +00:00
|
|
|
delete cache[id];
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
2015-03-18 09:18:03 +00:00
|
|
|
count = Object.keys(cache).length;
|
2014-09-06 12:09:30 +00:00
|
|
|
this.collection(model, {});
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2015-03-18 09:18:03 +00:00
|
|
|
this.saveToFile({ count: count }, callback);
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.count = function count(model, where, options, callback) {
|
2014-09-06 12:09:30 +00:00
|
|
|
var cache = this.collection(model);
|
2014-01-24 17:09:53 +00:00
|
|
|
var data = Object.keys(cache);
|
|
|
|
if (where) {
|
2016-04-14 14:41:19 +00:00
|
|
|
var filter = { where: where };
|
|
|
|
data = data.map(function(id) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.fromDb(model, cache[id]);
|
|
|
|
}.bind(this));
|
|
|
|
data = data.filter(applyFilter(filter));
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
process.nextTick(function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
callback(null, data.length);
|
|
|
|
});
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2014-06-17 23:30:02 +00:00
|
|
|
Memory.prototype.update =
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.updateAll = function updateAll(model, where, data, options, cb) {
|
2014-06-17 23:30:02 +00:00
|
|
|
var self = this;
|
2014-09-06 12:09:30 +00:00
|
|
|
var cache = this.collection(model);
|
2014-06-17 23:30:02 +00:00
|
|
|
var filter = null;
|
|
|
|
where = where || {};
|
2016-04-14 14:41:19 +00:00
|
|
|
filter = applyFilter({ where: where });
|
2014-06-17 23:30:02 +00:00
|
|
|
|
|
|
|
var ids = Object.keys(cache);
|
2015-03-12 04:10:30 +00:00
|
|
|
var count = 0;
|
2016-04-14 14:41:19 +00:00
|
|
|
async.each(ids, function(id, done) {
|
2014-06-17 23:30:02 +00:00
|
|
|
var inst = self.fromDb(model, cache[id]);
|
|
|
|
if (!filter || filter(inst)) {
|
2015-03-12 04:10:30 +00:00
|
|
|
count++;
|
2015-02-02 16:44:36 +00:00
|
|
|
// The id value from the cache is string
|
|
|
|
// Get the real id from the inst
|
|
|
|
id = self.getIdValue(model, inst);
|
2015-05-13 16:36:29 +00:00
|
|
|
self.updateAttributes(model, id, data, options, done);
|
2014-06-17 23:30:02 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick(done);
|
|
|
|
}
|
2016-04-14 14:41:19 +00:00
|
|
|
}, function(err) {
|
2015-03-12 04:10:30 +00:00
|
|
|
if (err) return cb(err);
|
2016-04-14 14:41:19 +00:00
|
|
|
self.saveToFile({ count: count }, cb);
|
2014-06-17 23:30:02 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
Memory.prototype.updateAttributes = function updateAttributes(model, id, data, options, 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);
|
2013-06-21 21:56:21 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
throw err;
|
2013-06-21 21:56:21 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
// Do not modify the data object passed in arguments
|
|
|
|
data = Object.create(data);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.setIdValue(model, data, id);
|
|
|
|
|
2014-09-06 12:09:30 +00:00
|
|
|
var cachedModels = this.collection(model);
|
|
|
|
var modelData = cachedModels && this.collection(model)[id];
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (modelData) {
|
2015-05-13 16:36:29 +00:00
|
|
|
this.save(model, data, options, cb);
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
|
|
|
cb(new Error('Could not update attributes. Object with id ' + id + ' does not exist!'));
|
|
|
|
}
|
2011-10-03 13:36:43 +00:00
|
|
|
};
|
|
|
|
|
2015-12-04 18:49:00 +00:00
|
|
|
Memory.prototype.replaceById = function(model, id, data, options, cb) {
|
|
|
|
var self = this;
|
|
|
|
if (!id) {
|
|
|
|
var err = new Error('You must provide an id when replacing!');
|
|
|
|
return process.nextTick(function() { cb(err); });
|
|
|
|
}
|
|
|
|
// Do not modify the data object passed in arguments
|
|
|
|
data = Object.create(data);
|
|
|
|
this.setIdValue(model, data, id);
|
|
|
|
var cachedModels = this.collection(model);
|
|
|
|
var modelData = cachedModels && this.collection(model)[id];
|
|
|
|
if (!modelData) {
|
|
|
|
var msg = 'Could not replace. Object with id ' + id + ' does not exist!';
|
|
|
|
return process.nextTick(function() { cb(new Error(msg)); });
|
|
|
|
}
|
|
|
|
|
|
|
|
var newModelData = {};
|
2016-04-14 14:41:19 +00:00
|
|
|
for (var key in data) {
|
2015-12-04 18:49:00 +00:00
|
|
|
var val = data[key];
|
2016-04-14 14:41:19 +00:00
|
|
|
if (typeof val === 'function') {
|
2015-12-04 18:49:00 +00:00
|
|
|
continue; // Skip methods
|
|
|
|
}
|
|
|
|
newModelData[key] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collection(model)[id] = serialize(newModelData);
|
2016-04-14 14:41:19 +00:00
|
|
|
this.saveToFile(newModelData, function(err) {
|
2015-12-04 18:49:00 +00:00
|
|
|
cb(err, self.fromDb(model, newModelData));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Memory.prototype.replaceOrCreate = function(model, data, options, callback) {
|
|
|
|
var self = this;
|
|
|
|
var idName = self.idNames(model)[0];
|
|
|
|
var idValue = self.getIdValue(model, data);
|
2016-04-14 14:41:19 +00:00
|
|
|
var filter = { where: {}};
|
2015-12-04 18:49:00 +00:00
|
|
|
filter.where[idName] = idValue;
|
|
|
|
var nodes = self._findAllSkippingIncludes(model, filter);
|
|
|
|
var found = nodes[0];
|
|
|
|
|
|
|
|
if (!found) {
|
2016-04-14 14:41:19 +00:00
|
|
|
// Calling _createSync to update the collection in a sync way and
|
2015-12-04 18:49:00 +00:00
|
|
|
// to guarantee to create it in the same turn of even loop
|
|
|
|
return self._createSync(model, data, function(err, id) {
|
|
|
|
if (err) return process.nextTick(function() { cb(err); });
|
|
|
|
self.saveToFile(id, function(err, id) {
|
|
|
|
self.setIdValue(model, data, id);
|
|
|
|
callback(err, self.fromDb(model, data), { isNewInstance: true });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var id = self.getIdValue(model, data);
|
|
|
|
self.collection(model)[id] = serialize(data);
|
|
|
|
self.saveToFile(data, function(err) {
|
2016-04-14 14:41:19 +00:00
|
|
|
callback(err, self.fromDb(model, data), { isNewInstance: false });
|
2015-12-04 18:49:00 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +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
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.exec = function(callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
this.onTransactionExec();
|
|
|
|
setTimeout(callback, 50);
|
2013-04-01 13:49:12 +00:00
|
|
|
};
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.buildNearFilter = function(filter) {
|
2013-06-26 03:31:00 +00:00
|
|
|
// noop
|
2016-04-14 14:41:19 +00:00
|
|
|
};
|
2013-06-26 03:31:00 +00:00
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
Memory.prototype.automigrate = function(models, cb) {
|
2014-09-06 12:38:57 +00:00
|
|
|
var self = this;
|
2014-10-16 19:04:16 +00:00
|
|
|
|
|
|
|
if ((!cb) && ('function' === typeof models)) {
|
|
|
|
cb = models;
|
|
|
|
models = undefined;
|
|
|
|
}
|
|
|
|
// First argument is a model name
|
|
|
|
if ('string' === typeof models) {
|
|
|
|
models = [models];
|
|
|
|
}
|
|
|
|
|
|
|
|
models = models || Object.keys(self._models);
|
|
|
|
if (models.length === 0) {
|
|
|
|
return process.nextTick(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidModels = models.filter(function(m) {
|
|
|
|
return !(m in self._models);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (invalidModels.length) {
|
|
|
|
return process.nextTick(function() {
|
|
|
|
cb(new Error('Cannot migrate models not attached to this datasource: ' +
|
|
|
|
invalidModels.join(' ')));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-06 12:38:57 +00:00
|
|
|
models.forEach(function(m) {
|
|
|
|
self.initCollection(m);
|
|
|
|
});
|
2014-10-16 19:04:16 +00:00
|
|
|
if (cb) process.nextTick(cb);
|
2016-04-14 14:41:19 +00:00
|
|
|
};
|
2014-09-06 12:38:57 +00:00
|
|
|
|
2011-10-03 13:36:43 +00:00
|
|
|
function merge(base, update) {
|
2014-05-09 22:27:45 +00:00
|
|
|
if (!base) {
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
// We cannot use Object.keys(update) if the update is an instance of the model
|
|
|
|
// class as the properties are defined at the ModelClass.prototype level
|
2016-04-14 14:41:19 +00:00
|
|
|
for (var key in update) {
|
2014-05-09 22:27:45 +00:00
|
|
|
var val = update[key];
|
2016-04-14 14:41:19 +00:00
|
|
|
if (typeof val === 'function') {
|
2014-05-09 22:27:45 +00:00
|
|
|
continue; // Skip methods
|
|
|
|
}
|
|
|
|
base[key] = val;
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
return base;
|
2014-05-07 14:47:12 +00:00
|
|
|
}
|