Allow `attributes` as an alias for `properties` (for LDL)
This commit is contained in:
parent
ec05d0b5a8
commit
0e49dc94ec
25
lib/dao.js
25
lib/dao.js
|
@ -95,6 +95,7 @@ DataAccessObject.applyScope = function(query, inst) {
|
||||||
|
|
||||||
DataAccessObject.applyProperties = function(data, inst) {
|
DataAccessObject.applyProperties = function(data, inst) {
|
||||||
var properties = this.definition.settings.properties;
|
var properties = this.definition.settings.properties;
|
||||||
|
properties = properties || this.definition.settings.attributes;
|
||||||
if (typeof properties === 'object') {
|
if (typeof properties === 'object') {
|
||||||
util._extend(data, properties);
|
util._extend(data, properties);
|
||||||
} else if (typeof properties === 'function') {
|
} else if (typeof properties === 'function') {
|
||||||
|
@ -107,6 +108,10 @@ DataAccessObject.applyProperties = function(data, inst) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DataAccessObject.lookupModel = function(data) {
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an instance of Model with given data and save to the attached data source. Callback is optional.
|
* Create an instance of Model with given data and save to the attached data source. Callback is optional.
|
||||||
* Example:
|
* Example:
|
||||||
|
@ -127,7 +132,7 @@ DataAccessObject.create = function (data, callback) {
|
||||||
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
||||||
|
|
||||||
var Model = this;
|
var Model = this;
|
||||||
var modelName = Model.modelName;
|
var self = this;
|
||||||
|
|
||||||
if (typeof data === 'function') {
|
if (typeof data === 'function') {
|
||||||
callback = data;
|
callback = data;
|
||||||
|
@ -154,6 +159,7 @@ DataAccessObject.create = function (data, callback) {
|
||||||
|
|
||||||
for (var i = 0; i < data.length; i += 1) {
|
for (var i = 0; i < data.length; i += 1) {
|
||||||
(function (d, i) {
|
(function (d, i) {
|
||||||
|
Model = self.lookupModel(d); // data-specific
|
||||||
instances.push(Model.create(d, function (err, inst) {
|
instances.push(Model.create(d, function (err, inst) {
|
||||||
if (err) {
|
if (err) {
|
||||||
errors[i] = err;
|
errors[i] = err;
|
||||||
|
@ -169,7 +175,11 @@ DataAccessObject.create = function (data, callback) {
|
||||||
function modelCreated() {
|
function modelCreated() {
|
||||||
if (--wait === 0) {
|
if (--wait === 0) {
|
||||||
callback(gotError ? errors : null, instances);
|
callback(gotError ? errors : null, instances);
|
||||||
if(!gotError) instances.forEach(Model.emit.bind('changed'));
|
if(!gotError) {
|
||||||
|
instances.forEach(function(inst) {
|
||||||
|
inst.constructor.emit('changed');
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,6 +198,9 @@ DataAccessObject.create = function (data, callback) {
|
||||||
this.applyProperties(enforced, obj);
|
this.applyProperties(enforced, obj);
|
||||||
obj.setAttributes(enforced);
|
obj.setAttributes(enforced);
|
||||||
|
|
||||||
|
Model = this.lookupModel(data); // data-specific
|
||||||
|
if (Model !== obj.constructor) obj = new Model(data);
|
||||||
|
|
||||||
data = obj.toObject(true);
|
data = obj.toObject(true);
|
||||||
|
|
||||||
// validation required
|
// validation required
|
||||||
|
@ -204,6 +217,7 @@ DataAccessObject.create = function (data, callback) {
|
||||||
obj.trigger('save', function (saveDone) {
|
obj.trigger('save', function (saveDone) {
|
||||||
|
|
||||||
var _idName = idName(Model);
|
var _idName = idName(Model);
|
||||||
|
var modelName = Model.modelName;
|
||||||
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
|
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
|
||||||
if (id) {
|
if (id) {
|
||||||
obj.__data[_idName] = id;
|
obj.__data[_idName] = id;
|
||||||
|
@ -251,7 +265,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
if (stillConnecting(this.getDataSource(), this, arguments)) {
|
if (stillConnecting(this.getDataSource(), this, arguments)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
var self = this;
|
||||||
var Model = this;
|
var Model = this;
|
||||||
if (!getIdValue(this, data)) {
|
if (!getIdValue(this, data)) {
|
||||||
return this.create(data, callback);
|
return this.create(data, callback);
|
||||||
|
@ -265,6 +279,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
update = inst.toObject(false);
|
update = inst.toObject(false);
|
||||||
this.applyProperties(update, inst);
|
this.applyProperties(update, inst);
|
||||||
update = removeUndefined(update);
|
update = removeUndefined(update);
|
||||||
|
Model = this.lookupModel(update);
|
||||||
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
|
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
|
||||||
var obj;
|
var obj;
|
||||||
if (data && !(data instanceof Model)) {
|
if (data && !(data instanceof Model)) {
|
||||||
|
@ -286,6 +301,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
if (inst) {
|
if (inst) {
|
||||||
inst.updateAttributes(data, callback);
|
inst.updateAttributes(data, callback);
|
||||||
} else {
|
} else {
|
||||||
|
Model = self.lookupModel(data);
|
||||||
var obj = new Model(data);
|
var obj = new Model(data);
|
||||||
obj.save(data, callback);
|
obj.save(data, callback);
|
||||||
}
|
}
|
||||||
|
@ -774,7 +790,8 @@ DataAccessObject.find = function find(query, cb) {
|
||||||
this.getDataSource().connector.all(this.modelName, query, function (err, data) {
|
this.getDataSource().connector.all(this.modelName, query, function (err, data) {
|
||||||
if (data && data.forEach) {
|
if (data && data.forEach) {
|
||||||
data.forEach(function (d, i) {
|
data.forEach(function (d, i) {
|
||||||
var obj = new self(d, {fields: query.fields, applySetters: false, persisted: true});
|
var Model = self.lookupModel(d);
|
||||||
|
var obj = new Model(d, {fields: query.fields, applySetters: false, persisted: true});
|
||||||
|
|
||||||
if (query && query.include) {
|
if (query && query.include) {
|
||||||
if (query.collect) {
|
if (query.collect) {
|
||||||
|
|
|
@ -64,6 +64,12 @@ describe('default scope', function () {
|
||||||
scopes: { active: { where: { active: true } } }
|
scopes: { active: { where: { active: true } } }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Product.lookupModel = function(data) {
|
||||||
|
var m = this.dataSource.models[data.kind];
|
||||||
|
if (m.base === this) return m;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
Tool = db.define('Tool', Product.definition.properties, {
|
Tool = db.define('Tool', Product.definition.properties, {
|
||||||
base: 'Product',
|
base: 'Product',
|
||||||
scope: { where: { kind: 'Tool' }, order: 'name' },
|
scope: { where: { kind: 'Tool' }, order: 'name' },
|
||||||
|
@ -94,7 +100,7 @@ describe('default scope', function () {
|
||||||
|
|
||||||
Thing = db.define('Thing', Product.definition.properties, {
|
Thing = db.define('Thing', Product.definition.properties, {
|
||||||
base: 'Product',
|
base: 'Product',
|
||||||
properties: propertiesFn,
|
attributes: propertiesFn,
|
||||||
scope: scopeFn,
|
scope: scopeFn,
|
||||||
mongodb: { collection: 'Product' },
|
mongodb: { collection: 'Product' },
|
||||||
memory: { collection: 'Product' }
|
memory: { collection: 'Product' }
|
||||||
|
@ -206,6 +212,7 @@ describe('default scope', function () {
|
||||||
Product.findById(ids.toolA, function(err, inst) {
|
Product.findById(ids.toolA, function(err, inst) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
inst.name.should.equal('Tool A');
|
inst.name.should.equal('Tool A');
|
||||||
|
inst.should.be.instanceof(Tool);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -245,6 +252,13 @@ describe('default scope', function () {
|
||||||
products[2].name.should.equal('Widget A');
|
products[2].name.should.equal('Widget A');
|
||||||
products[3].name.should.equal('Widget B');
|
products[3].name.should.equal('Widget B');
|
||||||
products[4].name.should.equal('Widget Z');
|
products[4].name.should.equal('Widget Z');
|
||||||
|
|
||||||
|
products[0].should.be.instanceof(Product);
|
||||||
|
products[0].should.be.instanceof(Tool);
|
||||||
|
|
||||||
|
products[2].should.be.instanceof(Product);
|
||||||
|
products[2].should.be.instanceof(Widget);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -702,11 +716,20 @@ describe('default scope', function () {
|
||||||
cat.products(function(err, products) {
|
cat.products(function(err, products) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
products.should.have.length(4);
|
products.should.have.length(4);
|
||||||
products[0].should.be.instanceof(Product);
|
|
||||||
products[0].name.should.equal('Thing A');
|
products[0].name.should.equal('Thing A');
|
||||||
products[1].name.should.equal('Tool A');
|
products[1].name.should.equal('Tool A');
|
||||||
products[2].name.should.equal('Widget A');
|
products[2].name.should.equal('Widget A');
|
||||||
products[3].name.should.equal('Widget B');
|
products[3].name.should.equal('Widget B');
|
||||||
|
|
||||||
|
products[0].should.be.instanceof(Product);
|
||||||
|
products[0].should.be.instanceof(Thing);
|
||||||
|
|
||||||
|
products[1].should.be.instanceof(Product);
|
||||||
|
products[1].should.be.instanceof(Tool);
|
||||||
|
|
||||||
|
products[2].should.be.instanceof(Product);
|
||||||
|
products[2].should.be.instanceof(Widget);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue