From 08faf9f5ded5ac254ab6bc57b1eb454a2dd135da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Sat, 6 Feb 2016 11:22:53 +0100 Subject: [PATCH] Update describe-operation-hooks - drop custom findOrCreate implementation - memory connector provides an atomic implementation out of the box now - add new methods `replaceOrCreate` and `replaceByID` - fix error reporting to include the stack trace in the console output [back-port of pull request #847] --- support/describe-operation-hooks.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/support/describe-operation-hooks.js b/support/describe-operation-hooks.js index d9a30391..cfb38084 100644 --- a/support/describe-operation-hooks.js +++ b/support/describe-operation-hooks.js @@ -61,6 +61,21 @@ var operations = [ { id: ds.existingInstance.id, name: 'new name' }); }, + function replaceOrCreate_create(ds) { + return ds.TestModel.replaceOrCreate({ id: 'not-found', name: 'not found' }); + }, + + function replaceOrCreate_update(ds) { + return ds.TestModel.replaceOrCreate( + { id: ds.existingInstance.id, name: 'new name' }); + }, + + function replaceById(ds) { + return ds.TestModel.replaceById( + ds.existingInstance.id, + { name: 'new name' }); + }, + function updateAll(ds) { return ds.TestModel.updateAll({ name: 'searched' }, { name: 'updated' }); }, @@ -88,22 +103,12 @@ operations.forEach(function(op) { p = p.then(runner(op)); }); -p.then(report, console.error); +p.then(report, function(err) { console.error(err.stack); }); function createOptimizedDataSource() { var ds = new DataSource({ connector: Memory }); ds.name = 'Optimized'; - - 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)); - }; - return ds; } @@ -114,6 +119,7 @@ function createUnoptimizedDataSource() { // disable optimized methods ds.connector.updateOrCreate = false; ds.connector.findOrCreate = false; + ds.connector.replaceOrCreate = false; return ds; }