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