Skip to content

Commit

Permalink
working on schema
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed May 16, 2015
1 parent 9825fd8 commit 0f3aa49
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 74 deletions.
26 changes: 0 additions & 26 deletions addon/model-registry.js

This file was deleted.

6 changes: 3 additions & 3 deletions addon/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions addon/schema.js
Original file line number Diff line number Diff line change
@@ -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);
}


};
45 changes: 0 additions & 45 deletions tests/unit/model-registry-test.js

This file was deleted.

55 changes: 55 additions & 0 deletions tests/unit/schema-test.js
Original file line number Diff line number Diff line change
@@ -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'});
});

0 comments on commit 0f3aa49

Please sign in to comment.