-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9825fd8
commit 0f3aa49
Showing
5 changed files
with
134 additions
and
74 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}); | ||
}); | ||
|