/
mongodb.js
220 lines (191 loc) · 5.92 KB
/
mongodb.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
var Promise = require('bluebird')
var through = require('through')
var mongodb = require('mongodb')
var resourceError = require('resource-error')
var invoke = require('ninvoke')
var MongoDb = module.exports = function MongoDb (db) {
var self = this
this.db = Promise.resolve(db)
this.ready = this.db.then(function (db) {
self._db = db
return self
})
}
MongoDb.connect = function (connectionString, options) {
// create new MongoDbProvider from new MongoClient
var db = invoke(mongodb.MongoClient, 'connect',
connectionString, options)
return new MongoDb(db)
}
var proto = MongoDb.prototype
proto.disconnect = function () {
return this.db.then(function (db) {
var forceClose = true
return invoke(db, 'close', forceClose)
})
}
proto.getCollectionNames = function () {
return this.db.then(function (db) {
return invoke(db, 'collectionNames').then(function (names) {
return names.map(function (name) {
return name.name.replace(/^[^\.]*\./, '')
})
})
})
}
proto.dropCollection = function (collectionName) {
return this._collection({collection: collectionName})
.then(function (collection) {
return invoke(collection, 'drop')
})
}
// (Query) => Promise
proto.run = function (query) {
var self = this
// todo: more robust query checking (valid commands, etc)
switch (query.command) {
case 'read':
case 'count':
case 'exists':
case 'insert':
case 'update':
case 'findAndModify':
case 'modifyAndFind':
case 'pull':
case 'upsert':
case 'remove':
case 'removeAll':
case 'aggregate':
return self._collection(query).then(function (collection) {
return self['_' + query.command](collection, query)
})
default:
return Promise.reject(new Error('Unknown command:' + query.command))
}
}
// (Query) => Stream
proto.runAsStream = function (query) {
var err
if (!query || query.command !== 'read') {
err = new Error('Query command must be "read"')
}
if (query && query.err) {
err = query.err
}
var outStream = through(function (data) { this.queue(data) })
if (err) {
process.nextTick(function () {
outStream.emit('error', err)
})
return outStream
}
var self = this
self._collection(query).then(function (collection) {
return self._find(collection, query)
.then(function (cursor) {
cursor.stream().pipe(outStream)
})
})
.catch(function (err) {
process.nextTick(function () {
outStream.emit('error', err)
})
})
return outStream
}
// (Query) => Promise
proto._read = function (collection, query) {
return this._find(collection, query).then(function (cursor) {
var results = invoke(cursor, 'toArray')
// handle expected scalar return value
if (query.first) {
results = results.then(function (results) {
if (results.length) {
return results[0]
}
if ('_default' in query) {
return query._default
}
throw new resourceError.NotFound(
'Query returned no results and no default value specified: ' +
query.collection + ' ' + JSON.stringify(query.query))
})
}
return results
})
}
// (MongoCollection, Query) => Promise<MongoCursor>
proto._find = function (collection, query) {
query.options = query.options || {}
query.options.fields = query.options.fields || {}
return invoke(collection, 'find', query.query, query.options)
}
// (Query) => Promise<MongoCollection>
proto._collection = function (query) {
return this.db.then(function (db) {
return db.collection(query.collection)
})
}
// (MongoCollection, Query) => Promise<Number>
proto._count = function (collection, query) {
return invoke(collection, 'count', query.query)
}
// (MongoCollection?, Query) => Promise<Boolean>
proto._exists = function (collection, query) {
return this._count(collection, query.query).then(function (count) {
return count > 0
})
}
// (Query) => Promise
proto._aggregate = function (collection, query) {
if (!Array.isArray(query.commandArg)) {
return Promise.reject(new TypeError('Argument must be an array'))
}
return invoke(collection, 'aggregate', query.commandArg, query.options)
}
// (Query) => Promise
proto._insert = function (collection, query) {
return invoke(collection, 'insert', query.commandArg, query.options)
}
// (Query) => Promise
proto._update = function (collection, query) {
return invoke(collection, 'update',
query.query, query.commandArg, query.options)
}
// returns the document BEFORE the changes object has been applied
proto._findAndModify = function (collection, query) {
query.options.sort = query.options.sort || {_id: 1}
query.options.new = false
query.options.upsert = false
return invoke(collection, 'findAndModify',
query.query, query.options.sort, query.commandArg, query.options)
}
// returns the document AFTER the changes object has been applied
proto._modifyAndFind = function (collection, query) {
query.options.sort = query.options.sort || {_id: 1}
query.options.new = true
query.options.upsert = false
return invoke(collection, 'findAndModify',
query.query, query.options.sort, query.commandArg, query.options)
}
proto._pull = function (collection, query) {
return invoke(collection, 'findAndRemove',
query.query, query.options.sort, query.options)
}
proto._upsert = function (collection, query) {
query.query = query.query || {}
query.options.upsert = true
return invoke(collection, 'update',
query.query, query.commandArg, query.options)
}
proto._remove = function (collection, query) {
if (!query.query || !Object.keys(query.query).length) {
return Promise.reject(
new resourceError.Invalid('No `where` query specified. ' +
'Use `removeAll` to remove all documents in a collection.'))
}
return invoke(collection, 'remove', query.query, query.options)
}
proto._removeAll = function (collection, query) {
return invoke(collection, 'remove', query.options)
}