@dockstat/sqlite-wrapper 1.2.7 → 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.
- package/README.md +493 -39
- package/index.ts +436 -156
- package/package.json +11 -5
- package/query-builder/base.ts +103 -141
- package/query-builder/delete.ts +276 -187
- package/query-builder/index.ts +95 -117
- package/query-builder/insert.ts +184 -153
- package/query-builder/select.ts +155 -180
- package/query-builder/update.ts +195 -165
- package/query-builder/where.ts +165 -200
- package/types.ts +134 -149
- package/utils/index.ts +44 -0
- package/utils/logger.ts +184 -0
- package/utils/sql.ts +241 -0
- package/utils/transformer.ts +256 -0
package/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Database, SQLQueryBindings } from "bun:sqlite"
|
|
1
|
+
import type { Database, SQLQueryBindings } from "bun:sqlite"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* All SQLite data types including affinity types
|
|
@@ -42,9 +42,12 @@ export const SQLiteTypes = {
|
|
|
42
42
|
|
|
43
43
|
// JSON (stored as TEXT)
|
|
44
44
|
JSON: "JSON" as const,
|
|
45
|
-
} as const;
|
|
46
45
|
|
|
47
|
-
|
|
46
|
+
// Modules (stored as TEXT)
|
|
47
|
+
MODULE: "TEXT" as const,
|
|
48
|
+
} as const
|
|
49
|
+
|
|
50
|
+
export type SQLiteType = (typeof SQLiteTypes)[keyof typeof SQLiteTypes]
|
|
48
51
|
|
|
49
52
|
/**
|
|
50
53
|
* SQLite built-in scalar functions
|
|
@@ -61,24 +64,18 @@ export const SQLiteFunctions = {
|
|
|
61
64
|
LENGTH: (expr: string) => `LENGTH(${expr})`,
|
|
62
65
|
LOWER: (expr: string) => `LOWER(${expr})`,
|
|
63
66
|
UPPER: (expr: string) => `UPPER(${expr})`,
|
|
64
|
-
TRIM: (expr: string, chars?: string) =>
|
|
65
|
-
chars ? `TRIM(${expr}, '${chars}')` : `TRIM(${expr})`,
|
|
67
|
+
TRIM: (expr: string, chars?: string) => (chars ? `TRIM(${expr}, '${chars}')` : `TRIM(${expr})`),
|
|
66
68
|
LTRIM: (expr: string, chars?: string) =>
|
|
67
69
|
chars ? `LTRIM(${expr}, '${chars}')` : `LTRIM(${expr})`,
|
|
68
70
|
RTRIM: (expr: string, chars?: string) =>
|
|
69
71
|
chars ? `RTRIM(${expr}, '${chars}')` : `RTRIM(${expr})`,
|
|
70
72
|
SUBSTR: (expr: string, start: number, length?: number) =>
|
|
71
|
-
length
|
|
72
|
-
? `SUBSTR(${expr}, ${start}, ${length})`
|
|
73
|
-
: `SUBSTR(${expr}, ${start})`,
|
|
73
|
+
length ? `SUBSTR(${expr}, ${start}, ${length})` : `SUBSTR(${expr}, ${start})`,
|
|
74
74
|
SUBSTRING: (expr: string, start: number, length?: number) =>
|
|
75
|
-
length
|
|
76
|
-
? `SUBSTRING(${expr}, ${start}, ${length})`
|
|
77
|
-
: `SUBSTRING(${expr}, ${start})`,
|
|
75
|
+
length ? `SUBSTRING(${expr}, ${start}, ${length})` : `SUBSTRING(${expr}, ${start})`,
|
|
78
76
|
REPLACE: (expr: string, old: string, replacement: string) =>
|
|
79
77
|
`REPLACE(${expr}, '${old}', '${replacement}')`,
|
|
80
|
-
PRINTF: (format: string, ...args: string[]) =>
|
|
81
|
-
`PRINTF('${format}', ${args.join(", ")})`,
|
|
78
|
+
PRINTF: (format: string, ...args: string[]) => `PRINTF('${format}', ${args.join(", ")})`,
|
|
82
79
|
|
|
83
80
|
// Math functions
|
|
84
81
|
ABS: (expr: string) => `ABS(${expr})`,
|
|
@@ -94,8 +91,7 @@ export const SQLiteFunctions = {
|
|
|
94
91
|
|
|
95
92
|
// Conditional
|
|
96
93
|
COALESCE: (...exprs: string[]) => `COALESCE(${exprs.join(", ")})`,
|
|
97
|
-
IFNULL: (expr: string, replacement: string) =>
|
|
98
|
-
`IFNULL(${expr}, ${replacement})`,
|
|
94
|
+
IFNULL: (expr: string, replacement: string) => `IFNULL(${expr}, ${replacement})`,
|
|
99
95
|
NULLIF: (expr1: string, expr2: string) => `NULLIF(${expr1}, ${expr2})`,
|
|
100
96
|
IIF: (condition: string, trueValue: string, falseValue: string) =>
|
|
101
97
|
`IIF(${condition}, ${trueValue}, ${falseValue})`,
|
|
@@ -106,20 +102,17 @@ export const SQLiteFunctions = {
|
|
|
106
102
|
AVG: (expr: string) => `AVG(${expr})`,
|
|
107
103
|
TOTAL: (expr: string) => `TOTAL(${expr})`,
|
|
108
104
|
GROUP_CONCAT: (expr: string, separator?: string) =>
|
|
109
|
-
separator
|
|
110
|
-
? `GROUP_CONCAT(${expr}, '${separator}')`
|
|
111
|
-
: `GROUP_CONCAT(${expr})`,
|
|
105
|
+
separator ? `GROUP_CONCAT(${expr}, '${separator}')` : `GROUP_CONCAT(${expr})`,
|
|
112
106
|
|
|
113
107
|
// JSON functions (SQLite 3.45+)
|
|
114
108
|
JSON: (expr: string) => `JSON(${expr})`,
|
|
115
|
-
JSON_EXTRACT: (json: string, path: string) =>
|
|
116
|
-
`JSON_EXTRACT(${json}, '${path}')`,
|
|
109
|
+
JSON_EXTRACT: (json: string, path: string) => `JSON_EXTRACT(${json}, '${path}')`,
|
|
117
110
|
JSON_TYPE: (json: string, path?: string) =>
|
|
118
111
|
path ? `JSON_TYPE(${json}, '${path}')` : `JSON_TYPE(${json})`,
|
|
119
112
|
JSON_VALID: (expr: string) => `JSON_VALID(${expr})`,
|
|
120
113
|
JSON_ARRAY: (...values: string[]) => `JSON_ARRAY(${values.join(", ")})`,
|
|
121
114
|
JSON_OBJECT: (...pairs: string[]) => `JSON_OBJECT(${pairs.join(", ")})`,
|
|
122
|
-
} as const
|
|
115
|
+
} as const
|
|
123
116
|
|
|
124
117
|
/**
|
|
125
118
|
* SQLite keywords and special values
|
|
@@ -141,59 +134,54 @@ export const SQLiteKeywords = {
|
|
|
141
134
|
FAIL: "FAIL" as const,
|
|
142
135
|
IGNORE: "IGNORE" as const,
|
|
143
136
|
REPLACE: "REPLACE" as const,
|
|
144
|
-
} as const
|
|
137
|
+
} as const
|
|
145
138
|
|
|
146
139
|
/**
|
|
147
140
|
* Foreign key actions
|
|
148
141
|
*/
|
|
149
|
-
export type ForeignKeyAction =
|
|
150
|
-
| "CASCADE"
|
|
151
|
-
| "SET NULL"
|
|
152
|
-
| "RESTRICT"
|
|
153
|
-
| "NO ACTION"
|
|
154
|
-
| "SET DEFAULT";
|
|
142
|
+
export type ForeignKeyAction = "CASCADE" | "SET NULL" | "RESTRICT" | "NO ACTION" | "SET DEFAULT"
|
|
155
143
|
|
|
156
144
|
/**
|
|
157
145
|
* Column constraint options with comprehensive support
|
|
158
146
|
*/
|
|
159
147
|
export interface ColumnConstraints {
|
|
160
148
|
/** Column is PRIMARY KEY */
|
|
161
|
-
primaryKey?: boolean
|
|
149
|
+
primaryKey?: boolean
|
|
162
150
|
/** Column has AUTOINCREMENT (only valid with INTEGER PRIMARY KEY) */
|
|
163
|
-
autoincrement?: boolean
|
|
151
|
+
autoincrement?: boolean
|
|
164
152
|
/** Column is NOT NULL */
|
|
165
|
-
notNull?: boolean
|
|
153
|
+
notNull?: boolean
|
|
166
154
|
/** Column has UNIQUE constraint */
|
|
167
|
-
unique?: boolean
|
|
155
|
+
unique?: boolean
|
|
168
156
|
/** Default value - can be literal value, function call, or keyword */
|
|
169
|
-
default?: string | number | boolean | null | DefaultExpression
|
|
157
|
+
default?: string | number | boolean | null | DefaultExpression
|
|
170
158
|
/** Custom check constraint */
|
|
171
|
-
check?: string
|
|
159
|
+
check?: string
|
|
172
160
|
/** Collation sequence */
|
|
173
|
-
collate?: "BINARY" | "NOCASE" | "RTRIM" | string
|
|
161
|
+
collate?: "BINARY" | "NOCASE" | "RTRIM" | string
|
|
174
162
|
/** References another table (foreign key) */
|
|
175
163
|
references?: {
|
|
176
|
-
table: string
|
|
177
|
-
column: string
|
|
178
|
-
onDelete?: ForeignKeyAction
|
|
179
|
-
onUpdate?: ForeignKeyAction
|
|
180
|
-
}
|
|
164
|
+
table: string
|
|
165
|
+
column: string
|
|
166
|
+
onDelete?: ForeignKeyAction
|
|
167
|
+
onUpdate?: ForeignKeyAction
|
|
168
|
+
}
|
|
181
169
|
/** Generated column expression (GENERATED ALWAYS AS) */
|
|
182
170
|
generated?: {
|
|
183
|
-
expression: string
|
|
184
|
-
stored?: boolean
|
|
185
|
-
}
|
|
171
|
+
expression: string
|
|
172
|
+
stored?: boolean // true for STORED, false/undefined for VIRTUAL
|
|
173
|
+
}
|
|
186
174
|
/** Column comment (stored as metadata, not in schema) */
|
|
187
|
-
comment?: string
|
|
175
|
+
comment?: string
|
|
188
176
|
}
|
|
189
177
|
|
|
190
178
|
/**
|
|
191
179
|
* Type-safe default value expressions
|
|
192
180
|
*/
|
|
193
181
|
export type DefaultExpression = {
|
|
194
|
-
_type: "expression"
|
|
195
|
-
expression: string
|
|
196
|
-
}
|
|
182
|
+
_type: "expression"
|
|
183
|
+
expression: string
|
|
184
|
+
}
|
|
197
185
|
|
|
198
186
|
/**
|
|
199
187
|
* Helper to create default expressions
|
|
@@ -201,50 +189,49 @@ export type DefaultExpression = {
|
|
|
201
189
|
export const defaultExpr = (expression: string): DefaultExpression => ({
|
|
202
190
|
_type: "expression",
|
|
203
191
|
expression,
|
|
204
|
-
})
|
|
192
|
+
})
|
|
205
193
|
|
|
206
194
|
/**
|
|
207
195
|
* Type-safe column definition
|
|
208
196
|
*/
|
|
209
197
|
export interface ColumnDefinition extends ColumnConstraints {
|
|
210
198
|
/** SQLite data type */
|
|
211
|
-
type: SQLiteType
|
|
199
|
+
type: SQLiteType
|
|
212
200
|
/** Optional type parameters (e.g., VARCHAR(255)) */
|
|
213
|
-
length?: number
|
|
201
|
+
length?: number
|
|
214
202
|
/** Precision for DECIMAL/NUMERIC types */
|
|
215
|
-
precision?: number
|
|
203
|
+
precision?: number
|
|
216
204
|
/** Scale for DECIMAL/NUMERIC types */
|
|
217
|
-
scale?: number
|
|
205
|
+
scale?: number
|
|
218
206
|
}
|
|
219
207
|
|
|
220
208
|
/**
|
|
221
209
|
* Type-safe table schema definition
|
|
222
210
|
*/
|
|
223
|
-
export type TableSchema = Record<string, ColumnDefinition
|
|
211
|
+
export type TableSchema = Record<string, ColumnDefinition>
|
|
224
212
|
|
|
225
|
-
|
|
226
|
-
export type TypedTableSchema<T extends string = string> = Record<T, ColumnDefinition>;
|
|
213
|
+
export type TypedTableSchema<T extends string = string> = Record<T, ColumnDefinition>
|
|
227
214
|
|
|
228
215
|
/**
|
|
229
216
|
* Table constraint types
|
|
230
217
|
*/
|
|
231
|
-
export interface TableConstraints {
|
|
218
|
+
export interface TableConstraints<T> {
|
|
232
219
|
/** PRIMARY KEY constraint on multiple columns */
|
|
233
|
-
primaryKey?:
|
|
220
|
+
primaryKey?: ArrayKey<T>
|
|
234
221
|
/** UNIQUE constraints */
|
|
235
|
-
unique?: string[] | string[][]
|
|
222
|
+
unique?: string[] | string[][]
|
|
236
223
|
/** CHECK constraints */
|
|
237
|
-
check?: string[]
|
|
224
|
+
check?: string[]
|
|
238
225
|
/** FOREIGN KEY constraints */
|
|
239
226
|
foreignKeys?: Array<{
|
|
240
|
-
columns:
|
|
227
|
+
columns: ArrayKey<T>[]
|
|
241
228
|
references: {
|
|
242
|
-
table: string
|
|
243
|
-
columns: string[]
|
|
244
|
-
onDelete?: ForeignKeyAction
|
|
245
|
-
onUpdate?: ForeignKeyAction
|
|
246
|
-
}
|
|
247
|
-
}
|
|
229
|
+
table: string
|
|
230
|
+
columns: string[]
|
|
231
|
+
onDelete?: ForeignKeyAction
|
|
232
|
+
onUpdate?: ForeignKeyAction
|
|
233
|
+
}
|
|
234
|
+
}>
|
|
248
235
|
}
|
|
249
236
|
|
|
250
237
|
/**
|
|
@@ -252,17 +239,26 @@ export interface TableConstraints {
|
|
|
252
239
|
*/
|
|
253
240
|
export interface TableOptions<T> {
|
|
254
241
|
/** Add IF NOT EXISTS clause */
|
|
255
|
-
ifNotExists?: boolean
|
|
242
|
+
ifNotExists?: boolean
|
|
256
243
|
/** Create WITHOUT ROWID table */
|
|
257
|
-
withoutRowId?: boolean
|
|
244
|
+
withoutRowId?: boolean
|
|
258
245
|
/** Table constraints */
|
|
259
|
-
constraints?: TableConstraints
|
|
246
|
+
constraints?: TableConstraints<T>
|
|
260
247
|
/** Temporary table */
|
|
261
|
-
temporary?: boolean
|
|
248
|
+
temporary?: boolean
|
|
262
249
|
/** Table comment */
|
|
263
|
-
comment?: string
|
|
250
|
+
comment?: string
|
|
251
|
+
|
|
252
|
+
parser?: Partial<Parser<T>>
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export interface Parser<T> extends ModuleParser<T> {
|
|
256
|
+
JSON?: ArrayKey<T>
|
|
257
|
+
BOOLEAN?: ArrayKey<T>
|
|
258
|
+
}
|
|
264
259
|
|
|
265
|
-
|
|
260
|
+
interface ModuleParser<T> {
|
|
261
|
+
MODULE?: Partial<Record<keyof T, Bun.TranspilerOptions>>
|
|
266
262
|
}
|
|
267
263
|
|
|
268
264
|
/**
|
|
@@ -274,8 +270,8 @@ export const column = {
|
|
|
274
270
|
*/
|
|
275
271
|
integer: (
|
|
276
272
|
constraints?: ColumnConstraints & {
|
|
277
|
-
size?: "TINYINT" | "SMALLINT" | "MEDIUMINT" | "BIGINT"
|
|
278
|
-
}
|
|
273
|
+
size?: "TINYINT" | "SMALLINT" | "MEDIUMINT" | "BIGINT"
|
|
274
|
+
}
|
|
279
275
|
): ColumnDefinition => ({
|
|
280
276
|
type: constraints?.size || SQLiteTypes.INTEGER,
|
|
281
277
|
...constraints,
|
|
@@ -286,9 +282,9 @@ export const column = {
|
|
|
286
282
|
*/
|
|
287
283
|
text: (
|
|
288
284
|
constraints?: ColumnConstraints & {
|
|
289
|
-
length?: number
|
|
290
|
-
variant?: "VARCHAR" | "CHAR" | "CLOB" | "NCHAR" | "NVARCHAR"
|
|
291
|
-
}
|
|
285
|
+
length?: number
|
|
286
|
+
variant?: "VARCHAR" | "CHAR" | "CLOB" | "NCHAR" | "NVARCHAR"
|
|
287
|
+
}
|
|
292
288
|
): ColumnDefinition => ({
|
|
293
289
|
type: constraints?.variant || SQLiteTypes.TEXT,
|
|
294
290
|
length: constraints?.length,
|
|
@@ -298,9 +294,7 @@ export const column = {
|
|
|
298
294
|
/**
|
|
299
295
|
* Create a REAL column
|
|
300
296
|
*/
|
|
301
|
-
real: (
|
|
302
|
-
constraints?: ColumnConstraints & { variant?: "DOUBLE" | "FLOAT" },
|
|
303
|
-
): ColumnDefinition => ({
|
|
297
|
+
real: (constraints?: ColumnConstraints & { variant?: "DOUBLE" | "FLOAT" }): ColumnDefinition => ({
|
|
304
298
|
type: constraints?.variant || SQLiteTypes.REAL,
|
|
305
299
|
...constraints,
|
|
306
300
|
}),
|
|
@@ -318,10 +312,10 @@ export const column = {
|
|
|
318
312
|
*/
|
|
319
313
|
numeric: (
|
|
320
314
|
constraints?: ColumnConstraints & {
|
|
321
|
-
precision?: number
|
|
322
|
-
scale?: number
|
|
323
|
-
variant?: "DECIMAL"
|
|
324
|
-
}
|
|
315
|
+
precision?: number
|
|
316
|
+
scale?: number
|
|
317
|
+
variant?: "DECIMAL"
|
|
318
|
+
}
|
|
325
319
|
): ColumnDefinition => ({
|
|
326
320
|
type: constraints?.variant || SQLiteTypes.NUMERIC,
|
|
327
321
|
precision: constraints?.precision,
|
|
@@ -348,9 +342,7 @@ export const column = {
|
|
|
348
342
|
/**
|
|
349
343
|
* Create a TIMESTAMP column (stored as INTEGER by default)
|
|
350
344
|
*/
|
|
351
|
-
timestamp: (
|
|
352
|
-
constraints?: ColumnConstraints & { asText?: boolean },
|
|
353
|
-
): ColumnDefinition => ({
|
|
345
|
+
timestamp: (constraints?: ColumnConstraints & { asText?: boolean }): ColumnDefinition => ({
|
|
354
346
|
type: constraints?.asText ? SQLiteTypes.TEXT : SQLiteTypes.INTEGER,
|
|
355
347
|
...constraints,
|
|
356
348
|
}),
|
|
@@ -375,23 +367,16 @@ export const column = {
|
|
|
375
367
|
/**
|
|
376
368
|
* Create a JSON column (stored as TEXT)
|
|
377
369
|
*/
|
|
378
|
-
json: (
|
|
379
|
-
constraints: ColumnConstraints & { validateJson?: boolean },
|
|
380
|
-
): ColumnDefinition => ({
|
|
370
|
+
json: (constraints?: ColumnConstraints & { validateJson?: boolean }): ColumnDefinition => ({
|
|
381
371
|
type: SQLiteTypes.JSON,
|
|
382
|
-
check: constraints?.validateJson
|
|
383
|
-
? "JSON_VALID({{COLUMN}})"
|
|
384
|
-
: constraints?.check,
|
|
372
|
+
check: constraints?.validateJson ? "JSON_VALID({{COLUMN}})" : constraints?.check,
|
|
385
373
|
...constraints,
|
|
386
374
|
}),
|
|
387
375
|
|
|
388
376
|
/**
|
|
389
377
|
* Create a VARCHAR column with specified length
|
|
390
378
|
*/
|
|
391
|
-
varchar: (
|
|
392
|
-
length: number,
|
|
393
|
-
constraints?: ColumnConstraints,
|
|
394
|
-
): ColumnDefinition => ({
|
|
379
|
+
varchar: (length: number, constraints?: ColumnConstraints): ColumnDefinition => ({
|
|
395
380
|
type: SQLiteTypes.VARCHAR,
|
|
396
381
|
length,
|
|
397
382
|
...constraints,
|
|
@@ -400,10 +385,7 @@ export const column = {
|
|
|
400
385
|
/**
|
|
401
386
|
* Create a CHAR column with specified length
|
|
402
387
|
*/
|
|
403
|
-
char: (
|
|
404
|
-
length: number,
|
|
405
|
-
constraints?: ColumnConstraints,
|
|
406
|
-
): ColumnDefinition => ({
|
|
388
|
+
char: (length: number, constraints?: ColumnConstraints): ColumnDefinition => ({
|
|
407
389
|
type: SQLiteTypes.CHAR,
|
|
408
390
|
length,
|
|
409
391
|
...constraints,
|
|
@@ -413,10 +395,7 @@ export const column = {
|
|
|
413
395
|
* Create an auto-incrementing primary key column
|
|
414
396
|
*/
|
|
415
397
|
id: (
|
|
416
|
-
constraints?: Omit<
|
|
417
|
-
ColumnConstraints,
|
|
418
|
-
"primaryKey" | "autoincrement" | "notNull"
|
|
419
|
-
>,
|
|
398
|
+
constraints?: Omit<ColumnConstraints, "primaryKey" | "autoincrement" | "notNull">
|
|
420
399
|
): ColumnDefinition => ({
|
|
421
400
|
type: SQLiteTypes.INTEGER,
|
|
422
401
|
primaryKey: true,
|
|
@@ -428,15 +407,13 @@ export const column = {
|
|
|
428
407
|
/**
|
|
429
408
|
* Create a UUID column (stored as TEXT)
|
|
430
409
|
*/
|
|
431
|
-
uuid: (
|
|
432
|
-
constraints?: ColumnConstraints & { generateDefault?: boolean },
|
|
433
|
-
): ColumnDefinition => ({
|
|
410
|
+
uuid: (constraints?: ColumnConstraints & { generateDefault?: boolean }): ColumnDefinition => ({
|
|
434
411
|
type: SQLiteTypes.TEXT,
|
|
435
412
|
length: 36,
|
|
436
413
|
default: constraints?.generateDefault
|
|
437
414
|
? defaultExpr(
|
|
438
|
-
|
|
439
|
-
|
|
415
|
+
"lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))"
|
|
416
|
+
)
|
|
440
417
|
: constraints?.default,
|
|
441
418
|
...constraints,
|
|
442
419
|
}),
|
|
@@ -446,8 +423,8 @@ export const column = {
|
|
|
446
423
|
*/
|
|
447
424
|
createdAt: (
|
|
448
425
|
constraints?: Omit<ColumnConstraints, "default" | "notNull"> & {
|
|
449
|
-
asText?: boolean
|
|
450
|
-
}
|
|
426
|
+
asText?: boolean
|
|
427
|
+
}
|
|
451
428
|
): ColumnDefinition => ({
|
|
452
429
|
type: constraints?.asText ? SQLiteTypes.DATETIME : SQLiteTypes.INTEGER,
|
|
453
430
|
notNull: true,
|
|
@@ -457,11 +434,22 @@ export const column = {
|
|
|
457
434
|
...constraints,
|
|
458
435
|
}),
|
|
459
436
|
|
|
437
|
+
/**
|
|
438
|
+
* Creates a function Column that will be parsed and transpiled using Bun.transpiler
|
|
439
|
+
*/
|
|
440
|
+
module: (constraints?: ColumnConstraints): ColumnDefinition => ({
|
|
441
|
+
type: SQLiteTypes.MODULE,
|
|
442
|
+
comment:
|
|
443
|
+
constraints?.comment ||
|
|
444
|
+
"A simple Module column, with automatic serilisation and deserilisation using Bun.Transpiler()",
|
|
445
|
+
...constraints,
|
|
446
|
+
}),
|
|
447
|
+
|
|
460
448
|
/**
|
|
461
449
|
* Create an updated_at timestamp column
|
|
462
450
|
*/
|
|
463
451
|
updatedAt: (
|
|
464
|
-
constraints?: Omit<ColumnConstraints, "default"> & { asText?: boolean }
|
|
452
|
+
constraints?: Omit<ColumnConstraints, "default"> & { asText?: boolean }
|
|
465
453
|
): ColumnDefinition => ({
|
|
466
454
|
type: constraints?.asText ? SQLiteTypes.DATETIME : SQLiteTypes.INTEGER,
|
|
467
455
|
default: constraints?.asText
|
|
@@ -477,10 +465,10 @@ export const column = {
|
|
|
477
465
|
refTable: string,
|
|
478
466
|
refColumn = "id",
|
|
479
467
|
constraints?: ColumnConstraints & {
|
|
480
|
-
onDelete?: ForeignKeyAction
|
|
481
|
-
onUpdate?: ForeignKeyAction
|
|
482
|
-
type?: SQLiteType
|
|
483
|
-
}
|
|
468
|
+
onDelete?: ForeignKeyAction
|
|
469
|
+
onUpdate?: ForeignKeyAction
|
|
470
|
+
type?: SQLiteType
|
|
471
|
+
}
|
|
484
472
|
): ColumnDefinition => ({
|
|
485
473
|
type: constraints?.type || SQLiteTypes.INTEGER,
|
|
486
474
|
references: {
|
|
@@ -495,16 +483,13 @@ export const column = {
|
|
|
495
483
|
/**
|
|
496
484
|
* Create an enum column (with CHECK constraint)
|
|
497
485
|
*/
|
|
498
|
-
enum: (
|
|
499
|
-
values: string[],
|
|
500
|
-
constraints?: ColumnConstraints,
|
|
501
|
-
): ColumnDefinition => ({
|
|
486
|
+
enum: (values: string[], constraints?: ColumnConstraints): ColumnDefinition => ({
|
|
502
487
|
type: SQLiteTypes.TEXT,
|
|
503
488
|
notNull: true,
|
|
504
489
|
check: `{{COLUMN}} IN (${values.map((v) => `'${v}'`).join(", ")})`,
|
|
505
490
|
...constraints,
|
|
506
491
|
}),
|
|
507
|
-
}
|
|
492
|
+
}
|
|
508
493
|
|
|
509
494
|
/**
|
|
510
495
|
* SQL function helpers for use in defaults and expressions
|
|
@@ -527,97 +512,97 @@ export const sql = {
|
|
|
527
512
|
null: () => null,
|
|
528
513
|
true: () => 1,
|
|
529
514
|
false: () => 0,
|
|
530
|
-
}
|
|
515
|
+
}
|
|
531
516
|
|
|
532
517
|
/**
|
|
533
518
|
* Enhanced createTable method signature
|
|
534
519
|
*/
|
|
535
|
-
export type CreateTableColumns = string | Record<string, string> | TableSchema
|
|
520
|
+
export type CreateTableColumns = string | Record<string, string> | TableSchema
|
|
536
521
|
|
|
537
522
|
/**
|
|
538
523
|
* Query builder state interface
|
|
539
524
|
*/
|
|
540
525
|
export interface QueryBuilderState<T extends Record<string, unknown>> {
|
|
541
|
-
db: Database
|
|
542
|
-
tableName: string
|
|
543
|
-
whereConditions: string[]
|
|
544
|
-
whereParams: SQLQueryBindings[]
|
|
526
|
+
db: Database // Database instance from bun:sqlite
|
|
527
|
+
tableName: string
|
|
528
|
+
whereConditions: string[]
|
|
529
|
+
whereParams: SQLQueryBindings[]
|
|
545
530
|
regexConditions: Array<{
|
|
546
|
-
column: keyof T
|
|
547
|
-
regex: RegExp
|
|
548
|
-
}
|
|
549
|
-
|
|
531
|
+
column: keyof T
|
|
532
|
+
regex: RegExp
|
|
533
|
+
}>
|
|
534
|
+
parser?: Parser<T>
|
|
550
535
|
}
|
|
551
536
|
|
|
552
537
|
/**
|
|
553
538
|
* Column names type for SELECT operations
|
|
554
539
|
*/
|
|
555
|
-
export type ColumnNames<T> = ["*"] | Array<keyof T
|
|
540
|
+
export type ColumnNames<T> = ["*"] | Array<keyof T>
|
|
556
541
|
|
|
557
542
|
/**
|
|
558
543
|
* Order direction for ORDER BY clauses
|
|
559
544
|
*/
|
|
560
|
-
export type OrderDirection = "ASC" | "DESC"
|
|
545
|
+
export type OrderDirection = "ASC" | "DESC"
|
|
561
546
|
|
|
562
547
|
/**
|
|
563
548
|
* WHERE condition object type
|
|
564
549
|
*/
|
|
565
|
-
export type WhereCondition<T> = Partial<T
|
|
550
|
+
export type WhereCondition<T> = Partial<T>
|
|
566
551
|
|
|
567
552
|
/**
|
|
568
553
|
* Regex condition object type
|
|
569
554
|
*/
|
|
570
|
-
export type RegexCondition<T> = Partial<Record<keyof T, RegExp | string
|
|
555
|
+
export type RegexCondition<T> = Partial<Record<keyof T, RegExp | string>>
|
|
571
556
|
|
|
572
557
|
/**
|
|
573
558
|
* Insert result interface
|
|
574
559
|
*/
|
|
575
560
|
export interface InsertResult {
|
|
576
|
-
insertId: number
|
|
577
|
-
changes: number
|
|
561
|
+
insertId: number
|
|
562
|
+
changes: number
|
|
578
563
|
}
|
|
579
564
|
|
|
580
565
|
/**
|
|
581
566
|
* Update result interface
|
|
582
567
|
*/
|
|
583
568
|
export interface UpdateResult {
|
|
584
|
-
changes: number
|
|
569
|
+
changes: number
|
|
585
570
|
}
|
|
586
571
|
|
|
587
572
|
/**
|
|
588
573
|
* Delete result interface
|
|
589
574
|
*/
|
|
590
575
|
export interface DeleteResult {
|
|
591
|
-
changes: number
|
|
576
|
+
changes: number
|
|
592
577
|
}
|
|
593
578
|
|
|
594
579
|
/**
|
|
595
580
|
* Insert options interface
|
|
596
581
|
*/
|
|
597
582
|
export interface InsertOptions {
|
|
598
|
-
orIgnore?: boolean
|
|
599
|
-
orReplace?: boolean
|
|
600
|
-
orAbort?: boolean
|
|
601
|
-
orFail?: boolean
|
|
602
|
-
orRollback?: boolean
|
|
583
|
+
orIgnore?: boolean
|
|
584
|
+
orReplace?: boolean
|
|
585
|
+
orAbort?: boolean
|
|
586
|
+
orFail?: boolean
|
|
587
|
+
orRollback?: boolean
|
|
603
588
|
}
|
|
604
589
|
|
|
605
590
|
/**
|
|
606
591
|
* JSON column configuration
|
|
607
592
|
*/
|
|
608
|
-
export type
|
|
593
|
+
export type ArrayKey<T> = Array<keyof T>
|
|
609
594
|
|
|
610
595
|
/**
|
|
611
596
|
* Generic database row type
|
|
612
597
|
*/
|
|
613
|
-
export type DatabaseRow = Record<string, unknown
|
|
598
|
+
export type DatabaseRow = Record<string, unknown>
|
|
614
599
|
|
|
615
600
|
/**
|
|
616
601
|
* SQL parameter type
|
|
617
602
|
*/
|
|
618
|
-
export type SqlParameter = SQLQueryBindings
|
|
603
|
+
export type SqlParameter = SQLQueryBindings
|
|
619
604
|
|
|
620
605
|
/**
|
|
621
606
|
* Database row with unknown structure
|
|
622
607
|
*/
|
|
623
|
-
export type DatabaseRowData = Record<string, SQLQueryBindings
|
|
608
|
+
export type DatabaseRowData = Record<string, SQLQueryBindings>
|
package/utils/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for sqlite-wrapper
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all utility modules for easy importing.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Logger utilities
|
|
8
|
+
export {
|
|
9
|
+
addLoggerParents,
|
|
10
|
+
createLogger,
|
|
11
|
+
logger,
|
|
12
|
+
SqliteLogger,
|
|
13
|
+
} from "./logger"
|
|
14
|
+
|
|
15
|
+
// SQL utilities
|
|
16
|
+
export {
|
|
17
|
+
buildBetweenClause,
|
|
18
|
+
buildCondition,
|
|
19
|
+
buildDeleteSQL,
|
|
20
|
+
buildInClause,
|
|
21
|
+
buildInsertSQL,
|
|
22
|
+
buildPlaceholders,
|
|
23
|
+
buildSelectSQL,
|
|
24
|
+
buildSetClause,
|
|
25
|
+
buildUpdateSQL,
|
|
26
|
+
escapeValue,
|
|
27
|
+
isSQLFunction,
|
|
28
|
+
normalizeOperator,
|
|
29
|
+
quoteIdentifier,
|
|
30
|
+
quoteIdentifiers,
|
|
31
|
+
quoteString,
|
|
32
|
+
} from "./sql"
|
|
33
|
+
|
|
34
|
+
// Transformer utilities
|
|
35
|
+
export {
|
|
36
|
+
getParserSummary,
|
|
37
|
+
hasTransformations,
|
|
38
|
+
type RowData,
|
|
39
|
+
type TransformOptions,
|
|
40
|
+
transformFromDb,
|
|
41
|
+
transformRowsFromDb,
|
|
42
|
+
transformRowsToDb,
|
|
43
|
+
transformToDb,
|
|
44
|
+
} from "./transformer"
|