UNPKG

3.24 kBtext/coffeescriptView Raw
1'use strict'
2
3Util = require './util'
4
5###*
6sync memory storage, implements ResourceClientInterface
7
8@class MemoryResource
9@implements ResourceClientInterface
10###
11class MemoryResource
12
13 @restore: (obj) ->
14
15 { pool, currentIdNum } = obj
16
17 memoryResource = new MemoryResource()
18 memoryResource.pool = pool
19 memoryResource.currentIdNum = currentIdNum
20
21 return memoryResource
22
23
24 constructor: ->
25 @currentIdNum = 1
26 @pool = {}
27
28
29 ###*
30 Generate id
31
32 @method generateId
33 @public
34 @param {Object} data
35 @return {String}
36 ###
37 generateId: ->
38
39 id = @currentIdNum
40
41 while @pool[id]?
42 id = ++@currentIdNum
43
44 return id.toString()
45
46
47
48 ###*
49 Create new instance of Model class, saved in database
50
51 @method create
52 @public
53 @param {Object} data
54 @return {Object}
55 ###
56 create: (data = {}) ->
57 data.id ?= @generateId()
58
59 @pool[data.id] = Util.clone data
60
61
62 ###*
63 Update or insert a model instance
64 The update will override any specified attributes in the request data object. It won’t remove existing ones unless the value is set to null.
65
66 @method upsert
67 @public
68 @param {Object} data
69 @return {Object}
70 ###
71 upsert: (data = {}) ->
72 return @create data
73
74
75 ###*
76 Find object by ID.
77
78 @method findById
79 @public
80 @param {String} id
81 @return {Object}
82 ###
83 findById: (id) ->
84 Util.clone @pool[id]
85
86
87
88
89 ###*
90 Find all model instances that match filter specification.
91
92 @method find
93 @public
94 @param {Object} filter
95 @return {Array(Object)}
96 ###
97 find: (filter = {}) ->
98
99 { where } = filter
100
101 return (Util.clone(obj) for id, obj of @pool) if not where
102
103 throw new Error '"find" method with "where" is currently unimplemented.'
104
105 ###*
106 Find one model instance that matches filter specification. Same as find, but limited to one result
107
108 @method findOne
109 @public
110 @param {Object} filter
111 @return {Object}
112 ###
113 findOne: (filter) ->
114
115 @find(filter)[0]
116
117
118
119 ###*
120 Destroy model instance
121
122 @method destroyById
123 @public
124 @param {Object} data
125 ###
126 destroy: (data) ->
127 delete @pool[data?.id]
128
129
130 ###*
131 Destroy model instance with the specified ID.
132
133 @method destroyById
134 @public
135 @param {String} id
136 ###
137 destroyById: (id) ->
138 delete @pool[id]
139
140
141 ###*
142 Update set of attributes.
143
144 @method updateAttributes
145 @public
146 @param {Object} data
147 @return {Object}
148 ###
149 updateAttributes: (id, data) ->
150 pooledData = @pool[id]
151 throw new Error("id #{id} is not found") if pooledData
152 for k, v of data
153 pooledData[k] = v
154
155 @pool[id] = pooledData
156
157 return Util.clone pooledData
158
159
160 ###*
161 Count all registered data
162
163 @method count
164 @return {Number} total
165 ###
166 count: ->
167 Object.keys(@pool).length
168
169
170 ###*
171 create plain object
172
173 @method toPlainObject
174 @return {Object} plainObject
175 ###
176 toPlainObject: ->
177 pool : Util.clone @pool
178 currentIdNum : @currentIdNum
179
180module.exports = MemoryResource