@storecraft/database-sql-base 1.2.5 → 1.3.0

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.
@@ -1,724 +1,724 @@
1
- // @ts-nocheck
2
- import { ExpressionWrapper, Kysely } from 'kysely'
3
- import { jsonArrayFrom, stringArrayFrom } from './con.helpers.json.js'
4
- import { SQL } from '../index.js';
5
- import { query_to_eb } from './utils.query.js';
6
-
7
- /**
8
- * This file contains the shared commands without execute, but only returns
9
- * array of query builders to mayne support batches and multi statements
10
- */
11
-
12
- /**
13
- * @template ReturnType
14
- * @typedef {(input: Kysely<import('../index.js').Database>) => ReturnType} Callback
15
- */
16
-
17
- /**
18
- * @typedef {import('kysely').SelectQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database> |
19
- * import('kysely').DeleteQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database, DeleteResult> |
20
- * import('kysely').InsertQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database, InsertResult> |
21
- * import('kysely').UpdateQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database>
22
- * } Builder
23
- */
24
-
25
- /**
26
- * @typedef {object} Executable
27
- * @prop {(kysely?: Kysely) => Promise<any>} execute
28
- * @prop {import('kysely').Compilable["compile"]} compile
29
- */
30
- /**
31
- *
32
- * @template {any[]} Input
33
- *
34
- * @param {SQL} driver
35
- *
36
- */
37
- const trans_or_batch = (driver) => {
38
- const prefers_batch = driver.config.prefers_multiple_statements_over_transaction;
1
+ // // @ts-nocheck
2
+ // import { ExpressionWrapper, Kysely } from 'kysely'
3
+ // import { jsonArrayFrom, stringArrayFrom } from './con.helpers.json.js'
4
+ // import { SQL } from '../index.js';
5
+ // import { query_to_eb } from './utils.query.js';
6
+
7
+ // /**
8
+ // * This file contains the shared commands without execute, but only returns
9
+ // * array of query builders to mayne support batches and multi statements
10
+ // */
11
+
12
+ // /**
13
+ // * @template ReturnType
14
+ // * @typedef {(input: Kysely<import('../index.js').Database>) => ReturnType} Callback
15
+ // */
16
+
17
+ // /**
18
+ // * @typedef {import('kysely').SelectQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database> |
19
+ // * import('kysely').DeleteQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database, DeleteResult> |
20
+ // * import('kysely').InsertQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database, InsertResult> |
21
+ // * import('kysely').UpdateQueryBuilder<import('../types.sql.tables.js').Database, keyof import('../types.sql.tables.js').Database>
22
+ // * } Builder
23
+ // */
24
+
25
+ // /**
26
+ // * @typedef {object} Executable
27
+ // * @prop {(kysely?: Kysely) => Promise<any>} execute
28
+ // * @prop {import('kysely').Compilable["compile"]} compile
29
+ // */
30
+ // /**
31
+ // *
32
+ // * @template {any[]} Input
33
+ // *
34
+ // * @param {SQL} driver
35
+ // *
36
+ // */
37
+ // const trans_or_batch = (driver) => {
38
+ // const prefers_batch = driver.config.prefers_multiple_statements_over_transaction;
39
39
 
40
- if(prefers_batch) {
41
- /**
42
- *
43
- */
44
- return {
45
- /**
46
- * @param {Callback<Input>} callback
47
- */
48
- execute: async (callback) => {
49
- const inputs = callback(driver.client);
50
- /** @type {Executable[]} */
51
- const inputs_2 = (inputs ?? []).flat(100).filter(Boolean);
52
- const combined = sql.join(inputs_2, sql`;`);
53
- const r = await combined.execute(driver.client);
54
- return r;
55
- }
56
- }
57
- }
58
-
59
- return {
60
- /**
61
- *
62
- * @param {Callback<Input>} callback
63
- */
64
- execute: async (callback) => {
65
- const trans = await driver.client.transaction().execute(
66
- async (trx) => {
67
- const inputs = callback(trx);
68
- /** @type {Executable[]} */
69
- const inputs_2 = (inputs ?? []).flat(100).filter(Boolean);
70
- const results = [];
71
- for (const t of inputs_2) {
72
- results.push(await t.execute())
73
- }
74
- return results;
75
- }
76
- );
40
+ // if(prefers_batch) {
41
+ // /**
42
+ // *
43
+ // */
44
+ // return {
45
+ // /**
46
+ // * @param {Callback<Input>} callback
47
+ // */
48
+ // execute: async (callback) => {
49
+ // const inputs = callback(driver.client);
50
+ // /** @type {Executable[]} */
51
+ // const inputs_2 = (inputs ?? []).flat(100).filter(Boolean);
52
+ // const combined = sql.join(inputs_2, sql`;`);
53
+ // const r = await combined.execute(driver.client);
54
+ // return r;
55
+ // }
56
+ // }
57
+ // }
58
+
59
+ // return {
60
+ // /**
61
+ // *
62
+ // * @param {Callback<Input>} callback
63
+ // */
64
+ // execute: async (callback) => {
65
+ // const trans = await driver.client.transaction().execute(
66
+ // async (trx) => {
67
+ // const inputs = callback(trx);
68
+ // /** @type {Executable[]} */
69
+ // const inputs_2 = (inputs ?? []).flat(100).filter(Boolean);
70
+ // const results = [];
71
+ // for (const t of inputs_2) {
72
+ // results.push(await t.execute())
73
+ // }
74
+ // return results;
75
+ // }
76
+ // );
77
77
 
78
- }
79
- }
80
-
81
- }
82
-
83
- /**
84
- * @param {SQL} driver
85
- * @param {keyof Database} table_name
86
- *
87
- * @returns {import('@storecraft/core/database').db_crud["count"]}
88
- */
89
- export const count_regular = (driver, table_name) => {
90
- return async (query) => {
91
-
92
- const result = await driver.client
93
- .selectFrom(table_name)
94
- .select(
95
- (eb) => eb.fn.countAll().as('count')
96
- )
97
- .where(
98
- (eb) => {
99
- return query_to_eb(eb, query, table_name);
100
- }
101
- )
102
- .executeTakeFirst();
103
-
104
- return Number(result.count);
105
- }
106
- }
107
-
108
- /**
109
- *
110
- * @param {string} id_or_handle
111
- */
112
- export const where_id_or_handle_entity = (id_or_handle) => {
113
- /**
114
- * @param {import('kysely').ExpressionBuilder<Database>} eb
115
- */
116
- return (eb) => eb.or(
117
- [
118
- eb('entity_handle', '=', id_or_handle),
119
- eb('entity_id', '=', id_or_handle),
120
- ]
121
- );
122
- }
123
-
124
- /**
125
- *
126
- * @param {string} id_or_handle
127
- */
128
- export const where_id_or_handle_table = (id_or_handle) => {
129
- /**
130
- * @param {import('kysely').ExpressionBuilder<Database>} eb
131
- */
132
- return (eb) => eb.or(
133
- [
134
- eb('id', '=', id_or_handle),
135
- eb('handle', '=', id_or_handle),
136
- ]
137
- );
138
- }
139
-
140
- /**
141
- * @typedef { keyof Pick<Database,
142
- * 'entity_to_media' | 'entity_to_search_terms'
143
- * | 'entity_to_tags_projections' | 'products_to_collections'
144
- * | 'products_to_discounts' | 'products_to_variants'
145
- * | 'products_to_related_products' | 'storefronts_to_other'>
146
- * } EntityTableKeys
147
- */
148
-
149
- /**
150
- * helper to generate entity values delete
151
- *
152
- * @param {EntityTableKeys} entity_table_name
153
- */
154
- export const delete_entity_values_by_value_or_reporter = (entity_table_name) => {
155
- /**
156
- *
157
- * @param {Kysely<Database>} trx
158
- * @param {string} value delete by entity value
159
- * @param {string} [reporter] delete by reporter
160
- */
161
- return (trx, value, reporter=undefined) => {
162
-
163
- return trx.deleteFrom(entity_table_name).where(
164
- eb => eb.or(
165
- [
166
- value && eb('value', '=', value),
167
- reporter && eb('reporter', '=', reporter),
168
- ].filter(Boolean)
169
- )
170
- )
171
- //.executeTakeFirst();
172
- }
173
- }
174
-
175
- /**
176
- * helper to generate entity values delete
177
- *
178
- * @param {EntityTableKeys} entity_table_name
179
- */
180
- export const delete_entity_values_of_by_entity_id_or_handle =
181
- (entity_table_name) => {
182
- /**
183
- *
184
- * @param {Kysely<import('../index.js').Database>} trx
185
- * @param {string} entity_id delete by id
186
- * @param {string} [entity_handle=entity_id] delete by handle
187
- */
188
- return (trx, entity_id, entity_handle=undefined) => {
189
- return trx.deleteFrom(entity_table_name).where(
190
- eb => eb.or(
191
- [
192
- eb('entity_id', '=', entity_id),
193
- eb('entity_handle', '=', entity_handle ?? entity_id),
194
- ]
195
- )
196
- )
197
- //.executeTakeFirst();
198
- }
199
- }
200
-
201
- /**
202
- * helper to generate entity values for simple tables
203
- * @param {EntityTableKeys} entity_table_name
204
- */
205
- export const insert_entity_array_values_of = (entity_table_name) => {
206
- /**
207
- *
208
- * @param {Kysely<Database>} trx
209
- * @param {string[]} values values of the entity
210
- * @param {string} item_id whom the tags belong to
211
- * @param {string} [item_handle] whom the tags belong to
212
- * @param {boolean} [delete_previous=true] if true and `reporter`,
213
- * then will delete by reporter, otherwise by `item_id/item_handle`
214
- * @param {string} [reporter=undefined] the reporter of the batch values
215
- * (another segment technique)
216
- * @param {string} [context=undefined] the context (another segment technique)
217
- */
218
- return (trx, values, item_id, item_handle, delete_previous=true,
219
- reporter=undefined, context=undefined) => {
220
-
221
- const queries = [];
222
-
223
- if(delete_previous) {
224
- if(reporter) {
225
- queries.push(
226
- delete_entity_values_by_value_or_reporter(entity_table_name)(
227
- trx, undefined, reporter
228
- )
229
- )
230
- } else {
231
- queries.push(
232
- delete_entity_values_of_by_entity_id_or_handle(entity_table_name)(
233
- trx, item_id, item_handle
234
- )
235
- )
236
- }
237
- }
238
-
239
- if(!values?.length)
240
- return queries;
78
+ // }
79
+ // }
80
+
81
+ // }
82
+
83
+ // /**
84
+ // * @param {SQL} driver
85
+ // * @param {keyof Database} table_name
86
+ // *
87
+ // * @returns {import('@storecraft/core/database').db_crud["count"]}
88
+ // */
89
+ // export const count_regular = (driver, table_name) => {
90
+ // return async (query) => {
91
+
92
+ // const result = await driver.client
93
+ // .selectFrom(table_name)
94
+ // .select(
95
+ // (eb) => eb.fn.countAll().as('count')
96
+ // )
97
+ // .where(
98
+ // (eb) => {
99
+ // return query_to_eb(eb, query, table_name);
100
+ // }
101
+ // )
102
+ // .executeTakeFirst();
103
+
104
+ // return Number(result.count);
105
+ // }
106
+ // }
107
+
108
+ // /**
109
+ // *
110
+ // * @param {string} id_or_handle
111
+ // */
112
+ // export const where_id_or_handle_entity = (id_or_handle) => {
113
+ // /**
114
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
115
+ // */
116
+ // return (eb) => eb.or(
117
+ // [
118
+ // eb('entity_handle', '=', id_or_handle),
119
+ // eb('entity_id', '=', id_or_handle),
120
+ // ]
121
+ // );
122
+ // }
123
+
124
+ // /**
125
+ // *
126
+ // * @param {string} id_or_handle
127
+ // */
128
+ // export const where_id_or_handle_table = (id_or_handle) => {
129
+ // /**
130
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
131
+ // */
132
+ // return (eb) => eb.or(
133
+ // [
134
+ // eb('id', '=', id_or_handle),
135
+ // eb('handle', '=', id_or_handle),
136
+ // ]
137
+ // );
138
+ // }
139
+
140
+ // /**
141
+ // * @typedef { keyof Pick<Database,
142
+ // * 'entity_to_media' | 'entity_to_search_terms'
143
+ // * | 'entity_to_tags_projections' | 'products_to_collections'
144
+ // * | 'products_to_discounts' | 'products_to_variants'
145
+ // * | 'products_to_related_products' | 'storefronts_to_other'>
146
+ // * } EntityTableKeys
147
+ // */
148
+
149
+ // /**
150
+ // * helper to generate entity values delete
151
+ // *
152
+ // * @param {EntityTableKeys} entity_table_name
153
+ // */
154
+ // export const delete_entity_values_by_value_or_reporter = (entity_table_name) => {
155
+ // /**
156
+ // *
157
+ // * @param {Kysely<Database>} trx
158
+ // * @param {string} value delete by entity value
159
+ // * @param {string} [reporter] delete by reporter
160
+ // */
161
+ // return (trx, value, reporter=undefined) => {
162
+
163
+ // return trx.deleteFrom(entity_table_name).where(
164
+ // eb => eb.or(
165
+ // [
166
+ // value && eb('value', '=', value),
167
+ // reporter && eb('reporter', '=', reporter),
168
+ // ].filter(Boolean)
169
+ // )
170
+ // )
171
+ // //.executeTakeFirst();
172
+ // }
173
+ // }
174
+
175
+ // /**
176
+ // * helper to generate entity values delete
177
+ // *
178
+ // * @param {EntityTableKeys} entity_table_name
179
+ // */
180
+ // export const delete_entity_values_of_by_entity_id_or_handle =
181
+ // (entity_table_name) => {
182
+ // /**
183
+ // *
184
+ // * @param {Kysely<import('../index.js').Database>} trx
185
+ // * @param {string} entity_id delete by id
186
+ // * @param {string} [entity_handle=entity_id] delete by handle
187
+ // */
188
+ // return (trx, entity_id, entity_handle=undefined) => {
189
+ // return trx.deleteFrom(entity_table_name).where(
190
+ // eb => eb.or(
191
+ // [
192
+ // eb('entity_id', '=', entity_id),
193
+ // eb('entity_handle', '=', entity_handle ?? entity_id),
194
+ // ]
195
+ // )
196
+ // )
197
+ // //.executeTakeFirst();
198
+ // }
199
+ // }
200
+
201
+ // /**
202
+ // * helper to generate entity values for simple tables
203
+ // * @param {EntityTableKeys} entity_table_name
204
+ // */
205
+ // export const insert_entity_array_values_of = (entity_table_name) => {
206
+ // /**
207
+ // *
208
+ // * @param {Kysely<Database>} trx
209
+ // * @param {string[]} values values of the entity
210
+ // * @param {string} item_id whom the tags belong to
211
+ // * @param {string} [item_handle] whom the tags belong to
212
+ // * @param {boolean} [delete_previous=true] if true and `reporter`,
213
+ // * then will delete by reporter, otherwise by `item_id/item_handle`
214
+ // * @param {string} [reporter=undefined] the reporter of the batch values
215
+ // * (another segment technique)
216
+ // * @param {string} [context=undefined] the context (another segment technique)
217
+ // */
218
+ // return (trx, values, item_id, item_handle, delete_previous=true,
219
+ // reporter=undefined, context=undefined) => {
220
+
221
+ // const queries = [];
222
+
223
+ // if(delete_previous) {
224
+ // if(reporter) {
225
+ // queries.push(
226
+ // delete_entity_values_by_value_or_reporter(entity_table_name)(
227
+ // trx, undefined, reporter
228
+ // )
229
+ // )
230
+ // } else {
231
+ // queries.push(
232
+ // delete_entity_values_of_by_entity_id_or_handle(entity_table_name)(
233
+ // trx, item_id, item_handle
234
+ // )
235
+ // )
236
+ // }
237
+ // }
238
+
239
+ // if(!values?.length)
240
+ // return queries;
241
241
 
242
242
 
243
- queries.push(
244
- trx.insertInto(entity_table_name).values(
245
- values.map(t => ({
246
- entity_handle: item_handle,
247
- entity_id: item_id,
248
- value: t,
249
- reporter,
250
- context
251
- })
252
- )
253
- )
254
- );
243
+ // queries.push(
244
+ // trx.insertInto(entity_table_name).values(
245
+ // values.map(t => ({
246
+ // entity_handle: item_handle,
247
+ // entity_id: item_id,
248
+ // value: t,
249
+ // reporter,
250
+ // context
251
+ // })
252
+ // )
253
+ // )
254
+ // );
255
255
 
256
- //.executeTakeFirst();
257
-
258
- return queries;
259
- }
260
- }
261
-
262
- /**
263
- * helper to generate entity values delete
264
- *
265
- * @param {EntityTableKeys} entity_table_name
266
- */
267
- export const insert_entity_values_of = (entity_table_name) => {
268
- /**
269
- *
270
- * @param {Kysely<Database>} trx
271
- * @param {{value: string, reporter: string}[]} values values of the entity
272
- * @param {string} item_id whom the tags belong to
273
- * @param {string} [item_handle] whom the tags belong to
274
- * @param {string} [context=undefined] the context (another segment technique)
275
- */
276
- return (trx, values, item_id, item_handle, context=undefined) => {
277
-
278
- if(!values?.length)
279
- return undefined;
280
-
281
- return trx.insertInto(entity_table_name).values(
282
- values.map(t => (
283
- {
284
- entity_id: item_id,
285
- entity_handle: item_handle,
286
- value: t.value,
287
- reporter: t.reporter,
288
- context
289
- }
290
- )
291
- )
292
- )
293
- //.executeTakeFirst();
294
- }
295
- }
296
-
297
-
298
- /**
299
- * Delete previous entities by `id/handle` and insert new ones
300
- *
301
- * @param {EntityTableKeys} entity_table
302
- */
303
- export const insert_entity_array_values_with_delete_of = (entity_table) => {
304
- /**
305
- * @param {Kysely<Database>} trx
306
- * @param {string[]} values values of the entity
307
- * @param {string} item_id entity id
308
- * @param {string} [item_handle] entity handle
309
- * @param {string} [context] context
310
- */
311
- return (trx, values, item_id, item_handle, context) => {
312
- return insert_entity_array_values_of(entity_table)(
313
- trx, values, item_id, item_handle, true, undefined, context
314
- )
315
- };
316
- }
317
-
318
- export const insert_tags_of = insert_entity_array_values_with_delete_of('entity_to_tags_projections');
319
- export const insert_search_of = insert_entity_array_values_with_delete_of('entity_to_search_terms');
320
- export const insert_media_of = insert_entity_array_values_with_delete_of('entity_to_media');
321
-
322
- export const delete_tags_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_tags_projections');
323
- export const delete_search_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_search_terms');
324
- export const delete_media_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_media');
325
-
326
- /**
327
- * @typedef {import('../index.js').Database} Database
328
- */
329
-
330
-
331
- /**
332
- * @template {keyof Database} T
333
- *
334
- * @param {Kysely<Database>} trx
335
- * @param {T} table_name
336
- * @param {import('kysely').InsertObject<Database, T>} item values of the entity
337
- *
338
- */
339
- export const regular_upsert_me = (trx, table_name, item) => {
340
-
341
- const queries = [];
342
-
343
- // TODO: maybe use only `id`
344
- queries.push(
345
- trx.deleteFrom(table_name).where(
346
- eb => eb.or(
347
- [
348
- item.id && eb('id', '=', item.id),
349
- item.handle && eb('handle', '=', item.handle),
350
- ].filter(Boolean)
351
- )
352
- )
353
- //.execute();
354
- );
355
-
356
- queries.push(
357
- trx.insertInto(table_name).values(item)
358
- //.executeTakeFirst()
359
- );
360
-
361
- return queries;
362
- }
363
-
364
-
365
- /**
366
- *
367
- * @param {Kysely<Database>} trx
368
- * @param {keyof Database} table_name
369
- * @param {string} id_or_handle
370
- */
371
- export const delete_me = (trx, table_name, id_or_handle) => {
372
- // console.log('delete ', id_or_handle)
373
- return trx.deleteFrom(table_name).where(
374
- where_id_or_handle_table(id_or_handle)
375
- );
376
- //.executeTakeFirst();
377
- }
378
-
379
- /**
380
- *
381
- * @param {import('kysely').ExpressionBuilder<Database>} eb
382
- * @param {string | ExpressionWrapper<Database>} id_or_handle
383
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
384
- */
385
- export const with_tags = (eb, id_or_handle, sql_type) => {
386
- return stringArrayFrom(
387
- select_values_of_entity_by_entity_id_or_handle(
388
- eb, 'entity_to_tags_projections', id_or_handle
389
- ), sql_type
390
- ).as('tags');
391
- }
392
-
393
- /**
394
- *
395
- * @param {import('kysely').ExpressionBuilder<Database>} eb
396
- * @param {string | ExpressionWrapper<Database>} id_or_handle
397
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
398
- */
399
- export const with_search = (eb, id_or_handle, sql_type) => {
400
- return stringArrayFrom(
401
- select_values_of_entity_by_entity_id_or_handle(
402
- eb, 'entity_to_search_terms', id_or_handle
403
- ), sql_type
404
- ).as('search');
405
- }
406
-
407
- /**
408
- *
409
- * @param {import('kysely').ExpressionBuilder<Database>} eb
410
- * @param {string | ExpressionWrapper<Database>} id_or_handle
411
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
412
- */
413
- export const with_media = (eb, id_or_handle, sql_type) => {
414
- return stringArrayFrom(
415
- select_values_of_entity_by_entity_id_or_handle(
416
- eb, 'entity_to_media', id_or_handle
417
- ), sql_type
418
- ).as('media');
419
- }
420
-
421
- /**
422
- * helper to select base attributes
423
- * @param {import('kysely').ExpressionBuilder<Database>} eb
424
- * @param {keyof Database} table
425
- * @return {import('kysely').SelectQueryBuilder<Database, table>}
426
- */
427
- const select_base_from = (eb, table) => {
428
- return [
429
- 'active', 'attributes', 'created_at', 'updated_at',
430
- 'description', 'handle', 'id'
431
- ].map(k => `${table}.${k}`).reduce(
432
- (p, c) => p.select(c), eb.selectFrom(table)
433
- );
434
- }
435
-
436
- /**
437
- * select as json array collections of a product
438
- *
439
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
440
- * @param {string | ExpressionWrapper<Database>} product_id_or_handle
441
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
442
- */
443
- export const products_with_collections = (eb, product_id_or_handle, sql_type) => {
444
- return jsonArrayFrom(
445
- select_base_from(eb, 'collections')
446
- .select('collections.title')
447
- .select('collections.published')
448
- .select(eb => [
449
- with_tags(eb, eb.ref('collections.id'), sql_type),
450
- with_media(eb, eb.ref('collections.id'), sql_type),
451
- ])
452
- .where('collections.id', 'in',
453
- eb => select_values_of_entity_by_entity_id_or_handle(
454
- eb, 'products_to_collections', product_id_or_handle
455
- )
456
- ), sql_type
457
- ).as('collections');
458
- }
459
-
460
- /**
461
- * select as json array collections of a product
462
- *
463
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
464
- * @param {string | ExpressionWrapper<Database>} product_id_or_handle
465
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
466
- */
467
- export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
468
- return jsonArrayFrom(
469
- select_base_from(eb, 'discounts')
470
- .select('discounts.title')
471
- .select('discounts.published')
472
- .select('discounts.application')
473
- .select('discounts.info')
474
- .select('discounts.priority')
475
- .select(eb => [
476
- with_tags(eb, eb.ref('discounts.id'), sql_type),
477
- with_media(eb, eb.ref('discounts.id'), sql_type),
478
- ])
479
- .where('discounts.id', 'in',
480
- eb => select_values_of_entity_by_entity_id_or_handle(
481
- eb, 'products_to_discounts', product_id_or_handle
482
- )
483
- ), sql_type
484
- ).as('discounts');
485
- }
486
-
487
- /**
488
- * select as json array collections of a product
489
- *
490
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
491
- * @param {string | ExpressionWrapper<Database>} product_id_or_handle
492
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
493
- */
494
- export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
495
- return jsonArrayFrom(
496
- select_base_from(eb, 'products')
497
- .select('products.compare_at_price')
498
- .select('products.parent_handle')
499
- .select('products.parent_id')
500
- .select('products.price')
501
- .select('products.qty')
502
- .select('products.title')
503
- .select('products.variant_hint')
504
- .select('products.variants_options')
505
- .select('products.video')
506
- .select(eb => [
507
- with_tags(eb, eb.ref('products.id'), sql_type),
508
- with_media(eb, eb.ref('products.id'), sql_type),
509
- ])
510
- .where('products.id', 'in',
511
- eb => select_values_of_entity_by_entity_id_or_handle(
512
- eb, 'products_to_variants', product_id_or_handle
513
- )
514
- ), sql_type
515
- ).as('variants');
516
- }
517
-
518
- /**
519
- * select as json array collections of a product
520
- *
521
- * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
522
- * @param {string | ExpressionWrapper<Database>} product_id_or_handle
523
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
524
- */
525
- export const products_with_related_products = (eb, product_id_or_handle, sql_type) => {
526
- return jsonArrayFrom(
527
- select_base_from(eb, 'products')
528
- .select('products.compare_at_price')
529
- .select('products.parent_handle')
530
- .select('products.parent_id')
531
- .select('products.price')
532
- .select('products.qty')
533
- .select('products.title')
534
- .select('products.variant_hint')
535
- .select('products.variants_options')
536
- .select('products.video')
537
- .select(eb => [
538
- with_tags(eb, eb.ref('products.id'), sql_type),
539
- with_media(eb, eb.ref('products.id'), sql_type),
540
- ])
541
- .where('products.id', 'in',
542
- eb => select_values_of_entity_by_entity_id_or_handle(
543
- eb, 'products_to_related_products', product_id_or_handle
544
- )
545
- ), sql_type
546
- ).as('related_products');
547
- }
548
-
549
-
550
- /**
551
- * select as json array collections of a product
552
- *
553
- * @param {import('kysely').ExpressionBuilder<Database, 'storefronts'>} eb
554
- * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
555
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
556
- */
557
- export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
558
- return jsonArrayFrom(
559
- select_base_from(eb, 'collections')
560
- .select('collections.title')
561
- .select('collections.published')
562
- .select(eb => [
563
- with_tags(eb, eb.ref('collections.id'), sql_type),
564
- with_media(eb, eb.ref('collections.id'), sql_type),
565
- ])
566
- .where('collections.id', 'in',
567
- eb => select_values_of_entity_by_entity_id_or_handle(
568
- eb, 'storefronts_to_other', sf_id_or_handle
569
- )
570
- ), sql_type
571
- ).as('collections');
572
- }
573
-
574
- /**
575
- * select as json array collections of a product
576
- *
577
- * @param {import('kysely').ExpressionBuilder<Database>} eb
578
- * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
579
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
580
- */
581
- export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
582
- return jsonArrayFrom(
583
- select_base_from(eb, 'products')
584
- .select('products.title')
585
- .select('products.compare_at_price')
586
- .select('products.parent_handle')
587
- .select('products.parent_id')
588
- .select('products.price')
589
- .select('products.qty')
590
- .select('products.variant_hint')
591
- .select('products.variants_options')
592
- .select('products.video')
593
- .select(eb => [
594
- with_tags(eb, eb.ref('products.id'), sql_type),
595
- with_media(eb, eb.ref('products.id'), sql_type),
596
- ])
597
- .where('products.id', 'in',
598
- eb => select_values_of_entity_by_entity_id_or_handle(
599
- eb, 'storefronts_to_other', sf_id_or_handle
600
- )
601
- ), sql_type
602
- ).as('products');
603
- }
604
-
605
- /**
606
- * select as json array collections of a product
607
- *
608
- * @param {import('kysely').ExpressionBuilder<Database>} eb
609
- * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
610
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
611
- */
612
- export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
613
- return jsonArrayFrom(
614
- select_base_from(eb, 'discounts')
615
- .select('discounts.application')
616
- .select('discounts.info')
617
- .select('discounts.priority')
618
- .select('discounts.published')
619
- .select('discounts.title')
620
- .select(eb => [
621
- with_tags(eb, eb.ref('discounts.id'), sql_type),
622
- with_media(eb, eb.ref('discounts.id'), sql_type),
623
- ])
624
- .where('discounts.id', 'in',
625
- eb => select_values_of_entity_by_entity_id_or_handle(
626
- eb, 'storefronts_to_other', sf_id_or_handle
627
- )
628
- ), sql_type
629
- ).as('discounts');
630
- }
631
-
632
- /**
633
- * select as json array collections of a product
634
- *
635
- * @param {import('kysely').ExpressionBuilder<Database>} eb
636
- * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
637
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
638
- */
639
- export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
640
- return jsonArrayFrom(
641
- select_base_from(eb, 'posts')
642
- .select('posts.text')
643
- .select('posts.title')
644
- .select(eb => [
645
- with_tags(eb, eb.ref('posts.id'), sql_type),
646
- with_media(eb, eb.ref('posts.id'), sql_type),
647
- ])
648
- .where('posts.id', 'in',
649
- eb => select_values_of_entity_by_entity_id_or_handle(
650
- eb, 'storefronts_to_other', sf_id_or_handle
651
- )
652
- ), sql_type
653
- ).as('posts');
654
- }
655
-
656
- /**
657
- * select as json array collections of a product
658
- *
659
- * @param {import('kysely').ExpressionBuilder<Database>} eb
660
- * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
661
- * @param {import('../types.public.d.ts').SqlDialectType} sql_type
662
- */
663
- export const storefront_with_shipping = (eb, sf_id_or_handle, sql_type) => {
664
- return jsonArrayFrom(
665
- select_base_from(eb, 'shipping_methods')
666
- .select('shipping_methods.price')
667
- .select('shipping_methods.title')
668
- .select(eb => [
669
- with_tags(eb, eb.ref('shipping_methods.id'), sql_type),
670
- with_media(eb, eb.ref('shipping_methods.id'), sql_type),
671
- ])
672
- .where('shipping_methods.id', 'in',
673
- eb => select_values_of_entity_by_entity_id_or_handle(
674
- eb, 'storefronts_to_other', sf_id_or_handle
675
- )
676
- ), sql_type
677
- ).as('shipping_methods');
678
- }
679
-
680
- /**
681
- * select all the entity values by entity id or handle
682
- *
683
- * @param {import('kysely').ExpressionBuilder<Database>} eb
684
- * @param {EntityTableKeys} entity_junction_table
685
- * @param {string | ExpressionWrapper<Database>} entity_id_or_handle
686
- */
687
- export const select_values_of_entity_by_entity_id_or_handle = (
688
- eb, entity_junction_table, entity_id_or_handle
689
- ) => {
690
- return eb
691
- .selectFrom(entity_junction_table)
692
- .select(`${entity_junction_table}.value`)
693
- .where(eb2 => eb2.or(
694
- [
695
- eb2(`${entity_junction_table}.entity_id`, '=', entity_id_or_handle),
696
- eb2(`${entity_junction_table}.entity_handle`, '=', entity_id_or_handle),
697
- ]
698
- )
699
- )
700
- .orderBy(`${entity_junction_table}.id`);
701
- }
702
-
703
- /**
704
- * select the entity ids which are constrained by value or reporter
705
- *
706
- * @param {import('kysely').ExpressionBuilder<Database>} eb
707
- * @param {EntityTableKeys} entity_junction_table
708
- * @param {string | ExpressionWrapper<Database>} value
709
- * @param {string | ExpressionWrapper<Database>} [reporter]
710
- */
711
- export const select_entity_ids_by_value_or_reporter =
712
- (eb, entity_junction_table, value, reporter=undefined) => {
713
- return eb
714
- .selectFrom(entity_junction_table)
715
- .select(`${entity_junction_table}.entity_id`)
716
- .where(eb2 => eb2.or(
717
- [
718
- eb2(`${entity_junction_table}.value`, '=', value ?? reporter),
719
- eb2(`${entity_junction_table}.reporter`, '=', reporter ?? value),
720
- ]
721
- )
722
- )
723
- .orderBy(`${entity_junction_table}.entity_id`);
724
- }
256
+ // //.executeTakeFirst();
257
+
258
+ // return queries;
259
+ // }
260
+ // }
261
+
262
+ // /**
263
+ // * helper to generate entity values delete
264
+ // *
265
+ // * @param {EntityTableKeys} entity_table_name
266
+ // */
267
+ // export const insert_entity_values_of = (entity_table_name) => {
268
+ // /**
269
+ // *
270
+ // * @param {Kysely<Database>} trx
271
+ // * @param {{value: string, reporter: string}[]} values values of the entity
272
+ // * @param {string} item_id whom the tags belong to
273
+ // * @param {string} [item_handle] whom the tags belong to
274
+ // * @param {string} [context=undefined] the context (another segment technique)
275
+ // */
276
+ // return (trx, values, item_id, item_handle, context=undefined) => {
277
+
278
+ // if(!values?.length)
279
+ // return undefined;
280
+
281
+ // return trx.insertInto(entity_table_name).values(
282
+ // values.map(t => (
283
+ // {
284
+ // entity_id: item_id,
285
+ // entity_handle: item_handle,
286
+ // value: t.value,
287
+ // reporter: t.reporter,
288
+ // context
289
+ // }
290
+ // )
291
+ // )
292
+ // )
293
+ // //.executeTakeFirst();
294
+ // }
295
+ // }
296
+
297
+
298
+ // /**
299
+ // * Delete previous entities by `id/handle` and insert new ones
300
+ // *
301
+ // * @param {EntityTableKeys} entity_table
302
+ // */
303
+ // export const insert_entity_array_values_with_delete_of = (entity_table) => {
304
+ // /**
305
+ // * @param {Kysely<Database>} trx
306
+ // * @param {string[]} values values of the entity
307
+ // * @param {string} item_id entity id
308
+ // * @param {string} [item_handle] entity handle
309
+ // * @param {string} [context] context
310
+ // */
311
+ // return (trx, values, item_id, item_handle, context) => {
312
+ // return insert_entity_array_values_of(entity_table)(
313
+ // trx, values, item_id, item_handle, true, undefined, context
314
+ // )
315
+ // };
316
+ // }
317
+
318
+ // export const insert_tags_of = insert_entity_array_values_with_delete_of('entity_to_tags_projections');
319
+ // export const insert_search_of = insert_entity_array_values_with_delete_of('entity_to_search_terms');
320
+ // export const insert_media_of = insert_entity_array_values_with_delete_of('entity_to_media');
321
+
322
+ // export const delete_tags_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_tags_projections');
323
+ // export const delete_search_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_search_terms');
324
+ // export const delete_media_of = delete_entity_values_of_by_entity_id_or_handle('entity_to_media');
325
+
326
+ // /**
327
+ // * @typedef {import('../index.js').Database} Database
328
+ // */
329
+
330
+
331
+ // /**
332
+ // * @template {keyof Database} T
333
+ // *
334
+ // * @param {Kysely<Database>} trx
335
+ // * @param {T} table_name
336
+ // * @param {import('kysely').InsertObject<Database, T>} item values of the entity
337
+ // *
338
+ // */
339
+ // export const regular_upsert_me = (trx, table_name, item) => {
340
+
341
+ // const queries = [];
342
+
343
+ // // TODO: maybe use only `id`
344
+ // queries.push(
345
+ // trx.deleteFrom(table_name).where(
346
+ // eb => eb.or(
347
+ // [
348
+ // item.id && eb('id', '=', item.id),
349
+ // item.handle && eb('handle', '=', item.handle),
350
+ // ].filter(Boolean)
351
+ // )
352
+ // )
353
+ // //.execute();
354
+ // );
355
+
356
+ // queries.push(
357
+ // trx.insertInto(table_name).values(item)
358
+ // //.executeTakeFirst()
359
+ // );
360
+
361
+ // return queries;
362
+ // }
363
+
364
+
365
+ // /**
366
+ // *
367
+ // * @param {Kysely<Database>} trx
368
+ // * @param {keyof Database} table_name
369
+ // * @param {string} id_or_handle
370
+ // */
371
+ // export const delete_me = (trx, table_name, id_or_handle) => {
372
+ // // console.log('delete ', id_or_handle)
373
+ // return trx.deleteFrom(table_name).where(
374
+ // where_id_or_handle_table(id_or_handle)
375
+ // );
376
+ // //.executeTakeFirst();
377
+ // }
378
+
379
+ // /**
380
+ // *
381
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
382
+ // * @param {string | ExpressionWrapper<Database>} id_or_handle
383
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
384
+ // */
385
+ // export const with_tags = (eb, id_or_handle, sql_type) => {
386
+ // return stringArrayFrom(
387
+ // select_values_of_entity_by_entity_id_or_handle(
388
+ // eb, 'entity_to_tags_projections', id_or_handle
389
+ // ), sql_type
390
+ // ).as('tags');
391
+ // }
392
+
393
+ // /**
394
+ // *
395
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
396
+ // * @param {string | ExpressionWrapper<Database>} id_or_handle
397
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
398
+ // */
399
+ // export const with_search = (eb, id_or_handle, sql_type) => {
400
+ // return stringArrayFrom(
401
+ // select_values_of_entity_by_entity_id_or_handle(
402
+ // eb, 'entity_to_search_terms', id_or_handle
403
+ // ), sql_type
404
+ // ).as('search');
405
+ // }
406
+
407
+ // /**
408
+ // *
409
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
410
+ // * @param {string | ExpressionWrapper<Database>} id_or_handle
411
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
412
+ // */
413
+ // export const with_media = (eb, id_or_handle, sql_type) => {
414
+ // return stringArrayFrom(
415
+ // select_values_of_entity_by_entity_id_or_handle(
416
+ // eb, 'entity_to_media', id_or_handle
417
+ // ), sql_type
418
+ // ).as('media');
419
+ // }
420
+
421
+ // /**
422
+ // * helper to select base attributes
423
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
424
+ // * @param {keyof Database} table
425
+ // * @return {import('kysely').SelectQueryBuilder<Database, table>}
426
+ // */
427
+ // const select_base_from = (eb, table) => {
428
+ // return [
429
+ // 'active', 'attributes', 'created_at', 'updated_at',
430
+ // 'description', 'handle', 'id'
431
+ // ].map(k => `${table}.${k}`).reduce(
432
+ // (p, c) => p.select(c), eb.selectFrom(table)
433
+ // );
434
+ // }
435
+
436
+ // /**
437
+ // * select as json array collections of a product
438
+ // *
439
+ // * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
440
+ // * @param {string | ExpressionWrapper<Database>} product_id_or_handle
441
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
442
+ // */
443
+ // export const products_with_collections = (eb, product_id_or_handle, sql_type) => {
444
+ // return jsonArrayFrom(
445
+ // select_base_from(eb, 'collections')
446
+ // .select('collections.title')
447
+ // .select('collections.published')
448
+ // .select(eb => [
449
+ // with_tags(eb, eb.ref('collections.id'), sql_type),
450
+ // with_media(eb, eb.ref('collections.id'), sql_type),
451
+ // ])
452
+ // .where('collections.id', 'in',
453
+ // eb => select_values_of_entity_by_entity_id_or_handle(
454
+ // eb, 'products_to_collections', product_id_or_handle
455
+ // )
456
+ // ), sql_type
457
+ // ).as('collections');
458
+ // }
459
+
460
+ // /**
461
+ // * select as json array collections of a product
462
+ // *
463
+ // * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
464
+ // * @param {string | ExpressionWrapper<Database>} product_id_or_handle
465
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
466
+ // */
467
+ // export const products_with_discounts = (eb, product_id_or_handle, sql_type) => {
468
+ // return jsonArrayFrom(
469
+ // select_base_from(eb, 'discounts')
470
+ // .select('discounts.title')
471
+ // .select('discounts.published')
472
+ // .select('discounts.application')
473
+ // .select('discounts.info')
474
+ // .select('discounts.priority')
475
+ // .select(eb => [
476
+ // with_tags(eb, eb.ref('discounts.id'), sql_type),
477
+ // with_media(eb, eb.ref('discounts.id'), sql_type),
478
+ // ])
479
+ // .where('discounts.id', 'in',
480
+ // eb => select_values_of_entity_by_entity_id_or_handle(
481
+ // eb, 'products_to_discounts', product_id_or_handle
482
+ // )
483
+ // ), sql_type
484
+ // ).as('discounts');
485
+ // }
486
+
487
+ // /**
488
+ // * select as json array collections of a product
489
+ // *
490
+ // * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
491
+ // * @param {string | ExpressionWrapper<Database>} product_id_or_handle
492
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
493
+ // */
494
+ // export const products_with_variants = (eb, product_id_or_handle, sql_type) => {
495
+ // return jsonArrayFrom(
496
+ // select_base_from(eb, 'products')
497
+ // .select('products.compare_at_price')
498
+ // .select('products.parent_handle')
499
+ // .select('products.parent_id')
500
+ // .select('products.price')
501
+ // .select('products.qty')
502
+ // .select('products.title')
503
+ // .select('products.variant_hint')
504
+ // .select('products.variants_options')
505
+ // .select('products.video')
506
+ // .select(eb => [
507
+ // with_tags(eb, eb.ref('products.id'), sql_type),
508
+ // with_media(eb, eb.ref('products.id'), sql_type),
509
+ // ])
510
+ // .where('products.id', 'in',
511
+ // eb => select_values_of_entity_by_entity_id_or_handle(
512
+ // eb, 'products_to_variants', product_id_or_handle
513
+ // )
514
+ // ), sql_type
515
+ // ).as('variants');
516
+ // }
517
+
518
+ // /**
519
+ // * select as json array collections of a product
520
+ // *
521
+ // * @param {import('kysely').ExpressionBuilder<Database, 'products'>} eb
522
+ // * @param {string | ExpressionWrapper<Database>} product_id_or_handle
523
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
524
+ // */
525
+ // export const products_with_related_products = (eb, product_id_or_handle, sql_type) => {
526
+ // return jsonArrayFrom(
527
+ // select_base_from(eb, 'products')
528
+ // .select('products.compare_at_price')
529
+ // .select('products.parent_handle')
530
+ // .select('products.parent_id')
531
+ // .select('products.price')
532
+ // .select('products.qty')
533
+ // .select('products.title')
534
+ // .select('products.variant_hint')
535
+ // .select('products.variants_options')
536
+ // .select('products.video')
537
+ // .select(eb => [
538
+ // with_tags(eb, eb.ref('products.id'), sql_type),
539
+ // with_media(eb, eb.ref('products.id'), sql_type),
540
+ // ])
541
+ // .where('products.id', 'in',
542
+ // eb => select_values_of_entity_by_entity_id_or_handle(
543
+ // eb, 'products_to_related_products', product_id_or_handle
544
+ // )
545
+ // ), sql_type
546
+ // ).as('related_products');
547
+ // }
548
+
549
+
550
+ // /**
551
+ // * select as json array collections of a product
552
+ // *
553
+ // * @param {import('kysely').ExpressionBuilder<Database, 'storefronts'>} eb
554
+ // * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
555
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
556
+ // */
557
+ // export const storefront_with_collections = (eb, sf_id_or_handle, sql_type) => {
558
+ // return jsonArrayFrom(
559
+ // select_base_from(eb, 'collections')
560
+ // .select('collections.title')
561
+ // .select('collections.published')
562
+ // .select(eb => [
563
+ // with_tags(eb, eb.ref('collections.id'), sql_type),
564
+ // with_media(eb, eb.ref('collections.id'), sql_type),
565
+ // ])
566
+ // .where('collections.id', 'in',
567
+ // eb => select_values_of_entity_by_entity_id_or_handle(
568
+ // eb, 'storefronts_to_other', sf_id_or_handle
569
+ // )
570
+ // ), sql_type
571
+ // ).as('collections');
572
+ // }
573
+
574
+ // /**
575
+ // * select as json array collections of a product
576
+ // *
577
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
578
+ // * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
579
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
580
+ // */
581
+ // export const storefront_with_products = (eb, sf_id_or_handle, sql_type) => {
582
+ // return jsonArrayFrom(
583
+ // select_base_from(eb, 'products')
584
+ // .select('products.title')
585
+ // .select('products.compare_at_price')
586
+ // .select('products.parent_handle')
587
+ // .select('products.parent_id')
588
+ // .select('products.price')
589
+ // .select('products.qty')
590
+ // .select('products.variant_hint')
591
+ // .select('products.variants_options')
592
+ // .select('products.video')
593
+ // .select(eb => [
594
+ // with_tags(eb, eb.ref('products.id'), sql_type),
595
+ // with_media(eb, eb.ref('products.id'), sql_type),
596
+ // ])
597
+ // .where('products.id', 'in',
598
+ // eb => select_values_of_entity_by_entity_id_or_handle(
599
+ // eb, 'storefronts_to_other', sf_id_or_handle
600
+ // )
601
+ // ), sql_type
602
+ // ).as('products');
603
+ // }
604
+
605
+ // /**
606
+ // * select as json array collections of a product
607
+ // *
608
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
609
+ // * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
610
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
611
+ // */
612
+ // export const storefront_with_discounts = (eb, sf_id_or_handle, sql_type) => {
613
+ // return jsonArrayFrom(
614
+ // select_base_from(eb, 'discounts')
615
+ // .select('discounts.application')
616
+ // .select('discounts.info')
617
+ // .select('discounts.priority')
618
+ // .select('discounts.published')
619
+ // .select('discounts.title')
620
+ // .select(eb => [
621
+ // with_tags(eb, eb.ref('discounts.id'), sql_type),
622
+ // with_media(eb, eb.ref('discounts.id'), sql_type),
623
+ // ])
624
+ // .where('discounts.id', 'in',
625
+ // eb => select_values_of_entity_by_entity_id_or_handle(
626
+ // eb, 'storefronts_to_other', sf_id_or_handle
627
+ // )
628
+ // ), sql_type
629
+ // ).as('discounts');
630
+ // }
631
+
632
+ // /**
633
+ // * select as json array collections of a product
634
+ // *
635
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
636
+ // * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
637
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
638
+ // */
639
+ // export const storefront_with_posts = (eb, sf_id_or_handle, sql_type) => {
640
+ // return jsonArrayFrom(
641
+ // select_base_from(eb, 'posts')
642
+ // .select('posts.text')
643
+ // .select('posts.title')
644
+ // .select(eb => [
645
+ // with_tags(eb, eb.ref('posts.id'), sql_type),
646
+ // with_media(eb, eb.ref('posts.id'), sql_type),
647
+ // ])
648
+ // .where('posts.id', 'in',
649
+ // eb => select_values_of_entity_by_entity_id_or_handle(
650
+ // eb, 'storefronts_to_other', sf_id_or_handle
651
+ // )
652
+ // ), sql_type
653
+ // ).as('posts');
654
+ // }
655
+
656
+ // /**
657
+ // * select as json array collections of a product
658
+ // *
659
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
660
+ // * @param {string | ExpressionWrapper<Database>} sf_id_or_handle
661
+ // * @param {import('../types.public.d.ts').SqlDialectType} sql_type
662
+ // */
663
+ // export const storefront_with_shipping = (eb, sf_id_or_handle, sql_type) => {
664
+ // return jsonArrayFrom(
665
+ // select_base_from(eb, 'shipping_methods')
666
+ // .select('shipping_methods.price')
667
+ // .select('shipping_methods.title')
668
+ // .select(eb => [
669
+ // with_tags(eb, eb.ref('shipping_methods.id'), sql_type),
670
+ // with_media(eb, eb.ref('shipping_methods.id'), sql_type),
671
+ // ])
672
+ // .where('shipping_methods.id', 'in',
673
+ // eb => select_values_of_entity_by_entity_id_or_handle(
674
+ // eb, 'storefronts_to_other', sf_id_or_handle
675
+ // )
676
+ // ), sql_type
677
+ // ).as('shipping_methods');
678
+ // }
679
+
680
+ // /**
681
+ // * select all the entity values by entity id or handle
682
+ // *
683
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
684
+ // * @param {EntityTableKeys} entity_junction_table
685
+ // * @param {string | ExpressionWrapper<Database>} entity_id_or_handle
686
+ // */
687
+ // export const select_values_of_entity_by_entity_id_or_handle = (
688
+ // eb, entity_junction_table, entity_id_or_handle
689
+ // ) => {
690
+ // return eb
691
+ // .selectFrom(entity_junction_table)
692
+ // .select(`${entity_junction_table}.value`)
693
+ // .where(eb2 => eb2.or(
694
+ // [
695
+ // eb2(`${entity_junction_table}.entity_id`, '=', entity_id_or_handle),
696
+ // eb2(`${entity_junction_table}.entity_handle`, '=', entity_id_or_handle),
697
+ // ]
698
+ // )
699
+ // )
700
+ // .orderBy(`${entity_junction_table}.id`);
701
+ // }
702
+
703
+ // /**
704
+ // * select the entity ids which are constrained by value or reporter
705
+ // *
706
+ // * @param {import('kysely').ExpressionBuilder<Database>} eb
707
+ // * @param {EntityTableKeys} entity_junction_table
708
+ // * @param {string | ExpressionWrapper<Database>} value
709
+ // * @param {string | ExpressionWrapper<Database>} [reporter]
710
+ // */
711
+ // export const select_entity_ids_by_value_or_reporter =
712
+ // (eb, entity_junction_table, value, reporter=undefined) => {
713
+ // return eb
714
+ // .selectFrom(entity_junction_table)
715
+ // .select(`${entity_junction_table}.entity_id`)
716
+ // .where(eb2 => eb2.or(
717
+ // [
718
+ // eb2(`${entity_junction_table}.value`, '=', value ?? reporter),
719
+ // eb2(`${entity_junction_table}.reporter`, '=', reporter ?? value),
720
+ // ]
721
+ // )
722
+ // )
723
+ // .orderBy(`${entity_junction_table}.entity_id`);
724
+ // }