@dockstat/sqlite-wrapper 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 +578 -0
- package/dist/index.d.ts +412 -0
- package/dist/index.js +12 -0
- package/package.json +58 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
import { Database } from 'bun:sqlite';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Utility types for query building
|
|
7
|
+
*/
|
|
8
|
+
export type ColumnNames<T> = Array<keyof T> | [
|
|
9
|
+
"*"
|
|
10
|
+
];
|
|
11
|
+
export type WhereCondition<T> = Partial<Record<keyof T, string | number | boolean | null>>;
|
|
12
|
+
export type RegexCondition<T> = Partial<Record<keyof T, string | RegExp>>;
|
|
13
|
+
/**
|
|
14
|
+
* Result types for mutation operations
|
|
15
|
+
*/
|
|
16
|
+
export interface InsertResult {
|
|
17
|
+
/** The rowid of the last inserted row */
|
|
18
|
+
insertId: number;
|
|
19
|
+
/** Number of rows that were inserted */
|
|
20
|
+
changes: number;
|
|
21
|
+
}
|
|
22
|
+
export interface UpdateResult {
|
|
23
|
+
/** Number of rows that were updated */
|
|
24
|
+
changes: number;
|
|
25
|
+
}
|
|
26
|
+
export interface DeleteResult {
|
|
27
|
+
/** Number of rows that were deleted */
|
|
28
|
+
changes: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Options for INSERT operations
|
|
32
|
+
*/
|
|
33
|
+
export interface InsertOptions {
|
|
34
|
+
/** Use INSERT OR IGNORE */
|
|
35
|
+
orIgnore?: boolean;
|
|
36
|
+
/** Use INSERT OR REPLACE */
|
|
37
|
+
orReplace?: boolean;
|
|
38
|
+
/** Use INSERT OR ABORT (default) */
|
|
39
|
+
orAbort?: boolean;
|
|
40
|
+
/** Use INSERT OR FAIL */
|
|
41
|
+
orFail?: boolean;
|
|
42
|
+
/** Use INSERT OR ROLLBACK */
|
|
43
|
+
orRollback?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Configuration for JSON columns that should be automatically serialized/deserialized
|
|
47
|
+
*/
|
|
48
|
+
export interface JsonColumnConfig<T> {
|
|
49
|
+
/** Columns that contain JSON data and should be auto-serialized/deserialized */
|
|
50
|
+
jsonColumns?: Array<keyof T>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Main QueryBuilder class that combines all functionality using composition.
|
|
54
|
+
* This class provides a unified interface for SELECT, INSERT, UPDATE, and DELETE operations.
|
|
55
|
+
*
|
|
56
|
+
* Each operation type is implemented in a separate module for better maintainability:
|
|
57
|
+
* - SELECT: column selection, ordering, limiting, result execution
|
|
58
|
+
* - INSERT: single/bulk inserts with conflict resolution
|
|
59
|
+
* - UPDATE: safe updates with mandatory WHERE conditions
|
|
60
|
+
* - DELETE: safe deletes with mandatory WHERE conditions
|
|
61
|
+
* - WHERE: shared conditional logic across all operations
|
|
62
|
+
*/
|
|
63
|
+
export declare class QueryBuilder<T extends Record<string, any>> {
|
|
64
|
+
private selectBuilder;
|
|
65
|
+
private insertBuilder;
|
|
66
|
+
private updateBuilder;
|
|
67
|
+
private deleteBuilder;
|
|
68
|
+
constructor(db: Database, tableName: string, jsonConfig?: JsonColumnConfig<T>);
|
|
69
|
+
/**
|
|
70
|
+
* Synchronize the state between all builders so WHERE conditions are shared.
|
|
71
|
+
*/
|
|
72
|
+
private syncBuilderStates;
|
|
73
|
+
/**
|
|
74
|
+
* Add simple equality conditions to the WHERE clause.
|
|
75
|
+
*/
|
|
76
|
+
where(conditions: WhereCondition<T>): this;
|
|
77
|
+
/**
|
|
78
|
+
* Add regex conditions (applied client-side).
|
|
79
|
+
*/
|
|
80
|
+
whereRgx(conditions: RegexCondition<T>): this;
|
|
81
|
+
/**
|
|
82
|
+
* Add a raw SQL WHERE fragment with parameter binding.
|
|
83
|
+
*/
|
|
84
|
+
whereExpr(expr: string, params?: any[]): this;
|
|
85
|
+
/**
|
|
86
|
+
* Alias for whereExpr.
|
|
87
|
+
*/
|
|
88
|
+
whereRaw(expr: string, params?: any[]): this;
|
|
89
|
+
/**
|
|
90
|
+
* Add an IN clause with proper parameter binding.
|
|
91
|
+
*/
|
|
92
|
+
whereIn(column: keyof T, values: any[]): this;
|
|
93
|
+
/**
|
|
94
|
+
* Add a NOT IN clause with proper parameter binding.
|
|
95
|
+
*/
|
|
96
|
+
whereNotIn(column: keyof T, values: any[]): this;
|
|
97
|
+
/**
|
|
98
|
+
* Add a comparison operator condition.
|
|
99
|
+
*/
|
|
100
|
+
whereOp(column: keyof T, op: string, value: any): this;
|
|
101
|
+
/**
|
|
102
|
+
* Add a BETWEEN condition.
|
|
103
|
+
*/
|
|
104
|
+
whereBetween(column: keyof T, min: any, max: any): this;
|
|
105
|
+
/**
|
|
106
|
+
* Add a NOT BETWEEN condition.
|
|
107
|
+
*/
|
|
108
|
+
whereNotBetween(column: keyof T, min: any, max: any): this;
|
|
109
|
+
/**
|
|
110
|
+
* Add an IS NULL condition.
|
|
111
|
+
*/
|
|
112
|
+
whereNull(column: keyof T): this;
|
|
113
|
+
/**
|
|
114
|
+
* Add an IS NOT NULL condition.
|
|
115
|
+
*/
|
|
116
|
+
whereNotNull(column: keyof T): this;
|
|
117
|
+
/**
|
|
118
|
+
* Specify which columns to select.
|
|
119
|
+
*/
|
|
120
|
+
select(columns: ColumnNames<T>): this;
|
|
121
|
+
/**
|
|
122
|
+
* Add ORDER BY clause.
|
|
123
|
+
*/
|
|
124
|
+
orderBy(column: keyof T): this;
|
|
125
|
+
/**
|
|
126
|
+
* Set order direction to descending.
|
|
127
|
+
*/
|
|
128
|
+
desc(): this;
|
|
129
|
+
/**
|
|
130
|
+
* Set order direction to ascending.
|
|
131
|
+
*/
|
|
132
|
+
asc(): this;
|
|
133
|
+
/**
|
|
134
|
+
* Add LIMIT clause.
|
|
135
|
+
*/
|
|
136
|
+
limit(amount: number): this;
|
|
137
|
+
/**
|
|
138
|
+
* Add OFFSET clause.
|
|
139
|
+
*/
|
|
140
|
+
offset(start: number): this;
|
|
141
|
+
/**
|
|
142
|
+
* Execute the query and return all matching rows.
|
|
143
|
+
*/
|
|
144
|
+
all(): T[];
|
|
145
|
+
/**
|
|
146
|
+
* Execute the query and return the first matching row, or null.
|
|
147
|
+
*/
|
|
148
|
+
get(): T | null;
|
|
149
|
+
/**
|
|
150
|
+
* Execute the query and return the first matching row, or null.
|
|
151
|
+
*/
|
|
152
|
+
first(): T | null;
|
|
153
|
+
/**
|
|
154
|
+
* Execute a COUNT query and return the number of matching rows.
|
|
155
|
+
*/
|
|
156
|
+
count(): number;
|
|
157
|
+
/**
|
|
158
|
+
* Check if any rows match the current conditions.
|
|
159
|
+
*/
|
|
160
|
+
exists(): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Execute the query and return a single column value from the first row.
|
|
163
|
+
*/
|
|
164
|
+
value<K extends keyof T>(column: K): T[K] | null;
|
|
165
|
+
/**
|
|
166
|
+
* Execute the query and return an array of values from a single column.
|
|
167
|
+
*/
|
|
168
|
+
pluck<K extends keyof T>(column: K): T[K][];
|
|
169
|
+
/**
|
|
170
|
+
* Insert a single row or multiple rows into the table.
|
|
171
|
+
*/
|
|
172
|
+
insert(data: Partial<T> | Partial<T>[], options?: InsertOptions): InsertResult;
|
|
173
|
+
/**
|
|
174
|
+
* Insert with OR IGNORE conflict resolution.
|
|
175
|
+
*/
|
|
176
|
+
insertOrIgnore(data: Partial<T> | Partial<T>[]): InsertResult;
|
|
177
|
+
/**
|
|
178
|
+
* Insert with OR REPLACE conflict resolution.
|
|
179
|
+
*/
|
|
180
|
+
insertOrReplace(data: Partial<T> | Partial<T>[]): InsertResult;
|
|
181
|
+
/**
|
|
182
|
+
* Insert with OR ABORT conflict resolution.
|
|
183
|
+
*/
|
|
184
|
+
insertOrAbort(data: Partial<T> | Partial<T>[]): InsertResult;
|
|
185
|
+
/**
|
|
186
|
+
* Insert with OR FAIL conflict resolution.
|
|
187
|
+
*/
|
|
188
|
+
insertOrFail(data: Partial<T> | Partial<T>[]): InsertResult;
|
|
189
|
+
/**
|
|
190
|
+
* Insert with OR ROLLBACK conflict resolution.
|
|
191
|
+
*/
|
|
192
|
+
insertOrRollback(data: Partial<T> | Partial<T>[]): InsertResult;
|
|
193
|
+
/**
|
|
194
|
+
* Insert and get the inserted row back.
|
|
195
|
+
*/
|
|
196
|
+
insertAndGet(data: Partial<T>, options?: InsertOptions): T | null;
|
|
197
|
+
/**
|
|
198
|
+
* Batch insert with transaction support.
|
|
199
|
+
*/
|
|
200
|
+
insertBatch(rows: Partial<T>[], options?: InsertOptions): InsertResult;
|
|
201
|
+
/**
|
|
202
|
+
* Update rows matching the WHERE conditions.
|
|
203
|
+
*/
|
|
204
|
+
update(data: Partial<T>): UpdateResult;
|
|
205
|
+
/**
|
|
206
|
+
* Update or insert (upsert) using INSERT OR REPLACE.
|
|
207
|
+
*/
|
|
208
|
+
upsert(data: Partial<T>): UpdateResult;
|
|
209
|
+
/**
|
|
210
|
+
* Increment a numeric column by a specified amount.
|
|
211
|
+
*/
|
|
212
|
+
increment(column: keyof T, amount?: number): UpdateResult;
|
|
213
|
+
/**
|
|
214
|
+
* Decrement a numeric column by a specified amount.
|
|
215
|
+
*/
|
|
216
|
+
decrement(column: keyof T, amount?: number): UpdateResult;
|
|
217
|
+
/**
|
|
218
|
+
* Update and get the updated rows back.
|
|
219
|
+
*/
|
|
220
|
+
updateAndGet(data: Partial<T>): T[];
|
|
221
|
+
/**
|
|
222
|
+
* Batch update multiple rows with different values.
|
|
223
|
+
*/
|
|
224
|
+
updateBatch(updates: Array<{
|
|
225
|
+
where: Partial<T>;
|
|
226
|
+
data: Partial<T>;
|
|
227
|
+
}>): UpdateResult;
|
|
228
|
+
/**
|
|
229
|
+
* Delete rows matching the WHERE conditions.
|
|
230
|
+
*/
|
|
231
|
+
delete(): DeleteResult;
|
|
232
|
+
/**
|
|
233
|
+
* Delete and get the deleted rows back.
|
|
234
|
+
*/
|
|
235
|
+
deleteAndGet(): T[];
|
|
236
|
+
/**
|
|
237
|
+
* Soft delete - mark rows as deleted instead of physically removing them.
|
|
238
|
+
*/
|
|
239
|
+
softDelete(deletedColumn?: keyof T, deletedValue?: any): DeleteResult;
|
|
240
|
+
/**
|
|
241
|
+
* Restore soft deleted rows.
|
|
242
|
+
*/
|
|
243
|
+
restore(deletedColumn?: keyof T): DeleteResult;
|
|
244
|
+
/**
|
|
245
|
+
* Batch delete multiple sets of rows.
|
|
246
|
+
*/
|
|
247
|
+
deleteBatch(conditions: Array<Partial<T>>): DeleteResult;
|
|
248
|
+
/**
|
|
249
|
+
* Truncate the entire table (delete all rows).
|
|
250
|
+
*/
|
|
251
|
+
truncate(): DeleteResult;
|
|
252
|
+
/**
|
|
253
|
+
* Delete rows older than a specified timestamp.
|
|
254
|
+
*/
|
|
255
|
+
deleteOlderThan(timestampColumn: keyof T, olderThan: number): DeleteResult;
|
|
256
|
+
/**
|
|
257
|
+
* Delete duplicate rows based on specified columns.
|
|
258
|
+
*/
|
|
259
|
+
deleteDuplicates(columns: Array<keyof T>): DeleteResult;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* TypedSQLite — tiny wrapper around bun:sqlite `Database`.
|
|
263
|
+
*
|
|
264
|
+
* This class centralizes common helpers like `table()` (returns a typed QueryBuilder)
|
|
265
|
+
* and `createTable()` for creating tables easily.
|
|
266
|
+
*/
|
|
267
|
+
export declare class DB {
|
|
268
|
+
private db;
|
|
269
|
+
/**
|
|
270
|
+
* Open or create a SQLite database at `path`.
|
|
271
|
+
*
|
|
272
|
+
* @param path - Path to the SQLite file (e.g. "app.db"). Use ":memory:" for in-memory DB.
|
|
273
|
+
* @param options - Optional database configuration
|
|
274
|
+
*/
|
|
275
|
+
constructor(path: string, options?: {
|
|
276
|
+
pragmas?: Array<[
|
|
277
|
+
string,
|
|
278
|
+
any
|
|
279
|
+
]>;
|
|
280
|
+
loadExtensions?: string[];
|
|
281
|
+
});
|
|
282
|
+
/**
|
|
283
|
+
* Get a typed QueryBuilder for a given table name.
|
|
284
|
+
*
|
|
285
|
+
* Example:
|
|
286
|
+
* ```ts
|
|
287
|
+
* interface User { id: number; name: string; email: string; }
|
|
288
|
+
*
|
|
289
|
+
* // SELECT operations
|
|
290
|
+
* const users = db.table<User>("users")
|
|
291
|
+
* .select(["id", "name"])
|
|
292
|
+
* .where({ active: true })
|
|
293
|
+
* .orderBy("created_at")
|
|
294
|
+
* .desc()
|
|
295
|
+
* .limit(10)
|
|
296
|
+
* .all();
|
|
297
|
+
*
|
|
298
|
+
* // INSERT operations
|
|
299
|
+
* const insertResult = db.table<User>("users")
|
|
300
|
+
* .insert({ name: "John", email: "john@example.com" });
|
|
301
|
+
*
|
|
302
|
+
* // UPDATE operations
|
|
303
|
+
* const updateResult = db.table<User>("users")
|
|
304
|
+
* .where({ id: 1 })
|
|
305
|
+
* .update({ name: "Jane" });
|
|
306
|
+
*
|
|
307
|
+
* // DELETE operations
|
|
308
|
+
* const deleteResult = db.table<User>("users")
|
|
309
|
+
* .where({ active: false })
|
|
310
|
+
* .delete();
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* For tables with JSON columns, you can specify which columns should be auto-serialized/deserialized:
|
|
314
|
+
* ```ts
|
|
315
|
+
* interface Config { id: number; settings: object; metadata: any[]; }
|
|
316
|
+
*
|
|
317
|
+
* const config = db.table<Config>("config", { jsonColumns: ["settings", "metadata"] })
|
|
318
|
+
* .select(["*"])
|
|
319
|
+
* .get(); // settings and metadata will be automatically parsed from JSON
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* `QueryBuilder` supports the following notable methods:
|
|
323
|
+
*
|
|
324
|
+
* **SELECT methods:**
|
|
325
|
+
* - `select(columns: Array<keyof T> | ["*"])` — specify columns to select
|
|
326
|
+
* - `where(conditions: Partial<Record<keyof T, string | number | boolean | null>>)` — simple equality/IS NULL checks
|
|
327
|
+
* - `whereRgx(conditions: Partial<Record<keyof T, string | RegExp>>)` — regex conditions (applied client-side)
|
|
328
|
+
* - `whereExpr(expr: string, params?: any[])` / `whereRaw(...)` — raw SQL fragments with parameter binding
|
|
329
|
+
* - `whereIn(column: keyof T, values: any[])` — IN clause with parameter binding
|
|
330
|
+
* - `whereOp(column: keyof T, op: string, value: any)` — comparison operators (=, !=, <, >, <=, >=, LIKE, GLOB, IS)
|
|
331
|
+
* - `orderBy(column: keyof T)`, `asc()`, `desc()` — ordering
|
|
332
|
+
* - `limit(n)`, `offset(n)` — pagination
|
|
333
|
+
* - `all(): T[]`, `get(): T | null`, `first(): T | null`, `count(): number` — result execution
|
|
334
|
+
*
|
|
335
|
+
* **INSERT methods:**
|
|
336
|
+
* - `insert(data: Partial<T> | Partial<T>[], options?: InsertOptions): InsertResult` — insert single/multiple rows
|
|
337
|
+
* - `insertOrIgnore(data: Partial<T> | Partial<T>[]): InsertResult` — INSERT OR IGNORE
|
|
338
|
+
* - `insertOrReplace(data: Partial<T> | Partial<T>[]): InsertResult` — INSERT OR REPLACE
|
|
339
|
+
*
|
|
340
|
+
* **UPDATE methods:**
|
|
341
|
+
* - `update(data: Partial<T>): UpdateResult` — update rows (requires WHERE conditions)
|
|
342
|
+
*
|
|
343
|
+
* **DELETE methods:**
|
|
344
|
+
* - `delete(): DeleteResult` — delete rows (requires WHERE conditions)
|
|
345
|
+
*
|
|
346
|
+
* Notes:
|
|
347
|
+
* - Regex conditions in `whereRgx` are applied client-side after SQL execution
|
|
348
|
+
* - When regex conditions are present, ordering/limit/offset are also applied client-side
|
|
349
|
+
* - UPDATE and DELETE operations require at least one WHERE condition for safety
|
|
350
|
+
* - All mutations return result objects with `changes` count and `insertId` (for INSERT)
|
|
351
|
+
* - JSON columns are automatically serialized on INSERT/UPDATE and deserialized on SELECT
|
|
352
|
+
*
|
|
353
|
+
* @typeParam T - Row type for the table.
|
|
354
|
+
* @param tableName - The table name to operate on.
|
|
355
|
+
* @param jsonConfig - Optional configuration for JSON columns that should be auto-serialized/deserialized.
|
|
356
|
+
* @returns QueryBuilder<T>
|
|
357
|
+
*/
|
|
358
|
+
table<T extends Record<string, any>>(tableName: string, jsonConfig?: JsonColumnConfig<T>): QueryBuilder<T>;
|
|
359
|
+
/**
|
|
360
|
+
* Close the underlying SQLite database handle.
|
|
361
|
+
*
|
|
362
|
+
* After calling `close()` the instance should not be used.
|
|
363
|
+
*/
|
|
364
|
+
close(): void;
|
|
365
|
+
/**
|
|
366
|
+
* Create a table.
|
|
367
|
+
*
|
|
368
|
+
* Accepts either:
|
|
369
|
+
* - `columns` as a single SQL column-definition string:
|
|
370
|
+
* "id INTEGER PRIMARY KEY, name TEXT NOT NULL"
|
|
371
|
+
* - `columns` as object `{ colName: "SQL TYPE CONSTRAINTS", ... }`
|
|
372
|
+
*
|
|
373
|
+
* Options:
|
|
374
|
+
* - `ifNotExists`: prepend `IF NOT EXISTS`
|
|
375
|
+
* - `withoutRowId`: append `WITHOUT ROWID`
|
|
376
|
+
*
|
|
377
|
+
* The method will safely quote identifiers (double quotes).
|
|
378
|
+
*
|
|
379
|
+
* @param tableName - Table name to create.
|
|
380
|
+
* @param columns - Column definitions (SQL string) or a map of column→definition.
|
|
381
|
+
* @param options - Optional flags `{ ifNotExists?: boolean; withoutRowId?: boolean }`.
|
|
382
|
+
*
|
|
383
|
+
* @throws {Error} If column definitions are empty or invalid.
|
|
384
|
+
*/
|
|
385
|
+
createTable(tableName: string, columns: string | Record<string, string>, options?: {
|
|
386
|
+
ifNotExists?: boolean;
|
|
387
|
+
withoutRowId?: boolean;
|
|
388
|
+
}): void;
|
|
389
|
+
/**
|
|
390
|
+
* Set or get a PRAGMA value.
|
|
391
|
+
*
|
|
392
|
+
* @param name - PRAGMA name (e.g., "foreign_keys", "journal_mode")
|
|
393
|
+
* @param value - Value to set (omit to get current value)
|
|
394
|
+
* @returns Current value when getting, undefined when setting
|
|
395
|
+
*/
|
|
396
|
+
pragma(name: string, value?: any): any;
|
|
397
|
+
/**
|
|
398
|
+
* Load a SQLite extension.
|
|
399
|
+
*
|
|
400
|
+
* @param path - Absolute path to the compiled SQLite extension
|
|
401
|
+
*/
|
|
402
|
+
loadExtension(path: string): void;
|
|
403
|
+
/**
|
|
404
|
+
* Get direct access to the underlying SQLite database instance.
|
|
405
|
+
* Use this for advanced operations not covered by the QueryBuilder.
|
|
406
|
+
*
|
|
407
|
+
* @returns The underlying Database instance
|
|
408
|
+
*/
|
|
409
|
+
getDb(): Database;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export {};
|