bun-query-builder 0.1.25 → 0.1.26

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/dist/client.d.ts CHANGED
@@ -103,8 +103,8 @@ export declare interface BaseSelectQueryBuilder<DB extends DatabaseSchema<any>,
103
103
  groupByRaw: (fragment: SqlFragment) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
104
104
  having: (expr: WhereExpression<any>) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
105
105
  havingRaw: (fragment: SqlFragment) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
106
- addSelect: (...columns: (keyof DB[TTable]['columns'] & string | string)[]) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
107
- select?: (columns: (keyof DB[TTable]['columns'] & string | string)[]) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
106
+ addSelect: (...columns: ((keyof DB[TTable]['columns'] & string) | string | SqlFragment)[]) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
107
+ select?: (columns: string | SqlFragment | ((keyof DB[TTable]['columns'] & string) | string | SqlFragment)[]) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
108
108
  selectAll?: () => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
109
109
  orderByRaw: (fragment: SqlFragment) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
110
110
  union: (other: { toSQL: () => any }) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
@@ -148,7 +148,7 @@ export declare interface BaseSelectQueryBuilder<DB extends DatabaseSchema<any>,
148
148
  explain: () => Promise<any[]>
149
149
  simple: () => any
150
150
  toText?: () => string
151
- paginate: (perPage: number, page?: number) => Promise<{ data: SelectedRow<DB, TTable, TSelected>[], meta: { perPage: number, page: number, total: number, lastPage: number } }>
151
+ paginate: (perPage: number, page?: number, opts?: { tx?: { unsafe: (sql: string, params?: any[]) => any } }) => Promise<{ data: SelectedRow<DB, TTable, TSelected>[], meta: { perPage: number, page: number, total: number, lastPage: number } }>
152
152
  simplePaginate: (perPage: number, page?: number) => Promise<{ data: SelectedRow<DB, TTable, TSelected>[], meta: { perPage: number, page: number, hasMore: boolean } }>
153
153
  toSQL: () => string
154
154
  execute: () => Promise<SelectedRow<DB, TTable, TSelected>[]>
package/dist/config.d.ts CHANGED
@@ -13,18 +13,4 @@ export declare function getPlaceholders(count: number, startIndex?: number): str
13
13
  export declare function getConfig(): Promise<QueryBuilderConfig>;
14
14
  export declare function setConfig(userConfig: Partial<QueryBuilderConfig>): void;
15
15
  export declare const defaultConfig: QueryBuilderConfig;
16
- // For backwards compatibility — synchronous access with default fallback.
17
- //
18
- // Why `let` + an explicit `Object.assign(config, defaultConfig)` instead of
19
- // the obvious `export const config = defaultConfig`: when downstream code
20
- // (e.g. `bunfig`, our config loader) introduces a top-level `await`, Bun's
21
- // bundler may wrap this module's initializer in `__esm(async () => {...})`.
22
- // Static `const x = y` exports inside such a wrapper are reassigned only
23
- // once the async init runs, so callers that reach `setConfig(...)` before
24
- // that init (e.g. another module's top-level `setConfig` call mid-graph)
25
- // see `config` as `undefined` and `Object.assign(undefined, ...)` throws.
26
- // Initializing as `let config = {...defaultConfig}` outside the wrapper, and
27
- // having `setConfig` re-hydrate from defaults if it's somehow still empty,
28
- // keeps the surface synchronous for every consumer regardless of how the
29
- // module ends up bundled.
30
- export declare let config: QueryBuilderConfig;
16
+ export declare const config: QueryBuilderConfig;
package/dist/db.d.ts CHANGED
@@ -1,5 +1,20 @@
1
1
  import { Database } from 'bun:sqlite';
2
2
  import { SQL } from 'bun';
