From 6f111f81225d67ff9cd64f06e97f5f05aaaba39c Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 27 Aug 2013 22:32:01 -0700 Subject: [PATCH 1/2] Allows custom name of the id property for the memory connector --- lib/connectors/memory.js | 81 ++++++++++++++++++++++++++++++---------- lib/datasource.js | 3 ++ lib/scope.js | 1 + 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 83d8eb08..5a155e5f 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -37,21 +37,62 @@ Memory.prototype.define = function defineModel(descr) { this.ids[m] = 1; }; +/** + * Get the id property name for the given model + * @param {String} model The model name + * @returns {String} The id property name + * + * @private + */ +Memory.prototype.idName = function(model) { + return (this.dataSource && this.dataSource.idName && this.dataSource.idName(model)) || 'id'; +}; + +/** + * Get the id value for the given model + * @param {String} model The model name + * @param {Object} data The model instance data + * @returns {*} The id value + * + * @private + */ +Memory.prototype.getIdValue = function(model, data) { + return data && data[this.idName(model)]; +}; + +/** + * Set the id value for the given model + * @param {String} model The model name + * @param {Object} data The model instance data + * @param {*} value The id value + * + * @private + */ +Memory.prototype.setIdValue = function(model, data, value) { + if(data) { + data[this.idName(model)] = value; + } +}; + Memory.prototype.create = function create(model, data, callback) { + // 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 = data.id || currentId; + 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] = currentId + 1; + this.ids[model] = Number(currentId) + 1; - data.id = id; + var props = this._models[model].properties; + id = props[this.idName(model)].type(id); + this.setIdValue(model, data, id); this.cache[model][id] = JSON.stringify(data); process.nextTick(function() { callback(null, id); @@ -59,13 +100,13 @@ Memory.prototype.create = function create(model, data, callback) { }; Memory.prototype.updateOrCreate = function (model, data, callback) { - var mem = this; - this.exists(model, data.id, function (err, exists) { + var self = this; + this.exists(model, self.getIdValue(model, data), function (err, exists) { if (exists) { - mem.save(model, data, callback); + self.save(model, data, callback); } else { - mem.create(model, data, function (err, id) { - data.id = id; + self.create(model, data, function (err, id) { + self.setIdValue(model, data, id); callback(err, data); }); } @@ -73,7 +114,7 @@ Memory.prototype.updateOrCreate = function (model, data, callback) { }; Memory.prototype.save = function save(model, data, callback) { - this.cache[model][data.id] = JSON.stringify(data); + this.cache[model][this.getIdValue(model, data)] = JSON.stringify(data); process.nextTick(function () { callback(null, data); }); @@ -81,11 +122,12 @@ Memory.prototype.save = function save(model, data, callback) { Memory.prototype.exists = function exists(model, id, callback) { process.nextTick(function () { - callback(null, this.cache[model].hasOwnProperty(id)); + callback(null, this.cache[model] && this.cache[model].hasOwnProperty(id)); }.bind(this)); }; Memory.prototype.find = function find(model, id, callback) { + var self = this; process.nextTick(function () { callback(null, id in this.cache[model] && this.fromDb(model, this.cache[model][id])); }.bind(this)); @@ -108,11 +150,14 @@ Memory.prototype.fromDb = function(model, data) { if (props[key]) { switch(props[key].type.name) { case 'Date': - val = new Date(val.toString().replace(/GMT.*$/, 'GMT')); - break; + val = new Date(val.toString().replace(/GMT.*$/, 'GMT')); + break; case 'Boolean': - val = new Boolean(val); - break; + val = Boolean(val); + break; + case 'Number': + val = Number(val); + break; } } data[key] = val; @@ -125,12 +170,10 @@ Memory.prototype.all = function all(model, filter, callback) { var nodes = Object.keys(this.cache[model]).map(function (key) { return this.fromDb(model, this.cache[model][key]); }.bind(this)); - var isGeo = false; if (filter) { // do we need some sorting? if (filter.order) { - var props = this._models[model].properties; var orders = filter.order; if (typeof filter.order === "string") { orders = [filter.order]; @@ -161,7 +204,7 @@ Memory.prototype.all = function all(model, filter, callback) { // field selection if(filter.fields) { - nodes = nodes.map(utils.selectFields(filter.fields)) + nodes = nodes.map(utils.selectFields(filter.fields)); } // limit/skip @@ -260,7 +303,7 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) { Memory.prototype.count = function count(model, callback, where) { var cache = this.cache[model]; - var data = Object.keys(cache) + var data = Object.keys(cache); if (where) { var filter = {where: where}; data = data.map(function (id) { @@ -283,7 +326,7 @@ Memory.prototype.updateAttributes = function updateAttributes(model, id, data, c } } - data.id = id; + this.setIdValue(model, data, id); var cachedModels = this.cache[model]; var modelAsString = cachedModels && this.cache[model][id]; diff --git a/lib/datasource.js b/lib/datasource.js index 56b1fdf6..2d7215fb 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1255,6 +1255,9 @@ DataSource.prototype.idColumnName = function(modelName) { */ DataSource.prototype.idName = function(modelName) { var props = this.definitions[modelName].properties; + if(props.id && props.id.id) { + return 'id'; + } for(var key in props) { if(props[key].id) { return key; diff --git a/lib/scope.js b/lib/scope.js index a8e89954..ddefdedd 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -132,6 +132,7 @@ function defineScope(cls, targetClass, name, params, methods) { } function mergeParams(base, update) { + base = base || {}; if (update.where) { base.where = merge(base.where, update.where); } From 0866f33cec3131f9b4ece50648419f423f92aa96 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 28 Aug 2013 21:39:59 -0700 Subject: [PATCH 2/2] Check the existence of id --- lib/connectors/memory.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 5a155e5f..2749c040 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -91,7 +91,8 @@ Memory.prototype.create = function create(model, data, callback) { this.ids[model] = Number(currentId) + 1; var props = this._models[model].properties; - id = props[this.idName(model)].type(id); + var idName = this.idName(model); + id = (props[idName] && props[idName].type && props[idName].type(id)) || id; this.setIdValue(model, data, id); this.cache[model][id] = JSON.stringify(data); process.nextTick(function() {