@storecraft/database-mongodb 1.0.19 → 1.0.21

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.
@@ -1,26 +1,24 @@
1
1
  /**
2
2
  * @import { db_notifications as db_col } from '@storecraft/core/database'
3
3
  */
4
-
5
4
  import { Collection } from 'mongodb'
6
5
  import { MongoDB } from '../index.js'
7
- import { count_regular, get_regular, list_regular,
8
- remove_regular, upsert_regular } from './con.shared.js'
6
+ import {
7
+ count_regular, get_regular, list_regular,
8
+ remove_regular,
9
+ } from './con.shared.js'
9
10
  import { to_objid } from './utils.funcs.js';
10
11
  import { add_search_terms_relation_on } from './utils.relations.js';
11
12
  import { union } from '@storecraft/core/api/utils.func.js';
12
13
 
13
14
  /**
14
15
  * @param {MongoDB} d
15
- *
16
- *
17
16
  * @returns {Collection<db_col["$type_get"]>}
18
17
  */
19
18
  const col = (d) => d.collection('notifications');
20
19
 
21
20
  /**
22
21
  * @param {MongoDB} driver
23
- *
24
22
  * @returns {db_col["upsert"]}
25
23
  */
26
24
  const upsert = (driver) => {
@@ -29,11 +27,8 @@ const upsert = (driver) => {
29
27
  }
30
28
  }
31
29
 
32
-
33
30
  /**
34
31
  * @param {MongoDB} driver
35
- *
36
- *
37
32
  * @returns {db_col["upsertBulk"]}
38
33
  */
