diff --git a/addon/model-registry.js b/addon/model-registry.js deleted file mode 100644 index 4558c9d5f..000000000 --- a/addon/model-registry.js +++ /dev/null @@ -1,26 +0,0 @@ -import { pluralize } from './utils/inflector'; - -export default function(db) { - - this.db = db; - this._registry = {}; - - this.register = function(type, typeClass) { - this._registry[type] = typeClass; - }; - - this.create = function(type, attrs) { - var collection = pluralize(type); - - if (!this.db[collection]) { - db.createCollection(collection); - } - - var augmentedAttrs = this.db[collection].insert(attrs); - - var instance = new this._registry[type](augmentedAttrs); - - return instance; - } - -}; diff --git a/addon/model.js b/addon/model.js index 072ba4190..a43a45755 100644 --- a/addon/model.js +++ b/addon/model.js @@ -5,9 +5,9 @@ var Model = function(attrs) { this.attrs = attrs; - Object.keys(attrs).forEach(function(attr) { - _this[attr] = attrs[attr]; - }); + //Object.keys(attrs).forEach(function(attr) { + //_this[attr] = attrs[attr]; + //}); }; Model.extend = extend; diff --git a/addon/schema.js b/addon/schema.js new file mode 100644 index 000000000..e7a779216 --- /dev/null +++ b/addon/schema.js @@ -0,0 +1,76 @@ +import { pluralize } from './utils/inflector'; + +// repo.users.create({}); +// cache.users.create({}); + +// schema.create('user', {name: 'Link'}) +// schema.all('user') +// schema.find('user', 1) +// schema.find('user', [1, 2, 3]) +// schema.where('user', {admin: true}) + +// schema.user.create({}); +// schema.user.all(); +// schema.user.find(1); +// schema.user.where({admin: true}); +// schema.user.update(1, {admin: true}); + +export default function(db) { + + this.db = db; + this._registry = {}; + + this.register = function(type, typeClass) { + var _this = this; + + this._registry[type] = typeClass; + + this[type] = { + create: this._create.bind(this, type), + all: this._all.bind(this, type), + find: this._find.bind(this, type) + }; + + return this; + }; + + this._create = function(type, attrs) { + var collection = pluralize(type); + + if (!this.db[collection]) { + db.createCollection(collection); + } + + var augmentedAttrs = this.db[collection].insert(attrs); + + var instance = new this._registry[type](augmentedAttrs); + + return instance; + }, + + this._all = function(type) { + var collection = pluralize(type); + var data = db[collection]; + var ModelClass = this._registry[type]; + var models = []; + + data.forEach(function(attrs) { + models.push(new ModelClass(attrs)); + }); + + return models; + }, + + this._find = function(type, ids) { + var collection = pluralize(type); + if (!db[collection]) { + return null; + } + var attrs = db[collection].find(ids); + var ModelClass = this._registry[type]; + + return new ModelClass(attrs); + } + + +}; diff --git a/tests/unit/model-registry-test.js b/tests/unit/model-registry-test.js deleted file mode 100644 index 0eb1f4c12..000000000 --- a/tests/unit/model-registry-test.js +++ /dev/null @@ -1,45 +0,0 @@ -import ModelRegistry from 'ember-cli-mirage/model-registry'; -import Model from 'ember-cli-mirage/model'; -import Db from 'ember-cli-mirage/db'; - -module('mirage:model-registry'); - -test('it can be instantiated', function(assert) { - var modelRegistry = new ModelRegistry(new Db()); - assert.ok(modelRegistry); -}); - -test('it can create registered models', function(assert) { - var User = Model.extend(); - var modelRegistry = new ModelRegistry(new Db()); - - modelRegistry.register('user', User); - var user = modelRegistry.create('user', {name: 'Link'}); - - assert.ok(user instanceof Model); - assert.ok(user instanceof User); - assert.deepEqual(user.attrs, {id: 1, name: 'Link'}); -}); - -//test('it respects passed in attrs', function(assert) { - //var modelRegistry = new ModelRegistry(new Db()); - - //var User = Model.extend(); - //modelRegistry.register('user', User); - //var user = modelRegistry.create('user', {name: 'Link'}); - - //assert.deepEqual(user, {id: 1, name: 'Link'}); - //assert.equal(user.constructor, User); -//}); - -//test('it respects passed in attrs', function(assert) { - //var modelRegistry = new ModelRegistry(new Db()); - - //modelRegistry.register('user', Model.extend()); - //reg.find('user', 1); - //reg.users - //var user = modelRegistry.create('user', {name: 'Link'}); - - //assert.deepEqual(user, {id: 1, name: 'Link'}); -//}); - diff --git a/tests/unit/schema-test.js b/tests/unit/schema-test.js new file mode 100644 index 000000000..2c35fce75 --- /dev/null +++ b/tests/unit/schema-test.js @@ -0,0 +1,55 @@ +import Schema from 'ember-cli-mirage/schema'; +import Model from 'ember-cli-mirage/model'; +import Db from 'ember-cli-mirage/db'; + +module('mirage:schema'); + +test('it can be instantiated', function(assert) { + var schema = new Schema(new Db()); + assert.ok(schema); +}); + +test('it can create models', function(assert) { + var schema = new Schema(new Db()); + + var User = Model.extend(); + schema.register('user', User); + + var user = schema.user.create({name: 'Link'}); + + assert.ok(user instanceof Model); + assert.ok(user instanceof User); + assert.deepEqual(user.attrs, {id: 1, name: 'Link'}); +}); + +test('it can return all models', 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.all(); + + assert.ok(users[0] instanceof User); + assert.equal(users.length, 2); + assert.deepEqual(users[1].attrs, {id: 2, name: 'Zelda'}); +}); + +test('it can find models', 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 zelda = schema.user.find(2); + + assert.ok(zelda instanceof User); + assert.deepEqual(zelda.attrs, {id: 2, name: 'Zelda'}); +}); +