@storecraft/database-mongodb 1.0.13 → 1.0.14

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,3 +1,10 @@
1
+ /**
2
+ * @import { db_products as db_col, RegularGetOptions } from '@storecraft/core/database'
3
+ * @import { ProductType, VariantType } from '@storecraft/core/api'
4
+ * @import { WithRelations } from './utils.relations.js'
5
+ * @import { Filter, AnyBulkWriteOperation } from 'mongodb'
6
+ */
7
+
1
8
  import { Collection } from 'mongodb'
2
9
  import { MongoDB } from '../index.js'
3
10
  import {
@@ -25,18 +32,11 @@ import {
25
32
  test_product_filters_against_product
26
33
  } from '@storecraft/core/api/con.pricing.logic.js'
27
34
 
28
- /**
29
- * @typedef {import('@storecraft/core/database').db_products} db_col
30
- */
31
-
32
35
  /**
33
36
  * @param {MongoDB} d
34
37
  *
35
38
  *
36
- * @returns {Collection<
37
- * import('./utils.relations.js').WithRelations<db_col["$type_get"]>
38
- * >}
39
- *
39
+ * @returns {Collection<WithRelations<db_col["$type_get"]>>}
40
40
  */
41
41
  const col = (d) => d.collection('products');
42
42
 
@@ -304,7 +304,7 @@ const count = (driver) => count_regular(driver, col(driver));
304
304
  */
305
305
  const list_product_collections = (driver) => {
306
306
  return async (product) => {
307
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
307
+ /** @type {RegularGetOptions} */
308
308
  const options = {
309
309
  expand: ['collections']
310
310
  };
@@ -329,7 +329,7 @@ const list_product_collections = (driver) => {
329
329
  */
330
330
  const list_product_variants = (driver) => {
331
331
  return async (product) => {
332
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
332
+ /** @type {RegularGetOptions} */
333
333
  const options = {
334
334
  expand: ['variants']
335
335
  };
@@ -380,7 +380,7 @@ const list_related_products = (driver) => {
380
380
  */
381
381
  const list_product_discounts = (driver) => {
382
382
  return async (product) => {
383
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
383
+ /** @type {RegularGetOptions} */
384
384
  const options = {
385
385
  expand: ['discounts']
386
386
  };
@@ -462,12 +462,7 @@ const changeStockOfBy = (driver) => {
462
462
  return async (product_ids_or_handles, deltas) => {
463
463
 
464
464
  /**
465
- * @type {import('mongodb').AnyBulkWriteOperation<
466
- * import('./utils.relations.js').WithRelations<
467
- * import('@storecraft/core/api').ProductType |
468
- * import('@storecraft/core/api').VariantType
469
- * >
470
- * >[]}
465
+ * @type {AnyBulkWriteOperation<WithRelations<ProductType | VariantType>>[]}
471
466
  */
472
467
  let ops = []
473
468
 
package/src/con.search.js CHANGED
@@ -1,14 +1,14 @@
1
- import { MongoDB } from '../index.js'
2
- import { query_to_mongo } from './utils.query.js';
3
-
4
1
  /**
5
- * @typedef {import('@storecraft/core/database').search} db_col
2
+ * @import { search as db_col, db_driver } from '@storecraft/core/database'
3
+ * @import { QuickSearchResource, QuickSearchResult } from '@storecraft/core/api'
6
4
  */
7
5
 
6
+ import { MongoDB } from '../index.js'
7
+ import { query_to_mongo } from './utils.query.js';
8
8
 
9
9
 
10
10
  /**
11
- * @type {(keyof import('@storecraft/core/database').db_driver["resources"])[]}
11
+ * @type {(keyof db_driver["resources"])[]}
12
12
  */
13
13
  const tables = [
14
14
  'tags',
@@ -26,7 +26,7 @@ const tables = [
26
26
  ]
27
27
 
28
28
  /**
29
- * @type {Record<string, keyof import('@storecraft/core/database').db_driver["resources"]>}
29
+ * @type {Record<string, keyof db_driver["resources"]>}
30
30
  */
31
31
  const prefix_to_resource = {
32
32
  'au': 'auth_users',
@@ -49,7 +49,7 @@ const prefix_to_resource = {
49
49
  *
50
50
  * @param {string} id
51
51
  *
52
- * @returns {keyof import('@storecraft/core/database').db_driver["resources"]}
52
+ * @returns {keyof db_driver["resources"]}
53
53
  */
54
54
  export const id_to_resource = id => {
55
55
  let result = undefined;
@@ -103,7 +103,7 @@ export const quicksearch = (driver) => {
103
103
  const db = driver.mongo_client.db(driver.name);
104
104
 
105
105
 
106
- const items = /** @type {import('@storecraft/core/api').QuickSearchResource[]} */ (
106
+ const items = /** @type {QuickSearchResource[]} */ (
107
107
  await db.collection(tables_filtered[0]).aggregate(
108
108
  [
109
109
  ...pipeline,
@@ -124,7 +124,7 @@ export const quicksearch = (driver) => {
124
124
  );
125
125
 
126
126
 
127
- /** @type {import('@storecraft/core/api').QuickSearchResult} */
127
+ /** @type {QuickSearchResult} */
128
128
  const result = {};
129
129
 
130
130
  items.reduce(
package/src/con.shared.js CHANGED
@@ -1,3 +1,11 @@
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'
5
+ * @import { WithRelations } from './utils.relations.js'
6
+ * @import { WithId } from 'mongodb'
7
+ */
8
+
1
9
  import { Collection } from 'mongodb'
2
10
  import { MongoDB } from '../index.js'
3
11
  import { handle_or_id, isUndef, sanitize_array,
@@ -8,13 +16,13 @@ import { add_search_terms_relation_on } from './utils.relations.js'
8
16
 
9
17
 
10
18
  /**
11
- * @template {import('@storecraft/core/api').BaseType} T
12
- * @template {import('@storecraft/core/api').BaseType} G
19
+ * @template {BaseType} T
20
+ * @template {BaseType} G
13
21
  *
14
22
  * @param {MongoDB} driver
15
23
  * @param {Collection<G>} col
16
24
  *
17
- * @returns {import('@storecraft/core/database').db_crud<T, G>["upsert"]}
25
+ * @returns {db_crud<T, G>["upsert"]}
18
26
  */
19
27
  export const upsert_regular = (driver, col) => {
20
28
 
@@ -80,13 +88,10 @@ export const get_relations_names = item => {
80
88
  /**
81
89
  * Expand relations in-place
82
90
  *
83
- *
84
91
  * @template {any} T
85
92
  *
86
- *
87
- * @param {import('./utils.relations.js').WithRelations<T>[]} items
88
- * @param {import('@storecraft/core/api').ExpandQuery} [expand_query]
89
- *
93
+ * @param {WithRelations<T>[]} items
94
+ * @param {ExpandQuery<T>} [expand_query]
90
95
  */
91
96
  export const expand = (items, expand_query=undefined) => {
92
97
 
@@ -98,9 +103,9 @@ export const expand = (items, expand_query=undefined) => {
98
103
  const all = expand_query.includes('*');
99
104
 
100
105
  for(const item of items) {
101
- expand_query = all ? get_relations_names(item) : expand_query;
106
+ const what_to_expand = all ? get_relations_names(item) : expand_query;
102
107
 
103
- for(const e of (expand_query ?? [])) {
108
+ for(const e of (what_to_expand ?? [])) {
104
109
  // try to find embedded documents relations
105
110
  const rel = item?._relations?.[e];
106
111
  if(rel===undefined || rel===null)
@@ -139,8 +144,8 @@ export const zeroed_relations = {
139
144
 
140
145
 
141
146
  /**
142
- *
143
- * @param {import('@storecraft/core/database').RegularGetOptions["expand"]} expand
147
+ * @template T
148
+ * @param {ExpandQuery<T>} expand
144
149
  */
145
150
  export const expand_to_mongo_projection = (expand) => {
146
151
  let projection = {}
@@ -161,18 +166,16 @@ export const expand_to_mongo_projection = (expand) => {
161
166
  /**
162
167
  * @template T, G
163
168
  *
164
- *
165
169
  * @param {MongoDB} driver
166
170
  * @param {Collection<G>} col
167
171
  *
168
- *
169
- * @returns {import('@storecraft/core/database').db_crud<T, G>["get"]}
172
+ * @returns {db_crud<T, G>["get"]}
170
173
  */
171
174
  export const get_regular = (driver, col) => {
172
175
  return async (id_or_handle, options) => {
173
176
  const filter = handle_or_id(id_or_handle);
174
177
 
175
- /** @type {import('./utils.relations.js').WithRelations<import('mongodb').WithId<G>>} */
178
+ /** @type {WithRelations<WithId<G>>} */
176
179
  const res = await col.findOne(
177
180
  filter,
178
181
  {
@@ -192,15 +195,15 @@ export const get_regular = (driver, col) => {
192
195
  * should be instead
193
196
  *
194
197
  *
195
- * @template {import('@storecraft/core/api').withOptionalID} T
196
- * @template {import('@storecraft/core/api').withOptionalID} G
198
+ * @template {withOptionalID} T
199
+ * @template {withOptionalID} G
197
200
  *
198
201
  *
199
202
  * @param {MongoDB} driver
200
203
  * @param {Collection<G>} col
201
204
  *
202
205
  *
203
- * @returns {import('@storecraft/core/database').db_crud<T, G>["getBulk"]}
206
+ * @returns {db_crud<T, G>["getBulk"]}
204
207
  */
205
208
  export const get_bulk = (driver, col) => {
206
209
  return async (ids, options) => {
@@ -245,7 +248,7 @@ export const get_bulk = (driver, col) => {
245
248
  * @param {Collection<G>} col
246
249
  *
247
250
  *
248
- * @returns {import('@storecraft/core/database').db_crud<T, G>["remove"]}
251
+ * @returns {db_crud<T, G>["remove"]}
249
252
  */
250
253
  export const remove_regular = (driver, col) => {
251
254
  return async (id_or_handle) => {
@@ -259,15 +262,15 @@ export const remove_regular = (driver, col) => {
259
262
  }
260
263
 
261
264
  /**
262
- * @template {any} T
263
- * @template {any} G
265
+ * @template T
266
+ * @template G
264
267
  *
265
268
  *
266
269
  * @param {MongoDB} driver
267
270
  * @param {Collection<G>} col
268
271
  *
269
272
  *
270
- * @returns {import('@storecraft/core/database').db_crud<T, G>["list"]}
273
+ * @returns {db_crud<T, G>["list"]}
271
274
  */
272
275
  export const list_regular = (driver, col) => {
273
276
  return async (query) => {
@@ -276,11 +279,11 @@ export const list_regular = (driver, col) => {
276
279
 
277
280
  // console.log('reverse_sign', reverse_sign)
278
281
  // console.log('query', query)
279
- console.log('filter', JSON.stringify(filter, null, 2))
282
+ // console.log('filter', JSON.stringify(filter, null, 2))
280
283
  // console.log('sort', sort)
281
284
  // console.log('expand', query?.expand)
282
285
 
283
- /** @type {import('mongodb').WithId<G>[]} */
286
+ /** @type {WithRelations<WithId<G>>[]} */
284
287
  const items = await col.find(
285
288
  filter, {
286
289
  sort,
@@ -293,7 +296,7 @@ export const list_regular = (driver, col) => {
293
296
 
294
297
  // try expand relations, that were asked
295
298
  const items_expended = expand(items, query?.expand);
296
-
299
+
297
300
  const sanitized = sanitize_array(items_expended);
298
301
 
299
302
  // console.log('sanitized', sanitized)
@@ -311,7 +314,7 @@ export const list_regular = (driver, col) => {
311
314
  * @param {Collection<G>} col
312
315
  *
313
316
  *
314
- * @returns {import('@storecraft/core/database').db_crud<T, G>["count"]}
317
+ * @returns {db_crud<T, G>["count"]}
315
318
  */
316
319
  export const count_regular = (driver, col) => {
317
320
  return async (query) => {
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @import { db_shipping as db_col } from '@storecraft/core/database'
3
+ * @import { WithRelations } from './utils.relations.js'
4
+ */
5
+
1
6
  import { Collection } from 'mongodb'
2
7
  import { MongoDB } from '../index.js'
3
8
  import { count_regular, get_regular, list_regular } from './con.shared.js'
@@ -9,15 +14,11 @@ import {
9
14
  update_entry_on_all_connection_of_relation
10
15
  } from './utils.relations.js';
11
16
 
12
- /**
13
- * @typedef {import('@storecraft/core/database').db_shipping} db_col
14
- */
15
-
16
17
  /**
17
18
  * @param {MongoDB} d
18
19
  *
19
20
  *
20
- * @returns {Collection<import('./utils.relations.js').WithRelations<db_col["$type_get"]>>}
21
+ * @returns {Collection<WithRelations<db_col["$type_get"]>>}
21
22
  */
22
23
  const col = (d) => d.collection('shipping_methods');
23
24
 
@@ -1,3 +1,10 @@
1
+ /**
2
+ * @import {
3
+ * db_storefronts as db_col, RegularGetOptions
4
+ * } from '@storecraft/core/database'
5
+ * @import { WithRelations } from './utils.relations.js'
6
+ */
7
+
1
8
  import { Collection } from 'mongodb'
2
9
  import { MongoDB } from '../index.js'
3
10
  import { count_regular, get_regular, list_regular,
@@ -8,18 +15,10 @@ import {
8
15
  } from './utils.relations.js';
9
16
  import { report_document_media } from './con.images.js';
10
17
 
11
- /**
12
- * @typedef {import('@storecraft/core/database').db_storefronts} db_col
13
- */
14
-
15
18
  /**
16
19
  * @param {MongoDB} d
17
20
  *
18
- *
19
- * @returns {Collection<
20
- * import('./utils.relations.js').WithRelations<db_col["$type_get"]>>
21
- * }
22
- *
21
+ * @returns {Collection<WithRelations<db_col["$type_get"]>>}
23
22
  */
24
23
  const col = (d) => d.collection('storefronts');
25
24
 
@@ -113,7 +112,7 @@ const count = (driver) => count_regular(driver, col(driver));
113
112
  */
114
113
  const list_storefront_products = (driver) => {
115
114
  return async (product) => {
116
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
115
+ /** @type {RegularGetOptions} */
117
116
  const options = {
118
117
  expand: ['products']
119
118
  };
@@ -132,7 +131,7 @@ const list_storefront_products = (driver) => {
132
131
  */
133
132
  const list_storefront_collections = (driver) => {
134
133
  return async (product) => {
135
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
134
+ /** @type {RegularGetOptions} */
136
135
  const options = {
137
136
  expand: ['collections']
138
137
  };
@@ -151,7 +150,7 @@ const list_storefront_collections = (driver) => {
151
150
  */
152
151
  const list_storefront_discounts = (driver) => {
153
152
  return async (product) => {
154
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
153
+ /** @type {RegularGetOptions} */
155
154
  const options = {
156
155
  expand: ['discounts']
157
156
  };
@@ -169,7 +168,7 @@ const list_storefront_discounts = (driver) => {
169
168
  */
170
169
  const list_storefront_shipping_methods = (driver) => {
171
170
  return async (product) => {
172
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
171
+ /** @type {RegularGetOptions} */
173
172
  const options = {
174
173
  expand: ['shipping_methods']
175
174
  };
@@ -188,7 +187,7 @@ const list_storefront_shipping_methods = (driver) => {
188
187
  */
189
188
  const list_storefront_posts = (driver) => {
190
189
  return async (product) => {
191
- /** @type {import('@storecraft/core/database').RegularGetOptions} */
190
+ /** @type {RegularGetOptions} */
192
191
  const options = {
193
192
  expand: ['posts']
194
193
  };
package/src/con.tags.js CHANGED
@@ -1,13 +1,12 @@
1
+ /**
2
+ * @import { db_tags as db_col } from '@storecraft/core/database'
3
+ */
4
+
1
5
  import { Collection } from 'mongodb'
2
6
  import { MongoDB } from '../index.js'
3
7
  import { count_regular, get_regular, list_regular,
4
8
  remove_regular, upsert_regular } from './con.shared.js'
5
9
 
6
- /**
7
- * @typedef {import('@storecraft/core/database').db_tags} db_col
8
- */
9
-
10
-
11
10
  /**
12
11
  * @param {MongoDB} d
13
12
  *
@@ -1,13 +1,12 @@
1
+ /**
2
+ * @import { db_templates as db_col } from '@storecraft/core/database'
3
+ */
4
+
1
5
  import { Collection } from 'mongodb'
2
6
  import { MongoDB } from '../index.js'
3
7
  import { count_regular, get_regular, list_regular,
4
8
  remove_regular, upsert_regular } from './con.shared.js'
5
9
 
6
- /**
7
- * @typedef {import('@storecraft/core/database').db_templates} db_col
8
- */
9
-
10
-
11
10
  /**
12
11
  * @param {MongoDB} d
13
12
  *
@@ -140,10 +140,12 @@ export const handle_or_id = (handle_or_id) => {
140
140
  */
141
141
  export const objid_or_else_filter = (id_or_else, else_key='handle') => {
142
142
  try {
143
+ // @ts-ignore
143
144
  return {
144
145
  _id: to_objid(id_or_else)
145
146
  }
146
147
  } catch (e) {
148
+ // @ts-ignore
147
149
  return {
148
150
  [else_key]: id_or_else
149
151
  }
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @import { ApiQuery, Cursor, Tuple } from '@storecraft/core/api'
3
+ * @import { VQL } from '@storecraft/core/vql'
4
+ */
5
+
1
6
  import { to_objid } from "./utils.funcs.js";
2
7
  import { parse } from "@storecraft/core/vql";
3
8
 
@@ -17,7 +22,7 @@ let a = {
17
22
  * 4. (a1, a2, a3) >= (b1, b2, b3) ==> (a1 > b1) || (a1=b1 & a2>b2) || (a1=b1 & a2=b2 & a3>=b3)
18
23
  *
19
24
  *
20
- * @param {import("@storecraft/core/api").Cursor} c
25
+ * @param {Cursor} c
21
26
  * @param {'>' | '>=' | '<' | '<='} relation
22
27
  * @param {(x: [k: string, v: any]) => [k: string, v: any]} transformer
23
28
  * Your chance to change key and value
@@ -70,7 +75,7 @@ export const query_cursor_to_mongo = (c, relation, transformer=(x)=>x) => {
70
75
  }
71
76
 
72
77
  /**
73
- * @param {import("@storecraft/core/vql").VQL.Node} node
78
+ * @param {VQL.Node} node
74
79
  */
75
80
  export const query_vql_node_to_mongo = node => {
76
81
  if(node.op==='LEAF') {
@@ -106,7 +111,7 @@ export const query_vql_node_to_mongo = node => {
106
111
 
107
112
  /**
108
113
  *
109
- * @param {import("@storecraft/core/vql").VQL.Node} root
114
+ * @param {VQL.Node} root
110
115
  */
111
116
  export const query_vql_to_mongo = root => {
112
117
  return root ? query_vql_node_to_mongo(root) : undefined;
@@ -115,9 +120,7 @@ export const query_vql_to_mongo = root => {
115
120
  /**
116
121
  * Let's transform ids into mongo ids
117
122
  *
118
- *
119
- * @param {import("@storecraft/core/api").Tuple<>} c a cursor record
120
- *
123
+ * @param {Tuple} c a cursor record
121
124
  *
122
125
  * @returns {[k: string, v: any]}
123
126
  */
@@ -131,7 +134,7 @@ const transform = c => {
131
134
  * Convert an API Query into mongo dialect, also sanitize.
132
135
  *
133
136
  *
134
- * @param {import("@storecraft/core/api").ApiQuery} q
137
+ * @param {ApiQuery<any>} q
135
138
  */
136
139
  export const query_to_mongo = (q) => {
137
140
  try {
@@ -1,3 +1,10 @@
1
+ /**
2
+ * @import { ID } from '@storecraft/core/database'
3
+ * @import { BaseType } from '@storecraft/core/api'
4
+ * @import { WithRelations } from './utils.relations.js'
5
+ * @import { Filter } from 'mongodb'
6
+ */
7
+
1
8
  import { ClientSession, ObjectId } from 'mongodb';
2
9
  import { isDef, isUndef, to_objid } from './utils.funcs.js';
3
10
  import { MongoDB } from '../index.js';
@@ -6,12 +13,9 @@ import { zeroed_relations } from './con.shared.js';
6
13
  /**
7
14
  * @template {any} T
8
15
  *
9
- *
10
16
  * @typedef {Object} Relation
11
17
  * @property {ObjectId[]} [ids]
12
- * @property {Record<
13
- * import('@storecraft/core/database').ID, T>
14
- * } [entries]
18
+ * @property {Record<ID, T>} [entries]
15
19
  */
16
20
 
17
21
  /**
@@ -27,7 +31,7 @@ import { zeroed_relations } from './con.shared.js';
27
31
  * Basically creates an ids array and embedded documents for fast retrival.
28
32
  *
29
33
  *
30
- * @template {import('@storecraft/core/api').BaseType} T
34
+ * @template {BaseType} T
31
35
  *
32
36
  *
33
37
  * @param {MongoDB} driver our driver
@@ -50,7 +54,7 @@ export const create_explicit_relation = async (
50
54
  if(isUndef(value))
51
55
  return data;
52
56
 
53
- /** @type {import('@storecraft/core/api').BaseType[]} */
57
+ /** @type {BaseType[]} */
54
58
  const items = Array.isArray(value) ? value : [value];
55
59
 
56
60
  /** @type {WithRelations<any>} */
@@ -328,7 +332,7 @@ export const remove_specific_connection_of_relation = (
328
332
  * @param {MongoDB} driver mongodb driver instance
329
333
  * @param {string} collection the collection from which the `relation` is from
330
334
  * @param {string} relation_name the `relation` name
331
- * @param {import('mongodb').Filter<any>} from_object_filter
335
+ * @param {Filter<any>} from_object_filter
332
336
  * `mongodb` Filter to locate the first document, the from part of the connection
333
337
  * @param {ObjectId} entry_objid the proper `ObjectId` of the entry
334
338
  * @param {ClientSession} [session] client `session` for atomicity purposes
package/types.public.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { MongoClientOptions } from 'mongodb';
2
2
 
3
- export { MongoDB, migrateToLatest } from './index.js';
3
+ export { MongoDB, migrateToLatest, MongoVectorStore } from './index.js';
4
4
 
5
5
  export type Config = {
6
6
  /**
@@ -12,6 +12,7 @@ export type Config = {
12
12
  /**
13
13
  * @description the name of the database, if absent, will be infered at init
14
14
  * with env `app.platform.env.MONGODB_NAME`
15
+ * @default 'main'
15
16
  */
16
17
  db_name?: string;
17
18
 
@@ -19,4 +20,5 @@ export type Config = {
19
20
  * @description mongo client options
20
21
  */
21
22
  options?: MongoClientOptions;
22
- }
23
+ }
24
+