support optimized findOrCreate

Signed-off-by: Clark Wang <clark.wangs@gmail.com>

remove undefined for creating data in findOrCreate

Signed-off-by: Clark Wang <clark.wangs@gmail.com>

getLastGeneratedUid instead of force an id

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2015-02-03 15:34:49 +08:00
parent e9c966227d
commit 0bef56efc0
3 changed files with 114 additions and 29 deletions

View File

@ -373,9 +373,11 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
* @param {Object} query Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
* For example: `{where: {test: 'me'}}`.
* @param {Object} data Object to create.
* @param {Function} cb Callback called with (err, instance, created)
* @param {Function} callback Callback called with (err, instance, created)
*/
DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (query === undefined) {
query = {where: {}};
}
@ -388,14 +390,81 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
};
}
var self = this;
var Model = this;
Model.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record, false);
Model.create(data, function (err, record) {
callback(err, record, record != null);
if (this.getDataSource().connector.findOrCreate) {
query.limit = 1;
try {
this._normalize(query);
} catch (err) {
return process.nextTick(function () {
callback && callback(err);
});
}
this.applyScope(query);
Model.notifyObserversOf('access', { Model: Model, query: query }, function (err, ctx) {
if (err) return callback(err);
var query = ctx.query;
var enforced = {};
var Model = self.lookupModel(data);
var obj = data instanceof Model ? data : new Model(data);
Model.applyProperties(enforced, obj);
obj.setAttributes(enforced);
Model.notifyObserversOf('before save', { Model: Model, instance: obj }, function(err, ctx) {
if (err) return callback(err);
var obj = ctx.instance;
var data = obj.toObject(true);
// validation required
obj.isValid(function (valid) {
if (valid) {
findOrCreate(query, data);
} else {
callback(new ValidationError(obj), obj);
}
}, data);
});
});
});
function findOrCreate(query, data) {
var modelName = Model.modelName;
data = removeUndefined(data);
self.getDataSource().connector.findOrCreate(modelName, query, self._forDB(data), function (err, data, created) {
var obj, Model = self.lookupModel(data);
if (data) {
obj = new Model(data, {fields: query.fields, applySetters: false, persisted: true});
}
if (created) {
Model.notifyObserversOf('after save', { Model: Model, instance: obj }, function(err) {
callback(err, obj, created);
if(!err) Model.emit('changed', obj);
});
} else {
callback(err, obj, created);
}
});
}
} else {
Model.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record, false);
Model.create(data, function (err, record) {
callback(err, record, record != null);
});
});
}
};
/**

View File

@ -399,16 +399,29 @@ describe('Memory connector', function() {
});
});
});
});
require('./persistence-hooks.suite')(
new DataSource({ connector: Memory }),
should);
describe('Optimized connector', function() {
var ds = new DataSource({ connector: Memory });
// optimized methods
ds.connector.findOrCreate = function (model, query, data, callback) {
this.all(model, query, function (err, list) {
if (err || (list && list[0])) return callback(err, list && list[0], false);
this.create(model, data, function (err) {
callback(err, data, true);
});
}.bind(this));
};
require('./persistence-hooks.suite')(ds, should);
});
describe('Unoptimized connector', function() {
var ds = new DataSource({ connector: Memory });
// disable optimized methods
ds.connector.updateOrCreate = false;
ds.connector.findOrCreate = false;
require('./persistence-hooks.suite')(ds, should);
});

View File

@ -274,26 +274,25 @@ module.exports = function(dataSource, should) {
});
});
// TODO(bajtos) Enable this test for all connectors that
// provide optimized implementation of findOrCreate.
// The unoptimized implementation does not trigger the hook
// when an existing model was found.
it.skip('triggers `before save` hook when found', function(done) {
TestModel.observe('before save', pushContextAndNext());
if (dataSource.connector.findOrCreate) {
it('triggers `before save` hook when found', function(done) {
TestModel.observe('before save', pushContextAndNext());
TestModel.findOrCreate(
{ where: { name: existingInstance.name } },
{ name: existingInstance.name },
function(err, record, created) {
if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({ instance: {
id: record.id,
name: existingInstance.name,
extra: undefined
}}));
done();
});
});
TestModel.findOrCreate(
{ where: { name: existingInstance.name } },
{ name: existingInstance.name },
function(err, record, created) {
if (err) return done(err);
record.id.should.eql(existingInstance.id);
observedContexts.should.eql(aTestModelCtx({ instance: {
id: getLastGeneratedUid(),
name: existingInstance.name,
extra: undefined
}}));
done();
});
});
}
it('triggers `before save` hook when not found', function(done) {
TestModel.observe('before save', pushContextAndNext());
@ -1250,6 +1249,10 @@ module.exports = function(dataSource, should) {
lastId += 1;
return '' + lastId;
}
function getLastGeneratedUid() {
return '' + lastId;
}
});
function deepCloneToObject(obj) {