diff --git a/addon/db.js b/addon/db.js index 112116fbf..805749f7a 100644 --- a/addon/db.js +++ b/addon/db.js @@ -74,7 +74,20 @@ export default function() { return returnData; }; - this._find = function(collection, id) { + this._find = function(collection, ids) { + var _this = this; + + if (Ember.isArray(ids)) { + return ids.map(function(id) { + return _this._findRecordForId(collection, id); + }); + + } else { + return this._findRecordForId(collection, ids); + } + }; + + this._findRecordForId = function(collection, id) { // If parses, coerce to integer if (typeof id === "string" && allDigitsRegex.test(id)) { id = parseInt(id, 10); diff --git a/addon/schema.js b/addon/schema.js index e7a779216..d3a316ea7 100644 --- a/addon/schema.js +++ b/addon/schema.js @@ -63,7 +63,7 @@ export default function(db) { this._find = function(type, ids) { var collection = pluralize(type); - if (!db[collection]) { + if (db[collection]) { return null; } var attrs = db[collection].find(ids); diff --git a/tests/unit/db-test.js b/tests/unit/db-test.js index 3edf698f8..c75c39006 100644 --- a/tests/unit/db-test.js +++ b/tests/unit/db-test.js @@ -105,6 +105,7 @@ module('mirage:db#find', { db = new Db(); db.createCollection('contacts'); db.contacts.insert([ + {id: 1, name: 'Zelda'}, {id: 2, name: 'Link'}, {id: 'abc', name: 'Ganon'} ]); @@ -142,6 +143,12 @@ test('returns a record whose id is a string that start with numbers', function(a assert.deepEqual(contact, {id: '123-456', name: 'Epona'}); }); +test('returns multiple record that matche an arrya of ids', function(assert) { + var contacts = db.contacts.find([1, 2]); + + assert.deepEqual(contacts, [{id: 1, name: 'Zelda'}, {id: 2, name: 'Link'}]); +}); + module('mirage:db#where', { beforeEach: function() { diff --git a/tests/unit/schema-test.js b/tests/unit/schema-test.js index 2c35fce75..14a11b007 100644 --- a/tests/unit/schema-test.js +++ b/tests/unit/schema-test.js @@ -9,7 +9,7 @@ test('it can be instantiated', function(assert) { assert.ok(schema); }); -test('it can create models', function(assert) { +test('it can create registered models', function(assert) { var schema = new Schema(new Db()); var User = Model.extend(); @@ -22,6 +22,14 @@ test('it can create models', function(assert) { assert.deepEqual(user.attrs, {id: 1, name: 'Link'}); }); +test('it cannot create models that havnet been registered', function(assert) { + var schema = new Schema(new Db()); + + assert.throws(function() { + schema.user.create({name: 'Link'}); + }); +}); + test('it can return all models', function(assert) { var db = new Db(); db.createCollection('users'); @@ -38,7 +46,20 @@ test('it can return all models', function(assert) { assert.deepEqual(users[1].attrs, {id: 2, name: 'Zelda'}); }); -test('it can find models', function(assert) { +test('it returns an empty array when no models exist', function(assert) { + var db = new Db(); + db.createCollection('users'); + var schema = new Schema(db); + + var User = Model.extend(); + schema.register('user', User); + + var users = schema.user.all(); + + assert.equal(users.length, 0); +}); + +test('it can find a model by id', function(assert) { var db = new Db(); db.createCollection('users'); db.users.insert([{id: 1, name: 'Link'}, {id: 2, name: 'Zelda'}]); @@ -53,3 +74,19 @@ test('it can find models', function(assert) { assert.deepEqual(zelda.attrs, {id: 2, name: 'Zelda'}); }); +test('it can find multiple models by ids', function(assert) { + var db = new Db(); + db.createCollection('users'); + db.users.insert([{id: 1, name: 'Link'}, {id: 2, name: 'Zelda'}]); + var schema = new Schema(db); + + var User = Model.extend(); + schema.register('user', User); + + var users = schema.user.find([1, 2]); + + assert.ok(users[0] instanceof User); + assert.equal(users.length, 2); + assert.deepEqual(users[1].attrs, {id: 2, name: 'Zelda'}); +}); +