@storecraft/database-sql-base 1.0.23 → 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.
@@ -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;
@@ -383,40 +390,51 @@ const list = (driver) => {
383
390
  const expand_variants = expand.includes('*') || expand.includes('variants');
384
391
  const expand_related_products = expand.includes('*') || expand.includes('related_products');
385
392
 
386
- const items = await driver.client
387
- .selectFrom(table_name)
388
- .selectAll()
389
- .select(
390
- eb => [
391
- with_tags(eb, eb.ref('products.id'), driver.dialectType),
392
- with_media(eb, eb.ref('products.id'), driver.dialectType),
393
- with_search(eb, eb.ref('products.id'), driver.dialectType),
394
-
395
- expand_collections &&
396
- products_with_collections(eb, eb.ref('products.id'), driver.dialectType),
397
-
398
- expand_discounts &&
399
- 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
+ ),
400
407
 
401
- expand_variants &&
402
- 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
+ ),
403
412
 
404
- expand_related_products &&
405
- products_with_related_products(eb, eb.ref('products.id'), driver.dialectType),
406
- ].filter(Boolean)
407
- )
408
- .where(
409
- (eb) => {
410
- return query_to_eb(eb, query, table_name);
411
- }
412
- )
413
- .orderBy(query_to_sort(query, table_name))
414
- .limit(query.limitToLast ?? query.limit ?? 10)
415
- .execute();
413
+ expand_variants &&
414
+ products_with_variants(
415
+ eb, eb.ref('products.id'), driver.dialectType
416
+ ),
416
417
 
417
- if(query.limitToLast) items.reverse();
418
- // .compile();
419
- // 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();
420
438
 
421
439
  return sanitize_array(items);
422
440
  }
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
- // }
@@ -9,7 +9,7 @@ import { count_regular, delete_entity_values_by_value_or_reporter_and_context,
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 = 'shipping_methods'
15
15
 
@@ -111,24 +111,31 @@ const remove = (driver) => {
111
111
  const list = (driver) => {
112
112
  return async (query) => {
113
113
 
114
- const items = await driver.client
114
+ const items = await withQuery(
115
+ driver.client
115
116
  .selectFrom(table_name)
116
117
  .selectAll()
117
- .select(eb => [
118
- with_media(eb, eb.ref('shipping_methods.id'), driver.dialectType),
119
- with_tags(eb, eb.ref('shipping_methods.id'), driver.dialectType),
120
- with_search(eb, eb.ref('shipping_methods.id'), driver.dialectType),
121
- ].filter(Boolean))
122
- .where(
123
- (eb) => {
124
- return query_to_eb(eb, query, table_name);
125
- }
126
- )
127
- .orderBy(query_to_sort(query, 'shipping_methods'))
128
- .limit(query.limitToLast ?? query.limit ?? 10)
129
- .execute();
118
+ .select(
119
+ eb => [
120
+ with_media(eb, eb.ref('shipping_methods.id'), driver.dialectType),
121
+ with_tags(eb, eb.ref('shipping_methods.id'), driver.dialectType),
122
+ with_search(eb, eb.ref('shipping_methods.id'), driver.dialectType),
123
+ ].filter(Boolean)
124
+ ),
125
+ query, table_name
126
+ ).execute();
127
+
128
+ // .where(
129
+ // (eb) => {
130
+ // return query_to_eb(eb, query, table_name);
131
+ // }
132
+ // )
133
+ // .orderBy(query_to_sort(query, 'shipping_methods'))
134
+ // .limit(query.limitToLast ?? query.limit ?? 10)
135
+ // .execute();
130
136
 
131
- if(query.limitToLast) items.reverse();
137
+ if(query.limitToLast)
138
+ items.reverse();
132
139
 
133
140
  return sanitize_array(items);
134
141
  }
@@ -20,7 +20,7 @@ import { delete_entity_values_of_by_entity_id_or_handle_and_context,
20
20
  products_with_variants,
21
21
  products_with_related_products} from './con.shared.js'
22
22
  import { sanitize, sanitize_array } from './utils.funcs.js'
23
- import { query_to_eb, query_to_sort } from './utils.query.js'
23
+ import { withQuery } from './utils.query.js'
24
24
 
25
25
  export const table_name = 'storefronts'
26
26
 
@@ -193,7 +193,8 @@ const list = (driver) => {
193
193
  const expand_shipping = expand_all || expand.includes('shipping_methods');
194
194
  const expand_posts = expand_all || expand.includes('posts');
195
195
 
196
- const items = await driver.client
196
+ const items = await withQuery(
197
+ driver.client
197
198
  .selectFrom(table_name)
198
199
  .selectAll()
199
200
  .select(eb => [
@@ -216,17 +217,22 @@ const list = (driver) => {
216
217
  eb, eb.ref('storefronts.id'), driver.dialectType
217
218
  ),
218
219
  ].filter(Boolean)
219
- )
220
- .where(
221
- (eb) => {
222
- return query_to_eb(eb, query, table_name);
223
- }
224
- )
225
- .orderBy(query_to_sort(query, table_name))
226
- .limit(query.limitToLast ?? query.limit ?? 10)
227
- .execute();
220
+ ),
221
+ query, table_name
222
+ ).execute();
223
+
224
+
225
+ // .where(
226
+ // (eb) => {
227
+ // return query_to_eb(eb, query, table_name);
228
+ // }
229
+ // )
230
+ // .orderBy(query_to_sort(query, table_name))
231
+ // .limit(query.limitToLast ?? query.limit ?? 10)
232
+ // .execute();
228
233
 
229
- if(query.limitToLast) items.reverse();
234
+ if(query.limitToLast)
235
+ items.reverse();
230
236
 
231
237
  return sanitize_array(items);
232
238
  }
@@ -268,7 +274,7 @@ const get_default_auto_generated_storefront = (driver) => {
268
274
  ]
269
275
  )
270
276
  .where('active', '=', 1)
271
- .orderBy(['updated_at desc']),
277
+ .orderBy('updated_at', 'desc'),
272
278
  driver.dialectType
273
279
  ).as('collections'),
