@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.shared.js CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @import { db_crud } from '@storecraft/core/database'
3
+ * @import { Database } from '../types.sql.tables.js'
4
+ * @import { ExpressionBuilder, InsertObject } from 'kysely'
5
+ * @import { SqlDialectType } from '../types.public.js'
6
+ */
1
7
  import { ExpressionWrapper, InsertQueryBuilder, Kysely, Transaction } from 'kysely'
2
8
  import { jsonArrayFrom, stringArrayFrom } from './con.helpers.json.js'
3
9
  import { SQL } from '../index.js';
@@ -42,7 +48,7 @@ export const safe_trx = (k) => {
42
48
  * @param {SQL} driver
43
49
  * @param {keyof Database} table_name
44
50
  *
45
- * @returns {import('@storecraft/core/database').db_crud["count"]}
51
+ * @returns {db_crud["count"]}
46
52
  */
47
53
  export const count_regular = (driver, table_name) => {
48
54
  return async (query) => {
@@ -69,7 +75,7 @@ export const count_regular = (driver, table_name) => {
69
75
  */
70
76
  export const where_id_or_handle_entity = (id_or_handle) => {
71
77
  /**
72
- * @param {import('kysely').ExpressionBuilder<Database>} eb
78
+ * @param {ExpressionBuilder<Database>} eb
73
79
  */
74
80
  return (eb) => eb.or(
75
81
  [
@@ -85,7 +91,7 @@ export const where_id_or_handle_entity = (id_or_handle) => {
85
91
  */
86
92
  export const where_id_or_handle_table = (id_or_handle) => {
87
93
  /**
88
- * @param {import('kysely').ExpressionBuilder<Database>} eb
94
+ * @param {ExpressionBuilder<Database>} eb
89
95
  */
90
96
  return (eb) => eb.or(
91
97
  [
@@ -138,7 +144,7 @@ export const delete_entity_values_of_by_entity_id_or_handle =
138
144
  (entity_table_name) => {
139
145
  /**
140
146
  *
141
- * @param {Kysely<import('../index.js').Database>} trx
147
+ * @param {Kysely<Database>} trx
142
148
  * @param {string} entity_id delete by id
143
149
  * @param {string} [entity_handle=entity_id] delete by handle
144
150
  */
@@ -261,17 +267,13 @@ export const delete_tags_of = delete_entity_values_of_by_entity_id_or_handle('en
261
267
  export const delete_search_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_search_terms');
262
268
  export const delete_media_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_media');
263
269
 
264
- /**
265
- * @typedef {import('../index.js').Database} Database
266
- */
267
-
268
270
 
269
271
  /**
270
272
  * @template {keyof Database} T
271
273
  *
272
274
  * @param {Kysely<Database>} trx
273
275
  * @param {T} table_name
274
- * @param {import('kysely').InsertObject<Database, T>} item values of the entity
276
+ * @param {InsertObject<Database, T> & {id: string, handle: string}} item values of the entity
275
277
  *
276
278
  */
277
279
  export const regular_upsert_me = async (trx, table_name, item) => {
@@ -280,8 +282,10 @@ export const regular_upsert_me = async (trx, table_name, item) => {
280
282
  await trx.deleteFrom(table_name).where(
281
283
  eb => eb.or(
282
284
  [
283
- item.id && eb('id', '=', item.id),
284
- item.handle && eb('handle', '=', item.handle),
285
+ // @ts-ignore
286
+ item?.id && eb('id', '=', item.id),
287
+ // @ts-ignore
288
+ item?.handle && eb('handle', '=', item.handle),
285
289
  ].filter(Boolean)
286
290
  )
287
291
  ).execute();
@@ -305,9 +309,9 @@ export const delete_me = async (trx, table_name, id_or_handle) => {
305
309
 
306
310
  /**
307
311
  *
308
- * @param {import('kysely').ExpressionBuilder<Database>} eb
312
+ * @param {ExpressionBuilder<Database>} eb
309
313
  * @param {string | ExpressionWrapper<Database>} id_or_handle
310
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
314
+ * @param {SqlDialectType} sql_type
311
315
  */
312
316
  export const with_tags = (eb, id_or_handle, sql_type) => {
313
317
  return stringArrayFrom(
@@ -319,9 +323,9 @@ export const with_tags = (eb, id_or_handle, sql_type) => {
319
323
 
320
324
  /**
321
325
  *
322
- * @param {import('kysely').ExpressionBuilder<Database>} eb
326
+ * @param {ExpressionBuilder<Database>} eb
323
327
  * @param {string | ExpressionWrapper<Database>} id_or_handle
324
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
328
+ * @param {SqlDialectType} sql_type
325
329
  */
326
330
  export const with_search = (eb, id_or_handle, sql_type) => {
327
331
  return stringArrayFrom(
@@ -333,9 +337,9 @@ export const with_search = (eb, id_or_handle, sql_type) => {
333
337
 
334
338
  /**
335
339
  *
336
- * @param {import('kysely').ExpressionBuilder<Database>} eb
340
+ * @param {ExpressionBuilder<Database>} eb
337
341
  * @param {string | ExpressionWrapper<Database>} id_or_handle
338
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
342
+ * @param {SqlDialectType} sql_type
339
343
  */
340
344
  export const with_media = (eb, id_or_handle, sql_type) => {
341
345
  return stringArrayFrom(
@@ -345,31 +349,24 @@ export const with_media = (eb, id_or_handle, sql_type) => {
345
349
  ).as('media');
346
350
  }
347
351
 
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
352
 
363
353
  /**
364
354
  * select as json array collections of a product
365
355
  *
366
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
356
+ * @param {ExpressionBuilder<Database, 'products'>} eb
367
357
  * @param {string | ExpressionWrapper<Database>} product_id_or_handle
368
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
358
+ * @param {SqlDialectType} sql_type
369
359
  */
370
360
  export const products_with_collections = (eb, product_id_or_handle, sql_type) => {
371
361
  return jsonArrayFrom(
372
- select_base_from(eb, 'collections')
362
+ eb.selectFrom('collections')
363
+ .select('collections.active')
364
+ .select('collections.attributes')
365
+ .select('collections.created_at')
366
+ .select('collections.updated_at')
367
+ .select('collections.description')
368
+ .select('collections.handle')
369
+ .select('collections.id')
373
370
  .select('collections.title')
374
371
  .select('collections.published')
375
372
  .select(eb => [
@@ -387,13 +384,20 @@ export const products_with_collections = (eb, product_id_or_handle, sql_type) =>
387
384
  /**
388
385
  * select as json array collections of a product
389
386
  *
390
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
387
+ * @param {ExpressionBuilder<Database, 'products'>} eb
391
388
  * @param {string | ExpressionWrapper<Database>} product_id_or_handle
392
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
389
+ * @param {SqlDialectType} sql_type
393
390
  */
394
391
  export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
395
392
  return jsonArrayFrom(
396
- select_base_from(eb, 'discounts')
393
+ eb.selectFrom('discounts')
394
+ .select('discounts.active')
395
+ .select('discounts.attributes')
396
+ .select('discounts.created_at')
397
+ .select('discounts.updated_at')
398
+ .select('discounts.description')
399
+ .select('discounts.handle')
400
+ .select('discounts.id')
397
401
  .select('discounts.title')
398
402
  .select('discounts.published')
399
403
  .select('discounts.application')
@@ -414,13 +418,20 @@ export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
414
418
  /**
415
419
  * select as json array collections of a product
416
420
  *
417
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
421
+ * @param {ExpressionBuilder<Database, 'products'>} eb
418
422
  * @param {string | ExpressionWrapper<Database>} product_id_or_handle
419
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
423
+ * @param {SqlDialectType} sql_type
420
424
  */
421
425
  export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
422
426
  return jsonArrayFrom(
423
- select_base_from(eb, 'products')
427
+ eb.selectFrom('products')
428
+ .select('products.active')
429
+ .select('products.attributes')
430
+ .select('products.created_at')
431
+ .select('products.updated_at')
432
+ .select('products.description')
433
+ .select('products.handle')
434
+ .select('products.id')
424
435
  .select('products.compare_at_price')
425
436
  .select('products.parent_handle')
426
437
  .select('products.parent_id')
@@ -445,13 +456,20 @@ export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
445
456
  /**
446
457
  * select as json array collections of a product
447
458
  *
448
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
459
+ * @param {ExpressionBuilder<Database, 'products'>} eb
449
460
  * @param {string | ExpressionWrapper<Database>} product_id_or_handle
450
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
461
+ * @param {SqlDialectType} sql_type
451
462
  */
452
463
  export const products_with_related_products = (eb, product_id_or_handle, sql_type) => {
453
464
  return jsonArrayFrom(
454
- select_base_from(eb, 'products')
465
+ eb.selectFrom('products')
466
+ .select('products.active')
467
+ .select('products.attributes')
468
+ .select('products.created_at')
469
+ .select('products.updated_at')
470
+ .select('products.description')
471
+ .select('products.handle')
472
+ .select('products.id')
455
473
  .select('products.compare_at_price')
456
474
  .select('products.parent_handle')
457
475
  .select('products.parent_id')
@@ -477,16 +495,23 @@ export const products_with_related_products = (eb, product_id_or_handle, sql_typ
477
495
  /**
478
496
  * select as json array collections of a product
479
497
  *
480
- * @param {import('kysely').ExpressionBuilder<Database, 'storefronts'>} eb
498
+ * @param {ExpressionBuilder<Database, 'storefronts'>} eb
481
499
  * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
482
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
500
+ * @param {SqlDialectType} sql_type
483
501
  */
484
502
  export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
485
503
  return jsonArrayFrom(
486
- select_base_from(eb, 'collections')
487
- .select('collections.title')
488
- .select('collections.published')
489
- .select(eb => [
504
+ eb.selectFrom('collections')
505
+ .select('collections.active')
506
+ .select('collections.attributes')
507
+ .select('collections.created_at')
508
+ .select('collections.updated_at')
509
+ .select('collections.description')
510
+ .select('collections.handle')
511
+ .select('collections.id')
512
+ .select('collections.title')
513
+ .select('collections.published')
514
+ .select(eb => [
490
515
  with_tags(eb, eb.ref('collections.id'), sql_type),
491
516
  with_media(eb, eb.ref('collections.id'), sql_type),
492
517
  ])
@@ -501,13 +526,20 @@ export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
501
526
  /**
502
527
  * select as json array collections of a product
503
528
  *
504
- * @param {import('kysely').ExpressionBuilder<Database>} eb
529
+ * @param {ExpressionBuilder<Database>} eb
505
530
  * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
506
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
531
+ * @param {SqlDialectType} sql_type
507
532
  */
508
533
  export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
509
534
  return jsonArrayFrom(
510
- select_base_from(eb, 'products')
535
+ eb.selectFrom('products')
536
+ .select('products.active')
537
+ .select('products.attributes')
538
+ .select('products.created_at')
539
+ .select('products.updated_at')
540
+ .select('products.description')
541
+ .select('products.handle')
542
+ .select('products.id')
511
543
  .select('products.title')
512
544
  .select('products.compare_at_price')
513
545
  .select('products.parent_handle')
@@ -532,13 +564,20 @@ export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
532
564
  /**
533
565
  * select as json array collections of a product
534
566
  *
535
- * @param {import('kysely').ExpressionBuilder<Database>} eb
567
+ * @param {ExpressionBuilder<Database>} eb
536
568
  * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
537
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
569
+ * @param {SqlDialectType} sql_type
538
570
  */
539
571
  export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
540
572
  return jsonArrayFrom(
541
- select_base_from(eb, 'discounts')
573
+ eb.selectFrom('discounts')
574
+ .select('discounts.active')
575
+ .select('discounts.attributes')
576
+ .select('discounts.created_at')
577
+ .select('discounts.updated_at')
578
+ .select('discounts.description')
579
+ .select('discounts.handle')
580
+ .select('discounts.id')
542
581
  .select('discounts.application')
543
582
  .select('discounts.info')
544
583
  .select('discounts.priority')
@@ -559,13 +598,20 @@ export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
559
598
  /**
560
599
  * select as json array collections of a product
561
600
  *
562
- * @param {import('kysely').ExpressionBuilder<Database>} eb
601
+ * @param {ExpressionBuilder<Database>} eb
563
602
  * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
564
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
603
+ * @param {SqlDialectType} sql_type
565
604
  */
566
605
  export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
567
606
  return jsonArrayFrom(
568
- select_base_from(eb, 'posts')
607
+ eb.selectFrom('posts')
608
+ .select('posts.active')
609
+ .select('posts.attributes')
610
+ .select('posts.created_at')
611
+ .select('posts.updated_at')
612
+ .select('posts.description')
613
+ .select('posts.handle')
614
+ .select('posts.id')
569
615
  .select('posts.text')
570
616
  .select('posts.title')
571
617
  .select(eb => [
@@ -583,13 +629,20 @@ export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
583
629
  /**
584
630
  * select as json array collections of a product
585
631
  *
586
- * @param {import('kysely').ExpressionBuilder<Database>} eb
632
+ * @param {ExpressionBuilder<Database>} eb
587
633
  * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
588
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
634
+ * @param {SqlDialectType} sql_type
589
635
  */
590
636
  export const storefront_with_shipping = (eb, sf_id_or_handle, sql_type) => {
591
637
  return jsonArrayFrom(
592
- select_base_from(eb, 'shipping_methods')
638
+ eb.selectFrom('shipping_methods')
639
+ .select('shipping_methods.active')
640
+ .select('shipping_methods.attributes')
641
+ .select('shipping_methods.created_at')
642
+ .select('shipping_methods.updated_at')
643
+ .select('shipping_methods.description')
644
+ .select('shipping_methods.handle')
645
+ .select('shipping_methods.id')
593
646
  .select('shipping_methods.price')
594
647
  .select('shipping_methods.title')
595
648
  .select(eb => [
@@ -607,7 +660,7 @@ export const storefront_with_shipping = (eb, sf_id_or_handle, sql_type) => {
607
660
  /**
608
661
  * select all the entity values by entity id or handle
609
662
  *
610
- * @param {import('kysely').ExpressionBuilder<Database>} eb
663
+ * @param {ExpressionBuilder<Database>} eb
611
664
  * @param {EntityTableKeys} entity_junction_table
612
665
  * @param {string | ExpressionWrapper<Database>} entity_id_or_handle
613
666
  */
@@ -629,7 +682,7 @@ export const select_values_of_entity_by_entity_id_or_handle =
629
682
  /**
630
683
  * select the entity ids which are constrained by value or reporter
631
684
  *
632
- * @param {import('kysely').ExpressionBuilder<Database>} eb
685
+ * @param {ExpressionBuilder<Database>} eb
633
686
  * @param {EntityTableKeys} entity_junction_table
634
687
  * @param {string | ExpressionWrapper<Database>} value
635
688
  * @param {string | ExpressionWrapper<Database>} [reporter]
@@ -1,16 +1,16 @@
1
+ /**
2
+ * @import { db_shipping as db_col } from '@storecraft/core/database'
3
+ */
1
4
  import { SQL } from '../index.js'
2
5
  import { report_document_media } from './con.images.js'
3
6
  import { count_regular, delete_entity_values_by_value_or_reporter,
4
7
  delete_me, delete_media_of, delete_search_of,
5
8
  delete_tags_of, insert_media_of, insert_search_of,
6
9
  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'
10
+ with_media, with_search, with_tags } from './con.shared.js'
11
+ import { sanitize, sanitize_array } from './utils.funcs.js'
9
12
  import { query_to_eb, query_to_sort } from './utils.query.js'
10
13
 
11
- /**
12
- * @typedef {import('@storecraft/core/database').db_shipping} db_col
13
- */
14
14
  export const table_name = 'shipping_methods'
15
15
 
16
16
  /**
@@ -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
+ ].filter(Boolean)
66
+ )
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
  }
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @import { db_storefronts as db_col } from '@storecraft/core/database'
3
+ */
1
4
  import { SQL } from '../index.js'
2
5
  import { report_document_media } from './con.images.js'
3
6
  import { delete_entity_values_of_by_entity_id_or_handle,
@@ -8,13 +11,11 @@ import { delete_entity_values_of_by_entity_id_or_handle,
8
11
  storefront_with_products, storefront_with_shipping,
9
12
  regular_upsert_me, where_id_or_handle_table,
10
13
  with_media, with_tags,
11
- count_regular} from './con.shared.js'
12
- import { sanitize_array } from './utils.funcs.js'
14
+ count_regular,
15
+ with_search} from './con.shared.js'
16
+ import { sanitize, sanitize_array } from './utils.funcs.js'
13
17
  import { query_to_eb, query_to_sort } from './utils.query.js'
14
18
 
15
- /**
16
- * @typedef {import('@storecraft/core/database').db_storefronts} db_col
17
- */
18
19
  export const table_name = 'storefronts'
19
20
 
20
21
  /**
@@ -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,12 +1,14 @@
1
+ /**
2
+ * @import { db_tags 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, insert_search_of,
3
- regular_upsert_me, where_id_or_handle_table } from './con.shared.js'
4
- import { sanitize_array } from './utils.funcs.js'
6
+ regular_upsert_me, where_id_or_handle_table,
7
+ with_media,
8
+ with_search} from './con.shared.js'
9
+ import { sanitize, sanitize_array } from './utils.funcs.js'
5
10
  import { query_to_eb, query_to_sort } from './utils.query.js'
6
11
 
7
- /**
8
- * @typedef {import('@storecraft/core/database').db_tags} db_col
9
- */
10
12
  export const table_name = 'tags'
11
13
 
12
14
  /**
@@ -47,8 +49,13 @@ const get = (driver) => {
47
49
  return driver.client
48
50
  .selectFrom(table_name)
49
51
  .selectAll()
52
+ .select(eb => [
53
+ with_search(eb, id_or_handle, driver.dialectType),
54
+ ].filter(Boolean)
55
+ )
50
56
  .where(where_id_or_handle_table(id_or_handle))
51
- .executeTakeFirst();
57
+ .executeTakeFirst()
58
+ .then(sanitize);
52
59
  }
53
60
  }
54
61
 
@@ -89,12 +96,15 @@ const list = (driver) => {
89
96
  const items = await driver.client
90
97
  .selectFrom(table_name)
91
98
  .selectAll()
99
+ .select(eb => [
100
+ with_search(eb, eb.ref('tags.id'), driver.dialectType),
101
+ ].filter(Boolean))
92
102
  .where(
93
103
  (eb) => {
94
104
  return query_to_eb(eb, query, table_name);
95
105
  }
96
106
  )
97
- .orderBy(query_to_sort(query))
107
+ .orderBy(query_to_sort(query, table_name))
98
108
  .limit(query.limitToLast ?? query.limit ?? 10)
99
109
  .execute();
100
110
 
@@ -1,14 +1,16 @@
1
+ /**
2
+ * @import { db_templates as db_col } from '@storecraft/core/database'
3
+ * @import { Database } from '../types.sql.tables.js'
4
+ */
1
5
  import { Kysely } from 'kysely'
2
6
  import { SQL } from '../index.js'
3
7
  import { count_regular, delete_me, delete_search_of, insert_search_of,
4
- regular_upsert_me, safe_trx, where_id_or_handle_table } from './con.shared.js'
5
- import { sanitize_array } from './utils.funcs.js'
8
+ regular_upsert_me, safe_trx, where_id_or_handle_table,
9
+ with_search} from './con.shared.js'
10
+ import { sanitize, sanitize_array } from './utils.funcs.js'
6
11
  import { query_to_eb, query_to_sort } from './utils.query.js'
7
12
  import { base64 } from '@storecraft/core/crypto';
8
13
 
9
- /**
10
- * @typedef {import('@storecraft/core/database').db_templates} db_col
11
- */
12
14
  export const table_name = 'templates'
13
15
 
14
16
  /**
@@ -25,8 +27,7 @@ const decode_if_base64 = val => {
25
27
  }
26
28
 
27
29
  /**
28
- * @param {Kysely<import('../index.js').Database>} client
29
- *
30
+ * @param {Kysely<Database>} client
30
31
  *
31
32
  * @returns {db_col["upsert"]}
32
33
  */
@@ -70,8 +71,13 @@ const get = (driver) => {
70
71
  return driver.client
71
72
  .selectFrom(table_name)
72
73
  .selectAll()
74
+ .select(eb => [
75
+ with_search(eb, id_or_handle, driver.dialectType),
76
+ ].filter(Boolean)
77
+ )
73
78
  .where(where_id_or_handle_table(id_or_handle))
74
- .executeTakeFirst();
79
+ .executeTakeFirst()
80
+ .then(sanitize);
75
81
  }
76
82
  }
77
83
 
@@ -114,12 +120,17 @@ const list = (driver) => {
114
120
  const items = await driver.client
115
121
  .selectFrom(table_name)
116
122
  .selectAll()
123
+ .select(
124
+ eb => [
125
+ with_search(eb, eb.ref('templates.id'), driver.dialectType),
126
+ ].filter(Boolean)
127
+ )
117
128
  .where(
118
129
  (eb) => {
119
130
  return query_to_eb(eb, query, table_name);
120
131
  }
121
132
  )
122
- .orderBy(query_to_sort(query))
133
+ .orderBy(query_to_sort(query, table_name))
123
134
  .limit(query.limitToLast ?? query.limit ?? 10)
124
135
  .execute();
125
136
 
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @import {
3
+ * KyselyPlugin, PluginTransformQueryArgs, RootOperationNode,
4
+ * PluginTransformResultArgs, QueryResult, UnknownRow
5
+ * } from 'kysely'
6
+ */
1
7
  import { sanitize } from './utils.funcs.js';
2
8
 
3
9
  /**
@@ -6,20 +12,16 @@ import { sanitize } from './utils.funcs.js';
6
12
  * - sanitize `undefined` and `null` values
7
13
  * - `active` keys will be transformed to `boolean`
8
14
  *
9
- *
10
- * @typedef {import('kysely').KyselyPlugin} KyselyPlugin
11
- *
12
- *
13
15
  * @implements {KyselyPlugin}
14
16
  */
15
17
  export class SanitizePlugin {
16
18
 
17
19
  /**
18
20
  *
19
- * @param {import('kysely').PluginTransformQueryArgs} args
21
+ * @param {PluginTransformQueryArgs} args
20
22
  *
21
23
  *
22
- * @returns {import('kysely').RootOperationNode}
24
+ * @returns {RootOperationNode}
23
25
  */
24
26
  transformQuery(args) {
25
27
  return args.node;
@@ -27,10 +29,10 @@ export class SanitizePlugin {
27
29
 
28
30
  /**
29
31
  *
30
- * @param {import('kysely').PluginTransformResultArgs} args
32
+ * @param {PluginTransformResultArgs} args
31
33
  *
32
34
  *
33
- * @returns {Promise<import('kysely').QueryResult<import('kysely').UnknownRow>>}
35
+ * @returns {Promise<QueryResult<UnknownRow>>}
34
36
  */
35
37
  transformResult(args){
36
38
  sanitize(args.result.rows);