From 9dbc8fa50b9fd25386a67e2b5fcc9d33f4435a38 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Tue, 22 Jan 2013 01:21:31 +0700 Subject: [PATCH 1/3] Implement findOrCreate, requested in #190 --- lib/abstract-class.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 8167359c..bb10a2f7 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -258,6 +258,34 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call } }; +/** + * Find one record, same as `all`, limited by 1 and return object, not collection, + * if not found, create using data provided as second argument + * + * @param {Object} query - search conditions: {where: {test: 'me'}}. + * @param {Object} data - object to create. + * @param {Function} cb - callback called with (err, instance) + */ +AbstractClass.findOrCreate = function findOrCreate(query, data, callback) { + if (typeof query === 'undefined') { + query = {where: {}}; + } + if (typeof data === 'function' || typeof data === 'undefined') { + callback = data; + data = query && query.where; + } + if (typeof callback === 'undefined') { + callback = function () {}; + } + + var t = this; + this.findOne(query, function (err, record) { + if (err) return callback(err); + if (record) return callback(null, record); + t.create(data, callback); + }); +}; + /** * Check whether object exitst in database * @@ -341,7 +369,7 @@ AbstractClass.all = function all(params, cb) { /** * Find one record, same as `all`, limited by 1 and return object, not collection * - * @param {Object} params - search conditions + * @param {Object} params - search conditions: {where: {test: 'me'}} * @param {Function} cb - callback called with (err, instance) */ AbstractClass.findOne = function findOne(params, cb) { From 91fee1d56587e8496a342f95643868a18b498360 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Tue, 22 Jan 2013 01:21:43 +0700 Subject: [PATCH 2/3] Make memory adapter async --- lib/adapters/memory.js | 24 +++++++++++++++++------- test/common_test.js | 35 +++++++++++++---------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/adapters/memory.js b/lib/adapters/memory.js index 9be7438f..314b1555 100644 --- a/lib/adapters/memory.js +++ b/lib/adapters/memory.js @@ -20,7 +20,9 @@ Memory.prototype.create = function create(model, data, callback) { var id = data.id || this.ids[model]++; data.id = id; this.cache[model][id] = data; - callback(null, id); + process.nextTick(function () { + callback(null, id); + }); }; Memory.prototype.updateOrCreate = function (model, data, callback) { @@ -39,20 +41,26 @@ Memory.prototype.updateOrCreate = function (model, data, callback) { Memory.prototype.save = function save(model, data, callback) { this.cache[model][data.id] = data; - callback(null, data); + process.nextTick(function () { + callback(null, data); + }); }; Memory.prototype.exists = function exists(model, id, callback) { - callback(null, this.cache[model].hasOwnProperty(id)); + process.nextTick(function () { + callback(null, this.cache[model].hasOwnProperty(id)); + }.bind(this)); }; Memory.prototype.find = function find(model, id, callback) { - callback(null, this.cache[model][id]); + process.nextTick(function () { + callback(null, this.cache[model][id]); + }.bind(this)); }; Memory.prototype.destroy = function destroy(model, id, callback) { delete this.cache[model][id]; - callback(); + process.nextTick(callback); }; Memory.prototype.all = function all(model, filter, callback) { @@ -132,7 +140,7 @@ Memory.prototype.destroyAll = function destroyAll(model, callback) { delete this.cache[model][id]; }.bind(this)); this.cache[model] = {}; - callback(); + process.nextTick(callback); }; Memory.prototype.count = function count(model, callback, where) { @@ -149,7 +157,9 @@ Memory.prototype.count = function count(model, callback, where) { return ok; }); } - callback(null, data.length); + process.nextTick(function () { + callback(null, data.length); + }); }; Memory.prototype.updateAttributes = function updateAttributes(model, id, data, cb) { diff --git a/test/common_test.js b/test/common_test.js index 329a437e..80e04c48 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -543,28 +543,6 @@ function testOrm(schema) { //User.create(function (e, u) { // u.posts.create({}, function (e, p) { // find all posts for a user. - /* User.all(function (err,users) { - for (var i=0;i Date: Tue, 22 Jan 2013 01:22:08 +0700 Subject: [PATCH 3/3] Add adapter (memory-bogus) test --- test/adapters_test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/adapters_test.js diff --git a/test/adapters_test.js b/test/adapters_test.js new file mode 100644 index 00000000..dd621a92 --- /dev/null +++ b/test/adapters_test.js @@ -0,0 +1,10 @@ +var jdb = require('jugglingdb'), + Schema = jdb.Schema, + test = jdb.test; + +var schema = new Schema('memory'); + +test(module.exports, schema); + +test.skip('hasMany should be cached'); +