@storecraft/database-sql-base 1.0.23 → 1.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/database-sql-base",
3
- "version": "1.0.23",
3
+ "version": "1.0.24",
4
4
  "description": "Official SQL Database driver for storecraft",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -7,7 +7,7 @@ import {
7
7
  count_regular, delete_me, insert_search_of, insert_tags_of,
8
8
  regular_upsert_me, where_id_or_handle_table, with_media, with_tags
9
9
  } from './con.shared.js'
10
- import { query_to_eb, query_to_sort } from './utils.query.js';
10
+ import { withQuery } from './utils.query.js';
11
11
  import { remove as remove_customer_and_auth_user } from './con.customers.js';
12
12
 
13
13
 
@@ -126,22 +126,26 @@ const removeByEmail = (driver) => {
126
126
  const list = (driver) => {
127
127
  return async (query) => {
128
128
 
129
- const items = await driver.client.selectFrom(table_name)
129
+ const items = await withQuery(
130
+ driver.client.selectFrom(table_name)
130
131
  .selectAll()
131
132
  .select(eb => [
132
133
  with_tags(eb, eb.ref('auth_users.id'), driver.dialectType),
133
134
  with_media(eb, eb.ref('auth_users.id'), driver.dialectType),
134
- ])
135
- .where(
136
- (eb) => {
137
- return query_to_eb(eb, query, table_name);
138
- }
139
- )
140
- .orderBy(query_to_sort(query, 'auth_users'))
141
- .limit(query.limitToLast ?? query.limit ?? 10)
142
- .execute();
143
-
144
- if(query.limitToLast) items.reverse();
135
+ ]),
136
+ query, table_name
137
+ // .where(
138
+ // (eb) => {
139
+ // return query_to_eb(eb, query, table_name);
140
+ // }
141
+ // )
142
+ // .orderBy(query_to_sort(query, 'auth_users'))
143
+ // .limit(query.limitToLast ?? query.limit ?? 10)
144
+ )
145
+ .execute();
146
+
147
+ if(query.limitToLast)
148
+ items.reverse();
145
149
 
146
150
  return sanitize_array(items);
147
151
  }
@@ -13,7 +13,7 @@ import {
13
13
  with_search
14
14
  } from './con.shared.js'
15
15
  import { sanitize, sanitize_array } from './utils.funcs.js'
16
- import { query_to_eb, query_to_sort } from './utils.query.js'
16
+ import { query_to_eb, withQuery, withSort } from './utils.query.js'
17
17
 
18
18
  export const table_name = 'collections'
19
19
 
@@ -120,22 +120,26 @@ const remove = (driver) => {
120
120
  const list = (driver) => {
121
121
  return async (query) => {
122
122
 
123
- const items = await driver.client
123
+ const items = await withQuery(
124
+ driver.client
124
125
  .selectFrom(table_name)
125
126
  .selectAll()
126
127
  .select(eb => [
127
128
  with_tags(eb, eb.ref('collections.id'), driver.dialectType),
128
129
  with_media(eb, eb.ref('collections.id'), driver.dialectType),
129
130
  with_search(eb, eb.ref('collections.id'), driver.dialectType),
130
- ])
131
- .where(
132
- (eb) => {
133
- return query_to_eb(eb, query, table_name);
134
- }
135
- )
136
- .orderBy(query_to_sort(query, 'collections'))
137
- .limit(query.limitToLast ?? query.limit ?? 10)
138
- .execute();
131
+ ]),
132
+ query, table_name
133
+ ).execute();
134
+
135
+ // .where(
136
+ // (eb) => {
137
+ // return query_to_eb(eb, query, table_name);
138
+ // }
139
+ // )
140
+ // .orderBy(query_to_sort(query, 'collections'))
141
+ // .limit(query.limitToLast ?? query.limit ?? 10)
142
+ // .execute();
139
143
 
140
144
  if(query.limitToLast) items.reverse();
141
145
 
@@ -150,7 +154,8 @@ const list = (driver) => {
150
154
  const list_collection_products = (driver) => {
151
155
  return async (handle_or_id, query={}) => {
152
156
 
153
- const items = await driver.client
157
+ const items = await withSort(
158
+ driver.client
154
159
  .selectFrom('products')
155
160
  .innerJoin(
156
161
  'products_to_collections',
@@ -177,14 +182,17 @@ const list_collection_products = (driver) => {
177
182
  ].filter(Boolean)
178
183
  )
179
184
  )
180
- .orderBy(query_to_sort(query, 'products'))
181
- .limit(query.limitToLast ?? query.limit ?? 10)
182
- .execute();
185
+ .limit(query.limitToLast ?? query.limit ?? 10),
186
+ query, 'products'
187
+ ).execute()
188
+ // .orderBy(query_to_sort(query, 'products'))
189
+ // .execute();
183
190
 
184
191
  // .compile();
185
192
  // console.log(items[0])
186
193
 
187
- if(query.limitToLast) items.reverse();
194
+ if(query.limitToLast)
195
+ items.reverse();
188
196
 
189
197
  return sanitize_array(items);
190
198
  }
@@ -8,7 +8,7 @@ import { count_regular, delete_me, delete_media_of, delete_search_of,
8
8
  insert_tags_of, regular_upsert_me, where_id_or_handle_table,
9
9
  with_media, with_search, with_tags} from './con.shared.js'
10
10
  import { sanitize, sanitize_array } from './utils.funcs.js'
11
- import { query_to_eb, query_to_sort } from './utils.query.js'
11
+ import { query_to_eb, withQuery, withSort } from './utils.query.js'
12
12
 
13
13
  export const table_name = 'customers'
14
14
 
@@ -130,22 +130,25 @@ export const remove = (driver) => {
130
130
  const list = (driver) => {
131
131
  return async (query) => {
132
132
 
133
- const items = await driver.client
133
+ const items = await withQuery(
134
+ driver.client
134
135
  .selectFrom(table_name)
135
136
  .selectAll()
136
137
  .select(eb => [
137
138
  with_media(eb, eb.ref('customers.id'), driver.dialectType),
138
139
  with_tags(eb, eb.ref('customers.id'), driver.dialectType),
139
140
  with_search(eb, eb.ref('customers.id'), driver.dialectType),
140
- ].filter(Boolean))
141
- .where(
142
- (eb) => {
143
- return query_to_eb(eb, query, table_name);
144
- }
145
- )
146
- .orderBy(query_to_sort(query, table_name))
147
- .limit(query.limitToLast ?? query.limit ?? 10)
148
- .execute();
141
+ ].filter(Boolean)),
142
+ query, table_name
143
+ ).execute();
144
+
145
+ // .where(
146
+ // (eb) => {
147
+ // return query_to_eb(eb, query, table_name);
148
+ // }
149
+ // .orderBy(query_to_sort(query, table_name))
150
+ // .limit(query.limitToLast ?? query.limit ?? 10)
151
+ // .execute();
149
152
 
150
153
  if(query.limitToLast) items.reverse();
151
154
 
@@ -160,7 +163,8 @@ const list = (driver) => {
160
163
  const list_customer_orders = (driver) => {
161
164
  return async (id, query) => {
162
165
 
163
- const items = await driver.client
166
+ const items = await withSort(
167
+ driver.client
164
168
  .selectFrom('orders')
165
169
  .selectAll()
166
170
  .select(eb => [
@@ -180,11 +184,14 @@ const list_customer_orders = (driver) => {
180
184
  ].filter(Boolean)
181
185
  )
182
186
  )
183
- .orderBy(query_to_sort(query, 'orders'))
184
- .limit(query.limitToLast ?? query.limit ?? 10)
185
- .execute();
186
-
187
- if(query.limitToLast) items.reverse();
187
+ .limit(query.limitToLast ?? query.limit ?? 10),
188
+ query, 'orders'
189
+ ).execute();
190
+ // .orderBy(query_to_sort(query, 'orders'))
191
+ // .execute();
192
+
193
+ if(query.limitToLast)
194
+ items.reverse();
188
195
 
189
196
  return sanitize_array(items);
190
197
  }
@@ -7,7 +7,7 @@ import {
7
7
  helper_compute_product_extra_tags_because_of_discount_side_effect_for_db
8
8
  } from '@storecraft/core/database'
9
9
  import { SQL } from '../index.js'
10
- import { discount_to_conjunctions } from './con.discounts.utils.js'
10
+ import { discount_to_conjunctions, is_order_discount } from './con.discounts.utils.js'
11
11
  import {
12
12
  delete_entity_values_by_value_or_reporter_and_context,
13
13
  delete_me, delete_media_of, delete_search_of,
@@ -16,7 +16,7 @@ import {
16
16
  with_media, with_tags, count_regular, with_search,
17
17
  } from './con.shared.js'
18
18
  import { sanitize, sanitize_array } from './utils.funcs.js'
19
- import { query_to_eb, query_to_sort } from './utils.query.js'
19
+ import { query_to_eb, withQuery, withSort } from './utils.query.js'
20
20
  import { report_document_media } from './con.images.js'
21
21
 
22
22
  export const table_name = 'discounts'
@@ -59,7 +59,13 @@ const upsert = (driver) => {
59
59
  trx, extra_tag);
60
60
  }
61
61
 
62
- if(item.active && item.application.id===enums.DiscountApplicationEnum.Auto.id) {
62
+ // make connections into `products_to_discounts`, only applicable for
63
+ // `product` discounts and automatic discounts
64
+ if(
65
+ item.active &&
66
+ item.application.id===enums.DiscountApplicationEnum.Auto.id &&
67
+ !is_order_discount(item)
68
+ ) {
63
69
  // make connections
64
70
  await trx
65
71
  .insertInto('products_to_discounts')
@@ -243,24 +249,24 @@ const remove = (driver) => {
243
249
  const list = (driver) => {
244
250
  return async (query) => {
245
251
 
246
- const items = await driver.client
252
+ const items = await withQuery(
253
+ driver.client
247
254
  .selectFrom(table_name)
248
255
  .selectAll()
249
256
  .select(eb => [
250
257
  with_media(eb, eb.ref('discounts.id'), driver.dialectType),
251
258
  with_tags(eb, eb.ref('discounts.id'), driver.dialectType),
252
259
  with_search(eb, eb.ref('discounts.id'), driver.dialectType),
253
- ].filter(Boolean))
254
- .where(
255
- (eb) => {
256
- return query_to_eb(eb, query, table_name);
257
- }
258
- )
259
- .orderBy(query_to_sort(query, table_name))
260
- .limit(query.limitToLast ?? query.limit ?? 10)
261
- .execute();
260
+ ].filter(Boolean)),
261
+ query, table_name
262
+ ).execute();
263
+
264
+ // .orderBy(query_to_sort(query, table_name))
265
+ // .limit(query.limitToLast ?? query.limit ?? 10)
266
+ // .execute();
262
267
 
263
- if(query.limitToLast) items.reverse();
268
+ if(query.limitToLast)
269
+ items.reverse();
264
270
 
265
271
  return sanitize_array(items);
266
272
  }
@@ -273,7 +279,8 @@ const list = (driver) => {
273
279
  const list_discount_products = (driver) => {
274
280
  return async (handle_or_id, query={}) => {
275
281
 
276
- const items = await driver.client
282
+ const items = await withSort(
283
+ driver.client
277
284
  .selectFrom('products')
278
285
  .innerJoin(
279
286
  'products_to_discounts',
@@ -298,11 +305,15 @@ const list_discount_products = (driver) => {
298
305
  ].filter(Boolean)
299
306
  )
300
307
  )
301
- .orderBy(query_to_sort(query, 'products'))
302
- .limit(query.limitToLast ?? query.limit ?? 10)
303
- .execute();
308
+ .limit(query.limitToLast ?? query.limit ?? 10),
309
+ query, 'products'
310
+ ).execute();
311
+
312
+ // .orderBy(query_to_sort(query, 'products'))
313
+ // .execute();
304
314
 
305
- if(query.limitToLast) items.reverse();
315
+ if(query.limitToLast)
316
+ items.reverse();
306
317
 
307
318
  return sanitize_array(items);
308
319
  }
@@ -8,9 +8,10 @@
8
8
  * @import { ExpressionBuilder, BinaryOperator } from 'kysely'
9
9
  */
10
10
  import { enums } from "@storecraft/core/api";
11
+ import { assert } from "console";
11
12
 
12
13
  /** @param {DiscountType} d */
13
- const is_order_discount = d => {
14
+ export const is_order_discount = d => {
14
15
  return (
15
16
  (d.info.details.type===enums.DiscountMetaEnum.order.type) ||
16
17
  // @ts-ignore
@@ -19,7 +20,7 @@ const is_order_discount = d => {
19
20
  }
20
21
 
21
22
  /** @param {DiscountType} d */
22
- const is_automatic_discount = d => {
23
+ export const is_automatic_discount = d => {
23
24
  return (d.application.id===enums.DiscountApplicationEnum.Auto.id);
24
25
  }
25
26
 
@@ -53,7 +54,7 @@ const eb_in = (eb, table, value) => {
53
54
  }
54
55
 
55
56
  /**
56
- * create a mongodb conjunctions clauses from discount, intended
57
+ * @description create a filter conjunctions clauses from discount
57
58
  * for filtering.
58
59
  * @param {ExpressionBuilder<Database, 'products'>} eb
59
60
  * @param {DiscountType} d
@@ -65,7 +66,10 @@ export const discount_to_conjunctions = (eb, d) => {
65
66
  d.active && d?.info?.filters?.length
66
67
  );
67
68
 
68
- if(!is_good) return [];
69
+ assert(
70
+ is_good,
71
+ `discount_to_conjunctions: discount is not a product discount`
72
+ );
69
73
 
70
74
  const conjunctions = [];
71
75
  const filters = d.info.filters;
package/src/con.images.js CHANGED
@@ -9,7 +9,7 @@ import { count_regular, delete_me, delete_search_of,
9
9
  insert_search_of, regular_upsert_me, where_id_or_handle_table
10
10
  } from './con.shared.js'
11
11
  import { sanitize, sanitize_array } from './utils.funcs.js'
12
- import { query_to_eb, query_to_sort } from './utils.query.js'
12
+ import { withQuery } from './utils.query.js'
13
13
  import { Transaction } from 'kysely'
14
14
  import { ID } from '@storecraft/core/api/utils.func.js'
15
15
  import {
@@ -196,19 +196,24 @@ export const report_document_media = (driver) => {
196
196
  */
197
197
  const list = (driver) => {
198
198
  return async (query) => {
199
- const items = await driver.client
199
+ const items = await withQuery(
200
+ driver.client
200
201
  .selectFrom(table_name)
201
- .selectAll()
202
- .where(
203
- (eb) => {
204
- return query_to_eb(eb, query, table_name);
205
- }
206
- )
207
- .orderBy(query_to_sort(query, 'images')) // ts complains because `usage` field is absent
208
- .limit(query.limitToLast ?? query.limit ?? 10)
209
- .execute();
210
-
211
- if(query.limitToLast) items.reverse();
202
+ .selectAll(),
203
+ query, table_name
204
+ ).execute()
205
+
206
+ // .where(
207
+ // (eb) => {
208
+ // return query_to_eb(eb, query, table_name);
209
+ // }
210
+ // )
211
+ // .orderBy(query_to_sort(query, 'images')) // ts complains because `usage` field is absent
212
+ // .limit(query.limitToLast ?? query.limit ?? 10)
213
+ // .execute();
214
+
215
+ if(query.limitToLast)
216
+ items.reverse();
212
217
 
213
218
  return sanitize_array(items);
214
219
  }
@@ -8,7 +8,7 @@ import {
8
8
  with_search
9
9
  } from './con.shared.js'
10
10
  import { sanitize_array } from './utils.funcs.js'
11
- import { query_to_eb, query_to_sort } from './utils.query.js'
11
+ import { withQuery } from './utils.query.js'
12
12
 
13
13
  export const table_name = 'notifications'
14
14
 
@@ -117,22 +117,29 @@ const remove = (driver) => {
117
117
  const list = (driver) => {
118
118
  return async (query) => {
119
119
 
120
- const items = await driver.client
120
+ const items = await withQuery(
121
+ driver.client
121
122
  .selectFrom(table_name)
122
123
  .selectAll()
123
- .select(eb => [
124
- with_search(eb, eb.ref('notifications.id'), driver.dialectType),
125
- ].filter(Boolean))
126
- .where(
127
- (eb) => {
128
- return query_to_eb(eb, query, table_name);
129
- }
130
- )
131
- .orderBy(query_to_sort(query, 'notifications'))
132
- .limit(query.limitToLast ?? query.limit ?? 10)
133
- .execute();
134
-
135
- if(query.limitToLast) items.reverse();
124
+ .select(
125
+ eb => [
126
+ with_search(eb, eb.ref('notifications.id'), driver.dialectType),
127
+ ].filter(Boolean)
128
+ ),
129
+ query, table_name
130
+ ).execute();
131
+
132
+ // .where(
133
+ // (eb) => {
134
+ // return query_to_eb(eb, query, table_name);
135
+ // }
136
+ // )
137
+ // .orderBy(query_to_sort(query, 'notifications'))
138
+ // .limit(query.limitToLast ?? query.limit ?? 10)
139
+ // .execute();
140
+
141
+ if(query.limitToLast)
142
+ items.reverse();
136
143
 
137
144
  return sanitize_array(items);
138
145
  }
package/src/con.orders.js CHANGED
@@ -9,7 +9,7 @@ import { count_regular, delete_me, delete_media_of, delete_search_of,
9
9
  insert_tags_of, regular_upsert_me, where_id_or_handle_table,
10
10
  with_media, with_search, with_tags} from './con.shared.js'
11
11
  import { sanitize, sanitize_array } from './utils.funcs.js'
12
- import { query_to_eb, query_to_sort } from './utils.query.js'
12
+ import { withQuery } from './utils.query.js'
13
13
 
14
14
  export const table_name = 'orders'
15
15
 
@@ -129,31 +129,38 @@ const remove = (driver) => {
129
129
  const list = (driver) => {
130
130
  return async (query) => {
131
131
 
132
- const items = await driver.client
132
+ const items = await withQuery(
133
+ driver.client
133
134
  .selectFrom(table_name)
134
135
  // .selectAll()
135
136
  .select(
136
137
  [
137
138
  'active', 'address', 'attributes', 'contact', 'coupons', 'created_at',
138
139
  'updated_at', 'description', 'handle', 'id', 'line_items', 'notes',
139
- 'payment_gateway', 'pricing', 'shipping_method', 'status', 'validation'
140
+ 'payment_gateway', 'pricing', 'shipping_method', 'status', 'validation',
140
141
  ]
141
142
  )
142
- .select(eb => [
143
- with_media(eb, eb.ref('orders.id'), driver.dialectType),
144
- with_tags(eb, eb.ref('orders.id'), driver.dialectType),
145
- with_search(eb, eb.ref('orders.id'), driver.dialectType),
146
- ].filter(Boolean))
147
- .where(
148
- (eb) => {
149
- return query_to_eb(eb, query, table_name);
150
- }
151
- )
152
- .orderBy(query_to_sort(query, table_name))
153
- .limit(query.limitToLast ?? query.limit ?? 10)
154
- .execute();
155
-
156
- if(query.limitToLast) items.reverse();
143
+ .select(
144
+ eb => [
145
+ with_media(eb, eb.ref('orders.id'), driver.dialectType),
146
+ with_tags(eb, eb.ref('orders.id'), driver.dialectType),
147
+ with_search(eb, eb.ref('orders.id'), driver.dialectType),
148
+ ].filter(Boolean)
149
+ ),
150
+ query, table_name
151
+ ).execute();
152
+
153
+ // .where(
154
+ // (eb) => {
155
+ // return query_to_eb(eb, query, table_name);
156
+ // }
157
+ // )
158
+ // .orderBy(query_to_sort(query, table_name))
159
+ // .limit(query.limitToLast ?? query.limit ?? 10)
160
+ // .execute();
161
+
162
+ if(query.limitToLast)
163
+ items.reverse();
157
164
 
158
165
  return sanitize_array(items);
159
166
  }
package/src/con.posts.js CHANGED
@@ -1,17 +1,17 @@
1
1
  /**
2
2
  * @import { db_posts as db_col } from '@storecraft/core/database'
3
3
  */
4
-
5
4
  import { SQL } from '../index.js'
6
- import { stringArrayFrom } from './con.helpers.json.js'
7
5
  import { report_document_media } from './con.images.js'
8
- import { count_regular, delete_entity_values_by_value_or_reporter_and_context,
6
+ import {
7
+ count_regular, delete_entity_values_by_value_or_reporter_and_context,
9
8
  delete_me, delete_media_of, delete_search_of, delete_tags_of,
10
9
  insert_media_of, insert_search_of, insert_tags_of,
11
10
  regular_upsert_me, where_id_or_handle_table,
12
- with_media, with_search, with_tags} from './con.shared.js'
11
+ with_media, with_search, with_tags
12
+ } from './con.shared.js'
13
13
  import { sanitize, sanitize_array } from './utils.funcs.js'
14
- import { query_to_eb, query_to_sort } from './utils.query.js'
14
+ import { withQuery, withSort } from './utils.query.js'
15
15
 
16
16
  export const table_name = 'posts'
17
17
 
@@ -60,12 +60,13 @@ const get = (driver) => {
60
60
  const result = await driver.client
61
61
  .selectFrom(table_name)
62
62
  .selectAll()
63
- .select(eb => [
64
- with_media(eb, id_or_handle, driver.dialectType),
65
- with_tags(eb, id_or_handle, driver.dialectType),
66
- with_search(eb, id_or_handle, driver.dialectType),
67
- ]
68
- .filter(Boolean))
63
+ .select(
64
+ eb => [
65
+ with_media(eb, id_or_handle, driver.dialectType),
66
+ with_tags(eb, id_or_handle, driver.dialectType),
67
+ with_search(eb, id_or_handle, driver.dialectType),
68
+ ].filter(Boolean)
69
+ )
69
70
  .where(where_id_or_handle_table(id_or_handle))
70
71
  .executeTakeFirst();
71
72
 
@@ -113,25 +114,24 @@ const remove = (driver) => {
113
114
  const list = (driver) => {
114
115
  return async (query) => {
115
116
 
116
- const items = await driver.client
117
+ const items = await withQuery(
118
+ driver.client
117
119
  .selectFrom(table_name)
118
120
  .selectAll()
119
- .select(eb => [
120
- with_media(eb, eb.ref('posts.id'), driver.dialectType),
121
- with_tags(eb, eb.ref('posts.id'), driver.dialectType),
122
- with_search(eb, eb.ref('posts.id'), driver.dialectType),
123
- ].filter(Boolean))
124
- .where(
125
- (eb) => {
126
- return query_to_eb(eb, query, table_name);
127
- }
128
- )
129
- .orderBy(query_to_sort(query, 'posts'))
130
- // .orderBy()
131
- .limit(query.limitToLast ?? query.limit ?? 10)
132
- .execute();
133
-
134
- if(query.limitToLast) items.reverse();
121
+ .select(
122
+ eb => [
123
+ with_media(eb, eb.ref('posts.id'), driver.dialectType),
124
+ with_tags(eb, eb.ref('posts.id'), driver.dialectType),
125
+ with_search(eb, eb.ref('posts.id'), driver.dialectType),
126
+ ].filter(Boolean)
127
+ ),
128
+ query, table_name
129
+ )
130
+ .execute();
131
+ // console.log({items})
132
+
133
+ if(query.limitToLast)
134
+ items.reverse();
135
135
 
136
136
  return sanitize_array(items);
137
137
  }