forked from serby/save-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb-engine.js
232 lines (198 loc) · 6.07 KB
/
mongodb-engine.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var _ = require('lodash')
, emptyFn = function () {}
, EventEmitter = require('events').EventEmitter
, ObjectID = require('mongodb').ObjectID
module.exports = function (collection, engineOptions) {
var self = new EventEmitter()
, options = _.extend({}, engineOptions,
{ idProperty: '_id' })
function create(object, callback) {
callback = callback || emptyFn
// if id is any falsy consider it empty
if (!object[options.idProperty]) {
delete object[options.idProperty]
}
self.emit('create', object)
collection.insert(_.extend({}, object), { safe: true }, function (error, data) {
if (error) {
return callback(error)
} else {
data[0] = objectIdToString(data[0])
self.emit('afterCreate', data[0])
callback(error, data[0])
}
})
}
function createOrUpdate(object, callback) {
if (typeof object[options.idProperty] === 'undefined') {
// Create a new object
self.create(object, callback)
} else {
// Try and find the object first to update
self.read(object[options.idProperty], function(err, entity) {
if (err) {
return callback(err)
}
if (entity) {
// We found the object so update
self.update(object, callback)
} else {
// We didn't find the object so create
self.create(object, callback)
}
})
}
}
function castIdProperty(query) {
var newQuery = _.extend({}, query)
// only convert if id is present
if (!query[options.idProperty]) {
return newQuery
}
try {
newQuery[options.idProperty] = new ObjectID(newQuery[options.idProperty])
} catch (e) {
if (e.message === 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters') {
return newQuery
}
}
return newQuery
}
function read(id, callback) {
var query = {}
query[options.idProperty] = id
self.emit('read', id)
callback = callback || emptyFn
collection.findOne(castIdProperty(query), function (error, entity) {
callback(error, entity === null ? undefined : objectIdToString(entity))
})
}
function update(object, overwrite, callback) {
if (typeof overwrite === 'function') {
callback = overwrite
overwrite = false
}
self.emit('update', object, overwrite)
callback = callback || emptyFn
var query = {}
, updateObject = _.extend({}, object)
, updateData = overwrite ? updateObject : { $set: updateObject }
, id = object[options.idProperty]
if (id === undefined || id === null) {
return callback(new Error('Object has no \''
+ options.idProperty + '\' property'))
}
query[options.idProperty] = id;
delete updateObject[options.idProperty]
collection.findAndModify(castIdProperty(query), [['_id', 'asc']], updateData,
{ 'new': true }, function (error, entity) {
if (error) {
return callback(error)
}
if (!entity) {
callback(new Error('No object found with \'' + options.idProperty +
'\' = \'' + id + '\''))
} else {
entity = objectIdToString(entity)
self.emit('afterUpdate', entity)
callback(null, entity)
}
})
}
function updateMany(query, object, callback) {
self.emit('updateMany', query, object)
callback = callback || emptyFn
collection.update(query, { $set: object }, { multi: true, safe: true, upsert: false }, function (error) {
self.emit('afterUpdateMany', query, object)
callback(error)
})
}
function deleteMany(query, callback) {
self.emit('deleteMany', query)
callback = callback || emptyFn
collection.remove(castIdProperty(query), function (error) {
if (error) {
return callback(error)
}
self.emit('afterDeleteMany', query)
callback()
})
}
/**
* Deletes one object. Returns an error if the object can not be found
* or if the ID property is not present.
*
* @param {Object} object to delete
* @param {Function} callback
* @api public
*/
function del(id, callback) {
callback = callback || emptyFn
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function or empty')
}
self.emit('delete', id)
var query = {}
query[options.idProperty] = id
collection.remove(castIdProperty(query), function (error) {
if (error) {
return callback(error)
}
self.emit('afterDelete', id)
callback()
})
}
// Because your application using save shouldn't know about engine internals
// ObjectID must be converted to strings before returning.
function objectIdToString(entity) {
entity[options.idProperty] = entity[options.idProperty].toString()
return entity
}
function find(query, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback !== 'function') {
throw new Error('callback must be a function')
}
self.emit('find', query)
collection.find(castIdProperty(query), {}, options).toArray(function (error, data) {
if (error) {
return callback(error)
}
callback(undefined, data.map(objectIdToString))
})
}
function findOne(query, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
self.emit('findOne', query)
collection.findOne(castIdProperty(query), options, function (error, entity) {
if (error) {
return callback(error)
}
callback(error, entity === null ? undefined : objectIdToString(entity))
})
}
function count(query, callback) {
self.emit('count', query)
collection.count(query, callback)
}
return _.extend(self,
{ create: create
, createOrUpdate: createOrUpdate
, read: read
, update: update
, updateMany: updateMany
, deleteMany: deleteMany
, 'delete': del
, find: find
, findOne: findOne
, count: count
, idProperty: options.idProperty
, idType: ObjectID
})
}