@drawbridge/mongodb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/index.d.mts +500 -0
- package/dist/index.d.ts +500 -0
- package/dist/index.js +362 -0
- package/dist/index.mjs +378 -0
- package/package.json +31 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// index.js
|
|
2
|
+
var { ObjectId } = require("mongodb");
|
|
3
|
+
var ids = ({
|
|
4
|
+
_id
|
|
5
|
+
}) => {
|
|
6
|
+
const id = _id || new ObjectId();
|
|
7
|
+
return {
|
|
8
|
+
_id: id,
|
|
9
|
+
id: id.toString()
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
var formats = {
|
|
13
|
+
documents: {
|
|
14
|
+
insert: (data, authenticated) => {
|
|
15
|
+
const date = /* @__PURE__ */ new Date();
|
|
16
|
+
return {
|
|
17
|
+
createdAt: date,
|
|
18
|
+
updatedAt: date,
|
|
19
|
+
...ids(data),
|
|
20
|
+
...authenticated && {
|
|
21
|
+
createdBy: (authenticated == null ? void 0 : authenticated.id) + "." + date.getTime()
|
|
22
|
+
},
|
|
23
|
+
...data
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
update: ({
|
|
27
|
+
$set,
|
|
28
|
+
$setOnInsert,
|
|
29
|
+
...rest
|
|
30
|
+
}, authenticated) => {
|
|
31
|
+
const date = /* @__PURE__ */ new Date();
|
|
32
|
+
return {
|
|
33
|
+
$set: {
|
|
34
|
+
updatedAt: date,
|
|
35
|
+
...authenticated && {
|
|
36
|
+
updatedBy: (authenticated == null ? void 0 : authenticated.id) + "." + date.getTime()
|
|
37
|
+
},
|
|
38
|
+
...$set
|
|
39
|
+
},
|
|
40
|
+
$setOnInsert: {
|
|
41
|
+
...ids(rest),
|
|
42
|
+
createdAt: date,
|
|
43
|
+
...authenticated && {
|
|
44
|
+
createdBy: (authenticated == null ? void 0 : authenticated.id) + "." + date.getTime()
|
|
45
|
+
},
|
|
46
|
+
...$setOnInsert
|
|
47
|
+
},
|
|
48
|
+
...rest
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
timeseries: {
|
|
53
|
+
insert: (data) => {
|
|
54
|
+
const { _id, id } = ids(data);
|
|
55
|
+
const date = /* @__PURE__ */ new Date();
|
|
56
|
+
return {
|
|
57
|
+
_id,
|
|
58
|
+
meta: {
|
|
59
|
+
...data,
|
|
60
|
+
id
|
|
61
|
+
},
|
|
62
|
+
time: date
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
update: ({
|
|
66
|
+
$set
|
|
67
|
+
}) => {
|
|
68
|
+
return {
|
|
69
|
+
$set
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
module.exports = ({
|
|
75
|
+
client,
|
|
76
|
+
database
|
|
77
|
+
}) => ({
|
|
78
|
+
aggregate: async ({
|
|
79
|
+
collection,
|
|
80
|
+
options = {},
|
|
81
|
+
pipeline = []
|
|
82
|
+
}) => {
|
|
83
|
+
return await database.collection(collection).aggregate(pipeline, options).toArray();
|
|
84
|
+
},
|
|
85
|
+
bulk: async ({
|
|
86
|
+
collection,
|
|
87
|
+
options = {},
|
|
88
|
+
pipeline = []
|
|
89
|
+
}) => {
|
|
90
|
+
return await database.collection(collection).bulkWrite(pipeline, options);
|
|
91
|
+
},
|
|
92
|
+
count: async ({
|
|
93
|
+
collection,
|
|
94
|
+
options = {},
|
|
95
|
+
query
|
|
96
|
+
}) => {
|
|
97
|
+
return await database.collection(collection).countDocuments(query, options);
|
|
98
|
+
},
|
|
99
|
+
create: async ({
|
|
100
|
+
authenticated,
|
|
101
|
+
collection,
|
|
102
|
+
data,
|
|
103
|
+
multiple = false,
|
|
104
|
+
options,
|
|
105
|
+
pre,
|
|
106
|
+
timeseries = false
|
|
107
|
+
}) => {
|
|
108
|
+
const endpoint = multiple ? "insertMany" : "insertOne";
|
|
109
|
+
const format = timeseries ? "timeseries" : "documents";
|
|
110
|
+
let result;
|
|
111
|
+
try {
|
|
112
|
+
if (multiple) {
|
|
113
|
+
result = data.map((item) => formats[format].insert(item, authenticated));
|
|
114
|
+
} else {
|
|
115
|
+
result = formats[format].insert(data, authenticated);
|
|
116
|
+
}
|
|
117
|
+
if (pre instanceof Function) {
|
|
118
|
+
result = await pre(result);
|
|
119
|
+
}
|
|
120
|
+
await database.collection(collection)[endpoint](
|
|
121
|
+
result,
|
|
122
|
+
timeseries ? {} : {
|
|
123
|
+
returnDocument: "after",
|
|
124
|
+
returnNewDocument: true,
|
|
125
|
+
...options
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
return result;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
delete: async ({
|
|
134
|
+
collection,
|
|
135
|
+
query,
|
|
136
|
+
multiple,
|
|
137
|
+
options = {}
|
|
138
|
+
}) => {
|
|
139
|
+
let result;
|
|
140
|
+
try {
|
|
141
|
+
if (multiple) {
|
|
142
|
+
result = await database.collection(collection).find(
|
|
143
|
+
query,
|
|
144
|
+
{},
|
|
145
|
+
options
|
|
146
|
+
).toArray();
|
|
147
|
+
await database.collection(collection).deleteMany(
|
|
148
|
+
query,
|
|
149
|
+
options
|
|
150
|
+
);
|
|
151
|
+
} else {
|
|
152
|
+
result = await database.collection(collection).findOne(
|
|
153
|
+
query
|
|
154
|
+
);
|
|
155
|
+
await database.collection(collection).deleteOne(
|
|
156
|
+
query,
|
|
157
|
+
options
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
formats,
|
|
166
|
+
get: async ({
|
|
167
|
+
collection,
|
|
168
|
+
extend = [],
|
|
169
|
+
options = {},
|
|
170
|
+
refine = {},
|
|
171
|
+
query
|
|
172
|
+
}) => {
|
|
173
|
+
try {
|
|
174
|
+
const pipeline = [
|
|
175
|
+
{
|
|
176
|
+
$match: query
|
|
177
|
+
},
|
|
178
|
+
...extend
|
|
179
|
+
];
|
|
180
|
+
if (refine) {
|
|
181
|
+
pipeline.push({
|
|
182
|
+
$match: refine
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
const result = await database.collection(collection).aggregate(
|
|
186
|
+
pipeline,
|
|
187
|
+
options
|
|
188
|
+
).toArray();
|
|
189
|
+
return result == null ? void 0 : result[0];
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
paged: async ({
|
|
195
|
+
collection,
|
|
196
|
+
limit,
|
|
197
|
+
filters = {},
|
|
198
|
+
refine,
|
|
199
|
+
search = null,
|
|
200
|
+
skip,
|
|
201
|
+
sort,
|
|
202
|
+
...rest
|
|
203
|
+
}) => {
|
|
204
|
+
var _a, _b, _c;
|
|
205
|
+
try {
|
|
206
|
+
const pipelines = {
|
|
207
|
+
query: []
|
|
208
|
+
};
|
|
209
|
+
if (filters && Object.keys(filters).length > 0) {
|
|
210
|
+
pipelines.query.push(
|
|
211
|
+
{
|
|
212
|
+
$match: filters
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
pipelines.query.push({
|
|
217
|
+
$sort: Object.keys(sort).reduce(
|
|
218
|
+
(accumulator, key) => {
|
|
219
|
+
accumulator[key] = Number(sort[key]);
|
|
220
|
+
return accumulator;
|
|
221
|
+
},
|
|
222
|
+
{}
|
|
223
|
+
)
|
|
224
|
+
});
|
|
225
|
+
if (((rest == null ? void 0 : rest.extend) || []).length > 0) {
|
|
226
|
+
pipelines.query = [...pipelines.query, ...rest.extend];
|
|
227
|
+
}
|
|
228
|
+
if (refine && Object.keys(refine).length > 0) {
|
|
229
|
+
pipelines.query.push(
|
|
230
|
+
{
|
|
231
|
+
$match: refine
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if ((search == null ? void 0 : search.value) && ((_a = search == null ? void 0 : search.keys) == null ? void 0 : _a.length) > 0) {
|
|
236
|
+
const value = search.value.trim().replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
237
|
+
search = {
|
|
238
|
+
$match: {
|
|
239
|
+
$or: ((search == null ? void 0 : search.keys) || []).reduce(
|
|
240
|
+
(accumulator, key) => {
|
|
241
|
+
accumulator.push({
|
|
242
|
+
[key]: {
|
|
243
|
+
$regex: value,
|
|
244
|
+
$options: "i"
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
return accumulator;
|
|
248
|
+
},
|
|
249
|
+
[]
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
pipelines.query.push(search);
|
|
254
|
+
}
|
|
255
|
+
const dataStages = [];
|
|
256
|
+
if (skip && limit) {
|
|
257
|
+
dataStages.push({
|
|
258
|
+
$skip: Number(Number(Number(skip) * Number(limit)))
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
if (limit) {
|
|
262
|
+
dataStages.push({
|
|
263
|
+
$limit: Number(limit)
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (rest == null ? void 0 : rest.project) {
|
|
267
|
+
dataStages.push({
|
|
268
|
+
$project: rest == null ? void 0 : rest.project
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
const facetPipeline = [
|
|
272
|
+
...pipelines.query,
|
|
273
|
+
{
|
|
274
|
+
$facet: {
|
|
275
|
+
items: dataStages,
|
|
276
|
+
total: [
|
|
277
|
+
{
|
|
278
|
+
$group: {
|
|
279
|
+
_id: null,
|
|
280
|
+
total: {
|
|
281
|
+
$sum: 1
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
];
|
|
289
|
+
const [facetResult] = await database.collection(collection).aggregate(facetPipeline).toArray();
|
|
290
|
+
const items = (facetResult == null ? void 0 : facetResult.items) || [];
|
|
291
|
+
const total = ((_c = (_b = facetResult == null ? void 0 : facetResult.total) == null ? void 0 : _b[0]) == null ? void 0 : _c.total) || 0;
|
|
292
|
+
const offset = Number(Number(skip) + 1) * Number(limit);
|
|
293
|
+
const hasPreviousPage = Boolean(Number(Number(skip)) * Number(limit) > 0);
|
|
294
|
+
const hasNextPage = Boolean(offset < Number(total));
|
|
295
|
+
return {
|
|
296
|
+
items,
|
|
297
|
+
pageInfo: {
|
|
298
|
+
hasNextPage,
|
|
299
|
+
hasPreviousPage,
|
|
300
|
+
offset: hasNextPage ? offset : total,
|
|
301
|
+
total
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
} catch (error) {
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
transaction: async (callback) => {
|
|
309
|
+
try {
|
|
310
|
+
const result = await new Promise(
|
|
311
|
+
(resolve, reject) => client.withSession(
|
|
312
|
+
async (session) => session.withTransaction(
|
|
313
|
+
async (session2) => resolve(await callback(session2)),
|
|
314
|
+
null
|
|
315
|
+
).catch(reject)
|
|
316
|
+
).catch(reject)
|
|
317
|
+
);
|
|
318
|
+
return result;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
update: async ({
|
|
324
|
+
authenticated,
|
|
325
|
+
collection,
|
|
326
|
+
data,
|
|
327
|
+
query,
|
|
328
|
+
multiple = false,
|
|
329
|
+
options,
|
|
330
|
+
timeseries = false
|
|
331
|
+
}) => {
|
|
332
|
+
let result = {};
|
|
333
|
+
try {
|
|
334
|
+
if (multiple || timeseries) {
|
|
335
|
+
const format = timeseries ? "timeseries" : "documents";
|
|
336
|
+
result.value = await database.collection(collection).find(
|
|
337
|
+
query,
|
|
338
|
+
{},
|
|
339
|
+
options
|
|
340
|
+
).toArray();
|
|
341
|
+
await database.collection(collection).updateMany(
|
|
342
|
+
query,
|
|
343
|
+
formats[format].update(data, authenticated),
|
|
344
|
+
options
|
|
345
|
+
);
|
|
346
|
+
} else {
|
|
347
|
+
result = await database.collection(collection).findOneAndUpdate(
|
|
348
|
+
query,
|
|
349
|
+
formats.documents.update(data, authenticated),
|
|
350
|
+
{
|
|
351
|
+
returnDocument: "after",
|
|
352
|
+
returnNewDocument: true,
|
|
353
|
+
...options
|
|
354
|
+
}
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
return result;
|
|
358
|
+
} catch (error) {
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
});
|