This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
266 lines (214 loc) · 6.39 KB
/
index.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const { ObjectID } = require('mongodb');
const errors = require('@feathersjs/errors');
const { _ } = require('@feathersjs/commons');
const { AdapterService, select } = require('@feathersjs/adapter-commons');
const errorHandler = require('./error-handler');
// Create the service.
class Service extends AdapterService {
constructor (options) {
if (!options) {
throw new Error('MongoDB options have to be provided');
}
super(Object.assign({
id: '_id'
}, options));
}
get Model () {
return this.options.Model;
}
set Model (value) {
this.options.Model = value;
}
_objectifyId (id) {
if (this.options.disableObjectify) {
return id;
}
if (this.id === '_id' && ObjectID.isValid(id)) {
id = new ObjectID(id.toString());
}
return id;
}
_multiOptions (id, params = {}) {
const { query } = this.filterQuery(params);
const options = Object.assign({ multi: true }, params.mongodb || params.options);
if (id !== null) {
options.multi = false;
query.$and = (query.$and || []).concat({ [this.id]: this._objectifyId(id) });
}
return { query, options };
}
_getSelect (select) {
if (Array.isArray(select)) {
var result = {};
select.forEach(name => {
result[name] = 1;
});
return result;
}
return select;
}
_findOrGet (id, params = {}) {
if (id === null) {
return this._find(params);
}
return this._get(id, params);
}
_normalizeId (id, data) {
if (this.id === '_id') {
// Default Mongo IDs cannot be updated. The Mongo library handles
// this automatically.
return _.omit(data, this.id);
} else if (id !== null) {
// If not using the default Mongo _id field set the ID to its
// previous value. This prevents orphaned documents.
return Object.assign({}, data, { [this.id]: id });
} else {
return data;
}
}
// Map stray records into $set
_remapModifiers (data) {
var set = {};
// Step through the rooot
for (var key in data) {
// Check for keys that aren't modifiers
if (key.charAt(0) !== '$') {
// Move them to set, and remove their record
set[key] = data[key];
delete data[key];
}
// If the '$set' modifier is used, add that to the temp variable
if (key === '$set') {
set = Object.assign(set, data[key]);
delete data[key];
}
}
// If we have a $set, then attach to the data object
if (Object.keys(set).length > 0) {
data.$set = set;
}
return data;
}
_find (params = {}) {
// Start with finding all, and limit when necessary.
const { filters, query, paginate } = this.filterQuery(params);
if (query[this.id]) {
query[this.id] = this._objectifyId(query[this.id]);
}
const q = this.Model.find(query);
if (filters.$select) {
q.project(this._getSelect(filters.$select));
}
if (filters.$sort) {
q.sort(filters.$sort);
}
if (params.collation) {
q.collation(params.collation);
}
if (params.hint) {
q.hint(params.hint);
}
if (filters.$limit) {
q.limit(filters.$limit);
}
if (filters.$skip) {
q.skip(filters.$skip);
}
let runQuery = total => {
return q.toArray().then(data => {
return {
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data
};
});
};
if (filters.$limit === 0) {
runQuery = total => {
return Promise.resolve({
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data: []
});
};
}
if (paginate && paginate.default) {
return this.Model.countDocuments(query).then(runQuery);
}
return runQuery().then(page => page.data);
}
_get (id, params = {}) {
const { query } = this.filterQuery(params);
query.$and = (query.$and || []).concat({ [this.id]: this._objectifyId(id) });
return this.Model.findOne(query).then(data => {
if (!data) {
throw new errors.NotFound(`No record found for id '${id}'`);
}
return data;
}).then(select(params, this.id)).catch(errorHandler);
}
_create (data, params = {}) {
const setId = item => {
const entry = Object.assign({}, item);
// Generate a MongoId if we use a custom id
if (this.id !== '_id' && typeof entry[this.id] === 'undefined') {
entry[this.id] = new ObjectID().toHexString();
}
return entry;
};
const promise = Array.isArray(data)
? this.Model.insertMany(data.map(setId))
: this.Model.insertOne(setId(data));
return promise.then(result => result.ops.length > 1 ? result.ops : result.ops[0])
.then(select(params, this.id))
.catch(errorHandler);
}
_patch (id, data, params = {}) {
let { query, options } = this._multiOptions(id, params);
if (params.collation) {
query = Object.assign(query, { collation: params.collation });
}
const findParams = Object.assign({}, params, {
paginate: false
});
const remapModifier = this._remapModifiers(this._normalizeId(id, data));
return this.Model.updateMany(query, remapModifier, options)
.then(() => this._findOrGet(id, findParams))
.then(select(params, this.id))
.catch(errorHandler);
}
_update (id, data, params = {}) {
if (Array.isArray(data) || id === null) {
return Promise.reject(
new errors.BadRequest('Not replacing multiple records. Did you mean `patch`?')
);
}
const { query, options } = this._multiOptions(id, params);
return this.Model.replaceOne(query, this._normalizeId(id, data), options)
.then(() => this._findOrGet(id, params))
.then(select(params, this.id))
.catch(errorHandler);
}
_remove (id, params = {}) {
let { query } = this._multiOptions(id, params);
if (params.collation) {
query = Object.assign(query, { collation: params.collation });
}
const findParams = Object.assign({}, params, {
paginate: false,
query: params.query
});
return this._findOrGet(id, findParams)
.then(items => {
return this.Model.deleteMany(query)
.then(() => items)
.then(select(params, this.id));
}).catch(errorHandler);
}
}
module.exports = function init (options) {
return new Service(options);
};
module.exports.Service = Service;