274
280
 
@@ -287,7 +293,7 @@ const get_default_auto_generated_storefront = (driver) => {
287
293
  ]
288
294
  )
289
295
  .where('active', '=', 1)
290
- .orderBy(['updated_at desc'])
296
+ .orderBy('updated_at', 'desc')
291
297
  .limit(10),
292
298
  dialectType
293
299
  ).as('products'),
@@ -303,7 +309,7 @@ const get_default_auto_generated_storefront = (driver) => {
303
309
  ]
304
310
  )
305
311
  .where('active', '=', 1)
306
- .orderBy(['updated_at desc']),
312
+ .orderBy('updated_at', 'desc'),
307
313
  dialectType
308
314
  ).as('discounts'),
309
315
 
@@ -318,7 +324,7 @@ const get_default_auto_generated_storefront = (driver) => {
318
324
  ]
319
325
  )
320
326
  .where('active', '=', 1)
321
- .orderBy(['updated_at desc']),
327
+ .orderBy('updated_at', 'desc'),
322
328
  dialectType
323
329
  ).as('shipping_methods'),
324
330
 
@@ -333,7 +339,7 @@ const get_default_auto_generated_storefront = (driver) => {
333
339
  ]
334
340
  )
335
341
  .where('active', '=', 1)
336
- .orderBy(['updated_at desc'])
342
+ .orderBy('updated_at', 'desc')
337
343
  .limit(3),
338
344
  dialectType
339
345
  ).as('posts'),
package/src/con.tags.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  with_media, with_search
9
9
  } from './con.shared.js'
10
10
  import { sanitize, sanitize_array } from './utils.funcs.js'
11
- import { query_to_eb, query_to_sort } from './utils.query.js'
11
+ import { withQuery } from './utils.query.js'
12
12
 