3
+ import type { PoolConfig } from './types';
4
+ /**
5
+ * Map the qb-level `pool` config (ms-based, ergonomic) onto the Bun SQL
6
+ * driver's native option names (second resolution). Only the knobs Bun's
7
+ * `SQL` actually honors are emitted — `min`/`autoReconnect` are accepted on
8
+ * `PoolConfig` for forward-compatibility but the driver manages them itself,
9
+ * so they are intentionally not passed through. See
10
+ * stacksjs/bun-query-builder#1014.
11
+ */
12
+ export declare function resolvePoolOptions(pool?: PoolConfig): {
13
+ max?: number
14
+ idleTimeout?: number
15
+ connectionTimeout?: number
16
+ maxLifetime?: number
17
+ };
3
18
  /**
4
19
  * Returns a Bun SQL instance configured for the current dialect and database settings.
5
20
  * For SQLite, uses bun:sqlite directly for better compiled binary support.
@@ -16,6 +31,37 @@ export declare function resetConnection(): void;
16
31
  export declare function withFreshConnection<T>(fn: (sql: SQL) => Promise<T>): Promise<T>;
17
32
  // Export a lazy proxy - no connection is made until first use
18
33
  export declare const bunSql: SQL;
34
+ /**
35
+ * The query object returned by `connection.unsafe(...)` / a tagged template.
36
+ * Both Bun's native `SQL` query and our `createSQLiteSQL` wrapper satisfy this.
37
+ * See stacksjs/bun-query-builder#1044.
38
+ */
39
+ export declare interface DriverQuery {
40
+ execute: () => Promise<any>
41
+ values?: () => any
42
+ raw?: () => any
43
+ toString: () => string
44
+ cancel?: () => void
45
+ readonly sql?: string
46
+ [key: string]: any
47
+ }
48
+ /**
49
+ * The shared connection surface used across the dispatch path — both the
50
+ * `bun:sqlite` wrapper and Bun's native `SQL` satisfy it. Typing `_sql` against
51
+ * this (instead of `any`) is what lets the ~hundreds of `(_sql as any).unsafe`
52
+ * casts be dropped. See stacksjs/bun-query-builder#1044.
53
+ */
54
+ export declare interface DriverConnection {
55
+ (strings: TemplateStringsArray, ...values: any[]): DriverQuery
56
+ (value: any): any
57
+ unsafe: (sql: string, values?: any[]) => AwaitableDriverQuery
58
+ query?: (sql: string, params?: any[]) => any
59
+ close?: () => Promise<void> | void
60
+ _prepareStatement?: (sql: string) => any
61
+ [key: string]: any
62
+ }
63
+ /** An `unsafe(...)` result: a query object that is ALSO directly awaitable. */
64
+ export type AwaitableDriverQuery = DriverQuery & PromiseLike<any>;
19
65
  /**
20
66
  * SQLite wrapper that provides a SQL-like tagged template literal interface
21
67
  * using bun:sqlite's Database class for better compiled binary support.
@@ -27,5 +73,13 @@ declare class SQLiteWrapper {
27
73
  close(): void;
28
74
  get database(): Database;
29
75
  }
76
+ // NOTE: this module no longer installs a process-wide `unhandledRejection`
77
+ // handler. A library has no business doing so: ANY such listener suppresses the
78
+ // runtime's default crash for EVERY unhandled rejection in the consumer's
79
+ // process (the old handler's body matched only DB errors but silently swallowed
80
+ // the rest), masking genuine production bugs. Expected "database does not exist"
81
+ // errors during tests are surfaced/awaited at query time now (#1022); a test
82
+ // harness that needs to tolerate a missing DB should install its own handler.
83
+ // See stacksjs/bun-query-builder#1040.
30
84
  // Also export the SQL class for advanced usage
31
85
  export { SQL } from 'bun';
package/dist/orm.d.ts CHANGED
@@ -13,6 +13,18 @@ export type {
13
13
  FillableKeys,
14
14
  HiddenKeys,
15
15
  };
16
+ /**
17
+ * Extract an affected-row count from a Bun SQL driver result.
18
+ *
19
+ * Verified against live Postgres (Bun 1.3): a non-RETURNING `UPDATE`/`DELETE`
20
+ * returns an empty array carrying `{ count: <affected>, affectedRows: null,
21
+ * command: 'UPDATE' }`. So `count` must be checked BEFORE the `Array.length`
22
+ * fallback (which would be 0) — see stacksjs/bun-query-builder#1032. MySQL
23
+ * surfaces `affectedRows` instead.
24
+ */
25
+ export declare function extractChanges(res: any): number;
26
+ /** Extract a generated primary key from a Bun SQL driver result. */
27
+ export declare function extractInsertId(res: any): number | bigint | null;
16
28
  export declare function configureOrm(options: { database?: string | Database; verbose?: boolean }): void;
