@storecraft/database-sql-base 1.0.10 → 1.0.12

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/src/con.images.js CHANGED
@@ -1,9 +1,14 @@
1
+ /**
2
+ * @import { db_images as db_col } from '@storecraft/core/database'
3
+ * @import { Database } from '../types.sql.tables.js'
4
+ */
5
+
1
6
  import { func } from '@storecraft/core/api'
2
7
  import { SQL } from '../index.js'
3
8
  import { count_regular, delete_me, delete_search_of,
4
9
  insert_search_of, regular_upsert_me, where_id_or_handle_table
5
10
  } from './con.shared.js'
6
- import { sanitize_array } from './utils.funcs.js'
11
+ import { sanitize, sanitize_array } from './utils.funcs.js'
7
12
  import { query_to_eb, query_to_sort } from './utils.query.js'
8
13
  import { Transaction } from 'kysely'
9
14
  import { ID } from '@storecraft/core/api/utils.func.js'
@@ -11,9 +16,6 @@ import {
11
16
  image_url_to_handle, image_url_to_name
12
17
  } from '@storecraft/core/api/con.images.logic.js'
13
18
 
14
- /**
15
- * @typedef {import('@storecraft/core/database').db_images} db_col
16
- */
17
19
  export const table_name = 'images'
18
20
 
19
21
  /**
@@ -58,7 +60,8 @@ const get = (driver) => {
58
60
  .selectFrom(table_name)
59
61
  .selectAll()
60
62
  .where(where_id_or_handle_table(id_or_handle))
61
- .executeTakeFirst();
63
+ .executeTakeFirst()
64
+ .then(sanitize);
62
65
  }
63
66
  }
64
67
 
@@ -105,7 +108,7 @@ const remove = (driver) => {
105
108
  */
