@storecraft/database-sql-base 1.0.10 → 1.0.11

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.
@@ -72,9 +72,10 @@ export const query_cursor_to_eb = (eb, c, relation, transformer=(x)=>x) => {
72
72
 
73
73
 
74
74
  /**
75
+ * @template {keyof Database} T
75
76
  * @param {import("kysely").ExpressionBuilder<Database>} eb
76
77
  * @param {import("@storecraft/core/vql").VQL.Node} node
77
- * @param {keyof Database} table_name
78
+ * @param {T} table_name
78
79
  */
79
80
  export const query_vql_node_to_eb = (eb, node, table_name) => {
80
81
  if(node.op==='LEAF') {
@@ -146,9 +147,10 @@ const transform_boolean_to_0_or_1 = (kv) => {
146
147
  /**
147
148
  * Convert an API Query into mongo dialect, also sanitize.
148
149
  *
150
+ * @template {any} [T=any]
149
151
  *
150
152
  * @param {import("kysely").ExpressionBuilder<Database>} eb
151
- * @param {import("@storecraft/core/api").ApiQuery} q
153
+ * @param {import("@storecraft/core/api").ApiQuery<T>} q
152
154
  * @param {keyof Database} table_name
153
155
  *
154
156
  */
@@ -189,12 +191,22 @@ const SIGN = {
189
191
  '-1': 'desc'
190
192
  }
191
193
 
194
+ // export type DirectedOrderByStringReference<DB, TB extends keyof DB, O> = `${StringReference<DB, TB> | (keyof O & string)} ${OrderByDirection}`;
195
+
196
+ /**
197
+ * @import {DirectedOrderByStringReference} from './utils.types.js'
198
+ */
199
+ // OE extends OrderByExpression<DB, TB, O>
192
200
  /**
193
201
  * Convert an API Query into mongo dialect, also sanitize.
202
+ * @template {Record<string, any>} [Type=Record<string, any>]
203
+ * @template {keyof Database} [Table=keyof Database]
194
204
  *
195
- * @param {import("@storecraft/core/api").ApiQuery} q
205
+ * @param {import("@storecraft/core/api").ApiQuery<Type>} q
206
+ * @param {Table} table
207
+ * @returns {DirectedOrderByStringReference<Database, Table, Database[Table]>[]}
196
208
  */
197
- export const query_to_sort = (q={}) => {
209
+ export const query_to_sort = (q={}, table) => {
198
210
  // const sort_sign = q.order === 'asc' ? 'asc' : 'desc';
199
211
  // `reverse_sign=-1` means we need to reverse because of `limitToLast`
200
212
  const reverse_sign = (q.limitToLast && !q.limit) ? -1 : 1;
@@ -202,9 +214,11 @@ export const query_to_sort = (q={}) => {
202
214
  const sort_sign = (asc ? 1 : -1) * reverse_sign;
203
215
 
204
216
  // compute sort fields and order
205
- const sort = (q.sortBy?.length ? q.sortBy : ['updated_at', 'id']).map(
217
+ const keys = q.sortBy?.length ? q.sortBy : ['updated_at', 'id'];
218
+ const sort = keys.map(
206
219
  s => `${s} ${SIGN[sort_sign]}`
207
220
  )
208
-
221
+ // it's too complicated to map each ket to table column.
222
+ // kysely was designed to do this in place
209
223
  return sort;
210
224
  }
@@ -0,0 +1,17 @@
1
+ import { StringReference } from "kysely";
2
+
3
+ export type NoActive<T> = T extends {active:number} ? Omit<T, 'active'> & {active: boolean} : T;
4
+ export type ReplaceValues<T, Find extends any=any, ReplaceWith extends any=any> = {
5
+ [K in keyof T]: T[K] extends Find ? ReplaceWith : T[K]
6
+ };
7
+
8
+ export type ReplaceValuesOfKeys<T, Find extends keyof T=keyof T, ReplaceWith extends any=any> = {
9
+ [K in keyof T]: K extends Find ? ReplaceWith : T[K]
10
+ };
11
+
12
+ export type NO<T> = {
13
+ [P in keyof T]: T[P]
14
+ }
15
+
16
+ export type OrderByDirection = 'asc' | 'desc';
17
+ export type DirectedOrderByStringReference<DB, TB extends keyof DB, O> = `${StringReference<DB, TB> | (keyof O & string)} ${OrderByDirection}`;
@@ -240,7 +240,7 @@ export interface DiscountsTable extends Base {
240
240
  /** details and filters of the discount */
241
241
  info: JSONColumnType<DiscountInfo>;
242
242
  /** discount application (automatic and coupons) */
243
- application: JSONColumnType<DiscountApplicationEnum>;
243
+ application: JSONColumnType<DiscountApplicationEnum[keyof DiscountApplicationEnum]>;
244
244
  /** internal usage, the application type id */
245
245
  _application_id: number;
246
246
  /** internal usage, the discount type id */