@storecraft/database-sql-base 1.0.10 → 1.0.11

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.
@@ -10,11 +10,13 @@ import { delete_entity_values_of_by_entity_id_or_handle, delete_me, delete_media
10
10
  products_with_discounts,
11
11
  products_with_variants,
12
12
  count_regular,
13
- products_with_related_products} from './con.shared.js'
14
- import { sanitize_array } from './utils.funcs.js'
13
+ products_with_related_products,
14
+ with_search} from './con.shared.js'
15
+ import { sanitize, sanitize_array } from './utils.funcs.js'
15
16
  import { query_to_eb, query_to_sort } from './utils.query.js'
16
17
  import { Transaction } from 'kysely'
17
18
  import { report_document_media } from './con.images.js'
19
+ import { union } from '@storecraft/core/api/utils.func.js'
18
20
 
19
21
 
20
22
  /**
@@ -27,7 +29,7 @@ export const table_name = 'products'
27
29
  * @param {db_col["$type_upsert"]} item
28
30
  */
29
31
  const is_variant = item => {
30
- if(item && ('variant_hint' in item)) {
32
+ if(item && ('variant_hint' in item) && ('parent_handle' in item)&& ('parent_id' in item)) {
31
33
  return item.parent_handle && item.parent_id && item.variant_hint;
32
34
  }
33
35
 
@@ -61,7 +63,15 @@ const upsert = (driver) => {
61
63
 
62
64
  const eligible_discounts = discounts.filter(
63
65
  d => driver.app.api.pricing.test_product_filters_against_product(d.info.filters, item)
64
- );
66
+ );
67
+
68
+ search_terms = union(
69
+ [
70
+ search_terms,
71
+ eligible_discounts.map(d => `discount:${d.handle}`),
72
+ eligible_discounts.map(d => `discount:${d.id}`),
73
+ ]
74
+ );
65
75
 
66
76
  const t = await driver.client.transaction().execute(
67
77
  async (trx) => {
@@ -88,13 +98,13 @@ const upsert = (driver) => {
88
98
  qty: item.qty,
89
99
  variants_options: 'variants_options' in item ? JSON.stringify(item.variants_options) : undefined,
90
100
  // 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,
101
+ parent_handle: ('parent_handle' in item) ? item.parent_handle : undefined,
102
+ parent_id: ('parent_id' in item) ? item.parent_id : undefined,
103
+ variant_hint: ('variant_hint' in item) ? JSON.stringify(item.variant_hint) : undefined,
94
104
  });
95
105
 
96
106
  // PRODUCTS => VARIANTS
97
- if(item && 'variant_hint' in item && is_variant(item)) {
107
+ if(item && ('variant_hint' in item) && is_variant(item)) {
98
108
  // remove previous
99
109
  await delete_entity_values_by_value_or_reporter('products_to_variants')(
100
110
  trx, item.id, item.handle
@@ -167,6 +177,7 @@ const upsert = (driver) => {
167
177
  * @returns {db_col["get"]}
168
178
  */
169
179
  const get = (driver) => {
180
+ // @ts-ignore
170
181
  return (id_or_handle, options) => {
171
182
 
172
183
  const expand = options?.expand ?? ['*'];
@@ -186,12 +197,14 @@ const get = (driver) => {
186
197
  expand_collections && products_with_collections(eb, id_or_handle, dtype),
187
198
  expand_discounts && products_with_discounts(eb, id_or_handle, dtype),
188
199
  expand_variants && products_with_variants(eb, id_or_handle, dtype),
189
- expand_related_products && products_with_related_products(eb, id_or_handle, dtype)
200
+ expand_related_products && products_with_related_products(eb, id_or_handle, dtype),
201
+ with_search(eb, id_or_handle, dtype)
190
202
  ].filter(Boolean)
191
203
  )
192
204
  .where(where_id_or_handle_table(id_or_handle))
193
205
  // .compile()
194
- .executeTakeFirst();
206
+ .executeTakeFirst()
207
+ .then(sanitize);
195
208
  }
196
209
  }
197
210
 
@@ -202,6 +215,7 @@ const get = (driver) => {
202
215
  * @returns {db_col["getBulk"]}
203
216
  */
204
217
  const getBulk = (driver) => {
218
+ // @ts-ignore
205
219
  return async (ids, options) => {
206
220
 
207
221
  const expand = options?.expand ?? ['*'];
@@ -221,7 +235,8 @@ const getBulk = (driver) => {
221
235
  expand_collections && products_with_collections(eb, eb.ref('products.id'), dtype),
222
236
  expand_discounts && products_with_discounts(eb, eb.ref('products.id'), dtype),
223
237
  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)
238
+ expand_related_products && products_with_related_products(eb, eb.ref('products.id'), dtype),
239
+ with_search(eb, eb.ref('products.id'), dtype)
225
240
  ].filter(Boolean)
226
241
  )
227
242
  .where(
@@ -233,7 +248,8 @@ const getBulk = (driver) => {
233
248
  )
234
249
  )
235
250
  // .compile()
236
- .execute();
251
+ .execute()
252
+ .then(sanitize_array);
237
253
 
238
254
  return ids.map(
239
255
  id => r.find(s => s.id===id || s?.handle===id)
@@ -331,11 +347,13 @@ const remove = (driver) => {
331
347
  * @returns {db_col["list"]}
332
348
  */
333
349
  const list = (driver) => {
350
+ // @ts-ignore
334
351
  return async (query) => {
335
352
 
336
353
  const expand = query.expand ?? ['*'];
337
354
  const expand_collections = expand.includes('*') || expand.includes('collections');
338
355
  const expand_discounts = expand.includes('*') || expand.includes('discounts');
356
+ // @ts-ignore
339
357
  const expand_variants = expand.includes('*') || expand.includes('variants');
340
358
  const expand_related_products = expand.includes('*') || expand.includes('related_products');
341
359
 
@@ -346,10 +364,19 @@ const list = (driver) => {
346
364
  eb => [
347
365
  with_tags(eb, eb.ref('products.id'), driver.dialectType),
348
366
  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)
367
+ with_search(eb, eb.ref('products.id'), driver.dialectType),
368
+
369
+ expand_collections &&
370
+ products_with_collections(eb, eb.ref('products.id'), driver.dialectType),
371
+
372
+ expand_discounts &&
373
+ products_with_discounts(eb, eb.ref('products.id'), driver.dialectType),
374
+
375
+ expand_variants &&
376
+ products_with_variants(eb, eb.ref('products.id'), driver.dialectType),
377
+
378
+ expand_related_products &&
379
+ products_with_related_products(eb, eb.ref('products.id'), driver.dialectType),
353
380
  ].filter(Boolean)
354
381
  )
355
382
  .where(
@@ -357,14 +384,14 @@ const list = (driver) => {
357
384
  return query_to_eb(eb, query, table_name);
358
385
  }
359
386
  )
360
- .orderBy(query_to_sort(query))
387
+ .orderBy(query_to_sort(query, table_name))
361
388
  .limit(query.limitToLast ?? query.limit ?? 10)
362
389
  .execute();
363
390
 
364
- if(query.limitToLast) items.reverse();
391
+ if(query.limitToLast) items.reverse();
365
392
  // .compile();
366
393
  // console.log(items)
367
-
394
+
368
395
  return sanitize_array(items);
369
396
  }
370
397
  }
package/src/con.search.js CHANGED
@@ -46,9 +46,6 @@ const prefix_to_resource = {
46
46
 
47
47
  }
48
48
 
49
- /**
50
- * @type {Record<keyof import('@storecraft/core/database').db_driver["resources"], string[]>}
51
- */
52
49
  const resource_to_props = {
53
50
  'auth_users': ['id', 'handle'],
54
51
  'collections': ['id', 'handle'],
@@ -100,8 +97,12 @@ export const quicksearch = (driver) => {
100
97
  .entries(resource_to_props)
101
98
  .filter(t => all || expand.includes(t[0]))
102
99
  .map(
100
+ /**
101
+ * @param {any} param0
102
+ */
103
103
  ([table_name, props]) => {
104
104
  // console.log(table_name, props)
105
+ props
105
106
  return jsonArrayFrom(
106
107
  eb
107
108
  .selectFrom(table_name)
@@ -111,7 +112,7 @@ export const quicksearch = (driver) => {
111
112
  return query_to_eb(eb, query, table_name);
112
113
  }
113
114
  )
114
- .orderBy(query_to_sort(query))
115
+ .orderBy(query_to_sort(query, table_name))
115
116
  .limit(query.limit ?? 5),
116
117
  driver.dialectType
117
118
  ).as(table_name)
@@ -119,8 +120,10 @@ export const quicksearch = (driver) => {
119
120
  )
120
121
  )
121
122
 
122
- /** @type {import('@storecraft/core/api').QuickSearchResult} */
123
- const items = await sts.executeTakeFirst();
123
+
124
+ const items = (/** @type {import('@storecraft/core/api').QuickSearchResult} */(
125
+ await sts.executeTakeFirst())
126
+ );
124
127
 
125
128
  const sanitized = Object.fromEntries(
126
129
  Object.entries(items).filter(
package/src/con.shared.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-ignore
1
2
  import { ExpressionWrapper, InsertQueryBuilder, Kysely, Transaction } from 'kysely'
2
3
  import { jsonArrayFrom, stringArrayFrom } from './con.helpers.json.js'
3
4
  import { SQL } from '../index.js';
@@ -271,7 +272,7 @@ export const delete_media_of = delete_entity_values_of_by_entity_id_or_handle('e
271
272
  *
272
273
  * @param {Kysely<Database>} trx
273
274
  * @param {T} table_name
274
- * @param {import('kysely').InsertObject<Database, T>} item values of the entity
275
+ * @param {import('kysely').InsertObject<Database, T> & {id: string, handle: string}} item values of the entity
275
276
  *
276
277
  */
277
278
  export const regular_upsert_me = async (trx, table_name, item) => {
@@ -280,8 +281,10 @@ export const regular_upsert_me = async (trx, table_name, item) => {
280
281
  await trx.deleteFrom(table_name).where(
281
282
  eb => eb.or(
282
283
  [
283
- item.id && eb('id', '=', item.id),
284
- item.handle && eb('handle', '=', item.handle),
284
+ // @ts-ignore
285
+ item?.id && eb('id', '=', item.id),
286
+ // @ts-ignore
287
+ item?.handle && eb('handle', '=', item.handle),
285
288
  ].filter(Boolean)
286
289
  )
287
290
  ).execute();
@@ -345,20 +348,6 @@ export const with_media = (eb, id_or_handle, sql_type) => {
345
348
  ).as('media');
346
349
  }
347
350
 
348
- /**
349
- * helper to select base attributes
350
- * @param {import('kysely').ExpressionBuilder<Database>} eb
351
- * @param {keyof Database} table
352
- * @return {import('kysely').SelectQueryBuilder<Database, table>}
353
- */
354
- const select_base_from = (eb, table) => {
355
- return [
356
- 'active', 'attributes', 'created_at', 'updated_at',
357
- 'description', 'handle', 'id'
358
- ].map(k => `${table}.${k}`).reduce(
359
- (p, c) => p.select(c), eb.selectFrom(table)
360
- );
361
- }
362
351
 
363
352
  /**
364
353
  * select as json array collections of a product
@@ -369,7 +358,14 @@ const select_base_from = (eb, table) => {
369
358
  */
370
359
  export const products_with_collections = (eb, product_id_or_handle, sql_type) => {
371
360
  return jsonArrayFrom(
372
- select_base_from(eb, 'collections')
361
+ eb.selectFrom('collections')
362
+ .select('collections.active')
363
+ .select('collections.attributes')
364
+ .select('collections.created_at')
365
+ .select('collections.updated_at')
366
+ .select('collections.description')
367
+ .select('collections.handle')
368
+ .select('collections.id')
373
369
  .select('collections.title')
374
370
  .select('collections.published')
375
371
  .select(eb => [
@@ -393,7 +389,14 @@ export const products_with_collections = (eb, product_id_or_handle, sql_type) =>
393
389
  */
394
390
  export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
395
391
  return jsonArrayFrom(
396
- select_base_from(eb, 'discounts')
392
+ eb.selectFrom('discounts')
393
+ .select('discounts.active')
394
+ .select('discounts.attributes')
395
+ .select('discounts.created_at')
396
+ .select('discounts.updated_at')
397
+ .select('discounts.description')
398
+ .select('discounts.handle')
399
+ .select('discounts.id')
397
400
  .select('discounts.title')
398
401
  .select('discounts.published')
399
402
  .select('discounts.application')
@@ -420,7 +423,14 @@ export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
420
423
  */
421
424
  export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
422
425
  return jsonArrayFrom(
423
- select_base_from(eb, 'products')
426
+ eb.selectFrom('products')
427
+ .select('products.active')
428
+ .select('products.attributes')
429
+ .select('products.created_at')
430
+ .select('products.updated_at')
431
+ .select('products.description')
432
+ .select('products.handle')
433
+ .select('products.id')
424
434
  .select('products.compare_at_price')
425
435
  .select('products.parent_handle')
426
436
  .select('products.parent_id')
@@ -451,7 +461,14 @@ export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
451
461
  */
452
462
  export const products_with_related_products = (eb, product_id_or_handle, sql_type) => {
453
463
  return jsonArrayFrom(
454
- select_base_from(eb, 'products')
464
+ eb.selectFrom('products')
465
+ .select('products.active')
466
+ .select('products.attributes')
467
+ .select('products.created_at')
468
+ .select('products.updated_at')
469
+ .select('products.description')
470
+ .select('products.handle')
471
+ .select('products.id')
455
472
  .select('products.compare_at_price')
456
473
  .select('products.parent_handle')
457
474
  .select('products.parent_id')
@@ -483,10 +500,17 @@ export const products_with_related_products = (eb, product_id_or_handle, sql_typ
483
500
  */
484
501
  export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
485
502
  return jsonArrayFrom(
486
- select_base_from(eb, 'collections')
487
- .select('collections.title')
488
- .select('collections.published')
489
- .select(eb => [
503
+ eb.selectFrom('collections')
504
+ .select('collections.active')
505
+ .select('collections.attributes')
506
+ .select('collections.created_at')
507
+ .select('collections.updated_at')
508
+ .select('collections.description')
509
+ .select('collections.handle')
510
+ .select('collections.id')
511
+ .select('collections.title')
512
+ .select('collections.published')
513
+ .select(eb => [
490
514
  with_tags(eb, eb.ref('collections.id'), sql_type),
491
515
  with_media(eb, eb.ref('collections.id'), sql_type),
492
516
  ])
@@ -507,7 +531,14 @@ export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
507
531
  */
508
532
  export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
509
533
  return jsonArrayFrom(
510
- select_base_from(eb, 'products')
534
+ eb.selectFrom('products')
535
+ .select('products.active')
536
+ .select('products.attributes')
537
+ .select('products.created_at')
538
+ .select('products.updated_at')
539
+ .select('products.description')
540
+ .select('products.handle')
541
+ .select('products.id')
511
542
  .select('products.title')
512
543
  .select('products.compare_at_price')
513
544
  .select('products.parent_handle')
@@ -538,7 +569,14 @@ export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
538
569
  */
539
570
  export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
540
571
  return jsonArrayFrom(
541
- select_base_from(eb, 'discounts')
572
+ eb.selectFrom('discounts')
573
+ .select('discounts.active')
574
+ .select('discounts.attributes')
575
+ .select('discounts.created_at')
576
+ .select('discounts.updated_at')
577
+ .select('discounts.description')
578
+ .select('discounts.handle')
579
+ .select('discounts.id')
542
580
  .select('discounts.application')
543
581
  .select('discounts.info')
544
582
  .select('discounts.priority')
@@ -565,7 +603,14 @@ export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
565
603
  */
566
604
  export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
567
605
  return jsonArrayFrom(
568
- select_base_from(eb, 'posts')
606
+ eb.selectFrom('posts')
607
+ .select('posts.active')
608
+ .select('posts.attributes')
609
+ .select('posts.created_at')
610
+ .select('posts.updated_at')
611
+ .select('posts.description')
612
+ .select('posts.handle')
613
+ .select('posts.id')
569
614
  .select('posts.text')
570
615
  .select('posts.title')
571
616
  .select(eb => [
@@ -589,7 +634,14 @@ export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
589
634
  */
590
635
  export const storefront_with_shipping = (eb, sf_id_or_handle, sql_type) => {
591
636
  return jsonArrayFrom(
592
- select_base_from(eb, 'shipping_methods')
637
+ eb.selectFrom('shipping_methods')
638
+ .select('shipping_methods.active')
639
+ .select('shipping_methods.attributes')
640
+ .select('shipping_methods.created_at')
641
+ .select('shipping_methods.updated_at')
642
+ .select('shipping_methods.description')
643
+ .select('shipping_methods.handle')
644
+ .select('shipping_methods.id')
593
645
  .select('shipping_methods.price')
594
646
  .select('shipping_methods.title')
595
647
  .select(eb => [
@@ -4,8 +4,8 @@ import { count_regular, delete_entity_values_by_value_or_reporter,
4
4
  delete_me, delete_media_of, delete_search_of,
5
5
  delete_tags_of, insert_media_of, insert_search_of,
6
6
  insert_tags_of, 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'
7
+ with_media, with_search, with_tags } from './con.shared.js'
8
+ import { sanitize, sanitize_array } from './utils.funcs.js'
9
9
  import { query_to_eb, query_to_sort } from './utils.query.js'
10
10
 
11
11
  /**
@@ -54,17 +54,20 @@ const upsert = (driver) => {
54
54
  * @returns {db_col["get"]}
55
55
  */
56
56
  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();
57
+ return async (id_or_handle, options) => {
58
+ const result = await 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
+ with_search(eb, id_or_handle, driver.dialectType),
65
+ ]
66
+ .filter(Boolean))
67
+ .where(where_id_or_handle_table(id_or_handle))
68
+ .executeTakeFirst();
69
+
70
+ return sanitize(result);
68
71
  }
69
72
  }
70
73
 
@@ -114,18 +117,19 @@ const list = (driver) => {
114
117
  .select(eb => [
115
118
  with_media(eb, eb.ref('shipping_methods.id'), driver.dialectType),
116
119
  with_tags(eb, eb.ref('shipping_methods.id'), driver.dialectType),
120
+ with_search(eb, eb.ref('shipping_methods.id'), driver.dialectType),
117
121
  ].filter(Boolean))
118
122
  .where(
119
123
  (eb) => {
120
124
  return query_to_eb(eb, query, table_name);
121
125
  }
122
126
  )
123
- .orderBy(query_to_sort(query))
127
+ .orderBy(query_to_sort(query, 'shipping_methods'))
124
128
  .limit(query.limitToLast ?? query.limit ?? 10)
125
129
  .execute();
126
130
 
127
131
  if(query.limitToLast) items.reverse();
128
-
132
+
129
133
  return sanitize_array(items);
130
134
  }
131
135
  }
@@ -8,8 +8,9 @@ import { delete_entity_values_of_by_entity_id_or_handle,
8
8
  storefront_with_products, storefront_with_shipping,
9
9
  regular_upsert_me, where_id_or_handle_table,
10
10
  with_media, with_tags,
11
- count_regular} from './con.shared.js'
12
- import { sanitize_array } from './utils.funcs.js'
11
+ count_regular,
12
+ with_search} from './con.shared.js'
13
+ import { sanitize, sanitize_array } from './utils.funcs.js'
13
14
  import { query_to_eb, query_to_sort } from './utils.query.js'
14
15
 
15
16
  /**
@@ -97,6 +98,7 @@ const upsert = (driver) => {
97
98
  * @returns {db_col["get"]}
98
99
  */
99
100
  const get = (driver) => {
101
+ // @ts-ignore
100
102
  return (id_or_handle, options) => {
101
103
  const expand = options?.expand ?? ['*'];
102
104
  const expand_all = expand.includes('*');
@@ -112,6 +114,7 @@ const get = (driver) => {
112
114
  .select(eb => [
113
115
  with_media(eb, id_or_handle, driver.dialectType),
114
116
  with_tags(eb, id_or_handle, driver.dialectType),
117
+ with_search(eb, id_or_handle, driver.dialectType),
115
118
  expand_collections && storefront_with_collections(eb, id_or_handle, driver.dialectType),
116
119
  expand_products && storefront_with_products(eb, id_or_handle, driver.dialectType),
117
120
  expand_discounts && storefront_with_discounts(eb, id_or_handle, driver.dialectType),
@@ -120,7 +123,8 @@ const get = (driver) => {
120
123
  ]
121
124
  .filter(Boolean))
122
125
  .where(where_id_or_handle_table(id_or_handle))
123
- .executeTakeFirst();
126
+ .executeTakeFirst()
127
+ .then(sanitize);
124
128
  }
125
129
  }
126
130
 
@@ -162,6 +166,7 @@ const remove = (driver) => {
162
166
  * @returns {db_col["list"]}
163
167
  */
164
168
  const list = (driver) => {
169
+ // @ts-ignore
165
170
  return async (query) => {
166
171
  const expand = query?.expand ?? ['*'];
167
172
  const expand_all = expand.includes('*');
@@ -177,6 +182,7 @@ const list = (driver) => {
177
182
  .select(eb => [
178
183
  with_media(eb, eb.ref('storefronts.id'), driver.dialectType),
179
184
  with_tags(eb, eb.ref('storefronts.id'), driver.dialectType),
185
+ with_search(eb, eb.ref('storefronts.id'), driver.dialectType),
180
186
  expand_collections && storefront_with_collections(eb, eb.ref('storefronts.id'), driver.dialectType),
181
187
  expand_products && storefront_with_products(eb, eb.ref('storefronts.id'), driver.dialectType),
182
188
  expand_discounts && storefront_with_discounts(eb, eb.ref('storefronts.id'), driver.dialectType),
@@ -189,7 +195,7 @@ const list = (driver) => {
189
195
  return query_to_eb(eb, query, table_name);
190
196
  }
191
197
  )
192
- .orderBy(query_to_sort(query))
198
+ .orderBy(query_to_sort(query, table_name))
193
199
  .limit(query.limitToLast ?? query.limit ?? 10)
194
200
  .execute();
195
201
 
package/src/con.tags.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SQL } from '../index.js'
2
2
  import { count_regular, delete_me, delete_search_of, insert_search_of,
3
3
  regular_upsert_me, where_id_or_handle_table } from './con.shared.js'
4
- import { sanitize_array } from './utils.funcs.js'
4
+ import { sanitize, sanitize_array } from './utils.funcs.js'
5
5
  import { query_to_eb, query_to_sort } from './utils.query.js'
6
6
 
7
7
  /**
@@ -48,7 +48,8 @@ const get = (driver) => {
48
48
  .selectFrom(table_name)
49
49
  .selectAll()
50
50
  .where(where_id_or_handle_table(id_or_handle))
51
- .executeTakeFirst();
51
+ .executeTakeFirst()
52
+ .then(sanitize);
52
53
  }
53
54
  }
54
55
 
@@ -94,7 +95,7 @@ const list = (driver) => {
94
95
  return query_to_eb(eb, query, table_name);
95
96
  }
96
97
  )
97
- .orderBy(query_to_sort(query))
98
+ .orderBy(query_to_sort(query, table_name))
98
99
  .limit(query.limitToLast ?? query.limit ?? 10)
99
100
  .execute();
100
101
 
@@ -2,7 +2,7 @@ import { Kysely } from 'kysely'
2
2
  import { SQL } from '../index.js'
3
3
  import { count_regular, delete_me, delete_search_of, insert_search_of,
4
4
  regular_upsert_me, safe_trx, where_id_or_handle_table } from './con.shared.js'
5
- import { sanitize_array } from './utils.funcs.js'
5
+ import { sanitize, sanitize_array } from './utils.funcs.js'
6
6
  import { query_to_eb, query_to_sort } from './utils.query.js'
7
7
  import { base64 } from '@storecraft/core/crypto';
8
8
 
@@ -71,7 +71,8 @@ const get = (driver) => {
71
71
  .selectFrom(table_name)
72
72
  .selectAll()
73
73
  .where(where_id_or_handle_table(id_or_handle))
74
- .executeTakeFirst();
74
+ .executeTakeFirst()
75
+ .then(sanitize);
75
76
  }
76
77
  }
77
78
 
@@ -119,7 +120,7 @@ const list = (driver) => {
119
120
  return query_to_eb(eb, query, table_name);
120
121
  }
121
122
  )
122
- .orderBy(query_to_sort(query))
123
+ .orderBy(query_to_sort(query, table_name))
123
124
  .limit(query.limitToLast ?? query.limit ?? 10)
124
125
  .execute();
125
126
 
@@ -19,14 +19,13 @@ export const delete_keys = (...keys) => {
19
19
  }
20
20
  }
21
21
 
22
+
23
+
22
24
  /**
23
25
  * Sanitize null/undefined valued keys
24
- *
25
- *
26
- * @template {Record<string, any>} T
27
- *
28
- *
26
+ * @template T
29
27
  * @param {T} o
28
+ * @return {import("./utils.types.js").ReplaceValuesOfKeys<T, 'active' | 'confirmed_mail', boolean>}
30
29
  */
31
30
  export const sanitize = o => {
32
31
  for (const key in o) {
@@ -37,12 +36,19 @@ export const sanitize = o => {
37
36
  continue;
38
37
  }
39
38
  if(key==='active') {
39
+ // @ts-ignore
40
+ o[key] = Boolean(value);
41
+ }
42
+ else if(key==='confirmed_mail') {
43
+ // @ts-ignore
40
44
  o[key] = Boolean(value);
41
45
  }
42
46
  else if(key==='price') {
47
+ // @ts-ignore
43
48
  o[key] = parseFloat(value);
44
49
  }
45
50
  else if(key==='compare_at_price') {
51
+ // @ts-ignore
46
52
  o[key] = parseFloat(value);
47
53
  }
48
54
  else if(typeof value === 'object') {
@@ -50,6 +56,7 @@ export const sanitize = o => {
50
56
  }
51
57
  }
52
58
 
59
+ // @ts-ignore
53
60
  return o;
54
61
  }
55
62
 
@@ -62,10 +69,11 @@ export const sanitize = o => {
62
69
  * @param {T[]} arr
63
70
  */
64
71
  export const sanitize_array = arr => {
72
+ const xx = []
65
73
  for(const p of arr) {
66
- sanitize(p);
74
+ xx.push(sanitize(p));
67
75
  }
68
- return arr;
76
+ return xx;
69
77
  }
70
78
 
71
79
  /**