106
109
  export const report_document_media = (driver) => {
107
110
  /**
108
- * @param {Transaction<import('../index.js').Database>} [transaction]
111
+ * @param {Transaction<Database>} [transaction]
109
112
  */
110
113
  return async (item, transaction) => {
111
114
  if(!(item?.media?.length))
@@ -113,7 +116,7 @@ export const report_document_media = (driver) => {
113
116
 
114
117
  /**
115
118
  *
116
- * @param {Transaction<import('../index.js').Database>} trx
119
+ * @param {Transaction<Database>} trx
117
120
  */
118
121
  const doit = async (trx) => {
119
122
  const dates = func.apply_dates({});
@@ -193,7 +196,6 @@ export const report_document_media = (driver) => {
193
196
  */
194
197
  const list = (driver) => {
195
198
  return async (query) => {
196
-
197
199
  const items = await driver.client
198
200
  .selectFrom(table_name)
199
201
  .selectAll()
@@ -202,7 +204,7 @@ const list = (driver) => {
202
204
  return query_to_eb(eb, query, table_name);
203
205
  }
204
206
  )
205
- .orderBy(query_to_sort(query))
207
+ .orderBy(query_to_sort(query, 'images')) // ts complains because `usage` field is absent
206
208
  .limit(query.limitToLast ?? query.limit ?? 10)
207
209
  .execute();
208
210
 
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @import { db_notifications as db_col } from '@storecraft/core/database'
3
+ */
1
4
  import { SQL } from '../index.js'
2
5
  import { count_regular, delete_me, delete_search_of,
3
6
  insert_search_of, regular_upsert_me, where_id_or_handle_table,
@@ -5,9 +8,6 @@ import { count_regular, delete_me, delete_search_of,
5
8
  import { sanitize_array } from './utils.funcs.js'
6
9
  import { query_to_eb, query_to_sort } from './utils.query.js'
7
10
 
8
- /**
9
- * @typedef {import('@storecraft/core/database').db_notifications} db_col
10
- */
11
11
  export const table_name = 'notifications'
12
12
 
13
13
  /**
@@ -25,6 +25,7 @@ const upsert = (driver) => {
25
25
  item.id, item.id, table_name
26
26
  );
27
27
  await regular_upsert_me(trx, table_name, {
28
+ handle: null,
28
29
  created_at: item.created_at,
29
30
  updated_at: item.updated_at,
30
31
  message: item.message,
@@ -121,7 +122,7 @@ const list = (driver) => {
121
122
  return query_to_eb(eb, query, table_name);
122
123
  }
123
124
  )
124
- .orderBy(query_to_sort(query))
125
+ .orderBy(query_to_sort(query, 'notifications'))
125
126
  .limit(query.limitToLast ?? query.limit ?? 10)
126
127
  .execute();
127
128
 
package/src/con.orders.js CHANGED
@@ -1,15 +1,16 @@
1
+ /**
2
+ * @import { db_orders as db_col } from '@storecraft/core/database'
3
+ */
4
+
1
5
  import { SQL } from '../index.js'
2
6
  import { report_document_media } from './con.images.js'
3
7
  import { count_regular, delete_me, delete_media_of, delete_search_of,
4
8
  delete_tags_of, insert_media_of, insert_search_of,
5
9
  insert_tags_of, regular_upsert_me, where_id_or_handle_table,
6
- with_media, with_tags} from './con.shared.js'
7
- import { sanitize_array } from './utils.funcs.js'
10
+ with_media, with_search, with_tags} from './con.shared.js'
11
+ import { sanitize, sanitize_array } from './utils.funcs.js'
8
12
  import { query_to_eb, query_to_sort } from './utils.query.js'
9
13
 
10
- /**
11
- * @typedef {import('@storecraft/core/database').db_orders} db_col
12
- */
13
14
  export const table_name = 'orders'
14
15
 
15
16
  /**
@@ -82,10 +83,12 @@ const get = (driver) => {
82
83
  .select(eb => [
83
84
  with_media(eb, id_or_handle, driver.dialectType),
84
85
  with_tags(eb, id_or_handle, driver.dialectType),
86
+ with_search(eb, id_or_handle, driver.dialectType),
85
87
  ]
86
88
  .filter(Boolean))
87
89
  .where(where_id_or_handle_table(id_or_handle))
88
- .executeTakeFirst();
90
+ .executeTakeFirst()
91
+ .then(sanitize);
89
92
  }
90
93
  }
91
94
 
@@ -138,13 +141,14 @@ const list = (driver) => {
138
141
  .select(eb => [
139
142
  with_media(eb, eb.ref('orders.id'), driver.dialectType),
140
143
  with_tags(eb, eb.ref('orders.id'), driver.dialectType),
144
+ with_search(eb, eb.ref('orders.id'), driver.dialectType),
141
145
  ].filter(Boolean))
142
146
  .where(
143
147
  (eb) => {
144
148
  return query_to_eb(eb, query, table_name);
145
149
  }
146
150
  )
147
- .orderBy(query_to_sort(query))
151
+ .orderBy(query_to_sort(query, table_name))
148
152
  .limit(query.limitToLast ?? query.limit ?? 10)
149
153
  .execute();
150
154
 
package/src/con.posts.js CHANGED
@@ -1,16 +1,18 @@
1
+ /**
2
+ * @import { db_posts as db_col } from '@storecraft/core/database'
3
+ */
4
+
1
5
  import { SQL } from '../index.js'
6
+ import { stringArrayFrom } from './con.helpers.json.js'
2
7
  import { report_document_media } from './con.images.js'
3
8
  import { count_regular, delete_entity_values_by_value_or_reporter,
4
9
  delete_me, delete_media_of, delete_search_of, delete_tags_of,
5
10
  insert_media_of, insert_search_of, insert_tags_of,
6
11
  regular_upsert_me, where_id_or_handle_table,
7
- with_media, with_tags} from './con.shared.js'
8
- import { sanitize_array } from './utils.funcs.js'
12
+ with_media, with_search, with_tags} from './con.shared.js'
13
+ import { sanitize, sanitize_array } from './utils.funcs.js'
9
14
  import { query_to_eb, query_to_sort } from './utils.query.js'
10
15
 
11
- /**
12
- * @typedef {import('@storecraft/core/database').db_posts} db_col
13
- */
14
16
  export const table_name = 'posts'
15
17
 
16
18
  /**
@@ -54,17 +56,20 @@ const upsert = (driver) => {
54
56
  * @returns {db_col["get"]}
55
57
  */
56
58
  const get = (driver) => {
57
- return (id_or_handle, options) => {
58
- return driver.client
59
- .selectFrom(table_name)
60
- .selectAll()
61
- .select(eb => [
62
- with_media(eb, id_or_handle, driver.dialectType),
63
- with_tags(eb, id_or_handle, driver.dialectType),
64
- ]
65
- .filter(Boolean))
66
- .where(where_id_or_handle_table(id_or_handle))
67
- .executeTakeFirst();
59
+ return async (id_or_handle, options) => {
60
+ const result = await driver.client
61
+ .selectFrom(table_name)
62
+ .selectAll()
63
+ .select(eb => [
64
+ with_media(eb, id_or_handle, driver.dialectType),
65
+ with_tags(eb, id_or_handle, driver.dialectType),
66
+ with_search(eb, id_or_handle, driver.dialectType),
67
+ ]
68
+ .filter(Boolean))
69
+ .where(where_id_or_handle_table(id_or_handle))
70
+ .executeTakeFirst();
71
+
72
+ return sanitize(result);
68
73
  }
69
74
  }
70
75
 
@@ -114,18 +119,20 @@ const list = (driver) => {
114
119
  .select(eb => [
115
120
  with_media(eb, eb.ref('posts.id'), driver.dialectType),
116
121
  with_tags(eb, eb.ref('posts.id'), driver.dialectType),
122
+ with_search(eb, eb.ref('posts.id'), driver.dialectType),
117
123
  ].filter(Boolean))
118
124
  .where(
119
125
  (eb) => {
120
126
  return query_to_eb(eb, query, table_name);
121
127
  }
122
128
  )
123
- .orderBy(query_to_sort(query))
129
+ .orderBy(query_to_sort(query, 'posts'))
130
+ // .orderBy()
124
131
  .limit(query.limitToLast ?? query.limit ?? 10)
125
132
  .execute();
126
133
 
127
134
  if(query.limitToLast) items.reverse();
128
-
135
+
129
136
  return sanitize_array(items);
130
137
  }
131
138
  }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @import { ProductType, VariantType } from '@storecraft/core/api'
3
+ * @import { db_products as db_col } from '@storecraft/core/database'
4
+ * @import { Database } from '../types.sql.tables.js'
5
+ */
6
+
1
7
  import { enums } from '@storecraft/core/api'
2
8
  import { SQL } from '../index.js'
3
9
  import { delete_entity_values_of_by_entity_id_or_handle, delete_me, delete_media_of,
@@ -10,16 +16,15 @@ import { delete_entity_values_of_by_entity_id_or_handle, delete_me, delete_media
10
16
  products_with_discounts,
11
17
  products_with_variants,
12
18
  count_regular,
13
- products_with_related_products} from './con.shared.js'
14
- import { sanitize_array } from './utils.funcs.js'
19
+ products_with_related_products,
20
+ with_search} from './con.shared.js'
21
+ import { sanitize, sanitize_array } from './utils.funcs.js'
15
22
  import { query_to_eb, query_to_sort } from './utils.query.js'
16
23
  import { Transaction } from 'kysely'
17
24
  import { report_document_media } from './con.images.js'
25
+ import { union } from '@storecraft/core/api/utils.func.js'
18
26
 
19
27
 
20
- /**
21
- * @typedef {import('@storecraft/core/database').db_products} db_col
22
- */
23
28
  export const table_name = 'products'
24
29
 
25
30
  /**
@@ -27,7 +32,7 @@ export const table_name = 'products'
27
32
  * @param {db_col["$type_upsert"]} item
28
33
  */
29
34
  const is_variant = item => {
30
- if(item && ('variant_hint' in item)) {
35
+ if(item && ('variant_hint' in item) && ('parent_handle' in item)&& ('parent_id' in item)) {
31
36
  return item.parent_handle && item.parent_id && item.variant_hint;
32
37
  }
33
38
 
@@ -61,7 +66,15 @@ const upsert = (driver) => {
61
66
 
62
67
  const eligible_discounts = discounts.filter(
63
68
  d => driver.app.api.pricing.test_product_filters_against_product(d.info.filters, item)
64
- );
69
+ );
70
+
71
+ search_terms = union(
72
+ [
73
+ search_terms,
74
+ eligible_discounts.map(d => `discount:${d.handle}`),
75
+ eligible_discounts.map(d => `discount:${d.id}`),
76
+ ]
77
+ );
65
78
 
66
79
  const t = await driver.client.transaction().execute(
67
80
  async (trx) => {
@@ -88,13 +101,13 @@ const upsert = (driver) => {
88
101
  qty: item.qty,
89
102
  variants_options: 'variants_options' in item ? JSON.stringify(item.variants_options) : undefined,
90
103
  // no variants yet
91
- parent_handle: 'parent_handle' in item ? item.parent_handle : undefined,
92
- parent_id: 'parent_id' in item ? item.parent_id : undefined,
93
- variant_hint: 'variant_hint' in item ? JSON.stringify(item.variant_hint) : undefined,
104
+ parent_handle: ('parent_handle' in item) ? item.parent_handle : undefined,
105
+ parent_id: ('parent_id' in item) ? item.parent_id : undefined,
106
+ variant_hint: ('variant_hint' in item) ? JSON.stringify(item.variant_hint) : undefined,
94
107
  });
95
108
 
96
109
  // PRODUCTS => VARIANTS
97
- if(item && 'variant_hint' in item && is_variant(item)) {
110
+ if(item && ('variant_hint' in item) && is_variant(item)) {
98
111
  // remove previous
99
112
  await delete_entity_values_by_value_or_reporter('products_to_variants')(
100
113
  trx, item.id, item.handle
@@ -167,6 +180,7 @@ const upsert = (driver) => {
167
180
  * @returns {db_col["get"]}
168
181
  */
169
182
  const get = (driver) => {
183
+ // @ts-ignore
170
184
  return (id_or_handle, options) => {
171
185
 
172
186
  const expand = options?.expand ?? ['*'];
@@ -186,12 +200,14 @@ const get = (driver) => {
186
200
  expand_collections && products_with_collections(eb, id_or_handle, dtype),
187
201
  expand_discounts && products_with_discounts(eb, id_or_handle, dtype),
188
202
  expand_variants && products_with_variants(eb, id_or_handle, dtype),
189
- expand_related_products && products_with_related_products(eb, id_or_handle, dtype)
203
+ expand_related_products && products_with_related_products(eb, id_or_handle, dtype),
204
+ with_search(eb, id_or_handle, dtype)
190
205
  ].filter(Boolean)
191
206
  )
192
207
  .where(where_id_or_handle_table(id_or_handle))
193
208
  // .compile()
194
- .executeTakeFirst();
209
+ .executeTakeFirst()
210
+ .then(sanitize);
195
211
  }
196
212
  }
197
213
 
@@ -202,6 +218,7 @@ const get = (driver) => {
202
218
  * @returns {db_col["getBulk"]}
203
219
  */
204
220
  const getBulk = (driver) => {
221
+ // @ts-ignore
205
222
  return async (ids, options) => {
206
223
 
207
224
  const expand = options?.expand ?? ['*'];
@@ -221,7 +238,8 @@ const getBulk = (driver) => {
221
238
  expand_collections && products_with_collections(eb, eb.ref('products.id'), dtype),
222
239
  expand_discounts && products_with_discounts(eb, eb.ref('products.id'), dtype),
223
240
  expand_variants && products_with_variants(eb, eb.ref('products.id'), dtype),
224
- expand_related_products && products_with_related_products(eb, eb.ref('products.id'), dtype)
241
+ expand_related_products && products_with_related_products(eb, eb.ref('products.id'), dtype),
242
+ with_search(eb, eb.ref('products.id'), dtype)
225
243
  ].filter(Boolean)
226
244
  )
227
245
  .where(
@@ -233,7 +251,8 @@ const getBulk = (driver) => {
233
251
  )
234
252
  )
235
253
  // .compile()
236
- .execute();
254
+ .execute()
255
+ .then(sanitize_array);
237
256
 
238
257
  return ids.map(
239
258
  id => r.find(s => s.id===id || s?.handle===id)
@@ -247,10 +266,8 @@ const getBulk = (driver) => {
247
266
  */
248
267
  const remove_internal = (driver) => {
249
268
  /**
250
- * @param {import('@storecraft/core/api').ProductType |
251
- * import('@storecraft/core/api').VariantType
252
- * } product
253
- * @param {Transaction<import('../index.js').Database>} trx
269
+ * @param {ProductType | VariantType} product
270
+ * @param {Transaction<Database>} trx
254
271
  */
255
272
  return async (product, trx) => {
256
273
  // entities
@@ -331,11 +348,13 @@ const remove = (driver) => {
331
348
  * @returns {db_col["list"]}
332
349
  */
333
350
  const list = (driver) => {
351
+ // @ts-ignore
334
352
  return async (query) => {
335
353
 
336
354
  const expand = query.expand ?? ['*'];
337
355
  const expand_collections = expand.includes('*') || expand.includes('collections');
338
356
  const expand_discounts = expand.includes('*') || expand.includes('discounts');
357
+ // @ts-ignore
339
358
  const expand_variants = expand.includes('*') || expand.includes('variants');
340
359
  const expand_related_products = expand.includes('*') || expand.includes('related_products');
341
360
 
@@ -346,10 +365,19 @@ const list = (driver) => {
346
365
  eb => [
347
366
  with_tags(eb, eb.ref('products.id'), driver.dialectType),
348
367
  with_media(eb, eb.ref('products.id'), driver.dialectType),
349
- expand_collections && products_with_collections(eb, eb.ref('products.id'), driver.dialectType),
350
- expand_discounts && products_with_discounts(eb, eb.ref('products.id'), driver.dialectType),
351
- expand_variants && products_with_variants(eb, eb.ref('products.id'), driver.dialectType),
352
- expand_related_products && products_with_related_products(eb, eb.ref('products.id'), driver.dialectType)
368
+ with_search(eb, eb.ref('products.id'), driver.dialectType),
369
+
370
+ expand_collections &&
371
+ products_with_collections(eb, eb.ref('products.id'), driver.dialectType),
372
+
373
+ expand_discounts &&
374
+ products_with_discounts(eb, eb.ref('products.id'), driver.dialectType),
375
+
376
+ expand_variants &&
377
+ products_with_variants(eb, eb.ref('products.id'), driver.dialectType),
378
+
379
+ expand_related_products &&
380
+ products_with_related_products(eb, eb.ref('products.id'), driver.dialectType),
353
381
  ].filter(Boolean)
354
382
  )
355
383
  .where(
@@ -357,14 +385,14 @@ const list = (driver) => {
357
385
  return query_to_eb(eb, query, table_name);
358
386
  }
359
387
  )
360
- .orderBy(query_to_sort(query))
388
+ .orderBy(query_to_sort(query, table_name))
361
389
  .limit(query.limitToLast ?? query.limit ?? 10)
362
390
  .execute();
363
391
 
364
- if(query.limitToLast) items.reverse();
392
+ if(query.limitToLast) items.reverse();
365
393
  // .compile();
366
394
  // console.log(items)
367
-
395
+
368
396
  return sanitize_array(items);
369
397
  }
370
398
  }
package/src/con.search.js CHANGED
@@ -1,15 +1,14 @@
1
- import { SQL } from '../index.js'
2
- import { jsonArrayFrom } from './con.helpers.json.js';
3
- import { query_to_eb, query_to_sort } from './utils.query.js';
4
-
5
1
  /**
6
- * @typedef {import('@storecraft/core/database').search} db_col
2
+ * @import { QuickSearchResult } from '@storecraft/core/api'
3
+ * @import { search as db_col, db_driver } from '@storecraft/core/database'
7
4
  */
8
5
 
9
-
6
+ import { SQL } from '../index.js'
7
+ import { jsonArrayFrom } from './con.helpers.json.js';
8
+ import { query_to_eb, query_to_sort } from './utils.query.js';
10
9
 
11
10
  /**
12
- * @type {(keyof import('@storecraft/core/database').db_driver["resources"])[]}
11
+ * @type {(keyof db_driver["resources"])[]}
13
12
  */
14
13
  const tables = [
15
14
  'tags',
@@ -27,7 +26,7 @@ const tables = [
27
26
  ]
28
27
 
29
28
  /**
30
- * @type {Record<string, keyof import('@storecraft/core/database').db_driver["resources"]>}
29
+ * @type {Record<string, keyof db_driver["resources"]>}
31
30
  */
32
31
  const prefix_to_resource = {
33
32
  'au': 'auth_users',
@@ -46,9 +45,6 @@ const prefix_to_resource = {
46
45
 
47
46
  }
48
47
 
49
- /**
50
- * @type {Record<keyof import('@storecraft/core/database').db_driver["resources"], string[]>}
51
- */
52
48
  const resource_to_props = {
53
49
  'auth_users': ['id', 'handle'],
54
50
  'collections': ['id', 'handle'],
@@ -68,7 +64,7 @@ const resource_to_props = {
68
64
  *
69
65
  * @param {string} id
70
66
  *
71
- * @returns {keyof import('@storecraft/core/database').db_driver["resources"]}
67
+ * @returns {keyof db_driver["resources"]}
72
68
  */
73
69
  export const id_to_resource = id => {
74
70
  let result = undefined;
@@ -100,8 +96,12 @@ export const quicksearch = (driver) => {
100
96
  .entries(resource_to_props)
101
97
  .filter(t => all || expand.includes(t[0]))
102
98
  .map(
99
+ /**
100
+ * @param {any} param0
101
+ */
103
102
  ([table_name, props]) => {
104
103
  // console.log(table_name, props)
104
+ props
105
105
  return jsonArrayFrom(
106
106
  eb
107
107
  .selectFrom(table_name)
@@ -111,7 +111,7 @@ export const quicksearch = (driver) => {
111
111
  return query_to_eb(eb, query, table_name);
112
112
  }
113
113
  )
114
- .orderBy(query_to_sort(query))
114
+ .orderBy(query_to_sort(query, table_name))
115
115
  .limit(query.limit ?? 5),
116
116
  driver.dialectType
117
117
  ).as(table_name)
@@ -119,8 +119,10 @@ export const quicksearch = (driver) => {
119
119
  )
120
120
  )
121
121
 
122
- /** @type {import('@storecraft/core/api').QuickSearchResult} */
123
- const items = await sts.executeTakeFirst();
122
+
123
+ const items = (/** @type {QuickSearchResult} */(
124
+ await sts.executeTakeFirst())
125
+ );
124
126
 
125
127
  const sanitized = Object.fromEntries(
126
128
  Object.entries(items).filter(