13
13
  export const table_name = 'tags'
14
14
 
@@ -97,22 +97,30 @@ const remove = (driver) => {
97
97
  const list = (driver) => {
98
98
  return async (query) => {
99
99
 
100
- const items = await driver.client
100
+ const items = await withQuery(
101
+ driver.client
101
102
  .selectFrom(table_name)
102
103
  .selectAll()
103
- .select(eb => [
104
- with_search(eb, eb.ref('tags.id'), driver.dialectType),
105
- ].filter(Boolean))
106
- .where(
107
- (eb) => {
108
- return query_to_eb(eb, query, table_name);
109
- }
110
- )
111
- .orderBy(query_to_sort(query, table_name))
112
- .limit(query.limitToLast ?? query.limit ?? 10)
113
- .execute();
104
+ .select(
105
+ eb => [
106
+ with_search(eb, eb.ref('tags.id'), driver.dialectType),
107
+ ].filter(Boolean)
108
+ ),
109
+ query, table_name
110
+ ).execute();
111
+
112
+ // .where(
113
+ // (eb) => {
114
+ // return query_to_eb(eb, query, table_name);
115
+ // }
116
+ // )
117
+ // .orderBy(query_to_sort(query, table_name))
118
+ // .limit(query.limitToLast ?? query.limit ?? 10)
119
+ // .execute();
120
+
121
+ if(query.limitToLast)
122
+ items.reverse();
114
123
 
115
- if(query.limitToLast) items.reverse();
116
124
  return sanitize_array(items);
117
125
  }
118
126
  }
@@ -8,7 +8,7 @@ import { count_regular, delete_me, delete_search_of, insert_search_of,
8
8
  regular_upsert_me, safe_trx, where_id_or_handle_table,
9
9
  with_search} from './con.shared.js'
10
10
  import { sanitize, sanitize_array } from './utils.funcs.js'
11
- import { query_to_eb, query_to_sort } from './utils.query.js'
11
+ import { withQuery } from './utils.query.js'
12
12
  import { base64 } from '@storecraft/core/crypto';
13
13
 
14
14
  export const table_name = 'templates'
@@ -137,36 +137,33 @@ const remove = (driver) => {
137
137
  const list = (driver) => {
138
138
  return async (query) => {
139
139
 
140
- const items = await driver.client
140
+ const items = await withQuery(
141
+ driver.client
141
142
  .selectFrom(table_name)
142
143
  .selectAll()
143
144
  .select(
144
145
  eb => [
145
146
  with_search(eb, eb.ref('templates.id'), driver.dialectType),
146
147
  ].filter(Boolean)
147
- )
148
- .where(
149
- (eb) => {
150
- return query_to_eb(eb, query, table_name);
151
- }
152
- )
153
- .orderBy(query_to_sort(query, table_name))
154
- .limit(query.limitToLast ?? query.limit ?? 10)
155
- .execute()
156
- .then(
157
- (items) => {
158
- return items.map(
159
- (item) => {
160
- item.template_html = decode_base64_if_needed(item.template_html);
161
- item.template_text = decode_base64_if_needed(item.template_text);
162
- item.template_subject = decode_base64_if_needed(item.template_subject);
163
- return item;
164
- }
165
- )
166
- }
167
- );
148
+ ),
149
+ query, table_name
150
+ )
151
+ .execute()
152
+ .then(
153
+ (items) => {
154
+ return items.map(
155
+ (item) => {
156
+ item.template_html = decode_base64_if_needed(item.template_html);
157
+ item.template_text = decode_base64_if_needed(item.template_text);
158
+ item.template_subject = decode_base64_if_needed(item.template_subject);
159
+ return item;
160
+ }
161
+ )
162
+ }
163
+ );
168
164
 
169
- if(query.limitToLast) items.reverse();
165
+ if(query.limitToLast)
166
+ items.reverse();
170
167
 
171
168
  return sanitize_array(items);
172
169
  }