@storecraft/database-sql-base 1.0.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.
- package/README.md +126 -0
- package/TODO.md +2 -0
- package/db-strategy.md +3 -0
- package/driver.js +190 -0
- package/index.js +3 -0
- package/migrate.js +66 -0
- package/migrations.mssql/00000_init_tables.js +268 -0
- package/migrations.mysql/00000_init_tables.js +372 -0
- package/migrations.mysql/00001_seed_email_templates.js +1 -0
- package/migrations.postgres/00000_init_tables.js +358 -0
- package/migrations.postgres/00001_seed_email_templates.js +1 -0
- package/migrations.shared/00001_seed_email_templates.js +260 -0
- package/migrations.sqlite/00000_init_tables.js +357 -0
- package/migrations.sqlite/00001_seed_email_templates.js +1 -0
- package/package.json +47 -0
- package/src/con.auth_users.js +159 -0
- package/src/con.collections.js +197 -0
- package/src/con.customers.js +202 -0
- package/src/con.discounts.js +225 -0
- package/src/con.discounts.utils.js +180 -0
- package/src/con.helpers.json.js +231 -0
- package/src/con.helpers.json.mssql.js +233 -0
- package/src/con.helpers.json.mysql.js +239 -0
- package/src/con.helpers.json.postgres.js +223 -0
- package/src/con.helpers.json.sqlite.js +263 -0
- package/src/con.images.js +230 -0
- package/src/con.notifications.js +149 -0
- package/src/con.orders.js +156 -0
- package/src/con.posts.js +147 -0
- package/src/con.products.js +497 -0
- package/src/con.search.js +148 -0
- package/src/con.shared.js +616 -0
- package/src/con.shipping.js +147 -0
- package/src/con.storefronts.js +301 -0
- package/src/con.tags.js +120 -0
- package/src/con.templates.js +133 -0
- package/src/kysely.sanitize.plugin.js +40 -0
- package/src/utils.funcs.js +77 -0
- package/src/utils.query.js +195 -0
- package/tests/query.cursor.test.js +389 -0
- package/tests/query.vql.test.js +71 -0
- package/tests/runner.mssql-local.test.js +118 -0
- package/tests/runner.mysql-local.test.js +101 -0
- package/tests/runner.postgres-local.test.js +99 -0
- package/tests/runner.sqlite-local.test.js +99 -0
- package/tests/sandbox.test.js +71 -0
- package/tsconfig.json +21 -0
- package/types.public.d.ts +19 -0
- package/types.sql.tables.d.ts +247 -0
@@ -0,0 +1,147 @@
|
|
1
|
+
import { SQL } from '../driver.js'
|
2
|
+
import { report_document_media } from './con.images.js'
|
3
|
+
import { count_regular, delete_entity_values_by_value_or_reporter,
|
4
|
+
delete_me, delete_media_of, delete_search_of,
|
5
|
+
delete_tags_of, insert_media_of, insert_search_of,
|
6
|
+
insert_tags_of, regular_upsert_me, where_id_or_handle_table,
|
7
|
+
with_media, with_tags } from './con.shared.js'
|
8
|
+
import { sanitize_array } from './utils.funcs.js'
|
9
|
+
import { query_to_eb, query_to_sort } from './utils.query.js'
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @typedef {import('@storecraft/core/v-database').db_shipping} db_col
|
13
|
+
*/
|
14
|
+
export const table_name = 'shipping_methods'
|
15
|
+
|
16
|
+
/**
|
17
|
+
* @param {SQL} driver
|
18
|
+
* @returns {db_col["upsert"]}
|
19
|
+
*/
|
20
|
+
const upsert = (driver) => {
|
21
|
+
return async (item, search_terms) => {
|
22
|
+
const c = driver.client;
|
23
|
+
try {
|
24
|
+
const t = await c.transaction().execute(
|
25
|
+
async (trx) => {
|
26
|
+
await insert_search_of(trx, search_terms, item.id, item.handle, table_name);
|
27
|
+
await insert_media_of(trx, item.media, item.id, item.handle, table_name);
|
28
|
+
await insert_tags_of(trx, item.tags, item.id, item.handle, table_name);
|
29
|
+
await report_document_media(driver)(item, trx);
|
30
|
+
await regular_upsert_me(trx, table_name, {
|
31
|
+
active: item.active ? 1 : 0,
|
32
|
+
attributes: JSON.stringify(item.attributes),
|
33
|
+
description: item.description,
|
34
|
+
created_at: item.created_at,
|
35
|
+
updated_at: item.updated_at,
|
36
|
+
id: item.id,
|
37
|
+
handle: item.handle,
|
38
|
+
title: item.title,
|
39
|
+
price: item.price
|
40
|
+
});
|
41
|
+
}
|
42
|
+
);
|
43
|
+
} catch(e) {
|
44
|
+
console.log(e);
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
return true;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
/**
|
53
|
+
* @param {SQL} driver
|
54
|
+
* @returns {db_col["get"]}
|
55
|
+
*/
|
56
|
+
const get = (driver) => {
|
57
|
+
return (id_or_handle, options) => {
|
58
|
+
return driver.client
|
59
|
+
.selectFrom(table_name)
|
60
|
+
.selectAll()
|
61
|
+
.select(eb => [
|
62
|
+
with_media(eb, id_or_handle, driver.dialectType),
|
63
|
+
with_tags(eb, id_or_handle, driver.dialectType),
|
64
|
+
]
|
65
|
+
.filter(Boolean))
|
66
|
+
.where(where_id_or_handle_table(id_or_handle))
|
67
|
+
.executeTakeFirst();
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
/**
|
73
|
+
* @param {SQL} driver
|
74
|
+
* @returns {db_col["remove"]}
|
75
|
+
*/
|
76
|
+
const remove = (driver) => {
|
77
|
+
return async (id_or_handle) => {
|
78
|
+
try {
|
79
|
+
await driver.client.transaction().execute(
|
80
|
+
async (trx) => {
|
81
|
+
|
82
|
+
// entities
|
83
|
+
await delete_search_of(trx, id_or_handle);
|
84
|
+
await delete_media_of(trx, id_or_handle);
|
85
|
+
await delete_tags_of(trx, id_or_handle);
|
86
|
+
// STOREFRONT => SHIPPING
|
87
|
+
await delete_entity_values_by_value_or_reporter('storefronts_to_other')(
|
88
|
+
trx, id_or_handle, id_or_handle
|
89
|
+
);
|
90
|
+
// delete me
|
91
|
+
await delete_me(trx, table_name, id_or_handle);
|
92
|
+
}
|
93
|
+
);
|
94
|
+
|
95
|
+
} catch(e) {
|
96
|
+
console.log(e);
|
97
|
+
return false;
|
98
|
+
}
|
99
|
+
return true;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @param {SQL} driver
|
106
|
+
* @returns {db_col["list"]}
|
107
|
+
*/
|
108
|
+
const list = (driver) => {
|
109
|
+
return async (query) => {
|
110
|
+
|
111
|
+
const items = await driver.client
|
112
|
+
.selectFrom(table_name)
|
113
|
+
.selectAll()
|
114
|
+
.select(eb => [
|
115
|
+
with_media(eb, eb.ref('shipping_methods.id'), driver.dialectType),
|
116
|
+
with_tags(eb, eb.ref('shipping_methods.id'), driver.dialectType),
|
117
|
+
].filter(Boolean))
|
118
|
+
.where(
|
119
|
+
(eb) => {
|
120
|
+
return query_to_eb(eb, query, table_name);
|
121
|
+
}
|
122
|
+
)
|
123
|
+
.orderBy(query_to_sort(query))
|
124
|
+
.limit(query.limitToLast ?? query.limit ?? 10)
|
125
|
+
.execute();
|
126
|
+
|
127
|
+
if(query.limitToLast) items.reverse();
|
128
|
+
|
129
|
+
return sanitize_array(items);
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
/**
|
135
|
+
* @param {SQL} driver
|
136
|
+
* @return {db_col}}
|
137
|
+
* */
|
138
|
+
export const impl = (driver) => {
|
139
|
+
|
140
|
+
return {
|
141
|
+
get: get(driver),
|
142
|
+
upsert: upsert(driver),
|
143
|
+
remove: remove(driver),
|
144
|
+
list: list(driver),
|
145
|
+
count: count_regular(driver, table_name),
|
146
|
+
}
|
147
|
+
}
|
@@ -0,0 +1,301 @@
|
|
1
|
+
import { SQL } from '../driver.js'
|
2
|
+
import { report_document_media } from './con.images.js'
|
3
|
+
import { delete_entity_values_of_by_entity_id_or_handle,
|
4
|
+
delete_me, delete_media_of, delete_search_of,
|
5
|
+
delete_tags_of, insert_entity_values_of, insert_media_of,
|
6
|
+
insert_search_of, insert_tags_of, storefront_with_collections,
|
7
|
+
storefront_with_discounts, storefront_with_posts,
|
8
|
+
storefront_with_products, storefront_with_shipping,
|
9
|
+
regular_upsert_me, where_id_or_handle_table,
|
10
|
+
with_media, with_tags,
|
11
|
+
count_regular} from './con.shared.js'
|
12
|
+
import { sanitize_array } from './utils.funcs.js'
|
13
|
+
import { query_to_eb, query_to_sort } from './utils.query.js'
|
14
|
+
|
15
|
+
/**
|
16
|
+
* @typedef {import('@storecraft/core/v-database').db_storefronts} db_col
|
17
|
+
*/
|
18
|
+
export const table_name = 'storefronts'
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @param {SQL} driver
|
22
|
+
* @returns {db_col["upsert"]}
|
23
|
+
*/
|
24
|
+
const upsert = (driver) => {
|
25
|
+
return async (item, search_terms) => {
|
26
|
+
const c = driver.client;
|
27
|
+
try {
|
28
|
+
const t = await c.transaction().execute(
|
29
|
+
async (trx) => {
|
30
|
+
// regular entities
|
31
|
+
await insert_search_of(trx, search_terms, item.id, item.handle, table_name);
|
32
|
+
await insert_media_of(trx, item.media, item.id, item.handle, table_name);
|
33
|
+
await insert_tags_of(trx, item.tags, item.id, item.handle, table_name);
|
34
|
+
await report_document_media(driver)(item, trx);
|
35
|
+
// Explicit STOREFRONTS => PRODUCTS / COLLECTIONS / DISCOUNTS / SHIPPING / POSTS
|
36
|
+
// remove all the past connections of this storefront at once
|
37
|
+
await delete_entity_values_of_by_entity_id_or_handle('storefronts_to_other')(
|
38
|
+
trx, item.id, item.handle
|
39
|
+
);
|
40
|
+
if(item.collections) { // add this storefront's new collections connections
|
41
|
+
await insert_entity_values_of('storefronts_to_other')(
|
42
|
+
trx, item.collections.map(c => ({ value: c.id, reporter: c.handle})),
|
43
|
+
item.id, item.handle, 'collections'
|
44
|
+
);
|
45
|
+
}
|
46
|
+
if(item.products) { // add this storefront's new products connections
|
47
|
+
await insert_entity_values_of('storefronts_to_other')(
|
48
|
+
trx, item.products.map(c => ({ value: c.id, reporter: c.handle})),
|
49
|
+
item.id, item.handle, 'products'
|
50
|
+
);
|
51
|
+
}
|
52
|
+
if(item.discounts) { // add this storefront's new discounts connections
|
53
|
+
await insert_entity_values_of('storefronts_to_other')(
|
54
|
+
trx, item.discounts.map(c => ({ value: c.id, reporter: c.handle})),
|
55
|
+
item.id, item.handle, 'discounts'
|
56
|
+
);
|
57
|
+
}
|
58
|
+
if(item.posts) { // add this storefront's new posts connections
|
59
|
+
await insert_entity_values_of('storefronts_to_other')(
|
60
|
+
trx, item.posts.map(c => ({ value: c.id, reporter: c.handle})),
|
61
|
+
item.id, item.handle, 'posts'
|
62
|
+
);
|
63
|
+
}
|
64
|
+
if(item.shipping_methods) { // add this storefront's new shipping_methods connections
|
65
|
+
await insert_entity_values_of('storefronts_to_other')(
|
66
|
+
trx, item.shipping_methods.map(c => ({ value: c.id, reporter: c.handle})),
|
67
|
+
item.id, item.handle, 'shipping_methods'
|
68
|
+
);
|
69
|
+
}
|
70
|
+
|
71
|
+
// upsert me
|
72
|
+
await regular_upsert_me(trx, table_name, {
|
73
|
+
active: item.active ? 1 : 0,
|
74
|
+
attributes: JSON.stringify(item.attributes),
|
75
|
+
description: item.description,
|
76
|
+
created_at: item.created_at,
|
77
|
+
updated_at: item.updated_at,
|
78
|
+
id: item.id,
|
79
|
+
handle: item.handle,
|
80
|
+
published: item.published,
|
81
|
+
title: item.title,
|
82
|
+
video: item.video,
|
83
|
+
});
|
84
|
+
}
|
85
|
+
);
|
86
|
+
} catch(e) {
|
87
|
+
console.log(e);
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
return true;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @param {SQL} driver
|
97
|
+
* @returns {db_col["get"]}
|
98
|
+
*/
|
99
|
+
const get = (driver) => {
|
100
|
+
return (id_or_handle, options) => {
|
101
|
+
const expand = options?.expand ?? ['*'];
|
102
|
+
const expand_all = expand.includes('*');
|
103
|
+
const expand_collections = expand_all || expand.includes('collections');
|
104
|
+
const expand_products = expand_all || expand.includes('products');
|
105
|
+
const expand_discounts = expand_all || expand.includes('discounts');
|
106
|
+
const expand_shipping = expand_all || expand.includes('shipping_methods');
|
107
|
+
const expand_posts = expand_all || expand.includes('posts');
|
108
|
+
|
109
|
+
return driver.client
|
110
|
+
.selectFrom(table_name)
|
111
|
+
.selectAll()
|
112
|
+
.select(eb => [
|
113
|
+
with_media(eb, id_or_handle, driver.dialectType),
|
114
|
+
with_tags(eb, id_or_handle, driver.dialectType),
|
115
|
+
expand_collections && storefront_with_collections(eb, id_or_handle, driver.dialectType),
|
116
|
+
expand_products && storefront_with_products(eb, id_or_handle, driver.dialectType),
|
117
|
+
expand_discounts && storefront_with_discounts(eb, id_or_handle, driver.dialectType),
|
118
|
+
expand_shipping && storefront_with_shipping(eb, id_or_handle, driver.dialectType),
|
119
|
+
expand_posts && storefront_with_posts(eb, id_or_handle, driver.dialectType),
|
120
|
+
]
|
121
|
+
.filter(Boolean))
|
122
|
+
.where(where_id_or_handle_table(id_or_handle))
|
123
|
+
.executeTakeFirst();
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
/**
|
129
|
+
* @param {SQL} driver
|
130
|
+
* @returns {db_col["remove"]}
|
131
|
+
*/
|
132
|
+
const remove = (driver) => {
|
133
|
+
return async (id_or_handle) => {
|
134
|
+
try {
|
135
|
+
await driver.client.transaction().execute(
|
136
|
+
async (trx) => {
|
137
|
+
|
138
|
+
// entities
|
139
|
+
await delete_search_of(trx, id_or_handle);
|
140
|
+
await delete_tags_of(trx, id_or_handle);
|
141
|
+
await delete_media_of(trx, id_or_handle);
|
142
|
+
// delete storefront => other
|
143
|
+
await delete_entity_values_of_by_entity_id_or_handle('storefronts_to_other')(
|
144
|
+
trx, id_or_handle, id_or_handle
|
145
|
+
);
|
146
|
+
// delete me
|
147
|
+
await delete_me(trx, table_name, id_or_handle);
|
148
|
+
}
|
149
|
+
);
|
150
|
+
|
151
|
+
} catch(e) {
|
152
|
+
console.log(e);
|
153
|
+
return false;
|
154
|
+
}
|
155
|
+
return true;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
/**
|
161
|
+
* @param {SQL} driver
|
162
|
+
* @returns {db_col["list"]}
|
163
|
+
*/
|
164
|
+
const list = (driver) => {
|
165
|
+
return async (query) => {
|
166
|
+
const expand = query?.expand ?? ['*'];
|
167
|
+
const expand_all = expand.includes('*');
|
168
|
+
const expand_collections = expand_all || expand.includes('collections');
|
169
|
+
const expand_products = expand_all || expand.includes('products');
|
170
|
+
const expand_discounts = expand_all || expand.includes('discounts');
|
171
|
+
const expand_shipping = expand_all || expand.includes('shipping_methods');
|
172
|
+
const expand_posts = expand_all || expand.includes('posts');
|
173
|
+
|
174
|
+
const items = await driver.client
|
175
|
+
.selectFrom(table_name)
|
176
|
+
.selectAll()
|
177
|
+
.select(eb => [
|
178
|
+
with_media(eb, eb.ref('storefronts.id'), driver.dialectType),
|
179
|
+
with_tags(eb, eb.ref('storefronts.id'), driver.dialectType),
|
180
|
+
expand_collections && storefront_with_collections(eb, eb.ref('storefronts.id'), driver.dialectType),
|
181
|
+
expand_products && storefront_with_products(eb, eb.ref('storefronts.id'), driver.dialectType),
|
182
|
+
expand_discounts && storefront_with_discounts(eb, eb.ref('storefronts.id'), driver.dialectType),
|
183
|
+
expand_shipping && storefront_with_shipping(eb, eb.ref('storefronts.id'), driver.dialectType),
|
184
|
+
expand_posts && storefront_with_posts(eb, eb.ref('storefronts.id'), driver.dialectType),
|
185
|
+
].filter(Boolean)
|
186
|
+
)
|
187
|
+
.where(
|
188
|
+
(eb) => {
|
189
|
+
return query_to_eb(eb, query, table_name);
|
190
|
+
}
|
191
|
+
)
|
192
|
+
.orderBy(query_to_sort(query))
|
193
|
+
.limit(query.limitToLast ?? query.limit ?? 10)
|
194
|
+
.execute();
|
195
|
+
|
196
|
+
if(query.limitToLast) items.reverse();
|
197
|
+
|
198
|
+
return sanitize_array(items);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
/**
|
203
|
+
* @param {SQL} driver
|
204
|
+
* @returns {db_col["list_storefront_products"]}
|
205
|
+
*/
|
206
|
+
const list_storefront_products = (driver) => {
|
207
|
+
return async (product_id_or_handle) => {
|
208
|
+
// because we load everything (we don't expect storefronts to
|
209
|
+
// promote so many products), therefore we use the simple `get`
|
210
|
+
// method instead of a query
|
211
|
+
const item = await get(driver)(
|
212
|
+
product_id_or_handle, { expand: ['products'] }
|
213
|
+
);
|
214
|
+
return item?.products ?? []
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
/**
|
219
|
+
* @param {SQL} driver
|
220
|
+
* @returns {db_col["list_storefront_collections"]}
|
221
|
+
*/
|
222
|
+
const list_storefront_collections = (driver) => {
|
223
|
+
return async (product_id_or_handle) => {
|
224
|
+
// because we load everything (we don't expect storefronts to
|
225
|
+
// promote so many products), therefore we use the simple `get`
|
226
|
+
// method instead of a query
|
227
|
+
const item = await get(driver)(
|
228
|
+
product_id_or_handle, { expand: ['collections'] }
|
229
|
+
);
|
230
|
+
return item?.collections ?? []
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
/**
|
235
|
+
* @param {SQL} driver
|
236
|
+
* @returns {db_col["list_storefront_discounts"]}
|
237
|
+
*/
|
238
|
+
const list_storefront_discounts = (driver) => {
|
239
|
+
return async (product_id_or_handle) => {
|
240
|
+
// because we load everything (we don't expect storefronts to
|
241
|
+
// promote so many products), therefore we use the simple `get`
|
242
|
+
// method instead of a query
|
243
|
+
const item = await get(driver)(
|
244
|
+
product_id_or_handle, { expand: ['discounts'] }
|
245
|
+
);
|
246
|
+
return item?.discounts ?? []
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
/**
|
251
|
+
* @param {SQL} driver
|
252
|
+
* @returns {db_col["list_storefront_posts"]}
|
253
|
+
*/
|
254
|
+
const list_storefront_posts = (driver) => {
|
255
|
+
return async (product_id_or_handle) => {
|
256
|
+
// because we load everything (we don't expect storefronts to
|
257
|
+
// promote so many products), therefore we use the simple `get`
|
258
|
+
// method instead of a query
|
259
|
+
const item = await get(driver)(
|
260
|
+
product_id_or_handle, { expand: ['posts'] }
|
261
|
+
);
|
262
|
+
return item?.posts ?? []
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
/**
|
267
|
+
* @param {SQL} driver
|
268
|
+
* @returns {db_col["list_storefront_shipping_methods"]}
|
269
|
+
*/
|
270
|
+
const list_storefront_shipping_methods = (driver) => {
|
271
|
+
return async (product_id_or_handle) => {
|
272
|
+
// because we load everything (we don't expect storefronts to
|
273
|
+
// promote so many products), therefore we use the simple `get`
|
274
|
+
// method instead of a query
|
275
|
+
const item = await get(driver)(
|
276
|
+
product_id_or_handle, { expand: ['shipping_methods'] }
|
277
|
+
);
|
278
|
+
return item?.shipping_methods ?? []
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
/**
|
283
|
+
* @param {SQL} driver
|
284
|
+
* @return {db_col}}
|
285
|
+
* */
|
286
|
+
export const impl = (driver) => {
|
287
|
+
|
288
|
+
return {
|
289
|
+
get: get(driver),
|
290
|
+
upsert: upsert(driver),
|
291
|
+
remove: remove(driver),
|
292
|
+
list: list(driver),
|
293
|
+
list_storefront_products: list_storefront_products(driver),
|
294
|
+
list_storefront_collections: list_storefront_collections(driver),
|
295
|
+
list_storefront_discounts: list_storefront_discounts(driver),
|
296
|
+
list_storefront_posts: list_storefront_posts(driver),
|
297
|
+
list_storefront_shipping_methods: list_storefront_shipping_methods(driver),
|
298
|
+
count: count_regular(driver, table_name),
|
299
|
+
}
|
300
|
+
}
|
301
|
+
|
package/src/con.tags.js
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
import { SQL } from '../driver.js'
|
2
|
+
import { count_regular, delete_me, delete_search_of, insert_search_of,
|
3
|
+
regular_upsert_me, where_id_or_handle_table } from './con.shared.js'
|
4
|
+
import { sanitize_array } from './utils.funcs.js'
|
5
|
+
import { query_to_eb, query_to_sort } from './utils.query.js'
|
6
|
+
|
7
|
+
/**
|
8
|
+
* @typedef {import('@storecraft/core/v-database').db_tags} db_col
|
9
|
+
*/
|
10
|
+
export const table_name = 'tags'
|
11
|
+
|
12
|
+
/**
|
13
|
+
* @param {SQL} driver
|
14
|
+
* @returns {db_col["upsert"]}
|
15
|
+
*/
|
16
|
+
const upsert = (driver) => {
|
17
|
+
return async (item, search_terms) => {
|
18
|
+
const c = driver.client;
|
19
|
+
try {
|
20
|
+
const t = await c.transaction().execute(
|
21
|
+
async (trx) => {
|
22
|
+
await insert_search_of(trx, search_terms, item.id, item.handle, table_name);
|
23
|
+
await regular_upsert_me(trx, table_name, {
|
24
|
+
created_at: item.created_at,
|
25
|
+
updated_at: item.updated_at,
|
26
|
+
id: item.id,
|
27
|
+
handle: item.handle,
|
28
|
+
values: JSON.stringify(item.values)
|
29
|
+
});
|
30
|
+
}
|
31
|
+
);
|
32
|
+
} catch(e) {
|
33
|
+
console.log(e);
|
34
|
+
return false;
|
35
|
+
}
|
36
|
+
return true;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
/**
|
42
|
+
* @param {SQL} driver
|
43
|
+
* @returns {db_col["get"]}
|
44
|
+
*/
|
45
|
+
const get = (driver) => {
|
46
|
+
return (id_or_handle, options) => {
|
47
|
+
return driver.client
|
48
|
+
.selectFrom(table_name)
|
49
|
+
.selectAll()
|
50
|
+
.where(where_id_or_handle_table(id_or_handle))
|
51
|
+
.executeTakeFirst();
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
/**
|
57
|
+
* @param {SQL} driver
|
58
|
+
* @returns {db_col["remove"]}
|
59
|
+
*/
|
60
|
+
const remove = (driver) => {
|
61
|
+
return async (id_or_handle) => {
|
62
|
+
try {
|
63
|
+
await driver.client.transaction().execute(
|
64
|
+
async (trx) => {
|
65
|
+
|
66
|
+
// entities
|
67
|
+
await delete_search_of(trx, id_or_handle);
|
68
|
+
// delete me
|
69
|
+
await delete_me(trx, table_name, id_or_handle);
|
70
|
+
}
|
71
|
+
);
|
72
|
+
|
73
|
+
} catch(e) {
|
74
|
+
console.log(e);
|
75
|
+
return false;
|
76
|
+
}
|
77
|
+
return true;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
/**
|
83
|
+
* @param {SQL} driver
|
84
|
+
* @returns {db_col["list"]}
|
85
|
+
*/
|
86
|
+
const list = (driver) => {
|
87
|
+
return async (query) => {
|
88
|
+
|
89
|
+
const items = await driver.client
|
90
|
+
.selectFrom(table_name)
|
91
|
+
.selectAll()
|
92
|
+
.where(
|
93
|
+
(eb) => {
|
94
|
+
return query_to_eb(eb, query, table_name);
|
95
|
+
}
|
96
|
+
)
|
97
|
+
.orderBy(query_to_sort(query))
|
98
|
+
.limit(query.limitToLast ?? query.limit ?? 10)
|
99
|
+
.execute();
|
100
|
+
|
101
|
+
if(query.limitToLast) items.reverse();
|
102
|
+
return sanitize_array(items);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
|
107
|
+
/**
|
108
|
+
* @param {SQL} driver
|
109
|
+
* @return {db_col}}
|
110
|
+
* */
|
111
|
+
export const impl = (driver) => {
|
112
|
+
|
113
|
+
return {
|
114
|
+
get: get(driver),
|
115
|
+
upsert: upsert(driver),
|
116
|
+
remove: remove(driver),
|
117
|
+
list: list(driver),
|
118
|
+
count: count_regular(driver, table_name),
|
119
|
+
}
|
120
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
import { Kysely } from 'kysely'
|
2
|
+
import { SQL } from '../driver.js'
|
3
|
+
import { count_regular, delete_me, delete_search_of, insert_search_of,
|
4
|
+
regular_upsert_me, where_id_or_handle_table } from './con.shared.js'
|
5
|
+
import { sanitize_array } from './utils.funcs.js'
|
6
|
+
import { query_to_eb, query_to_sort } from './utils.query.js'
|
7
|
+
|
8
|
+
/**
|
9
|
+
* @typedef {import('@storecraft/core/v-database').db_templates} db_col
|
10
|
+
*/
|
11
|
+
export const table_name = 'templates'
|
12
|
+
|
13
|
+
|
14
|
+
/**
|
15
|
+
* @param {Kysely<import('../index.js').Database>} client
|
16
|
+
*
|
17
|
+
*
|
18
|
+
* @returns {db_col["upsert"]}
|
19
|
+
*/
|
20
|
+
export const upsert = (client) => {
|
21
|
+
return async (item, search_terms) => {
|
22
|
+
try {
|
23
|
+
const t = await client.transaction().execute(
|
24
|
+
async (trx) => {
|
25
|
+
await insert_search_of(trx, search_terms, item.id, item.handle, table_name);
|
26
|
+
await regular_upsert_me(trx, table_name, {
|
27
|
+
created_at: item.created_at,
|
28
|
+
updated_at: item.updated_at,
|
29
|
+
id: item.id,
|
30
|
+
title: item.title,
|
31
|
+
handle: item.handle,
|
32
|
+
template_html: item.template_html,
|
33
|
+
template_text: item.template_text,
|
34
|
+
reference_example_input: JSON.stringify(item.reference_example_input ?? {})
|
35
|
+
});
|
36
|
+
}
|
37
|
+
);
|
38
|
+
} catch(e) {
|
39
|
+
console.log(e);
|
40
|
+
return false;
|
41
|
+
}
|
42
|
+
return true;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
|
47
|
+
/**
|
48
|
+
* @param {SQL} driver
|
49
|
+
*
|
50
|
+
*
|
51
|
+
* @returns {db_col["get"]}
|
52
|
+
*/
|
53
|
+
const get = (driver) => {
|
54
|
+
return (id_or_handle, options) => {
|
55
|
+
return driver.client
|
56
|
+
.selectFrom(table_name)
|
57
|
+
.selectAll()
|
58
|
+
.where(where_id_or_handle_table(id_or_handle))
|
59
|
+
.executeTakeFirst();
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
/**
|
65
|
+
* @param {SQL} driver
|
66
|
+
*
|
67
|
+
*
|
68
|
+
* @returns {db_col["remove"]}
|
69
|
+
*/
|
70
|
+
const remove = (driver) => {
|
71
|
+
return async (id_or_handle) => {
|
72
|
+
try {
|
73
|
+
await driver.client.transaction().execute(
|
74
|
+
async (trx) => {
|
75
|
+
|
76
|
+
// entities
|
77
|
+
await delete_search_of(trx, id_or_handle);
|
78
|
+
// delete me
|
79
|
+
await delete_me(trx, table_name, id_or_handle);
|
80
|
+
}
|
81
|
+
);
|
82
|
+
|
83
|
+
} catch(e) {
|
84
|
+
console.log(e);
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
return true;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
/**
|
93
|
+
* @param {SQL} driver
|
94
|
+
* @returns {db_col["list"]}
|
95
|
+
*/
|
96
|
+
const list = (driver) => {
|
97
|
+
return async (query) => {
|
98
|
+
|
99
|
+
const items = await driver.client
|
100
|
+
.selectFrom(table_name)
|
101
|
+
.selectAll()
|
102
|
+
.where(
|
103
|
+
(eb) => {
|
104
|
+
return query_to_eb(eb, query, table_name);
|
105
|
+
}
|
106
|
+
)
|
107
|
+
.orderBy(query_to_sort(query))
|
108
|
+
.limit(query.limitToLast ?? query.limit ?? 10)
|
109
|
+
.execute();
|
110
|
+
|
111
|
+
if(query.limitToLast) items.reverse();
|
112
|
+
|
113
|
+
return sanitize_array(items);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
/**
|
119
|
+
* @param {SQL} driver
|
120
|
+
*
|
121
|
+
*
|
122
|
+
* @return {db_col}}
|
123
|
+
*/
|
124
|
+
export const impl = (driver) => {
|
125
|
+
|
126
|
+
return {
|
127
|
+
get: get(driver),
|
128
|
+
upsert: upsert(driver.client),
|
129
|
+
remove: remove(driver),
|
130
|
+
list: list(driver),
|
131
|
+
count: count_regular(driver, table_name),
|
132
|
+
}
|
133
|
+
}
|