Fix the id references to allow custom name other than 'id'
This commit is contained in:
parent
8aec0a3b47
commit
281cc38017
43
lib/dao.js
43
lib/dao.js
|
@ -39,6 +39,19 @@ function DataAccessObject() {
|
|||
}
|
||||
}
|
||||
|
||||
function idName(m) {
|
||||
return m.dataSource.idName ? m.dataSource.idName(m.modelName) : 'id';
|
||||
}
|
||||
|
||||
function getIdValue(m, data) {
|
||||
return data && data[m.dataSource.idName(m.modelName)];
|
||||
}
|
||||
|
||||
function setIdValue(m, data, value) {
|
||||
if(data) {
|
||||
data[idName(m)] = value;
|
||||
}
|
||||
}
|
||||
|
||||
DataAccessObject._forDB = function (data) {
|
||||
if(!(this.dataSource.isRelational && this.dataSource.isRelational())) {
|
||||
|
@ -92,6 +105,7 @@ DataAccessObject.create = function (data, callback) {
|
|||
var wait = data.length;
|
||||
if (wait === 0) callback(null, []);
|
||||
|
||||
var instances = [];
|
||||
for (var i = 0; i < data.length; i += 1) {
|
||||
(function(d, i) {
|
||||
instances.push(Model.create(d, function(err, inst) {
|
||||
|
@ -136,14 +150,15 @@ DataAccessObject.create = function (data, callback) {
|
|||
obj.trigger('create', function(createDone) {
|
||||
obj.trigger('save', function(saveDone) {
|
||||
|
||||
var _idName = idName(Model);
|
||||
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
|
||||
if (id) {
|
||||
obj.__data.id = id;
|
||||
obj.__dataWas.id = id;
|
||||
defineReadonlyProp(obj, 'id', id);
|
||||
obj.__data[_idName] = id;
|
||||
obj.__dataWas[_idName] = id;
|
||||
defineReadonlyProp(obj, _idName, id);
|
||||
}
|
||||
if (rev) {
|
||||
obj._rev = rev
|
||||
obj._rev = rev;
|
||||
}
|
||||
if (err) {
|
||||
return callback(err, obj);
|
||||
|
@ -179,16 +194,6 @@ function stillConnecting(dataSource, obj, args) {
|
|||
dataSource.connect();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
function getIdValue(m, data) {
|
||||
return data && data[m.dataSource.idName(m.modelName)];
|
||||
}
|
||||
|
||||
function setIdValue(m, data, value) {
|
||||
if(data) {
|
||||
data[m.dataSource.idName(m.modelName)] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -544,7 +549,7 @@ DataAccessObject.prototype.save = function (options, callback) {
|
|||
var Model = this.constructor;
|
||||
var modelName = Model.modelName;
|
||||
|
||||
if (!this.id) {
|
||||
if (!getIdValue(Model, this)) {
|
||||
return Model.create(this, callback);
|
||||
}
|
||||
|
||||
|
@ -594,7 +599,7 @@ DataAccessObject.prototype.save.http = [
|
|||
];
|
||||
|
||||
DataAccessObject.prototype.isNewRecord = function () {
|
||||
return !this.id;
|
||||
return !getIdValue(this.constructor, this);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -615,7 +620,7 @@ DataAccessObject.prototype.destroy = function (cb) {
|
|||
if (stillConnecting(this.constructor.dataSource, this, arguments)) return;
|
||||
|
||||
this.trigger('destroy', function (destroyed) {
|
||||
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
|
||||
this._adapter().destroy(this.constructor.modelName, getIdValue(this.constructor, this), function (err) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
@ -690,7 +695,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
|
|||
inst[key] = data[key];
|
||||
});
|
||||
|
||||
inst._adapter().updateAttributes(model, inst.id, inst.constructor._forDB(data), function (err) {
|
||||
inst._adapter().updateAttributes(model, getIdValue(inst.constructor, inst), inst.constructor._forDB(data), function (err) {
|
||||
if (!err) {
|
||||
// update _was attrs
|
||||
Object.keys(data).forEach(function (key) {
|
||||
|
@ -726,7 +731,7 @@ DataAccessObject.prototype.updateAttributes.http = [
|
|||
DataAccessObject.prototype.reload = function reload(callback) {
|
||||
if (stillConnecting(this.constructor.dataSource, this, arguments)) return;
|
||||
|
||||
this.constructor.findById(this.id, callback);
|
||||
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
||||
};
|
||||
|
||||
DataAccessObject.prototype.reload.shared = true;
|
||||
|
|
Loading…
Reference in New Issue