Skip to content

Commit ba74d85

Browse files
committed
Changes Model.one() to return only one instance (or null) instead of an Array (dresende#148)
1 parent 1c9e1ba commit ba74d85

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

lib/Model.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,28 @@ function Model(opts) {
297297

298298
model.one = function () {
299299
var args = Array.prototype.slice.apply(arguments);
300+
var cb = null;
301+
302+
// extract callback
303+
for (var i = 0; i < args.length; i++) {
304+
if (typeof args[i] == "function") {
305+
cb = args.splice(i, 1)[0];
306+
break;
307+
}
308+
}
309+
310+
if (cb === null) {
311+
throw new Error("Model.one() called without callback");
312+
}
313+
314+
// add limit 1
300315
args.push(1);
316+
args.push(function (err, results) {
317+
if (err) {
318+
return cb(err);
319+
}
320+
return cb(null, results.length ? results[0] : null);
321+
});
301322

302323
return this.find.apply(this, args);
303324
};

test/integration/test-one-conditions.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ common.createConnection(function (err, db) {
1313

1414
var TestModel = db.define('test_one_conditions', common.getModelProperties());
1515

16-
TestModel.one({ name: 'test1' }, function (err, Instances) {
16+
TestModel.one({ name: 'test2' }, function (err, Instance) {
1717
assert.equal(err, null);
18-
assert.equal(Array.isArray(Instances), true);
19-
assert.equal(Instances.length, 1);
18+
assert.equal(Instance, null);
2019
db.close();
2120
});
2221
});

test/integration/test-one-order.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ common.createConnection(function (err, db) {
1313

1414
var TestModel = db.define('test_one_order', common.getModelProperties());
1515

16-
TestModel.one("-name", function (err, Instances) {
16+
TestModel.one("-name", function (err, Instance) {
1717
assert.equal(err, null);
18-
assert.equal(Array.isArray(Instances), true);
19-
assert.equal(Instances.length, 1);
20-
assert.equal(Instances[0].id, 4);
18+
assert.equal(!Array.isArray(Instance), true);
19+
assert.equal(Instance.id, 4);
2120
db.close();
2221
});
2322
});

test/integration/test-one.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ common.createConnection(function (err, db) {
1111

1212
var TestModel = db.define('test_one', common.getModelProperties());
1313

14-
TestModel.one(function (err, Instances) {
14+
TestModel.one(function (err, Instance) {
1515
assert.equal(err, null);
16-
assert.equal(Array.isArray(Instances), true);
17-
assert.equal(Instances.length, 1);
16+
assert.equal(!Array.isArray(Instance), true);
17+
assert.equal(typeof Instance.id, "number");
1818
db.close();
1919
});
2020
});

0 commit comments

Comments
 (0)