@storecraft/database-mongodb 1.0.18 → 1.0.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/database-mongodb",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Storecraft database driver for mongodb on node / bun / deno platform",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -114,9 +114,11 @@ const count = (driver) => count_regular(driver, col(driver));
114
114
  * @returns {db_col["list_customer_orders"]}
115
115
  */
116
116
  const list_customer_orders = (driver) => {
117
- return async (customer_id, query) => {
117
+ return async (customer_id_or_email, query) => {
118
118
 
119
- const { filter: filter_query, sort, reverse_sign } = query_to_mongo(query);
119
+ const {
120
+ filter: filter_query, sort, reverse_sign
121
+ } = query_to_mongo(query);
120
122
 
121
123
  // console.log('query', query)
122
124
  // console.log('filter', JSON.stringify(filter_query, null, 2))
@@ -126,7 +128,7 @@ const list_customer_orders = (driver) => {
126
128
  /** @type {Filter<WithRelations<OrderData>>} */
127
129
  const filter = {
128
130
  $and: [
129
- {'_relations.search': `customer:${customer_id}` },
131
+ {'_relations.search': `customer:${customer_id_or_email}` },
130
132
  ]
131
133
  };
132
134
 
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @import {
3
- * DiscountType, FilterValue_p_in_price_range, FilterValue_p_not_in_collections,
4
- * FilterValue_p_in_collections, FilterValue_p_not_in_tags, FilterValue_p_in_tags,
5
- * FilterValue_p_in_products, FilterValue_p_not_in_products
3
+ * DiscountType, Filter_p_in_price_range, Filter_p_not_in_collections,
4
+ * Filter_p_in_collections, Filter_p_not_in_tags, Filter_p_in_tags,
5
+ * Filter_p_in_products, Filter_p_not_in_products
6
6
  * } from '@storecraft/core/api'
7
7
  */
8
8
 
@@ -11,7 +11,11 @@ import { to_objid_safe } from "./utils.funcs.js";
11
11
 
12
12
  /** @param {DiscountType} d */
13
13
  const is_order_discount = d => {
14
- return (d.info.details.meta.id===enums.DiscountMetaEnum.order.id);
14
+ return (
15
+ (d.info.details.type===enums.DiscountMetaEnum.order.type) ||
16
+ // @ts-ignore
17
+ (d.info.details.meta?.type===enums.DiscountMetaEnum.order.type)
18
+ );
15
19
  }
16
20
 
17
21
  /** @param {DiscountType} d */
@@ -40,7 +44,7 @@ export const discount_to_mongo_conjunctions = d => {
40
44
  const filters = d.info.filters;
41
45
 
42
46
  for(const filter of filters) {
43
- const op = filter.meta.op;
47
+ const op = filter.op ?? filter.meta.op;
44
48
 
45
49
  switch (op) {
46
50
  case enums.FilterMetaEnum.p_all.op:
@@ -48,58 +52,64 @@ export const discount_to_mongo_conjunctions = d => {
48
52
  break;
49
53
  case enums.FilterMetaEnum.p_in_products.op:
50
54
  {
51
- const cast = /** @type {FilterValue_p_in_products} */ (
52
- filter.value ?? []
55
+ const filter_cast = /** @type {Filter_p_in_products} */ (
56
+ filter
53
57
  );
58
+ const value = filter_cast?.value ?? [];
54
59
 
55
60
  conjunctions.push(
56
- { handle: { $in: cast.map(it => it.handle) } }
61
+ { handle: { $in: value.map(it => it.handle) } }
57
62
  );
58
63
  }
59
64
  break;
60
65
  case enums.FilterMetaEnum.p_not_in_products.op:
61
66
  {
62
- const cast = /** @type {FilterValue_p_not_in_products} */(
63
- filter.value ?? []
67
+ const filter_cast = /** @type {Filter_p_not_in_products} */ (
68
+ filter
64
69
  );
70
+
71
+ const value = filter_cast?.value ?? [];
65
72
 
66
73
  conjunctions.push(
67
- { handle: { $nin: cast.map(it => it.handle) } }
74
+ { handle: { $nin: value.map(it => it.handle) } }
68
75
  );
69
76
  }
70
77
  break;
71
78
  case enums.FilterMetaEnum.p_in_tags.op:
72
79
  {
73
- const cast = /** @type {FilterValue_p_in_tags} */ (
74
- filter.value ?? []
80
+ const filter_cast = /** @type {Filter_p_in_tags} */ (
81
+ filter
75
82
  );
83
+ const value = filter_cast?.value ?? [];
76
84
 
77
85
  conjunctions.push(
78
- { tags: { $in: cast } }
86
+ { tags: { $in: value } }
79
87
  );
80
88
  }
81
89
  break;
82
90
  case enums.FilterMetaEnum.p_not_in_tags.op:
83
91
  {
84
- const cast = /** @type {FilterValue_p_not_in_tags} */(
85
- filter.value ?? []
92
+ const filter_cast = /** @type {Filter_p_not_in_tags} */ (
93
+ filter
86
94
  );
95
+ const value = filter_cast?.value ?? [];
87
96
 
88
97
  conjunctions.push(
89
- { tags: { $nin: cast } }
98
+ { tags: { $nin: value } }
90
99
  );
91
100
  }
92
101
  break;
93
102
  case enums.FilterMetaEnum.p_in_collections.op:
94
103
  {
95
- const cast = /** @type {FilterValue_p_in_collections} */ (
96
- filter.value ?? []
104
+ const filter_cast = /** @type {Filter_p_in_collections} */ (
105
+ filter
97
106
  );
98
-
107
+ const value = filter_cast?.value ?? [];
108
+
99
109
  conjunctions.push(
100
110
  {
101
111
  '_relations.collections.ids': {
102
- $in: cast.map(c => to_objid_safe(c.id)).filter(Boolean)
112
+ $in: value.map(c => to_objid_safe(c.id)).filter(Boolean)
103
113
  }
104
114
  }
105
115
  );
@@ -107,14 +117,15 @@ export const discount_to_mongo_conjunctions = d => {
107
117
  break;
108
118
  case enums.FilterMetaEnum.p_not_in_collections.op:
109
119
  {
110
- const cast = /** @type {FilterValue_p_not_in_collections} */ (
111
- filter.value ?? []
120
+ const filter_cast = /** @type {Filter_p_not_in_collections} */ (
121
+ filter
112
122
  );
123
+ const value = filter_cast?.value ?? [];
113
124
 
114
125
  conjunctions.push(
115
126
  {
116
127
  '_relations.collections.ids': {
117
- $nin: cast.map(c => to_objid_safe(c.id)).filter(Boolean)
128
+ $nin: value.map(c => to_objid_safe(c.id)).filter(Boolean)
118
129
  }
119
130
  }
120
131
  );
@@ -122,16 +133,19 @@ export const discount_to_mongo_conjunctions = d => {
122
133
  break;
123
134
  case enums.FilterMetaEnum.p_in_price_range.op:
124
135
  {
125
- const cast = /** @type {FilterValue_p_in_price_range} */ (
136
+ const filter_cast = /** @type {Filter_p_in_price_range} */ (
137
+ filter
138
+ );
139
+ const value = /** @type {Filter_p_in_price_range["value"]} */ (
126
140
  {
127
141
  from: 0,
128
142
  to: Number.POSITIVE_INFINITY,
129
- ...(filter?.value ?? {})
143
+ ...(filter_cast?.value ?? {})
130
144
  }
131
145
  );
132
146
 
133
- const from = extract_abs_number(cast.from);
134
- const to = extract_abs_number(cast.to);
147
+ const from = extract_abs_number(value.from);
148
+ const to = extract_abs_number(value.to);
135
149
 
136
150
  const conj = { price: { $and: [] } };
137
151
 
@@ -308,107 +308,7 @@ const count = (driver) => count_regular(driver, col(driver));
308
308
 
309
309
 
310
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
311
  * @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
- /**
408
- * @param {MongoDB} driver
409
- *
410
- *
411
- * @returns {db_col["add_product_to_collection"]}
412
312
  */
413
313
  const add_product_to_collection = (driver) => {
414
314
  return async (product_id_or_handle, collection_handle_or_id) => {
@@ -437,9 +337,6 @@ const add_product_to_collection = (driver) => {
437
337
 
438
338
  /**
439
339
  * @param {MongoDB} driver
440
- *
441
- *
442
- * @returns {db_col["remove_product_from_collection"]}
443
340
  */
444
341
  const remove_product_from_collection = (driver) => {
445
342
  return async (product_id_or_handle, collection_handle_or_id) => {
@@ -466,52 +363,56 @@ const remove_product_from_collection = (driver) => {
466
363
 
467
364
  /**
468
365
  * @param {MongoDB} driver
469
- *
470
- *
471
366
  * @returns {db_col["changeStockOfBy"]}
472
367
  */
473
368
  const changeStockOfBy = (driver) => {
474
369
  return async (product_ids_or_handles, deltas) => {
475
370
 
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]) }
371
+ try {
372
+ /**
373
+ * @type {AnyBulkWriteOperation<WithRelations<ProductType | VariantType>>[]}
374
+ */
375
+ let ops = []
376
+
377
+ product_ids_or_handles.forEach(
378
+ (id, ix) => {
379
+ ops.push(
380
+ {
381
+ updateOne: {
382
+ filter: handle_or_id(id),
383
+ update: {
384
+ $inc: { qty: Math.round(deltas[ix]) }
385
+ }
489
386
  }
490
387
  }
491
- }
492
- );
493
-
494
- ops.push(
495
- {
496
- updateOne: {
497
- filter: handle_or_id(id),
498
- update: {
499
- $max: { qty: 0 }
388
+ );
389
+
390
+ ops.push(
391
+ {
392
+ updateOne: {
393
+ filter: handle_or_id(id),
394
+ update: {
395
+ $max: { qty: 0 }
396
+ }
500
397
  }
501
398
  }
502
- }
503
- );
504
-
505
- }
506
- );
507
-
508
- if(!ops.length)
509
- return;
510
-
511
- await driver.resources.products._col.bulkWrite(
512
- ops
513
- );
399
+ );
400
+
401
+ }
402
+ );
403
+
404
+ if(!ops.length)
405
+ return;
406
+
407
+ await driver.resources.products._col.bulkWrite(
408
+ ops
409
+ );
410
+ } catch(e) {
411
+ console.log(e);
412
+ return false;
413
+ }
514
414
 
415
+ return true;
515
416
  }
516
417
  }
517
418
 
@@ -560,12 +461,6 @@ export const impl = (driver) => {
560
461
  upsert: upsert(driver),
561
462
  remove: remove(driver),
562
463
  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
464
  list_used_products_tags: list_used_products_tags(driver),
570
465
  count: count(driver)
571
466
  }