@storecraft/database-sql-base 1.0.22 → 1.0.24

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.
@@ -2,11 +2,13 @@
2
2
  * @import { db_notifications as db_col } from '@storecraft/core/database'
3
3
  */
4
4
  import { SQL } from '../index.js'
5
- import { count_regular, delete_me, delete_search_of,
5
+ import {
6
+ count_regular, delete_me, delete_search_of,
6
7
  insert_search_of, regular_upsert_me, where_id_or_handle_table,
7
- with_search } from './con.shared.js'
8
+ with_search
9
+ } from './con.shared.js'
8
10
  import { sanitize_array } from './utils.funcs.js'
9
- import { query_to_eb, query_to_sort } from './utils.query.js'
11
+ import { withQuery } from './utils.query.js'
10
12
 
11
13
  export const table_name = 'notifications'
12
14
 
@@ -115,22 +117,29 @@ const remove = (driver) => {
115
117
  const list = (driver) => {
116
118
  return async (query) => {
117
119
 
118
- const items = await driver.client
120
+ const items = await withQuery(
121
+ driver.client
119
122
  .selectFrom(table_name)
120
123
  .selectAll()
121
- .select(eb => [
122
- with_search(eb, eb.ref('notifications.id'), driver.dialectType),
123
- ].filter(Boolean))
124
- .where(
125
- (eb) => {
126
- return query_to_eb(eb, query, table_name);
127
- }
128
- )
129
- .orderBy(query_to_sort(query, 'notifications'))
130
- .limit(query.limitToLast ?? query.limit ?? 10)
131
- .execute();
132
-
133
- if(query.limitToLast) items.reverse();
124
+ .select(
125
+ eb => [
126
+ with_search(eb, eb.ref('notifications.id'), driver.dialectType),
127
+ ].filter(Boolean)
128
+ ),
129
+ query, table_name
130
+ ).execute();
131
+
132
+ // .where(
133
+ // (eb) => {
134
+ // return query_to_eb(eb, query, table_name);
135
+ // }
136
+ // )
137
+ // .orderBy(query_to_sort(query, 'notifications'))
138
+ // .limit(query.limitToLast ?? query.limit ?? 10)
139
+ // .execute();
140
+
141
+ if(query.limitToLast)
142
+ items.reverse();
134
143
 
135
144
  return sanitize_array(items);
136
145
  }
package/src/con.orders.js CHANGED
@@ -9,7 +9,7 @@ import { count_regular, delete_me, delete_media_of, delete_search_of,
9
9
  insert_tags_of, regular_upsert_me, where_id_or_handle_table,
10
10
  with_media, with_search, with_tags} from './con.shared.js'
11
11
  import { sanitize, sanitize_array } from './utils.funcs.js'
12
- import { query_to_eb, query_to_sort } from './utils.query.js'
12
+ import { withQuery } from './utils.query.js'
13
13
 
14
14
  export const table_name = 'orders'
15
15
 
@@ -129,31 +129,38 @@ const remove = (driver) => {
129
129
  const list = (driver) => {
130
130
  return async (query) => {
131
131
 
132
- const items = await driver.client
132
+ const items = await withQuery(
133
+ driver.client
133
134
  .selectFrom(table_name)
134
135
  // .selectAll()
135
136
  .select(
136
137
  [
137
138
  'active', 'address', 'attributes', 'contact', 'coupons', 'created_at',
138
139
  'updated_at', 'description', 'handle', 'id', 'line_items', 'notes',
139
- 'payment_gateway', 'pricing', 'shipping_method', 'status', 'validation'
140
+ 'payment_gateway', 'pricing', 'shipping_method', 'status', 'validation',
140
141
  ]
141
142
  )
142
- .select(eb => [
143
- with_media(eb, eb.ref('orders.id'), driver.dialectType),
144
- with_tags(eb, eb.ref('orders.id'), driver.dialectType),
145
- with_search(eb, eb.ref('orders.id'), driver.dialectType),
146
- ].filter(Boolean))
147
- .where(
148
- (eb) => {
149
- return query_to_eb(eb, query, table_name);
150
- }
151
- )
152
- .orderBy(query_to_sort(query, table_name))
153
- .limit(query.limitToLast ?? query.limit ?? 10)
154
- .execute();
155
-
156
- if(query.limitToLast) items.reverse();
143
+ .select(
144
+ eb => [
145
+ with_media(eb, eb.ref('orders.id'), driver.dialectType),
146
+ with_tags(eb, eb.ref('orders.id'), driver.dialectType),
147
+ with_search(eb, eb.ref('orders.id'), driver.dialectType),
148
+ ].filter(Boolean)
149
+ ),
150
+ query, table_name
151
+ ).execute();
152
+
153
+ // .where(
154
+ // (eb) => {
155
+ // return query_to_eb(eb, query, table_name);
156
+ // }
157
+ // )
158
+ // .orderBy(query_to_sort(query, table_name))
159
+ // .limit(query.limitToLast ?? query.limit ?? 10)
160
+ // .execute();
161
+
162
+ if(query.limitToLast)
163
+ items.reverse();
157
164
 
158
165
  return sanitize_array(items);
159
166
  }
package/src/con.posts.js CHANGED
@@ -1,17 +1,17 @@
1
1
  /**
2
2
  * @import { db_posts as db_col } from '@storecraft/core/database'
3
3
  */
4
-
5
4
  import { SQL } from '../index.js'
6
- import { stringArrayFrom } from './con.helpers.json.js'
7
5
  import { report_document_media } from './con.images.js'
8
- import { count_regular, delete_entity_values_by_value_or_reporter_and_context,
6
+ import {
7
+ count_regular, delete_entity_values_by_value_or_reporter_and_context,
9
8
  delete_me, delete_media_of, delete_search_of, delete_tags_of,
10
9
  insert_media_of, insert_search_of, insert_tags_of,
11
10
  regular_upsert_me, where_id_or_handle_table,
12
- with_media, with_search, with_tags} from './con.shared.js'
11
+ with_media, with_search, with_tags
12
+ } from './con.shared.js'
13
13
  import { sanitize, sanitize_array } from './utils.funcs.js'
14
- import { query_to_eb, query_to_sort } from './utils.query.js'
14
+ import { withQuery, withSort } from './utils.query.js'
15
15
 
16
16
  export const table_name = 'posts'
17
17
 
@@ -60,12 +60,13 @@ const get = (driver) => {
60
60
  const result = await driver.client
61
61
  .selectFrom(table_name)
62
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))
63
+ .select(
64
+ eb => [
65
+ with_media(eb, id_or_handle, driver.dialectType),
66
+ with_tags(eb, id_or_handle, driver.dialectType),
67
+ with_search(eb, id_or_handle, driver.dialectType),
68
+ ].filter(Boolean)
69
+ )
69
70
  .where(where_id_or_handle_table(id_or_handle))
70
71
  .executeTakeFirst();
71
72
 
@@ -113,25 +114,24 @@ const remove = (driver) => {
113
114
  const list = (driver) => {
114
115
  return async (query) => {
115
116
 
116
- const items = await driver.client
117
+ const items = await withQuery(
118
+ driver.client
117
119
  .selectFrom(table_name)
118
120
  .selectAll()
119
- .select(eb => [
120
- with_media(eb, eb.ref('posts.id'), driver.dialectType),
121
- with_tags(eb, eb.ref('posts.id'), driver.dialectType),
122
- with_search(eb, eb.ref('posts.id'), driver.dialectType),
123
- ].filter(Boolean))
124
- .where(
125
- (eb) => {
126
- return query_to_eb(eb, query, table_name);
127
- }
128
- )
129
- .orderBy(query_to_sort(query, 'posts'))
130
- // .orderBy()
131
- .limit(query.limitToLast ?? query.limit ?? 10)
132
- .execute();
133
-
134
- if(query.limitToLast) items.reverse();
121
+ .select(
122
+ eb => [
123
+ with_media(eb, eb.ref('posts.id'), driver.dialectType),
124
+ with_tags(eb, eb.ref('posts.id'), driver.dialectType),
125
+ with_search(eb, eb.ref('posts.id'), driver.dialectType),
126
+ ].filter(Boolean)
127
+ ),
128
+ query, table_name
129
+ )
130
+ .execute();
131
+ // console.log({items})
132
+
133
+ if(query.limitToLast)
134
+ items.reverse();
135
135
 
136
136
  return sanitize_array(items);
137
137
  }
@@ -21,7 +21,7 @@ import {
21
21
  products_with_related_products,
22
22
  with_search} from './con.shared.js'
23
23
  import { sanitize, sanitize_array } from './utils.funcs.js'
24
- import { query_to_eb, query_to_sort } from './utils.query.js'
24
+ import { withQuery } from './utils.query.js'
25
25
  import { Transaction } from 'kysely'
26
26
  import { report_document_media } from './con.images.js'
27
27
  import { union } from '@storecraft/core/api/utils.func.js'
@@ -38,8 +38,15 @@ export const table_name = 'products'
38
38
  * @param {db_col["$type_upsert"]} item
39
39
  */
40
40
  const is_variant = item => {
41
- if(item && ('variant_hint' in item) && ('parent_handle' in item)&& ('parent_id' in item)) {
42
- return item.parent_handle && item.parent_id && item.variant_hint;
41
+ if(
42
+ item &&
43
+ ('variant_hint' in item) &&
44
+ ('parent_handle' in item) &&
45
+ ('parent_id' in item)
46
+ ) {
47
+ return item.parent_handle &&
48
+ item.parent_id &&
49
+ item.variant_hint;
43
50
  }
44
51
 
45
52
  return false;
@@ -75,6 +82,8 @@ const upsert = (driver) => {
75
82
  d.info.filters, item
76
83
  )
77
84
  );
85
+
86
+
78
87
 
79
88
  item.tags = union(
80
89
  [
@@ -381,112 +390,56 @@ const list = (driver) => {
381
390
  const expand_variants = expand.includes('*') || expand.includes('variants');
382
391
  const expand_related_products = expand.includes('*') || expand.includes('related_products');
383
392
 
384
- const items = await driver.client
385
- .selectFrom(table_name)
386
- .selectAll()
387
- .select(
388
- eb => [
389
- with_tags(eb, eb.ref('products.id'), driver.dialectType),
390
- with_media(eb, eb.ref('products.id'), driver.dialectType),
391
- with_search(eb, eb.ref('products.id'), driver.dialectType),
392
-
393
- expand_collections &&
394
- products_with_collections(eb, eb.ref('products.id'), driver.dialectType),
395
-
396
- expand_discounts &&
397
- products_with_discounts(eb, eb.ref('products.id'), driver.dialectType),
393
+ const items = await withQuery(
394
+ driver.client
395
+ .selectFrom(table_name)
396
+ .selectAll()
397
+ .select(
398
+ eb => [
399
+ with_tags(eb, eb.ref('products.id'), driver.dialectType),
400
+ with_media(eb, eb.ref('products.id'), driver.dialectType),
401
+ with_search(eb, eb.ref('products.id'), driver.dialectType),
402
+
403
+ expand_collections &&
404
+ products_with_collections(
405
+ eb, eb.ref('products.id'), driver.dialectType
406
+ ),
398
407
 
399
- expand_variants &&
400
- products_with_variants(eb, eb.ref('products.id'), driver.dialectType),
408
+ expand_discounts &&
409
+ products_with_discounts(
410
+ eb, eb.ref('products.id'), driver.dialectType
411
+ ),
401
412
 
402
- expand_related_products &&
403
- products_with_related_products(eb, eb.ref('products.id'), driver.dialectType),
404
- ].filter(Boolean)
405
- )
406
- .where(
407
- (eb) => {
408
- return query_to_eb(eb, query, table_name);
409
- }
410
- )
411
- .orderBy(query_to_sort(query, table_name))
412
- .limit(query.limitToLast ?? query.limit ?? 10)
413
- .execute();
413
+ expand_variants &&
414
+ products_with_variants(
415
+ eb, eb.ref('products.id'), driver.dialectType
416
+ ),
414
417
 
415
- if(query.limitToLast) items.reverse();
416
- // .compile();
417
- // console.log(items)
418
+ expand_related_products &&
419
+ products_with_related_products(
420
+ eb, eb.ref('products.id'), driver.dialectType
421
+ ),
422
+ ].filter(Boolean)
423
+ ),
424
+ query, table_name
425
+ ).execute();
426
+
427
+ // .where(
428
+ // (eb) => {
429
+ // return query_to_eb(eb, query, table_name);
430
+ // }
431
+ // )
432
+ // .orderBy(query_to_sort(query, table_name))
433
+ // .limit(query.limitToLast ?? query.limit ?? 10)
434
+ // .execute();
435
+
436
+ if(query.limitToLast)
437
+ items.reverse();
418
438
 
419
439
  return sanitize_array(items);
420
440
  }
421
441
  }
422
442
 
423
- /**
424
- * @param {SQL} driver
425
- * @returns {db_col["list_all_product_collections"]}
426
- */
427
- const list_product_collections = (driver) => {
428
- return async (product_id_or_handle) => {
429
- // we don't expect many collections per products,
430
- // therefore we use the simple `get` method instead of a query
431
- const item = await get(driver)(
432
- product_id_or_handle, { expand: ['collections'] }
433
- );
434
- return item?.collections ?? []
435
- }
436
- }
437
-
438
- /**
439
- * @param {SQL} driver
440
- * @returns {db_col["list_all_product_discounts"]}
441
- */
442
- const list_product_discounts = (driver) => {
443
- return async (product_id_or_handle) => {
444
- // we don't expect many discounts per products,
445
- // therefore we use the simple `get` method instead of a query
446
- const item = await get(driver)(
447
- product_id_or_handle, { expand: ['discounts'] }
448
- );
449
- return item?.discounts ?? []
450
- }
451
- }
452
-
453
- /**
454
- * @param {SQL} driver
455
- *
456
- * @returns {db_col["list_all_product_variants"]}
457
- */
458
- const list_product_variants = (driver) => {
459
- return async (product_id_or_handle) => {
460
- // we don't expect many discounts per products,
461
- // therefore we use the simple `get` method instead of a query
462
- const item = await get(driver)(
463
- product_id_or_handle, { expand: ['variants'] }
464
- );
465
-
466
- if(item && (`variants` in item))
467
- return item.variants ?? [];
468
-
469
- return [];
470
- }
471
- }
472
-
473
- /**
474
- * @param {SQL} driver
475
- *
476
- * @returns {db_col["list_all_related_products"]}
477
- */
478
- const list_related_products = (driver) => {
479
- return async (product_id_or_handle) => {
480
- // we don't expect many discounts per products,
481
- // therefore we use the simple `get` method instead of a query
482
- const item = await get(driver)(
483
- product_id_or_handle, { expand: ['related_products'] }
484
- );
485
-
486
- return item?.related_products ?? [];
487
- }
488
- }
489
-
490
443
  /**
491
444
  * @param {SQL} driver
492
445
  * @returns {db_col["list_used_products_tags"]}
@@ -520,33 +473,36 @@ const list_used_products_tags = (driver) => {
520
473
  */
521
474
  const changeStockOfBy = (driver) => {
522
475
  return async (product_ids_or_handles, deltas) => {
523
-
524
- await driver.client.transaction().execute(
525
- async (trx) => {
526
- for(let ix=0; ix < product_ids_or_handles.length; ix++ ) {
527
- const id = product_ids_or_handles[ix];
528
- const delta = deltas[ix];
529
-
530
- await trx
531
- .updateTable('products')
532
- .set(
533
- eb => (
534
- {
535
- qty: eb('qty', '+', delta)
536
- }
476
+ try {
477
+ await driver.client.transaction().execute(
478
+ async (trx) => {
479
+ for(let ix=0; ix < product_ids_or_handles.length; ix++ ) {
480
+ const id = product_ids_or_handles[ix];
481
+ const delta = deltas[ix];
482
+
483
+ await trx
484
+ .updateTable('products')
485
+ .set(
486
+ eb => (
487
+ {
488
+ qty: eb('qty', '+', delta)
489
+ }
490
+ )
537
491
  )
538
- )
539
- .where(
540
- where_id_or_handle_table(id)
541
- )
542
- .execute()
543
-
492
+ .where(
493
+ where_id_or_handle_table(id)
494
+ )
495
+ .execute()
496
+ }
544
497
  }
545
- }
546
- );
498
+ );
499
+ } catch(e) {
500
+ console.log(e);
501
+ return false;
502
+ }
547
503
 
504
+ return true;
548
505
  }
549
-
550
506
  }
551
507
 
552
508
 
@@ -564,10 +520,6 @@ export const impl = (driver) => {
564
520
  upsert: upsert(driver),
565
521
  remove: remove(driver),
566
522
  list: list(driver),
567
- list_all_product_collections: list_product_collections(driver),
568
- list_all_product_discounts: list_product_discounts(driver),
569
- list_all_product_variants: list_product_variants(driver),
570
- list_all_related_products: list_related_products(driver),
571
523
  list_used_products_tags: list_used_products_tags(driver),
572
524
  count: count_regular(driver, table_name),
573
525
  }
package/src/con.search.js CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { SQL } from '../index.js'
7
7
  import { jsonArrayFrom } from './con.helpers.json.js';
8
- import { query_to_eb, query_to_sort } from './utils.query.js';
8
+ import { withQuery } from './utils.query.js';
9
9
 
10
10
  /**
11
11
  * @type {(keyof db_driver["resources"])[]}
@@ -85,6 +85,7 @@ export const quicksearch = (driver) => {
85
85
  const db = driver.client;
86
86
  const expand = query.expand ?? ['*'];
87
87
  const all = expand.includes('*');
88
+ query.limit ??= 5;
88
89
 
89
90
  const sts = db
90
91
  .selectNoFrom(
@@ -99,16 +100,12 @@ export const quicksearch = (driver) => {
99
100
  // console.log(table_name, props)
100
101
  props
101
102
  return jsonArrayFrom(
102
- eb
103
- .selectFrom(table_name)
104
- .select(props)
105
- .where(
106
- (eb) => {
107
- return query_to_eb(eb, query, table_name);
108
- }
109
- )
110
- .orderBy(query_to_sort(query, table_name))
111
- .limit(query.limit ?? 5),
103
+ withQuery(
104
+ eb
105
+ .selectFrom(table_name)
106
+ .select(props),
107
+ query, table_name
108
+ ),
112
109
  driver.dialectType
113
110
  ).as(table_name)
114
111
  }
@@ -120,12 +117,15 @@ export const quicksearch = (driver) => {
120
117
  await sts.executeTakeFirst())
121
118
  );
122
119
 
120
+
123
121
  const sanitized = Object.fromEntries(
124
122
  Object.entries(items).filter(
125
123
  ([key, value]) => Boolean(value?.length)
126
124
  )
127
125
  );
128
-
126
+
127
+ // console.log('items', JSON.stringify(items, null, 2))
128
+
129
129
  // console.log('sanitized', JSON.stringify(sanitized, null, 2))
130
130
 
131
131
  return sanitized;
package/src/con.shared.js CHANGED
@@ -46,9 +46,9 @@ export const safe_trx = (k) => {
46
46
 
47
47
 
48
48
  /**
49
+ * @template {keyof Database} [T=(keyof Database)]
49
50
  * @param {SQL} driver
50
- * @param {QueryableTables} table_name
51
- *
51
+ * @param {T} table_name
52
52
  * @returns {db_crud["count"]}
53
53
  */
54
54
  export const count_regular = (driver, table_name) => {
@@ -71,7 +71,6 @@ export const count_regular = (driver, table_name) => {
71
71
  }
72
72
 
73
73
  /**
74
- *
75
74
  * @param {string} id_or_handle
76
75
  */
77
76
  export const where_id_or_handle_entity = (id_or_handle) => {
@@ -87,7 +86,6 @@ export const where_id_or_handle_entity = (id_or_handle) => {
87
86
  }
88
87
 
89
88
  /**
90
- *
91
89
  * @param {string} id_or_handle
92
90
  */
93
91
  export const where_id_or_handle_table = (id_or_handle) => {
@@ -148,7 +146,9 @@ export const delete_entity_values_by_value_or_reporter_and_context = (
148
146
  */
149
147
  return (trx, value, reporter, context) => {
150
148
 
151
- return trx.deleteFrom(entity_table_name).where(
149
+ return trx
150
+ .deleteFrom(entity_table_name)
151
+ .where(
152
152
  eb => eb.or(
153
153
  [
154
154
  value && eb('value', '=', value),
@@ -196,7 +196,9 @@ export const delete_entity_values_of_by_entity_id_or_handle_and_context = (
196
196
  * @param {string} [context=undefined] the context (another segment technique)
197
197
  */
198
198
  return (trx, entity_id, entity_handle=undefined, context=undefined) => {
199
- return trx.deleteFrom(entity_table_name).where(
199
+ return trx
200
+ .deleteFrom(entity_table_name)
201
+ .where(
200
202
  eb => eb.or(
201
203
  [
202
204
  entity_id && eb('entity_id', '=', entity_id),
@@ -316,18 +318,18 @@ export const delete_media_of = delete_entity_values_of_by_entity_id_or_handle_an
316
318
 
317
319
 
318
320
  /**
319
- * @template {keyof Database} T
320
- *
321
+ * @template {keyof Database} [T=(keyof Database)]
321
322
  * @param {Kysely<Database>} trx
322
323
  * @param {T} table_name
323
324
  * @param {InsertObject<Database, T> & {id: string, handle: string}} item values of the entity
324
- *
325
325
  */
326
326
  export const regular_upsert_me = async (trx, table_name, item) => {
327
327
 
328
328
  // TODO: maybe use only `id`
329
- await trx.deleteFrom(table_name).where(
330
- eb => eb.or(
329
+ await trx
330
+ .deleteFrom(table_name)
331
+ .where(
332
+ (eb) => eb.or(
331
333
  [
332
334
  // @ts-ignore
333
335
  item?.id && eb('id', '=', item.id),
@@ -337,7 +339,10 @@ export const regular_upsert_me = async (trx, table_name, item) => {
337
339
  )
338
340
  ).execute();
339
341
 
340
- return await trx.insertInto(table_name).values(item).executeTakeFirst()
342
+ return await trx
343
+ .insertInto(table_name)
344
+ .values(item)
345
+ .executeTakeFirst()
341
346
  }
342
347
 
343
348
 
@@ -725,26 +730,3 @@ export const select_values_of_entity_by_entity_id_or_handle =
725
730
  )
726
731
  .orderBy(`${entity_junction_table}.id`);
727
732
  }
728
-
729
- // /**
730
- // * select the entity ids which are constrained by value or reporter
731
- // *
732
- // * @param {ExpressionBuilder<Database>} eb
733
- // * @param {EntityTableKeys} entity_junction_table
734
- // * @param {string | ExpressionWrapper<Database>} value
735
- // * @param {string | ExpressionWrapper<Database>} [reporter]
736
- // */
737
- // export const select_entity_ids_by_value_or_reporter =
738
- // (eb, entity_junction_table, value, reporter=undefined) => {
739
- // return eb
740
- // .selectFrom(entity_junction_table)
741
- // .select(`${entity_junction_table}.entity_id`)
742
- // .where(eb2 => eb2.or(
743
- // [
744
- // eb2(`${entity_junction_table}.value`, '=', value ?? reporter),
745
- // eb2(`${entity_junction_table}.reporter`, '=', reporter ?? value),
746
- // ]
747
- // )
748
- // )
749
- // .orderBy(`${entity_junction_table}.entity_id`);
750
- // }