Merge pull request #8 from strongloop/id-fix
Allows custom name of the id property for the memory connector
This commit is contained in:
commit
bfcd850dfe
|
@ -37,21 +37,63 @@ 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;
|
||||
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() {
|
||||
callback(null, id);
|
||||
|
@ -59,13 +101,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 +115,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 +123,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 +151,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 +171,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 +205,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 +304,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 +327,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];
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue