@dockstat/sqlite-wrapper 1.2.3 → 1.2.5

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/index.ts CHANGED
@@ -209,9 +209,8 @@ class DB {
209
209
  */
210
210
  createTable<_T extends Record<string, unknown>>(
211
211
  tableName: string,
212
- columns: string | Record<string, string> | TableSchema,
213
- options?: TableOptions
214
- ): void {
212
+ columns: string | Record<string, string> | Partial<Record<Extract<keyof _T, string>, ColumnDefinition>> | TableSchema, options?: TableOptions<_T>,
213
+ ): QueryBuilder<_T> {
215
214
  const temp = options?.temporary ? 'TEMPORARY ' : ''
216
215
  const ifNot = options?.ifNotExists ? 'IF NOT EXISTS ' : ''
217
216
  const withoutRowId = options?.withoutRowId ? ' WITHOUT ROWID' : ''
@@ -276,6 +275,8 @@ class DB {
276
275
  if (options?.comment) {
277
276
  this.setTableComment(tableName, options.comment)
278
277
  }
278
+
279
+ return this.table<_T>(tableName)
279
280
  }
280
281
 
281
282
  /**
package/package.json CHANGED
@@ -1,11 +1,16 @@
1
1
  {
2
2
  "name": "@dockstat/sqlite-wrapper",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "A TypeScript wrapper around bun:sqlite with type-safe query building",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
7
7
  "types": "./index.ts",
8
- "files": ["index.ts", "query-builder/**/*", "README.md", "types.ts"],
8
+ "files": [
9
+ "index.ts",
10
+ "query-builder/**/*",
11
+ "README.md",
12
+ "types.ts"
13
+ ],
9
14
  "scripts": {
10
15
  "dev": "bun build index.ts",
11
16
  "lint": "biome lint .",
@@ -36,7 +36,6 @@ export abstract class BaseQueryBuilder<T extends Record<string, unknown>> {
36
36
  regexConditions: [],
37
37
  jsonColumns: jsonConfig?.jsonColumns,
38
38
  }
39
- this.logger.debug(`Created QueryBuilder for table: ${tableName}`)
40
39
  }
41
40
 
42
41
  /**
@@ -183,7 +183,7 @@ export class SelectQueryBuilder<
183
183
  `Executing SELECT query - query: ${query}, params: ${JSON.stringify(params)}, hasJsonColumns: ${!!this.state.jsonColumns}`
184
184
  )
185
185
  const rows = this.getDb()
186
- .prepare(query)
186
+ .prepare(query, params)
187
187
  .all() as T[]
188
188
  this.getLogger().debug(`Retrieved ${rows.length} rows from database`)
189
189
  const transformed = this.transformRowsFromDb(rows)
@@ -206,9 +206,6 @@ export class WhereQueryBuilder<
206
206
  throw new Error(`whereOp: operator "${op}" not supported`);
207
207
  }
208
208
 
209
- // Remove any existing conditions for this column
210
- this.removeExistingCondition(String(column));
211
-
212
209
  // Handle null special-casing for IS / IS NOT and equality operators
213
210
  if (
214
211
  (value === null || value === undefined) &&
package/types.ts CHANGED
@@ -223,6 +223,9 @@ export interface ColumnDefinition extends ColumnConstraints {
223
223
  */
224
224
  export type TableSchema = Record<string, ColumnDefinition>;
225
225
 
226
+
227
+ export type TypedTableSchema<T extends string = string> = Record<T, ColumnDefinition>;
228
+
226
229
  /**
227
230
  * Table constraint types
228
231
  */
@@ -248,7 +251,7 @@ export interface TableConstraints {
248
251
  /**
249
252
  * Enhanced table options
250
253
  */
251
- export interface TableOptions {
254
+ export interface TableOptions<T> {
252
255
  /** Add IF NOT EXISTS clause */
253
256
  ifNotExists?: boolean;
254
257
  /** Create WITHOUT ROWID table */
@@ -259,6 +262,8 @@ export interface TableOptions {
259
262
  temporary?: boolean;
260
263
  /** Table comment */
261
264
  comment?: string;
265
+ /** JSON config for table creation and returned query builder */
266
+ jsonConfig?: Array<keyof T>;
262
267
  }
263
268
 
264
269
  /**
@@ -372,10 +377,10 @@ export const column = {
372
377
  * Create a JSON column (stored as TEXT)
373
378
  */
374
379
  json: (
375
- constraints?: ColumnConstraints & { validateJson?: boolean },
380
+ constraints: ColumnConstraints & { validateJson?: boolean },
376
381
  ): ColumnDefinition => ({
377
382
  type: SQLiteTypes.JSON,
378
- check: constraints?.validateJson
383
+ check: constraints?.validateJson ?? true
379
384
  ? "JSON_VALID({{COLUMN}})"
380
385
  : constraints?.check,
381
386
  ...constraints,
@@ -431,8 +436,8 @@ export const column = {
431
436
  length: 36,
432
437
  default: constraints?.generateDefault
433
438
  ? defaultExpr(
434
- "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)))",
435
- )
439
+ "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)))",
440
+ )
436
441
  : constraints?.default,
437
442
  ...constraints,
438
443
  }),
@@ -496,6 +501,7 @@ export const column = {
496
501
  constraints?: ColumnConstraints,
497
502
  ): ColumnDefinition => ({
498
503
  type: SQLiteTypes.TEXT,
504
+ notNull: true,
499
505
  check: `{{COLUMN}} IN (${values.map((v) => `'${v}'`).join(", ")})`,
500
506
  ...constraints,
501
507
  }),