-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo-executer.js
285 lines (232 loc) · 7.69 KB
/
mongo-executer.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const F = require('fluture');
const R = require('ramda');
const { MongoClient } = require('mongodb');
const S = require('./util/sanctuary');
const { updateObject } = require('./util/mongo-3x-compatibility');
const mongoClientConnect = R.curry(MongoClient.connect);
const connect = (host, database) => F.node(
mongoClientConnect(`mongodb://${host}/${database}`, null),
);
const connectWithUri = (mongoUri, opts) => F.node(
mongoClientConnect(mongoUri, opts),
);
const rejectMongoOf = (collection, query, projection, object) => (e) => {
const err = {};
err.message = e.message;
err.stack = e.stack;
err.collection = collection;
err.query = query;
err.projection = projection;
err.object = object;
console.error('Mongo error', err); // eslint-disable-line no-console
return F.reject(err);
};
const skipper = R.invoker(1, 'skip');
const limmiter = R.invoker(1, 'limit');
const toArrayer = R.invoker(1, 'toArray');
const toArray = R.curry((skip, limit, cursor) => {
const chain = [];
if (skip) chain.push(skipper(skip));
if (limit) chain.push(limmiter(limit));
return new F((reject, resolve) => {
const resolver = (err, xs) => {
if (err) { reject(err); } else { resolve(xs); }
};
chain.push(toArrayer(resolver));
S.pipe(chain)(cursor);
});
});
/**
* mongo-mock doesn't return `cursor.result` for upserts.
* Instead it just returns the result `{n: Number}`.
* @param {Object} cursor
*/
const getResult = (cursor) => {
// Mongo 3.0.x support
if (cursor.result) {
return F.of(cursor.result);
}
// Mongo 3.1.x support
if (cursor.toArray) {
return F.node(done => cursor.toArray(done));
}
// mongo-mock support
return F.of(cursor);
};
const executeFind = R.curry((collection, query, projection, options, db) => {
const findInCollection = () => db.collection(collection).find(query).project(projection);
const rejector = rejectMongoOf(collection, query, projection);
return F.try(findInCollection)
.chainRej(rejector)
.chain(toArray(options.skip, options.limit));
});
const buildFind = (collection, query, projection, skip, limit) => executeFind(
collection,
query,
projection,
{ skip, limit },
);
const executeUpdate = R.curry((collection, query, object, upsert, db) => {
const updatedObject = updateObject(object);
const updateCollection = () => db.collection(collection).updateMany(
query,
updatedObject,
{ upsert },
);
const rejector = rejectMongoOf(collection, query, null, updatedObject);
return F.tryP(updateCollection)
.chainRej(rejector)
.chain(getResult);
});
const executeUpdateOne = R.curry((collection, query, object, upsert, db) => {
const updateCollection = () => db.collection(collection).updateOne(query, object, { upsert });
const rejector = rejectMongoOf(collection, query, null, object);
return F.tryP(updateCollection)
.chainRej(rejector)
.chain(getResult);
});
const executeUpdateMany = R.curry((collection, query, object, upsert, db) => {
const updateCollection = () => db.collection(collection).updateMany(query, object, { upsert });
const rejector = rejectMongoOf(collection, query, null, object);
return F.tryP(updateCollection)
.chainRej(rejector)
.chain(getResult);
});
const executeInsert = R.curry((collection, object, db) => {
let insertCollection;
if (Array.isArray(object)) {
insertCollection = () => db.collection(collection).insertMany(object);
} else {
insertCollection = () => db.collection(collection).insertOne(object);
}
const rejector = rejectMongoOf(collection, null, null, object);
return F.tryP(insertCollection)
.chainRej(rejector);
});
const executeInsertOne = R.curry((collection, object, db) => {
const insertCollection = () => db.collection(collection).insertOne(object);
const rejector = rejectMongoOf(collection, null, null, object);
return F.tryP(insertCollection)
.chainRej(rejector);
});
const executeInsertMany = R.curry((collection, objects, db) => {
const insertCollection = () => db.collection(collection).insertMany(objects);
const rejector = rejectMongoOf(collection, null, null, objects);
return F.tryP(insertCollection)
.chainRej(rejector);
});
const executeDeleteOne = R.curry((collection, object, db) => {
const deleteFromCollection = callback => db.collection(collection).deleteOne(object, callback);
const rejector = rejectMongoOf(collection, null, null, object);
return F.node(deleteFromCollection)
.chainRej(rejector);
});
const executePush = R.curry((collection, query, object, db) => {
const updateCollection = () => db.collection(collection).updateMany(query, { $push: object });
const rejector = rejectMongoOf(collection, query, null, object);
return F.tryP(updateCollection)
.chainRej(rejector)
.chain(getResult);
});
const executeAggregate = R.curry((collection, query, lookUp, sort, projection, db) => {
const aggregateCollection = () => callback => db.collection(collection).aggregate([
{ $match: query },
{ $lookup: lookUp },
{ $sort: sort },
{ $project: projection },
], callback);
const rejector = rejectMongoOf(collection, query, lookUp, projection);
return F.node(aggregateCollection(query, lookUp))
.chainRej(rejector)
.chain(getResult);
});
const buildUpdate = (collection, query, object) => executeUpdate(
collection,
query,
object,
false,
);
const buildUpdateOne = (collection, query, object) => executeUpdateOne(
collection,
query,
object,
false,
);
const buildUpdateMany = (collection, query, object) => executeUpdateMany(
collection,
query,
object,
false,
);
const buildUpsert = (collection, query, object) => executeUpdate(
collection,
query,
object,
true,
);
const buildUpsertOne = (collection, query, object) => executeUpdateOne(
collection,
query,
object,
true,
);
const buildUpsertMany = (collection, query, object) => executeUpdateMany(
collection,
query,
object,
true,
);
const buildInsert = executeInsert;
const buildInsertMany = executeInsertMany;
const buildInsertOne = executeInsertOne;
const buildDeleteOne = executeDeleteOne;
const buildPush = (collection, query, object) => executePush(collection, query, object);
const buildAggregate = (collection, query, lookUp, sort, projection) => executeAggregate(
collection,
query,
lookUp,
sort,
projection,
);
/**
* Please note when passing MongoClient in mongo parameter, dbName is required.
* MongoDB >= 3.1 will give you client, whereas MongoDB >= 3.0 will give you Db.
* @param {MongoClient|Db} mongo
* @param {String} dbName Database name (required if mongo is MongoClient)
*/
const withConnection = (mongo, dbName = '') => {
let db = mongo;
// For mongodb >=3.1.x
if (dbName && mongo.db) {
db = mongo.db(dbName);
}
if (!db.collection) {
throw new Error('When providing MongoClient as parameter 1 to withConnection, you must pass in the dbName as parameter 2.');
}
// (db -> Future e a) -> Future e Either e a
const executeSingle = S.pipe([
S.T(db),
F.fold(S.Left, S.Right),
]);
return Object.freeze({
executeSingle,
});
};
module.exports = Object.freeze({
buildFind,
buildUpsert,
buildUpsertOne,
buildUpsertMany,
buildInsert,
buildInsertOne,
buildInsertMany,
buildDeleteOne,
buildUpdate,
buildUpdateOne,
buildUpdateMany,
buildPush,
buildAggregate,
connect,
connectWithUri,
withConnection,
});