@storecraft/database-sqlite 1.0.1 → 1.0.3

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,202 +0,0 @@
1
- import { SQL } from '../index.js'
2
- import { report_document_media } from './con.images.js'
3
- import { count_regular, delete_me, delete_media_of, delete_search_of,
4
- delete_tags_of, insert_media_of, insert_search_of,
5
- insert_tags_of, regular_upsert_me, where_id_or_handle_table,
6
- with_media, with_tags} from './con.shared.js'
7
- import { sanitize_array } from './utils.funcs.js'
8
- import { query_to_eb, query_to_sort } from './utils.query.js'
9
-
10
- /**
11
- * @typedef {import('@storecraft/core/v-database').db_customers} db_col
12
- */
13
- export const table_name = 'customers'
14
-
15
- /**
16
- * @param {SQL} driver
17
- * @returns {db_col["upsert"]}
18
- */
19
- const upsert = (driver) => {
20
- return async (item, search_terms) => {
21
- const c = driver.client;
22
- try {
23
- const t = await c.transaction().execute(
24
- async (trx) => {
25
- await insert_search_of(trx, search_terms, item.id, item.id, table_name);
26
- await insert_media_of(trx, item.media, item.id, item.id, table_name);
27
- await insert_tags_of(trx, item.tags, item.id, item.id, table_name);
28
- await report_document_media(driver)(item, trx);
29
- await regular_upsert_me(trx, table_name, {
30
- active: item.active ? 1 : 0,
31
- attributes: JSON.stringify(item.attributes),
32
- description: item.description,
33
- created_at: item.created_at,
34
- updated_at: item.updated_at,
35
- handle: item.email,
36
- id: item.id,
37
- email: item.email,
38
- address: JSON.stringify(item.address),
39
- auth_id: item.auth_id,
40
- firstname: item.firstname,
41
- lastname: item.lastname,
42
- phone_number: item.phone_number
43
- });
44
- }
45
- );
46
- } catch(e) {
47
- console.log(e);
48
- return false;
49
- }
50
- return true;
51
- }
52
- }
53
-
54
-
55
- /**
56
- * @param {SQL} driver
57
- * @returns {db_col["get"]}
58
- */
59
- const get = (driver) => {
60
- return (id, options) => {
61
- return driver.client
62
- .selectFrom(table_name)
63
- .selectAll()
64
- .select(eb => [
65
- with_media(eb, id, driver.dialectType),
66
- with_tags(eb, id, driver.dialectType),
67
- ])
68
- .where(where_id_or_handle_table(id))
69
- .executeTakeFirst();
70
- }
71
- }
72
-
73
- /**
74
- * @param {SQL} driver
75
- * @returns {db_col["getByEmail"]}
76
- */
77
- const getByEmail = (driver) => {
78
- return async (email) => {
79
- return get(driver)(email);
80
- }
81
- }
82
-
83
-
84
- /**
85
- * @param {SQL} driver
86
- * @returns {db_col["remove"]}
87
- */
88
- const remove = (driver) => {
89
- return async (id) => {
90
- try {
91
- await driver.client.transaction().execute(
92
- async (trx) => {
93
-
94
- const valid_auth_id = `au_${id.split('_').at(-1)}`
95
- // entities
96
- await delete_search_of(trx, id);
97
- await delete_media_of(trx, id);
98
- await delete_tags_of(trx, id);
99
-
100
- // delete related auth user
101
- await trx
102
- .deleteFrom('auth_users')
103
- .where('auth_users.id', '=', valid_auth_id)
104
- .executeTakeFirst();
105
-
106
- // delete me
107
- await delete_me(trx, table_name, id);
108
- }
109
- );
110
- } catch(e) {
111
- console.log(e);
112
- return false;
113
- }
114
- return true;
115
- }
116
- }
117
-
118
-
119
- /**
120
- * @param {SQL} driver
121
- * @returns {db_col["list"]}
122
- */
123
- const list = (driver) => {
124
- return async (query) => {
125
-
126
- const items = await driver.client
127
- .selectFrom(table_name)
128
- .selectAll()
129
- .select(eb => [
130
- with_media(eb, eb.ref('customers.id'), driver.dialectType),
131
- with_tags(eb, eb.ref('customers.id'), driver.dialectType),
132
- ].filter(Boolean))
133
- .where(
134
- (eb) => {
135
- return query_to_eb(eb, query, table_name);
136
- }
137
- )
138
- .orderBy(query_to_sort(query))
139
- .limit(query.limitToLast ?? query.limit ?? 10)
140
- .execute();
141
-
142
- if(query.limitToLast) items.reverse();
143
-
144
- return sanitize_array(items);
145
- }
146
- }
147
-
148
- /**
149
- * @param {SQL} driver
150
- *
151
- *
152
- * @returns {db_col["list_customer_orders"]}
153
- */
154
- const list_customer_orders = (driver) => {
155
- return async (id, query) => {
156
-
157
- const items = await driver.client
158
- .selectFrom('orders')
159
- .selectAll()
160
- .select(eb => [
161
- with_media(eb, eb.ref('orders.id'), driver.dialectType),
162
- with_tags(eb, eb.ref('orders.id'), driver.dialectType),
163
- ])
164
- .where(
165
- (eb) => eb.and(
166
- [
167
- query_to_eb(eb, query, table_name),
168
- eb.or(
169
- [
170
- eb('_customer_id', '=', id),
171
- eb('_customer_email', '=', id),
172
- ]
173
- )
174
- ].filter(Boolean)
175
- )
176
- )
177
- .orderBy(query_to_sort(query))
178
- .limit(query.limitToLast ?? query.limit ?? 10)
179
- .execute();
180
-
181
- if(query.limitToLast) items.reverse();
182
-
183
- return sanitize_array(items);
184
- }
185
- }
186
-
187
- /**
188
- * @param {SQL} driver
189
- * @return {db_col}}
190
- * */
191
- export const impl = (driver) => {
192
-
193
- return {
194
- get: get(driver),
195
- getByEmail: getByEmail(driver),
196
- upsert: upsert(driver),
197
- remove: remove(driver),
198
- list: list(driver),
199
- list_customer_orders: list_customer_orders(driver),
200
- count: count_regular(driver, table_name),
201
- }
202
- }
@@ -1,225 +0,0 @@
1
- import { enums } from '@storecraft/core/v-api'
2
- import { SQL } from '../index.js'
3
- import { discount_to_conjunctions } from './con.discounts.utils.js'
4
- import { delete_entity_values_by_value_or_reporter,
5
- delete_me, delete_media_of, delete_search_of,
6
- delete_tags_of, insert_media_of, insert_search_of,
7
- insert_tags_of, select_entity_ids_by_value_or_reporter, regular_upsert_me, where_id_or_handle_table,
8
- with_media, with_tags,
9
- count_regular} from './con.shared.js'
10
- import { sanitize_array } from './utils.funcs.js'
11
- import { query_to_eb, query_to_sort } from './utils.query.js'
12
- import { report_document_media } from './con.images.js'
13
-
14
- /**
15
- * @typedef {import('@storecraft/core/v-database').db_discounts} db_col
16
- */
17
- export const table_name = 'discounts'
18
-
19
- /**
20
- * @param {SQL} driver
21
- * @returns {db_col["upsert"]}
22
- */
23
- const upsert = (driver) => {
24
- return async (item, search_terms) => {
25
- const c = driver.client;
26
- try {
27
- const t = await c.transaction().execute(
28
- async (trx) => {
29
- /// ENTITIES
30
- await insert_search_of(trx, search_terms, item.id, item.handle, table_name);
31
- await insert_media_of(trx, item.media, item.id, item.handle, table_name);
32
- await insert_tags_of(trx, item.tags, item.id, item.handle, table_name);
33
- await report_document_media(driver)(item, trx);
34
- //
35
- // PRODUCTS => DISCOUNTS
36
- //
37
- // remove all products relation to this discount
38
- await delete_entity_values_by_value_or_reporter('products_to_discounts')(
39
- trx, item.id, item.handle);
40
- if(item.active && item.application.id===enums.DiscountApplicationEnum.Auto.id) {
41
- // make connections
42
- await trx
43
- .insertInto('products_to_discounts')
44
- .columns(['entity_handle', 'entity_id', 'value', 'reporter'])
45
- .expression(eb =>
46
- eb.selectFrom('products')
47
- .select(eb => [
48
- 'handle as entity_handle',
49
- 'id as entity_id',
50
- eb.val(item.id).as('value'),
51
- eb.val(item.handle).as('reporter')
52
- ]
53
- )
54
- .where(
55
- eb => eb.and(discount_to_conjunctions(eb, item))
56
- )
57
- ).execute();
58
-
59
- }
60
-
61
- ///
62
- /// SAVE ME
63
- ///
64
- await regular_upsert_me(trx, table_name, {
65
- active: item.active ? 1 : 0,
66
- attributes: JSON.stringify(item.attributes),
67
- description: item.description,
68
- created_at: item.created_at,
69
- updated_at: item.updated_at,
70
- id: item.id,
71
- handle: item.handle,
72
- title: item.title,
73
- priority: item.priority ?? 0,
74
- published: item.published,
75
- application: JSON.stringify(item.application),
76
- info: JSON.stringify(item.info),
77
- _application_id: item.application.id,
78
- _discount_type_id: item.info.details.meta.id
79
- });
80
- }
81
- );
82
- } catch(e) {
83
- console.log(e);
84
- return false;
85
- }
86
- return true;
87
- }
88
- }
89
-
90
-
91
- /**
92
- * @param {SQL} driver
93
- * @returns {db_col["get"]}
94
- */
95
- const get = (driver) => {
96
- return (id_or_handle, options) => {
97
- return driver.client
98
- .selectFrom(table_name)
99
- .selectAll()
100
- .select(eb => [
101
- with_media(eb, id_or_handle, driver.dialectType),
102
- with_tags(eb, id_or_handle, driver.dialectType),
103
- ].filter(Boolean))
104
- .where(where_id_or_handle_table(id_or_handle))
105
- .executeTakeFirst();
106
- }
107
- }
108
-
109
-
110
- /**
111
- * @param {SQL} driver
112
- * @returns {db_col["remove"]}
113
- */
114
- const remove = (driver) => {
115
- return async (id_or_handle) => {
116
- try {
117
- await driver.client.transaction().execute(
118
- async (trx) => {
119
-
120
- // entities
121
- await delete_search_of(trx, id_or_handle);
122
- await delete_media_of(trx, id_or_handle);
123
- await delete_tags_of(trx, id_or_handle);
124
- // delete products -> discounts
125
- // PRODUCTS => DISCOUNTS
126
- await delete_entity_values_by_value_or_reporter('products_to_discounts')(
127
- trx, id_or_handle, id_or_handle);
128
- // STOREFRONT => DISCOUNTS
129
- await delete_entity_values_by_value_or_reporter('storefronts_to_other')(
130
- trx, id_or_handle, id_or_handle
131
- );
132
-
133
- // delete me
134
- await delete_me(trx, table_name, id_or_handle);
135
- }
136
- );
137
- } catch(e) {
138
- console.log(e);
139
- return false;
140
- }
141
- return true;
142
- }
143
- }
144
-
145
-
146
- /**
147
- * @param {SQL} driver
148
- * @returns {db_col["list"]}
149
- */
150
- const list = (driver) => {
151
- return async (query) => {
152
-
153
- const items = await driver.client
154
- .selectFrom(table_name)
155
- .selectAll()
156
- .select(eb => [
157
- with_media(eb, eb.ref('discounts.id'), driver.dialectType),
158
- with_tags(eb, eb.ref('discounts.id'), driver.dialectType),
159
- ].filter(Boolean))
160
- .where(
161
- (eb) => {
162
- return query_to_eb(eb, query, table_name);
163
- }
164
- )
165
- .orderBy(query_to_sort(query))
166
- .limit(query.limitToLast ?? query.limit ?? 10)
167
- .execute();
168
-
169
- if(query.limitToLast) items.reverse();
170
-
171
- return sanitize_array(items);
172
- }
173
- }
174
-
175
- /**
176
- * @param {SQL} driver
177
- * @returns {db_col["list_discount_products"]}
178
- */
179
- const list_discount_products = (driver) => {
180
- return async (handle_or_id, query={}) => {
181
-
182
- const items = await driver.client
183
- .selectFrom('products')
184
- .selectAll()
185
- .select(eb => [
186
- with_media(eb, eb.ref('products.id'), driver.dialectType),
187
- with_tags(eb, eb.ref('products.id'), driver.dialectType),
188
- ])
189
- .where(
190
- (eb) => eb.and(
191
- [
192
- query_to_eb(eb, query, 'products'),
193
- eb('products.id', 'in',
194
- eb => select_entity_ids_by_value_or_reporter( // select all the product ids by discount id
195
- eb, 'products_to_discounts', handle_or_id
196
- )
197
- )
198
- ].filter(Boolean)
199
- )
200
- )
201
- .orderBy(query_to_sort(query))
202
- .limit(query.limitToLast ?? query.limit ?? 10)
203
- .execute();
204
-
205
- if(query.limitToLast) items.reverse();
206
-
207
- return sanitize_array(items);
208
- }
209
- }
210
-
211
- /**
212
- * @param {SQL} driver
213
- * @return {db_col}}
214
- * */
215
- export const impl = (driver) => {
216
-
217
- return {
218
- get: get(driver),
219
- upsert: upsert(driver),
220
- remove: remove(driver),
221
- list: list(driver),
222
- list_discount_products: list_discount_products(driver),
223
- count: count_regular(driver, table_name),
224
- }
225
- }
@@ -1,180 +0,0 @@
1
- import { enums } from "@storecraft/core/v-api";
2
-
3
- /** @param {import("@storecraft/core/v-api").DiscountType} d */
4
- const is_order_discount = d => {
5
- return (d.info.details.meta.id===enums.DiscountMetaEnum.order.id);
6
- }
7
-
8
- /** @param {import("@storecraft/core/v-api").DiscountType} d */
9
- const is_automatic_discount = d => {
10
- return (d.application.id===enums.DiscountApplicationEnum.Auto.id);
11
- }
12
-
13
- const extract_abs_number = v => {
14
- return v && !isNaN(v) && v!==Infinity && Math.abs(v);
15
- }
16
- /**
17
- * @typedef {import("../index.js").Database} Database
18
- * @param {import("kysely").ExpressionBuilder<Database, 'products'>} eb
19
- * @param {keyof Pick<Database, 'entity_to_tags_projections' | 'products_to_collections'>} table
20
- * @param {import("kysely").BinaryOperator} op
21
- * @param {string[]} value
22
- */
23
- const eb_in = (eb, table, op, value) => {
24
- return eb.exists(
25
- eb => eb
26
- .selectFrom(table)
27
- .select('id')
28
- .where(
29
- eb => eb.and([
30
- eb.or(
31
- [
32
- eb(`${table}.entity_id`, '=', eb.ref('products.id')),
33
- eb(`${table}.entity_handle`, '=', eb.ref('products.handle')),
34
- ]
35
- ),
36
- eb(`${table}.value`, op, value)
37
- ])
38
- )
39
- )
40
- }
41
-
42
- /**
43
- * create a mongodb conjunctions clauses from discount, intended
44
- * for filtering.
45
- * @param {import("kysely").ExpressionBuilder<Database, 'products'>} eb
46
- * @param {import("@storecraft/core/v-api").DiscountType} d
47
- */
48
- export const discount_to_conjunctions = (eb, d) => {
49
- // discount has to be product discount + automatic + active + has filters
50
- const is_good = (
51
- !is_order_discount(d) && is_automatic_discount(d) &&
52
- d.active && d?.info?.filters?.length
53
- );
54
-
55
- if(!is_good) return [];
56
-
57
- const conjunctions = [];
58
- const filters = d.info.filters;
59
-
60
- for(const filter of filters) {
61
- const op = filter.meta.op;
62
-
63
- switch (op) {
64
- case enums.FilterMetaEnum.p_all.op:
65
- // do nothing
66
- break;
67
- case enums.FilterMetaEnum.p_in_products.op:
68
- {
69
- /** @type {import("@storecraft/core/v-api").FilterValue_p_in_products} */
70
- const cast = Array.isArray(filter?.value) ? filter.value : [];
71
-
72
- conjunctions.push(
73
- eb(
74
- 'products.handle', 'in',
75
- cast.map(item => item.handle).filter(Boolean)
76
- )
77
- );
78
- }
79
- break;
80
- case enums.FilterMetaEnum.p_not_in_products.op:
81
- {
82
- /** @type {import("@storecraft/core/v-api").FilterValue_p_not_in_products} */
83
- const cast = Array.isArray(filter?.value) ? filter.value : [];
84
-
85
- conjunctions.push(
86
- eb(
87
- 'products.handle', 'not in',
88
- cast.map(item => item.handle).filter(Boolean)
89
- )
90
- );
91
- }
92
- break;
93
- case enums.FilterMetaEnum.p_in_tags.op:
94
- {
95
- /** @type {import("@storecraft/core/v-api").FilterValue_p_in_tags} */
96
- const cast = Array.isArray(filter?.value) ? filter.value : [];
97
-
98
- conjunctions.push(
99
- eb_in(
100
- eb, 'entity_to_tags_projections', 'in',
101
- cast
102
- )
103
- );
104
- }
105
- break;
106
- case enums.FilterMetaEnum.p_not_in_tags.op:
107
- {
108
- /** @type {import("@storecraft/core/v-api").FilterValue_p_not_in_tags} */
109
- const cast = Array.isArray(filter?.value) ? filter.value : [];
110
-
111
- conjunctions.push(
112
- eb.not(
113
- eb_in(
114
- eb, 'entity_to_tags_projections', 'in',
115
- cast
116
- )
117
- )
118
- );
119
- }
120
- break;
121
- case enums.FilterMetaEnum.p_in_collections.op:
122
- {
123
- /** @type {import("@storecraft/core/v-api").FilterValue_p_in_collections} */
124
- const cast = Array.isArray(filter?.value) ? filter.value : [];
125
-
126
- // PROBLEM: we only have ids, but use handles in the filters
127
- conjunctions.push(
128
- eb_in(
129
- eb, 'products_to_collections', 'in',
130
- cast.map(c => c.id)
131
- )
132
- );
133
- }
134
- break;
135
- case enums.FilterMetaEnum.p_not_in_collections.op:
136
- {
137
- /** @type {import("@storecraft/core/v-api").FilterValue_p_not_in_collections} */
138
- const cast = Array.isArray(filter?.value) ? filter.value : [];
139
-
140
- conjunctions.push(
141
- eb.not(
142
- eb_in(
143
- eb, 'products_to_collections', 'in',
144
- cast.map(c => c.id)
145
- )
146
- )
147
- );
148
- }
149
- break;
150
- case enums.FilterMetaEnum.p_in_price_range.op:
151
- {
152
- /** @type {import("@storecraft/core/v-api").FilterValue_p_in_price_range} */
153
- const cast = {
154
- from: 0,
155
- to: Number.POSITIVE_INFINITY,
156
- ...(filter?.value ?? {}),
157
- };
158
-
159
- const from = extract_abs_number(cast.from);
160
- const to = extract_abs_number(cast.to);
161
-
162
- const conj = { price: { $and: [] } };
163
-
164
- if(from)
165
- conjunctions.push(eb('price', '>=', from));
166
-
167
- if(to)
168
- conjunctions.push(eb('price', '<', to));
169
-
170
- }
171
- break;
172
-
173
- default:
174
- break;
175
- }
176
- }
177
-
178
- return conjunctions;
179
- }
180
-