Coverage

98%
67
66
1

/Users/jordan/code/nuclear/examples/rest-api-example/src/mock-server.js

97%
42
41
1
LineHitsSource
1/**
2 * Simulates a server with a database
3 */
41var _ = require('lodash')
51var Promise = require('es6-promise').Promise
6
71var DATA = {
8 user: {
9 1: {
10 id: 1,
11 name: 'jordan',
12 email: 'jordan@nuclear.com',
13 },
14 2: {
15 id: 2,
16 name: 'jane',
17 email: 'jane@nuclear.com',
18 },
19 }
20}
21
22/**
23 * Stubable function to get data
24 */
251exports.__getData = function(entity) {
260 return DATA[entity]
27}
28
291exports.create = function(entity, instance) {
301 var entityMap = exports.__getData(entity)
311 var lastEntity = _(entityMap)
32 .toArray()
33 .sortBy(function(entry) {
342 return entry.id
35 })
36 .last()
37
381 var savedInstance = _.cloneDeep(instance)
391 savedInstance.id = lastEntity.id + 1
40
411 entityMap[savedInstance.id] = savedInstance
42
431 return new Promise(function(resolve, reject) {
441 resolve(savedInstance)
45 })
46}
47
481exports.update = function(entity, instance) {
492 return new Promise(function(resolve, reject) {
502 var entityMap = exports.__getData(entity)
512 if (!entityMap[instance.id]) {
521 reject('No entity with id=' + instance.id)
531 return
54 }
55
561 entityMap[instance.id] = instance
571 resolve(instance)
58 })
59}
60
611exports.fetch = function(entity, id) {
622 return new Promise(function(resolve, reject) {
632 var entityMap = exports.__getData(entity)
642 if (!entityMap[id]) {
651 reject('No entity with id=' + id)
661 return
67 }
68
691 resolve(entityMap[id])
70 })
71}
72
731exports.fetchAll = function(entity, params) {
743 return new Promise(function(resolve, reject) {
753 var entityMap = exports.__getData(entity)
763 var results = _(entityMap)
77 .filter(params)
78 .toArray()
79 .value()
80
813 resolve(results)
82 })
83}
84
851exports.delete = function(entity, instance) {
862 return new Promise(function(resolve, reject) {
872 var entityMap = exports.__getData(entity)
882 if (!entityMap[instance.id]) {
891 reject('No entity with id=' + instance.id)
901 return
91 }
92
931 delete entityMap[instance.id]
941 return resolve(instance)
95 })
96}
97

/Users/jordan/code/nuclear/examples/rest-api-example/src/modules/user/actions.js

100%
5
5
0
LineHitsSource
11var _ = require('lodash')
21var RestApi = require('../rest-api')
31var model = require('./model')
4
51var userApiActions = RestApi.createApiActions(model)
6
71module.exports = _.extend({}, userApiActions, {
8 // User specific actions go in here
9})
10

/Users/jordan/code/nuclear/examples/rest-api-example/src/modules/user/getters.js

100%
4
4
0
LineHitsSource
11var RestApi = require('../rest-api')
21var model = require('./model')
3
41exports.entityMap = RestApi.createEntityMapGetter(model)
5
61exports.byId = RestApi.createByIdGetter(model)
7

/Users/jordan/code/nuclear/examples/rest-api-example/src/modules/user/index.js

100%
2
2
0
LineHitsSource
11exports.actions = require('./actions')
2
31exports.getters = require('./getters')
4

/Users/jordan/code/nuclear/examples/rest-api-example/src/modules/user/model.js

100%
14
14
0
LineHitsSource
11var _ = require('lodash')
21var MockServer = require('../../mock-server')
3
41var ENTITY = 'user'
5
61exports.entity = ENTITY
7
8/**
9 * @param {User} instance
10 * @return {Promise}
11 */
121exports.save = function(instance) {
133 if (instance.id) {
142 return MockServer.update(ENTITY, instance)
15 } else {
161 return MockServer.create(ENTITY, instance)
17 }
18}
19
20/**
21 * @param {Number} id
22 * @return {Promise}
23 */
241exports.fetch = function(id) {
252 return MockServer.fetch(ENTITY, id)
26}
27
28/**
29 * @param {Object?} params
30 * @return {Promise}
31 */
321exports.fetchAll = function(params) {
333 return MockServer.fetchAll(ENTITY, params)
34}
35
36/**
37 * @param {User} instance
38 * @return {Promise}
39 */
401exports.delete = function(instance) {
412 return MockServer.delete(ENTITY, instance)
42}
43