sqlite-zod-orm 3.14.0 → 3.15.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqlite-zod-orm",
3
- "version": "3.14.0",
3
+ "version": "3.15.0",
4
4
  "description": "Type-safe SQLite ORM for Bun — Zod schemas, fluent queries, auto relationships, zero SQL",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/builder.ts CHANGED
@@ -27,7 +27,7 @@ import {
27
27
  * - Object-style: `.where({ name: 'Alice', age: { $gt: 18 } })`
28
28
  * - Callback-style (AST): `.where((c, f, op) => op.and(op.eq(c.name, 'Alice'), op.gt(c.age, 18)))`
29
29
  */
30
- export class QueryBuilder<T extends Record<string, any>> {
30
+ export class QueryBuilder<T extends Record<string, any>, TResult extends Record<string, any> = T> {
31
31
  private iqo: IQO;
32
32
  private tableName: string;
33
33
  private executor: (sql: string, params: any[], raw: boolean) => any[];
@@ -69,7 +69,9 @@ export class QueryBuilder<T extends Record<string, any>> {
69
69
  }
70
70
 
71
71
  /** Specify which columns to select. If called with no arguments, defaults to `*`. */
72
- select(...cols: (keyof T & string)[]): this {
72
+ select(): this;
73
+ select<K extends keyof T & string>(...cols: K[]): QueryBuilder<T, Pick<T, K>>;
74
+ select(...cols: string[]): any {
73
75
  this.iqo.selects.push(...cols);
74
76
  return this;
75
77
  }
@@ -264,20 +266,20 @@ export class QueryBuilder<T extends Record<string, any>> {
264
266
  // ---------- Terminal / Execution Methods ----------
265
267
 
266
268
  /** Execute the query and return all matching rows. */
267
- all(): T[] {
269
+ all(): TResult[] {
268
270
  const { sql, params } = compileIQO(this.tableName, this.iqo);
269
271
  const results = this.executor(sql, params, this.iqo.raw);
270
- return this._applyEagerLoads(results);
272
+ return this._applyEagerLoads(results) as unknown as TResult[];
271
273
  }
272
274
 
273
275
  /** Execute the query and return the first matching row, or null. */
274
- get(): T | null {
276
+ get(): TResult | null {
275
277
  this.iqo.limit = 1;
276
278
  const { sql, params } = compileIQO(this.tableName, this.iqo);
277
279
  const result = this.singleExecutor(sql, params, this.iqo.raw);
278
280
  if (!result) return null;
279
281
  const [loaded] = this._applyEagerLoads([result]);
280
- return loaded ?? null;
282
+ return (loaded ?? null) as TResult | null;
281
283
  }
282
284
 
283
285
  /** Execute the query and return the count of matching rows. */
@@ -291,7 +293,7 @@ export class QueryBuilder<T extends Record<string, any>> {
291
293
  }
292
294
 
293
295
  /** Alias for get() — returns the first matching row or null. */
294
- first(): T | null {
296
+ first(): TResult | null {
295
297
  return this.get();
296
298
  }
297
299
 
@@ -399,7 +401,7 @@ export class QueryBuilder<T extends Record<string, any>> {
399
401
  }
400
402
 
401
403
  /** Paginate results. Returns { data, total, page, perPage, pages }. */
402
- paginate(page: number = 1, perPage: number = 20): { data: T[]; total: number; page: number; perPage: number; pages: number } {
404
+ paginate(page: number = 1, perPage: number = 20): { data: TResult[]; total: number; page: number; perPage: number; pages: number } {
403
405
  const total = this.count();
404
406
  const pages = Math.ceil(total / perPage);
405
407
  this.iqo.limit = perPage;
@@ -434,10 +436,10 @@ export class QueryBuilder<T extends Record<string, any>> {
434
436
 
435
437
  // ---------- Thenable (async/await support) ----------
436
438
 
437
- then<TResult1 = T[], TResult2 = never>(
438
- onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | null,
439
- onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
440
- ): Promise<TResult1 | TResult2> {
439
+ then<TThen1 = TResult[], TThen2 = never>(
440
+ onfulfilled?: ((value: TResult[]) => TThen1 | PromiseLike<TThen1>) | null,
441
+ onrejected?: ((reason: any) => TThen2 | PromiseLike<TThen2>) | null,
442
+ ): Promise<TThen1 | TThen2> {
441
443
  try {
442
444
  const result = this.all();
443
445
  return Promise.resolve(result).then(onfulfilled, onrejected);
package/src/types.ts CHANGED
@@ -209,7 +209,10 @@ export type NavEntityAccessor<
209
209
  findOrCreate: (conditions: Partial<z.infer<S[Table & keyof S]>>, defaults?: Partial<z.infer<S[Table & keyof S]>>) => { entity: NavEntity<S, R, Table>; created: boolean };
210
210
  delete: ((id: number) => void) & (() => DeleteBuilder<NavEntity<S, R, Table>>);
211
211
  restore: (id: number) => void;
212
- select: (...cols: (keyof z.infer<S[Table & keyof S]> & string)[]) => QueryBuilder<NavEntity<S, R, Table>>;
212
+ select: {
213
+ (): QueryBuilder<NavEntity<S, R, Table>>;
214
+ <K extends (keyof z.infer<S[Table & keyof S]> | 'id') & string>(...cols: K[]): QueryBuilder<NavEntity<S, R, Table>, Pick<NavEntity<S, R, Table>, K>>;
215
+ };
213
216
  on: ((event: 'insert' | 'update', callback: (row: NavEntity<S, R, Table>) => void | Promise<void>) => () => void) &
214
217
  ((event: 'delete', callback: (row: { id: number }) => void | Promise<void>) => () => void);
215
218
  _tableName: string;
@@ -243,7 +246,10 @@ export type EntityAccessor<S extends z.ZodType<any>> = {
243
246
  delete: ((id: number) => void) & (() => DeleteBuilder<AugmentedEntity<S>>);
244
247
  /** Undo a soft delete by setting deletedAt = null. Requires softDeletes. */
245
248
  restore: (id: number) => void;
246
- select: (...cols: (keyof InferSchema<S> & string)[]) => QueryBuilder<AugmentedEntity<S>>;
249
+ select: {
250
+ (): QueryBuilder<AugmentedEntity<S>>;
251
+ <K extends (keyof InferSchema<S> | 'id') & string>(...cols: K[]): QueryBuilder<AugmentedEntity<S>, Pick<AugmentedEntity<S>, K>>;
252
+ };
247
253
  on: ((event: 'insert' | 'update', callback: (row: AugmentedEntity<S>) => void | Promise<void>) => () => void) &
248
254
  ((event: 'delete', callback: (row: { id: number }) => void | Promise<void>) => () => void);
249
255
  _tableName: string;