17
29
  /**
18
30
  * Return the underlying `bun:sqlite` Database backing the model layer.
@@ -309,6 +321,8 @@ declare class ModelInstance<TDef extends ModelDefinition, TSelected extends Colu
309
321
  update(data: Partial<Pick<InferModelAttributes<TDef>, FillableKeys<TDef>>>): Promise<this>;
310
322
  fresh(): Promise<ModelInstance<TDef, TSelected> | null>;
311
323
  delete(): Promise<boolean>;
324
+ restore(): Promise<this>;
325
+ trashed(): boolean;
312
326
  refresh(): Promise<this | null>;
313
327
  replicate(): ModelInstance<TDef, TSelected>;
314
328
  toArray(): Record<string, unknown>;
@@ -352,6 +366,8 @@ export declare class BelongsToManyRelationBuilder<TRel extends ModelDefinition>
352
366
  */
353
367
  declare class ModelQueryBuilder<TDef extends ModelDefinition, TSelected extends ColumnName<TDef> = ColumnName<TDef>> {
354
368
  constructor(definition: TDef);
369
+ withTrashed(): ModelQueryBuilder<TDef, TSelected>;
370
+ onlyTrashed(): ModelQueryBuilder<TDef, TSelected>;
355
371
  where<K extends ColumnName<TDef>>(column: K, value: K extends keyof ModelAttributes<TDef> ? ModelAttributes<TDef>[K] : unknown): ModelQueryBuilder<TDef, TSelected>;
356
372
  where<K extends ColumnName<TDef>>(column: K, operator: WhereOperator, value: K extends keyof ModelAttributes<TDef> ? ModelAttributes<TDef>[K] : unknown): ModelQueryBuilder<TDef, TSelected>;
357
373
  where<K extends ColumnName<TDef>>(column: K, operatorOrValue: WhereOperator | unknown, value?: unknown): ModelQueryBuilder<TDef, TSelected>;
@@ -0,0 +1,27 @@
1
+ import type { OnForeignKeyAction } from './schema';
2
+ /**
3
+ * Unwrap a single relation entry to a descriptor, or null if it isn't a valid
4
+ * relation entry. Accepts:
5
+ * - `'Model'`
6
+ * - `{ model: 'Model', foreignKey?, onDelete? }`
7
+ */
8
+ export declare function normalizeRelationEntry(entry: unknown): NormalizedRelation | null;
9
+ /**
10
+ * Flatten a relation declaration into descriptors. Accepts every supported
11
+ * shape: string array, object-in-array, record (name->Model) and record
12
+ * (name->config). Invalid entries are dropped.
13
+ */
14
+ export declare function normalizeRelationList(rel: unknown): NormalizedRelation[];
15
+ /**
16
+ * A relation entry reduced to the fields the schema/migration layers need.
17
+ *
18
+ * Single source of truth for unwrapping the supported relation-declaration
19
+ * shapes. Previously `meta.ts` (toRecord) and `migrations.ts`
20
+ * (normalizeBelongsTo) each re-implemented this — the exact shape that caused
21
+ * stacksjs/bun-query-builder#1023, and the fix had to be applied twice. See #1042.
22
+ */
23
+ export declare interface NormalizedRelation {
24
+ model: string
25
+ foreignKey?: string
26
+ onDelete?: OnForeignKeyAction
27
+ }