39
34
  const upsertBulk = (driver) => {
@@ -84,8 +79,6 @@ const count = (driver) => count_regular(driver, col(driver));
84
79
 
85
80
  /**
86
81
  * @param {MongoDB} driver
87
- *
88
- *
89
82
  * @return {db_col & { _col: ReturnType<col>}}
90
83
  */
91
84
  export const impl = (driver) => {
package/src/con.orders.js CHANGED
@@ -1,16 +1,15 @@
1
1
  /**
2
2
  * @import { db_orders as db_col } from '@storecraft/core/database'
3
3
  */
4
-
5
4
  import { Collection } from 'mongodb'
6
5
  import { MongoDB } from '../index.js'
7
- import { count_regular, get_regular, list_regular,
8
- remove_regular, upsert_regular } from './con.shared.js'
6
+ import {
7
+ count_regular, get_regular, list_regular,
8
+ remove_regular, upsert_regular
9
+ } from './con.shared.js'
9
10
 
10
11
  /**
11
12
  * @param {MongoDB} d
12
- *
13
- *
14
13
  * @returns {Collection<db_col["$type_get"]>}
15
14
  */
16
15
  const col = (d) => d.collection('orders');
@@ -44,8 +43,6 @@ const count = (driver) => count_regular(driver, col(driver));
44
43
 
45
44
  /**
46
45
  * @param {MongoDB} driver
47
- *
48
- *
49
46
  * @return {db_col & { _col: ReturnType<col>}}
50
47
  */
51
48
  export const impl = (driver) => {
package/src/con.posts.js CHANGED
@@ -2,7 +2,6 @@
2
2
  * @import { db_posts as db_col } from '@storecraft/core/database'
3
3
  * @import { WithRelations } from './utils.types.js'
4
4
  */
5
-
6
5
  import { Collection } from 'mongodb'
7
6
  import { MongoDB } from '../index.js'
8
7
  import { count_regular, get_regular, list_regular } from './con.shared.js'
@@ -14,23 +13,14 @@ import {
14
13
  update_entry_on_all_connection_of_relation
15
14
  } from './utils.relations.js';
16
15
 
17
-
18
- /**
19
- * @typedef {import('@storecraft/core/database').db_posts} db_col
20
- */
21
-
22
16
  /**
23
17
  * @param {MongoDB} d
24
- *
25
- *
26
18
  * @returns {Collection<WithRelations<db_col["$type_get"]>>}
27
19
  */
28
20
  const col = (d) => d.collection('posts');
29
21
 
30
22
  /**
31
23
  * @param {MongoDB} driver
32
- *
33
- *
34
24
  * @returns {db_col["upsert"]}
35
25
  */
36
26
  const upsert = (driver) => {
@@ -49,7 +39,6 @@ const upsert = (driver) => {
49
39
  ////
50
40
  // STOREFRONTS --> POSTS RELATION
51
41
  ////
52
-
53
42
  await update_entry_on_all_connection_of_relation(
54
43
  driver, 'storefronts', 'posts', objid, data, session
55
44
  );
@@ -82,8 +71,6 @@ const get = (driver) => get_regular(driver, col(driver));
82
71
 
83
72
  /**
84
73
  * @param {MongoDB} driver
85
- *
86
- *
87
74
  * @returns {db_col["remove"]}
88
75
  */
89
76
  const remove = (driver) => {
@@ -137,8 +124,6 @@ const count = (driver) => count_regular(driver, col(driver));
137
124
 
138
125
  /**
139
126
  * @param {MongoDB} driver
140
- *
141
- *
142
127
  * @return {db_col & { _col: ReturnType<col>}}
143
128
  */
144
129
  export const impl = (driver) => {
@@ -2,9 +2,8 @@
2
2
  * @import { db_products as db_col, RegularGetOptions } from '@storecraft/core/database'
3
3
  * @import { ProductType, VariantType } from '@storecraft/core/api'
4
4
  * @import { WithRelations } from './utils.types.js'
5
- * @import { Filter, AnyBulkWriteOperation } from 'mongodb'
5
+ * @import { AnyBulkWriteOperation } from 'mongodb'
6
6
  */
7
-
8
7
  import { Collection } from 'mongodb'
9
8
  import { MongoDB } from '../index.js'
10
9
  import {
@@ -38,16 +37,12 @@ import {
38
37
 
39
38
  /**
40
39
  * @param {MongoDB} d
41
- *
42
- *
43
40
  * @returns {Collection<WithRelations<db_col["$type_get"]>>}
44
41
  */
45
42
  const col = (d) => d.collection('products');
46
43
 
47
44
  /**
48
45
  * @param {MongoDB} driver
49
- *
50
- *
51
46
  * @returns {db_col["upsert"]}
52
47
  */
53
48
  const upsert = (driver) => {
@@ -206,15 +201,12 @@ const upsert = (driver) => {
206
201
 
207
202
 
208
203
  /**
209
- *
210
204
  * @param {MongoDB} driver
211
205
  */
212
206
  const get = (driver) => get_regular(driver, col(driver));
213
207
 
214
208
  /**
215
209
  * @param {MongoDB} driver
216
- *
217
- *
218
210
  * @returns {db_col["remove"]}
219
211
  */
220
212
  const remove = (driver) => {
@@ -307,108 +299,8 @@ const list = (driver) => list_regular(driver, col(driver));
307
299
  const count = (driver) => count_regular(driver, col(driver));
308
300
 
309
301
 
310
- /**
311
- * For now and because each product is related to very few
312
- * collections, I will not expose the query api, and use aggregate
313
- * instead.
314
- * @param {MongoDB} driver
315
- * @returns {db_col["list_all_product_collections"]}
316
- */
317
- const list_product_collections = (driver) => {
318
- return async (product) => {
319
- /** @type {RegularGetOptions} */
320
- const options = {
321
- expand: ['collections']
322
- };
323
-
324
- // We have collections embedded in products, so let's use it
325
- const item = await get_regular(driver, col(driver))(product, options);
326
-
327
- return sanitize_array(item?.collections ?? []);
328
- }
329
- }
330
-
331
- /**
332
- * For now and because each product is related to very few
333
- * collections, I will not expose the query api, and use aggregate
334
- * instead.
335
- *
336
- *
337
- * @param {MongoDB} driver
338
- *
339
- *
340
- * @returns {db_col["list_all_product_variants"]}
341
- */
342
- const list_product_variants = (driver) => {
343
- return async (product) => {
344
- /** @type {RegularGetOptions} */
345
- const options = {
346
- expand: ['variants']
347
- };
348
-
349
- // We have collections embedded in products, so let's use it
350
- const item = await get_regular(driver, col(driver))(product, options);
351
-
352
- if(item && ('variants' in item)) {
353
-
354
- return sanitize_array(item?.variants ?? []);
355
- }
356
-
357
- return [];
358
- }
359
- }
360
-
361
- /**
362
- * For now and because each product is related to very few
363
- * collections, I will not expose the query api, and use aggregate
364
- * instead.
365
- *
366
- *
367
- * @param {MongoDB} driver
368
- *
369
- *
370
- * @returns {db_col["list_all_related_products"]}
371
- */
372
- const list_related_products = (driver) => {
373
- return async (product) => {
374
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
375
- const options = {
376
- expand: ['related_products']
377
- };
378
-
379
- // We have collections embedded in products, so let's use it
380
- const item = await get_regular(driver, col(driver))(product, options);
381
-
382
- return sanitize_array(item?.related_products ?? []);
383
- }
384
- }
385
-
386
-
387
- /**
388
- * @param {MongoDB} driver
389
- *
390
- *
391
- * @returns {db_col["list_all_product_discounts"]}
392
- */
393
- const list_product_discounts = (driver) => {
394
- return async (product) => {
395
- /** @type {RegularGetOptions} */
396
- const options = {
397
- expand: ['discounts']
398
- };
399
-
400
- // We have collections embedded in products, so let's use it
401
- const item = await get_regular(driver, col(driver))(product, options);
402
-
403
- return sanitize_array(item?.discounts ?? []);
404
- }
405
- }
406
-
407
302
  /**
408
303
  * @param {MongoDB} driver
409
- *
410
- *
411
- * @returns {db_col["add_product_to_collection"]}
412
304
  */
413
305
  const add_product_to_collection = (driver) => {
414
306
  return async (product_id_or_handle, collection_handle_or_id) => {
@@ -437,9 +329,6 @@ const add_product_to_collection = (driver) => {
437
329
 
438
330
  /**
439
331
  * @param {MongoDB} driver
440
- *
441
- *
442
- * @returns {db_col["remove_product_from_collection"]}
443
332
  */
444
333
  const remove_product_from_collection = (driver) => {
445
334
  return async (product_id_or_handle, collection_handle_or_id) => {
@@ -466,52 +355,56 @@ const remove_product_from_collection = (driver) => {
466
355
 
467
356
  /**
468
357
  * @param {MongoDB} driver
469
- *
470
- *
471
358
  * @returns {db_col["changeStockOfBy"]}
472
359
  */
473
360
  const changeStockOfBy = (driver) => {
474
361
  return async (product_ids_or_handles, deltas) => {
475
362
 
476
- /**
477
- * @type {AnyBulkWriteOperation<WithRelations<ProductType | VariantType>>[]}
478
- */
479
- let ops = []
480
-
481
- product_ids_or_handles.forEach(
482
- (id, ix) => {
483
- ops.push(
484
- {
485
- updateOne: {
486
- filter: handle_or_id(id),
487
- update: {
488
- $inc: { qty: Math.round(deltas[ix]) }
363
+ try {
364
+ /**
365
+ * @type {AnyBulkWriteOperation<WithRelations<ProductType | VariantType>>[]}
366
+ */
367
+ let ops = []
368
+
369
+ product_ids_or_handles.forEach(
370
+ (id, ix) => {
371
+ ops.push(
372
+ {
373
+ updateOne: {
374
+ filter: handle_or_id(id),
375
+ update: {
376
+ $inc: { qty: Math.round(deltas[ix]) }
377
+ }
489
378
  }
490
379
  }
491
- }
492
- );
493
-
494
- ops.push(
495
- {
496
- updateOne: {
497
- filter: handle_or_id(id),
498
- update: {
499
- $max: { qty: 0 }
380
+ );
381
+
382
+ ops.push(
383
+ {
384
+ updateOne: {
385
+ filter: handle_or_id(id),
386
+ update: {
387
+ $max: { qty: 0 }
388
+ }
500
389
  }
501
390
  }
502
- }
503
- );
504
-
505
- }
506
- );
507
-
508
- if(!ops.length)
509
- return;
510
-
511
- await driver.resources.products._col.bulkWrite(
512
- ops
513
- );
391
+ );
392
+
393
+ }
394
+ );
395
+
396
+ if(!ops.length)
397
+ return;
398
+
399
+ await driver.resources.products._col.bulkWrite(
400
+ ops
401
+ );
402
+ } catch(e) {
403
+ console.log(e);
404
+ return false;
405
+ }
514
406
 
407
+ return true;
515
408
  }
516
409
  }
517
410
 
@@ -560,12 +453,6 @@ export const impl = (driver) => {
560
453
  upsert: upsert(driver),
561
454
  remove: remove(driver),
562
455
  list: list(driver),
563
- add_product_to_collection: add_product_to_collection(driver),
564
- remove_product_from_collection: remove_product_from_collection(driver),
565
- list_all_product_collections: list_product_collections(driver),
566
- list_all_product_variants: list_product_variants(driver),
567
- list_all_related_products: list_related_products(driver),
568
- list_all_product_discounts: list_product_discounts(driver),
569
456
  list_used_products_tags: list_used_products_tags(driver),
570
457
  count: count(driver)
571
458
  }
package/src/con.search.js CHANGED
@@ -2,11 +2,9 @@
2
2
  * @import { search as db_col, db_driver } from '@storecraft/core/database'
3
3
  * @import { QuickSearchResource, QuickSearchResult } from '@storecraft/core/api'
4
4
  */
5
-
6
5
  import { MongoDB } from '../index.js'
7
6
  import { query_to_mongo } from './utils.query.js';
8
7
 
9
-
10
8
  /**
11
9
  * @type {(keyof db_driver["resources"])[]}
12
10
  */
@@ -46,9 +44,7 @@ const prefix_to_resource = {
46
44
  }
47
45
 
48
46
  /**
49
- *
50
47
  * @param {string} id
51
- *
52
48
  * @returns {keyof db_driver["resources"]}
53
49
  */
54
50
  export const id_to_resource = id => {
@@ -65,8 +61,6 @@ export const id_to_resource = id => {
65
61
 
66
62
  /**
67
63
  * @param {MongoDB} driver
68
- *
69
- *
70
64
  * @returns {db_col["quicksearch"]}
71
65
  */
72
66
  export const quicksearch = (driver) => {
@@ -151,10 +145,8 @@ export const quicksearch = (driver) => {
151
145
 
152
146
  /**
153
147
  * @param {MongoDB} driver
154
- *
155
- *
156
148
  * @return {db_col}
157
- * */
149
+ */
158
150
  export const impl = (driver) => {
159
151
 
160
152
  return {
package/src/con.shared.js CHANGED
@@ -1,27 +1,28 @@
1
1
  /**
2
- * @import { db_crud, RegularGetOptions } from '@storecraft/core/database'
3
- * @import { ApiQuery, BaseType, Cursor, ExpandQuery, QuickSearchResource, QuickSearchResult, Tuple, withOptionalID } from '@storecraft/core/api'
4
- * @import { VQL } from '@storecraft/core/vql'
2
+ * @import { db_crud, withConcreteId, withConcreteIdAndHandle } from '@storecraft/core/database'
3
+ * @import {
4
+ * BaseType, ExpandQuery, withOptionalID
5
+ * } from '@storecraft/core/api'
5
6
  * @import { WithRelations } from './utils.types.js'
6
7
  * @import { WithId } from 'mongodb'
7
8
  */
8
-
9
9
  import { Collection } from 'mongodb'
10
10
  import { MongoDB } from '../index.js'
11
- import { handle_or_id, isUndef, sanitize_array,
12
- sanitize_one, to_objid } from './utils.funcs.js'
11
+ import {
12
+ handle_or_id, isUndef, sanitize_array,
13
+ sanitize_one, to_objid
14
+ } from './utils.funcs.js'
13
15
  import { query_to_mongo } from './utils.query.js'
14
16
  import { report_document_media } from './con.images.js'
15
17
  import { add_search_terms_relation_on } from './utils.relations.js'
16
18
 
17
19
 
18
20
  /**
19
- * @template {BaseType} T
20
- * @template {BaseType} G
21
- *
21
+ * @description Upsert function for MongoDB
22
+ * @template {Partial<withConcreteIdAndHandle<{}>>} T
23
+ * @template {Partial<withConcreteIdAndHandle<{}>>} G
22
24
  * @param {MongoDB} driver
23
25
  * @param {Collection<G>} col
24
- *
25
26
  * @returns {db_crud<T, G>["upsert"]}
26
27
  */
27
28
  export const upsert_regular = (driver, col) => {
@@ -72,20 +73,6 @@ export const upsert_regular = (driver, col) => {
72
73
 
73
74
  );
74
75
 
75
- // const res = await col.replaceOne(
76
- // // @ts-ignore
77
- // {
78
- // $or: [
79
- // data.id && { _id: to_objid(data.id) },
80
- // data.handle && { handle: data.handle }
81
- // ].filter(Boolean)
82
- // },
83
- // data,
84
- // {
85
- // session, upsert: true
86
- // }
87
- // );
88
-
89
76
  ////
90
77
  // REPORT IMAGES USAGE
91
78
  ////
@@ -105,7 +92,7 @@ export const upsert_regular = (driver, col) => {
105
92
  }
106
93
 
107
94
  /**
108
- * Extract relations names from item
95
+ * @description Extract relations names from item
109
96
  * @template {WithRelations<{}>} T
110
97
  * @param {T} item
111
98
  */
@@ -114,10 +101,8 @@ export const get_relations_names = item => {
114
101
  }
115
102
 
116
103
  /**
117
- * Expand relations in-place
118
- *
104
+ * @description Expand relations in-place
119
105
  * @template {any} T
120
- *
121
106
  * @param {WithRelations<T>[]} items
122
107
  * @param {ExpandQuery<T>} [expand_query]
123
108
  */
@@ -224,7 +209,7 @@ export const get_regular = (driver, col) => {
224
209
  }
225
210
 
226
211
  /**
227
- * get bulk of items, ordered, if something is missing, `undefined`
212
+ * @description get bulk of items, ordered, if something is missing, `undefined`
228
213
  * should be instead
229
214
  * @template {withOptionalID} T
230
215
  * @template {withOptionalID} G
@@ -234,11 +219,10 @@ export const get_regular = (driver, col) => {
234
219
  */
235
220
  export const get_bulk = (driver, col) => {
236
221
  return async (ids, options) => {
237
- const objids = ids.map(handle_or_id)
238
- .map(v => ('_id' in v) ? v._id : undefined)
239
- .filter(Boolean);
240
-
241
-
222
+ const objids = ids
223
+ .map(handle_or_id)
224
+ .map(v => ('_id' in v) ? v._id : undefined)
225
+ .filter(Boolean);
242
226
 
243
227
  const res = /** @type {(WithRelations<WithId<G>>)[]} */ (
244
228
  await col.find(
@@ -264,7 +248,6 @@ export const get_bulk = (driver, col) => {
264
248
  )
265
249
  );
266
250
 
267
-
268
251
  const sanitized = /** @type {G[]} */(
269
252
  /** @type {unknown} */(
270
253
  sanitize_array(res)
@@ -273,7 +256,9 @@ export const get_bulk = (driver, col) => {
273
256
 
274
257
  // now let's order them
275
258
  return ids.map(
276
- id => sanitized.find(s => s.id===id || s?.['handle']===id)
259
+ id => sanitized.find(
260
+ s => s.id===id || s?.['handle']===id
261
+ )
277
262
  );
278
263
 
279
264
  }
@@ -282,12 +267,8 @@ export const get_bulk = (driver, col) => {
282
267
 
283
268
  /**
284
269
  * @template T, G
285
- *
286
- *
287
270
  * @param {MongoDB} driver
288
271
  * @param {Collection<G>} col
289
- *
290
- *
291
272
  * @returns {db_crud<T, G>["remove"]}
292
273
  */
293
274
  export const remove_regular = (driver, col) => {
@@ -321,7 +302,8 @@ export const list_regular = (driver, col) => {
321
302
 
322
303
  /** @type {WithRelations<WithId<G>>[]} */
323
304
  const items = await col.find(
324
- filter, {
305
+ filter,
306
+ {
325
307
  sort,
326
308
  limit: reverse_sign==-1 ? query.limitToLast : query.limit,
327
309
  projection: expand_to_mongo_projection(query?.expand)
@@ -349,12 +331,8 @@ export const list_regular = (driver, col) => {
349
331
  /**
350
332
  * @template {any} T
351
333
  * @template {any} G
352
- *
353
- *
354
334
  * @param {MongoDB} driver
355
335
  * @param {Collection<G>} col
356
- *
357
- *
358
336
  * @returns {db_crud<T, G>["count"]}
359
337
  */
360
338
  export const count_regular = (driver, col) => {
@@ -2,7 +2,6 @@
2
2
  * @import { db_shipping as db_col } from '@storecraft/core/database'
3
3
  * @import { WithRelations } from './utils.types.js'
4
4
  */
5
-
6
5
  import { Collection } from 'mongodb'
7
6
  import { MongoDB } from '../index.js'
8
7
  import { count_regular, get_regular, list_regular } from './con.shared.js'
@@ -22,8 +21,6 @@ const col = (d) => d.collection('shipping_methods');
22
21
 
23
22
  /**
24
23
  * @param {MongoDB} driver
25
- *
26
- *
27
24
  * @returns {db_col["upsert"]}
28
25
  */
29
26
  const upsert = (driver) => {
@@ -79,8 +76,6 @@ const get = (driver) => get_regular(driver, col(driver));
79
76
 
80
77
  /**
81
78
  * @param {MongoDB} driver
82
- *
83
- *
84
79
  * @returns {db_col["remove"]}
85
80
  */
86
81
  const remove = (driver) => {
@@ -136,8 +131,6 @@ const count = (driver) => count_regular(driver, col(driver));
136
131
 
137
132
  /**
138
133
  * @param {MongoDB} driver
139
- *
140
- *
141
134
  * @return {db_col & { _col: ReturnType<col>}}
142
135
  */
143
136
  export const impl = (driver) => {
@@ -5,12 +5,13 @@
5
5
  * @import { StorefrontType } from '@storecraft/core/api'
6
6
  * @import { WithRelations } from './utils.types.js'
7
7
  */
8
-
9
8
  import { Collection } from 'mongodb'
10
9
  import { MongoDB } from '../index.js'
11
- import { count_regular, expand, get_regular, list_regular,
12
- remove_regular } from './con.shared.js'
13
- import { sanitize_array, sanitize_recursively, to_objid } from './utils.funcs.js'
10
+ import {
11
+ count_regular, expand, get_regular, list_regular,
12
+ remove_regular
13
+ } from './con.shared.js'
14
+ import { sanitize_recursively, to_objid } from './utils.funcs.js'
14
15
  import {
15
16
  add_search_terms_relation_on, create_explicit_relation, save_me
16
17
  } from './utils.relations.js';
@@ -24,8 +25,6 @@ const col = (d) => d.collection('storefronts');
24
25
 
25
26
  /**
26
27
  * @param {MongoDB} driver
27
- *
28
- *
29
28
  * @return {db_col["upsert"]}
30
29
  */
31
30
  const upsert = (driver) => {
@@ -223,10 +222,8 @@ const get_default_auto_generated_storefront = (driver) => {
223
222
 
224
223
  /**
225
224
  * @param {MongoDB} driver
226
- *
227
- *
228
225
  * @return {db_col & { _col: ReturnType<col>}}
229
- * */
226
+ */
230
227
  export const impl = (driver) => {
231
228
 
232
229
  return {