forked from serby/crud-service
/
crud-service.js
192 lines (174 loc) · 6.33 KB
/
crud-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var extend = require('util')._extend
, Pipe = require('piton-pipe')
, emptyFn = function () {}
, events = require('events')
module.exports = function CrudService(name, save, schema, options) {
var slug = (options && options.slug) ? options.slug : name.toLowerCase().replace(/ /g, '')
, plural = (options && options.plural) ? options.plural : name + 's'
, self = new events.EventEmitter()
var pre = {
create: Pipe.createPipe(),
createValidate: Pipe.createPipe(),
update: Pipe.createPipe(),
updateValidate: Pipe.createPipe(),
partialUpdate: Pipe.createPipe(),
partialValidate: Pipe.createPipe(),
'delete': Pipe.createPipe()
}
if (schema.schema[save.idProperty] === undefined) {
throw new Error('schema does not have the required property \'' +
save.idProperty + '\'')
}
return extend(self, {
name: name,
slug: slug,
plural: plural,
schema: schema,
idProperty: save.idProperty,
create: function (object, validateOptions, callback) {
if (typeof validateOptions === 'function') {
callback = validateOptions
}
callback = callback || emptyFn
var cleanObject = schema.cast(schema.stripUnknownProperties(schema.makeDefault(object), validateOptions.tag))
pre.createValidate.run(cleanObject, function (error, pipedObject) {
if (error) {
return callback(error)
}
schema.validate(pipedObject, validateOptions.set, validateOptions.tag, function (error, validationErrors) {
if (error) {
return callback(error)
}
if (Object.keys(validationErrors).length > 0) {
var validationError = new Error('Validation Error')
validationError.errors = validationErrors
return callback(validationError, pipedObject)
}
pre.create.run(pipedObject, function (error, pipedObject) {
if (error) {
return callback(error)
}
save.create(pipedObject, function (error, savedObject) {
if (error) {
return callback(error)
}
self.emit('create', savedObject)
callback(undefined, savedObject)
})
})
})
})
},
read: function(id, callback) {
return save.read(schema.castProperty(schema.schema[save.idProperty].type, id), callback)
},
update: function (object, validateOptions, callback) {
callback = callback || emptyFn
var cleanObject = schema.cast(schema.stripUnknownProperties(schema.makeDefault(object), validateOptions.tag))
pre.updateValidate.run(cleanObject, function (error, pipedObject) {
if (error) {
return callback(error)
}
schema.validate(pipedObject, validateOptions.set, validateOptions.tag,
function (error, validationErrors) {
if (error) {
return callback(error)
}
if (Object.keys(validationErrors).length > 0) {
var validationError = new Error('Validation Error')
validationError.errors = validationErrors
return callback(validationError, pipedObject)
}
pre.update.run(pipedObject, function (error, pipedObject) {
if (error) {
return callback(error, pipedObject)
}
save.update(pipedObject, function (error, savedObject) {
if (error) {
return callback(error)
}
self.emit('update', savedObject)
callback(undefined, savedObject)
})
})
})
})
},
partialUpdate: function (object, validateOptions, callback) {
callback = callback || emptyFn
if (typeof validateOptions === 'function') {
callback = validateOptions
}
if (!object[save.idProperty]) {
return callback(new Error('object have not ID property \'' + save.idProperty
+ '\''))
}
save.read(object[save.idProperty], function (error, readObject) {
if (error) {
return callback(error)
}
if (typeof readObject === 'undefined') {
return callback(new Error('Couldn\'t find object with an ' + save.idProperty + ' of ' + object[save.idProperty]))
}
readObject = extend(readObject, object)
var cleanObject = schema.cast(schema.stripUnknownProperties(readObject, validateOptions.tag))
pre.partialUpdate.run(cleanObject, function (error, pipedObject) {
if (error) {
return callback(error)
}
schema.validate(pipedObject, validateOptions.set, validateOptions.tag,
function (error, validationErrors) {
if (error) {
return callback(error)
}
if (Object.keys(validationErrors).length > 0) {
var validationError = new Error('Validation Error')
validationError.errors = validationErrors
return callback(validationError, pipedObject)
}
pre.partialUpdate.run(pipedObject, function (error, pipedObject) {
if (error) {
return callback(error, pipedObject)
}
// Now only update the keys the original object had.
var objectForUpdate = {}
Object.keys(object).forEach(function (key) {
objectForUpdate[key] = pipedObject[key]
})
save.update(objectForUpdate, function (error, savedObject) {
if (error) {
return callback(error)
}
self.emit('partialUpdate', savedObject)
callback(undefined, savedObject)
})
})
})
})
})
},
'delete': function (id, callback) {
save['delete'](id, function (error) {
if (error) {
return callback(error)
}
self.emit('delete', id)
if (typeof callback === 'function') callback()
})
},
deleteMany: function (query, callback) {
save.deleteMany(query, function (error) {
if (error) {
return callback(error)
}
self.emit('deleteMany', query)
callback()
})
},
count: save.count,
find: save.find,
pre: function (method, processor) {
return pre[method].add(processor)
}
})
}