@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.
- package/package.json +1 -1
- package/src/con.auth_users.js +17 -13
- package/src/con.collections.js +24 -16
- package/src/con.customers.js +24 -17
- package/src/con.discounts.js +30 -19
- package/src/con.discounts.utils.js +8 -4
- package/src/con.images.js +18 -13
- package/src/con.notifications.js +22 -15
- package/src/con.orders.js +25 -18
- package/src/con.posts.js +28 -28
- package/src/con.products.js +52 -34
- package/src/con.search.js +12 -12
- package/src/con.shared.js +17 -35
- package/src/con.shipping.js +23 -16
- package/src/con.storefronts.js +23 -17
- package/src/con.tags.js +22 -14
- package/src/con.templates.js +21 -24
- package/src/utils.query.OLD.js +259 -0
- package/src/utils.query.js +212 -204
- package/tests/sandbox.js +5 -6
package/src/con.products.js
CHANGED
@@ -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 {
|
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(
|
42
|
-
|
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
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
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
|
-
|
402
|
-
|
408
|
+
expand_discounts &&
|
409
|
+
products_with_discounts(
|
410
|
+
eb, eb.ref('products.id'), driver.dialectType
|
411
|
+
),
|
403
412
|
|
404
|
-
|
405
|
-
|
406
|
-
|
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
|
-
|
418
|
-
|
419
|
-
|
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 {
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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 {
|
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
|
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
|
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
|
330
|
-
|
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
|
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
|
-
// }
|
package/src/con.shipping.js
CHANGED
@@ -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 {
|
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
|
114
|
+
const items = await withQuery(
|
115
|
+
driver.client
|
115
116
|
.selectFrom(table_name)
|
116
117
|
.selectAll()
|
117
|
-
.select(
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
.
|
128
|
-
|
129
|
-
|
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)
|
137
|
+
if(query.limitToLast)
|
138
|
+
items.reverse();
|
132
139
|
|
133
140
|
return sanitize_array(items);
|
134
141
|
}
|
package/src/con.storefronts.js
CHANGED
@@ -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 {
|
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
|
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
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
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)
|
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(
|
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(
|
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(
|
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(
|
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(
|
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 {
|
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
|
100
|
+
const items = await withQuery(
|
101
|
+
driver.client
|
101
102
|
.selectFrom(table_name)
|
102
103
|
.selectAll()
|
103
|
-
.select(
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
.
|
112
|
-
|
113
|
-
|
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
|
}
|
package/src/con.templates.js
CHANGED
@@ -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 {
|
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
|
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
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
)
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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)
|
165
|
+
if(query.limitToLast)
|
166
|
+
items.reverse();
|
170
167
|
|
171
168
|
return sanitize_array(items);
|
172
169
|
}
|