workers-qb 1.11.1 → 1.12.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 +37 -4
- package/dist/index.d.mts +166 -35
- package/dist/index.d.ts +166 -35
- package/dist/index.js +105 -66
- package/dist/index.mjs +105 -66
- package/docs/advanced-queries.md +124 -7
- package/docs/basic-queries.md +144 -126
- package/docs/databases/d1.md +61 -34
- package/docs/databases/do.md +67 -52
- package/docs/databases/postgresql.md +67 -20
- package/docs/json-queries.md +55 -13
- package/docs/migrations.md +59 -12
- package/docs/type-check.md +199 -0
- package/package.json +8 -9
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,73 @@ declare enum JoinTypes {
|
|
|
19
19
|
CROSS = "CROSS"
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Schema-aware type utilities for type-safe query building.
|
|
24
|
+
*
|
|
25
|
+
* Define your database schema as a type and pass it to the query builder
|
|
26
|
+
* to get autocomplete for table names, column names, and inferred result types.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* type Schema = {
|
|
31
|
+
* users: {
|
|
32
|
+
* id: number
|
|
33
|
+
* name: string
|
|
34
|
+
* email: string
|
|
35
|
+
* }
|
|
36
|
+
* posts: {
|
|
37
|
+
* id: number
|
|
38
|
+
* user_id: number
|
|
39
|
+
* title: string
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const qb = new D1QB<Schema>(env.DB)
|
|
44
|
+
*
|
|
45
|
+
* // Now get autocomplete for tableName, fields, orderBy, etc.
|
|
46
|
+
* const users = await qb.fetchAll({
|
|
47
|
+
* tableName: 'users', // autocomplete: 'users' | 'posts'
|
|
48
|
+
* fields: ['id', 'name'], // autocomplete: 'id' | 'name' | 'email'
|
|
49
|
+
* }).execute()
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Base type for database schemas.
|
|
54
|
+
* A schema is a record of table names to their column types.
|
|
55
|
+
*/
|
|
56
|
+
type TableSchema = Record<string, Record<string, unknown>>;
|
|
57
|
+
/**
|
|
58
|
+
* Extract table names from a schema.
|
|
59
|
+
*/
|
|
60
|
+
type TableName<S extends TableSchema> = keyof S & string;
|
|
61
|
+
/**
|
|
62
|
+
* Extract column names for a specific table.
|
|
63
|
+
*/
|
|
64
|
+
type ColumnName<S extends TableSchema, T extends TableName<S>> = keyof S[T] & string;
|
|
65
|
+
/**
|
|
66
|
+
* Pick specific columns from a table's type.
|
|
67
|
+
*/
|
|
68
|
+
type SelectColumns<S extends TableSchema, T extends TableName<S>, F extends ColumnName<S, T>> = Pick<S[T], F>;
|
|
69
|
+
/**
|
|
70
|
+
* Type for INSERT data - all columns are optional to allow partial inserts.
|
|
71
|
+
* In practice, required columns will be enforced by the database.
|
|
72
|
+
*/
|
|
73
|
+
type InsertData<S extends TableSchema, T extends TableName<S>> = Partial<S[T]>;
|
|
74
|
+
/**
|
|
75
|
+
* Type for UPDATE data - all columns are optional.
|
|
76
|
+
*/
|
|
77
|
+
type UpdateData<S extends TableSchema, T extends TableName<S>> = Partial<S[T]>;
|
|
78
|
+
/**
|
|
79
|
+
* Helper to check if a schema is empty (for backwards compatibility).
|
|
80
|
+
* When Schema = {}, we fall back to loose types.
|
|
81
|
+
*/
|
|
82
|
+
type IsEmptySchema<S extends TableSchema> = keyof S extends never ? true : false;
|
|
83
|
+
/**
|
|
84
|
+
* Conditional type that returns strict types when schema is provided,
|
|
85
|
+
* or loose types when schema is empty.
|
|
86
|
+
*/
|
|
87
|
+
type SchemaAware<S extends TableSchema, Strict, Loose> = IsEmptySchema<S> extends true ? Loose : Strict;
|
|
88
|
+
|
|
22
89
|
declare class Raw {
|
|
23
90
|
isRaw: boolean;
|
|
24
91
|
content: any;
|
|
@@ -43,24 +110,24 @@ declare function trimQuery(query: string): string;
|
|
|
43
110
|
interface SelectExecuteOptions {
|
|
44
111
|
lazy?: boolean;
|
|
45
112
|
}
|
|
46
|
-
declare class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnObject, IsAsync extends boolean = true> {
|
|
113
|
+
declare class SelectBuilder<Schema extends TableSchema = {}, GenericResultWrapper = unknown, GenericResult = DefaultReturnObject, IsAsync extends boolean = true> {
|
|
47
114
|
_debugger: boolean;
|
|
48
115
|
_options: Partial<SelectAll>;
|
|
49
116
|
_fetchAll: (params: SelectAll) => QueryWithExtra<GenericResultWrapper, any, IsAsync>;
|
|
50
117
|
_fetchOne: (params: SelectOne) => QueryWithExtra<GenericResultWrapper, any, IsAsync>;
|
|
51
118
|
constructor(options: Partial<SelectAll>, fetchAll: (params: SelectAll) => QueryWithExtra<GenericResultWrapper, any, IsAsync>, fetchOne: (params: SelectOne) => QueryWithExtra<GenericResultWrapper, any, IsAsync>);
|
|
52
119
|
setDebugger(state: boolean): void;
|
|
53
|
-
tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
54
|
-
fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
55
|
-
where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
56
|
-
whereIn<T extends string | Array<string>, P extends T extends Array<string> ? Primitive[][] : Primitive[]>(fields: T, values: P): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
57
|
-
join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
58
|
-
groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
59
|
-
having(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
60
|
-
orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
61
|
-
offset(offset: SelectAll['offset']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
62
|
-
limit(limit: SelectAll['limit']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
63
|
-
_parseArray(fieldName: string, option: any, value: any): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
|
|
120
|
+
tableName(tableName: SelectAll['tableName']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
121
|
+
fields(fields: SelectAll['fields']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
122
|
+
where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
123
|
+
whereIn<T extends string | Array<string>, P extends T extends Array<string> ? Primitive[][] : Primitive[]>(fields: T, values: P): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
124
|
+
join(join: SelectAll['join']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
125
|
+
groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
126
|
+
having(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
127
|
+
orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
128
|
+
offset(offset: SelectAll['offset']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
129
|
+
limit(limit: SelectAll['limit']): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
130
|
+
_parseArray(fieldName: string, option: any, value: any): SelectBuilder<Schema, GenericResultWrapper, GenericResult, IsAsync>;
|
|
64
131
|
getQueryAll<P extends SelectExecuteOptions = SelectExecuteOptions>(options?: P): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync, P extends {
|
|
65
132
|
lazy: true;
|
|
66
133
|
} ? true : false>, IsAsync>;
|
|
@@ -90,7 +157,7 @@ type SimpleMerge<Destination, Source> = {
|
|
|
90
157
|
} & Source;
|
|
91
158
|
type Merge<Destination, Source> = Simplify<SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>> & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>>;
|
|
92
159
|
|
|
93
|
-
type Primitive = null | string | number | boolean | bigint | Raw | SelectAll | SelectBuilder<any, any, any>;
|
|
160
|
+
type Primitive = null | string | number | boolean | bigint | ArrayBuffer | Raw | SelectAll | SelectBuilder<any, any, any>;
|
|
94
161
|
type QueryLoggerMeta = {
|
|
95
162
|
duration?: number;
|
|
96
163
|
};
|
|
@@ -98,7 +165,7 @@ type QueryBuilderOptions<IsAsync extends boolean = true> = {
|
|
|
98
165
|
logger?: (query: RawQuery, meta: QueryLoggerMeta) => MaybeAsync<IsAsync, void>;
|
|
99
166
|
};
|
|
100
167
|
type DefaultObject = Record<string, Primitive>;
|
|
101
|
-
type DefaultReturnObject = Record<string, null | string | number | boolean | bigint>;
|
|
168
|
+
type DefaultReturnObject = Record<string, null | string | number | boolean | bigint | ArrayBuffer>;
|
|
102
169
|
type Where = {
|
|
103
170
|
conditions: string | Array<string>;
|
|
104
171
|
params?: Primitive | Primitive[];
|
|
@@ -157,7 +224,6 @@ type InsertMultiple = Omit<Insert, 'data' | 'returning'> & {
|
|
|
157
224
|
returning: string | Array<string>;
|
|
158
225
|
};
|
|
159
226
|
type InsertWithoutReturning = Omit<Insert, 'returning'>;
|
|
160
|
-
type test<I extends Insert = Insert> = I;
|
|
161
227
|
type Update = {
|
|
162
228
|
tableName: string;
|
|
163
229
|
data: DefaultObject;
|
|
@@ -230,12 +296,69 @@ type CountResult<GenericResultWrapper> = OneResult<GenericResultWrapper, {
|
|
|
230
296
|
type AsyncType<T> = Promise<T>;
|
|
231
297
|
type SyncType<T> = T;
|
|
232
298
|
type MaybeAsync<IsAsync extends boolean, T> = IsAsync extends true ? AsyncType<T> : SyncType<T>;
|
|
299
|
+
/**
|
|
300
|
+
* Schema-aware SELECT parameters.
|
|
301
|
+
* When a schema is provided, tableName and fields get autocomplete.
|
|
302
|
+
*/
|
|
303
|
+
type TypedSelectOne<S extends TableSchema, T extends TableName<S>, F extends ColumnName<S, T> = ColumnName<S, T>> = {
|
|
304
|
+
tableName: T;
|
|
305
|
+
fields?: F[] | F | '*';
|
|
306
|
+
where?: Where;
|
|
307
|
+
join?: Join | Array<Join>;
|
|
308
|
+
groupBy?: ColumnName<S, T> | ColumnName<S, T>[] | string | string[];
|
|
309
|
+
having?: Where;
|
|
310
|
+
orderBy?: Partial<Record<ColumnName<S, T>, OrderTypes | string>> | string | string[];
|
|
311
|
+
offset?: number;
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* Schema-aware SELECT ALL parameters (includes limit and lazy).
|
|
315
|
+
*/
|
|
316
|
+
type TypedSelectAll<S extends TableSchema, T extends TableName<S>, F extends ColumnName<S, T> = ColumnName<S, T>> = TypedSelectOne<S, T, F> & {
|
|
317
|
+
limit?: number;
|
|
318
|
+
lazy?: boolean;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Schema-aware INSERT parameters.
|
|
322
|
+
*/
|
|
323
|
+
type TypedInsert<S extends TableSchema, T extends TableName<S>> = {
|
|
324
|
+
tableName: T;
|
|
325
|
+
data: Partial<S[T]> | Array<Partial<S[T]>>;
|
|
326
|
+
returning?: ColumnName<S, T>[] | ColumnName<S, T> | '*';
|
|
327
|
+
onConflict?: string | ConflictTypes | ConflictUpsert;
|
|
328
|
+
};
|
|
329
|
+
/**
|
|
330
|
+
* Schema-aware UPDATE parameters.
|
|
331
|
+
*/
|
|
332
|
+
type TypedUpdate<S extends TableSchema, T extends TableName<S>> = {
|
|
333
|
+
tableName: T;
|
|
334
|
+
data: Partial<S[T]>;
|
|
335
|
+
where?: Where;
|
|
336
|
+
returning?: ColumnName<S, T>[] | ColumnName<S, T> | '*';
|
|
337
|
+
onConflict?: string | ConflictTypes;
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Schema-aware DELETE parameters.
|
|
341
|
+
*/
|
|
342
|
+
type TypedDelete<S extends TableSchema, T extends TableName<S>> = {
|
|
343
|
+
tableName: T;
|
|
344
|
+
where: Where;
|
|
345
|
+
returning?: ColumnName<S, T>[] | ColumnName<S, T> | '*';
|
|
346
|
+
orderBy?: Partial<Record<ColumnName<S, T>, OrderTypes | string>> | string | string[];
|
|
347
|
+
limit?: number;
|
|
348
|
+
offset?: number;
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Infer the result type based on selected fields.
|
|
352
|
+
* If fields is '*' or undefined, returns full table type.
|
|
353
|
+
* If fields is an array, returns Pick of those fields.
|
|
354
|
+
*/
|
|
355
|
+
type InferResult<S extends TableSchema, T extends TableName<S>, F> = F extends '*' ? S[T] : F extends ColumnName<S, T>[] ? Pick<S[T], F[number]> : F extends ColumnName<S, T> ? Pick<S[T], F> : S[T];
|
|
233
356
|
|
|
234
357
|
declare function defaultLogger(query: RawQuery, meta: QueryLoggerMeta): any;
|
|
235
358
|
declare function asyncLoggerWrapper<Async extends boolean = true>(query: Query<any, Async> | Query<any, Async>[], loggerFunction: CallableFunction | undefined, innerFunction: () => any): Promise<any>;
|
|
236
359
|
declare function syncLoggerWrapper<Async extends boolean = false>(query: Query<any, Async> | Query<any, Async>[], loggerFunction: CallableFunction | undefined, innerFunction: () => any): any;
|
|
237
360
|
|
|
238
|
-
declare class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true> {
|
|
361
|
+
declare class QueryBuilder<Schema extends TableSchema = {}, GenericResultWrapper = unknown, IsAsync extends boolean = true> {
|
|
239
362
|
protected options: QueryBuilderOptions<IsAsync>;
|
|
240
363
|
loggerWrapper: typeof asyncLoggerWrapper;
|
|
241
364
|
constructor(options?: QueryBuilderOptions<IsAsync>);
|
|
@@ -252,19 +375,27 @@ declare class QueryBuilder<GenericResultWrapper, IsAsync extends boolean = true>
|
|
|
252
375
|
tableName: string;
|
|
253
376
|
ifExists?: boolean;
|
|
254
377
|
}): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync>, IsAsync>;
|
|
255
|
-
select<
|
|
378
|
+
select<T extends TableName<Schema>>(tableName: T): SelectBuilder<Schema, GenericResultWrapper, Schema[T], IsAsync>;
|
|
379
|
+
select<GenericResult = DefaultReturnObject>(tableName: string): SelectBuilder<{}, GenericResultWrapper, GenericResult, IsAsync>;
|
|
380
|
+
fetchOne<T extends TableName<Schema>, F extends ColumnName<Schema, T> = ColumnName<Schema, T>>(params: TypedSelectOne<Schema, T, F>): QueryWithExtra<GenericResultWrapper, OneResult<GenericResultWrapper, InferResult<Schema, T, F[] | undefined>>, IsAsync>;
|
|
256
381
|
fetchOne<GenericResult = DefaultReturnObject>(params: SelectOne): QueryWithExtra<GenericResultWrapper, OneResult<GenericResultWrapper, GenericResult>, IsAsync>;
|
|
382
|
+
fetchAll<T extends TableName<Schema>, F extends ColumnName<Schema, T> = ColumnName<Schema, T>, P extends TypedSelectAll<Schema, T, F> = TypedSelectAll<Schema, T, F>>(params: P): QueryWithExtra<GenericResultWrapper, ArrayResult<GenericResultWrapper, InferResult<Schema, T, F[] | undefined>, IsAsync, P extends {
|
|
383
|
+
lazy: true;
|
|
384
|
+
} ? true : false>, IsAsync>;
|
|
257
385
|
fetchAll<GenericResult = DefaultReturnObject, P extends SelectAll = SelectAll>(params: P): QueryWithExtra<GenericResultWrapper, ArrayResult<GenericResultWrapper, GenericResult, IsAsync, P extends {
|
|
258
386
|
lazy: true;
|
|
259
387
|
} ? true : false>, IsAsync>;
|
|
260
388
|
raw<GenericResult = DefaultReturnObject>(params: RawQueryFetchOne): Query<OneResult<GenericResultWrapper, GenericResult>, IsAsync>;
|
|
261
389
|
raw<GenericResult = DefaultReturnObject>(params: RawQueryFetchAll): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync>, IsAsync>;
|
|
262
390
|
raw<GenericResult = DefaultReturnObject>(params: RawQueryWithoutFetching): Query<GenericResultWrapper, IsAsync>;
|
|
391
|
+
insert<T extends TableName<Schema>>(params: TypedInsert<Schema, T>): Query<GenericResultWrapper, IsAsync>;
|
|
263
392
|
insert<GenericResult = DefaultReturnObject>(params: InsertOne): Query<OneResult<GenericResultWrapper, GenericResult>, IsAsync>;
|
|
264
393
|
insert<GenericResult = DefaultReturnObject>(params: InsertMultiple): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync>, IsAsync>;
|
|
265
394
|
insert<GenericResult = DefaultReturnObject>(params: InsertWithoutReturning): Query<GenericResultWrapper, IsAsync>;
|
|
395
|
+
update<T extends TableName<Schema>>(params: TypedUpdate<Schema, T>): Query<GenericResultWrapper, IsAsync>;
|
|
266
396
|
update<GenericResult = DefaultReturnObject>(params: UpdateReturning): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync>, IsAsync>;
|
|
267
397
|
update<GenericResult = DefaultReturnObject>(params: UpdateWithoutReturning): Query<GenericResultWrapper, IsAsync>;
|
|
398
|
+
delete<T extends TableName<Schema>>(params: TypedDelete<Schema, T>): Query<GenericResultWrapper, IsAsync>;
|
|
268
399
|
delete<GenericResult = DefaultReturnObject>(params: DeleteReturning): Query<ArrayResult<GenericResultWrapper, GenericResult, IsAsync>, IsAsync>;
|
|
269
400
|
delete<GenericResult = DefaultReturnObject>(params: DeleteWithoutReturning): Query<GenericResultWrapper, IsAsync>;
|
|
270
401
|
protected _parse_arguments(row: DefaultObject): Array<any>;
|
|
@@ -311,20 +442,20 @@ type MigrationOptions = {
|
|
|
311
442
|
tableName?: string;
|
|
312
443
|
};
|
|
313
444
|
declare class syncMigrationsBuilder<GenericResultWrapper> {
|
|
314
|
-
_builder: QueryBuilder<GenericResultWrapper, false>;
|
|
445
|
+
_builder: QueryBuilder<any, GenericResultWrapper, false>;
|
|
315
446
|
_migrations: Array<Migration>;
|
|
316
447
|
_tableName: string;
|
|
317
|
-
constructor(options: MigrationOptions, builder: QueryBuilder<GenericResultWrapper, false>);
|
|
448
|
+
constructor(options: MigrationOptions, builder: QueryBuilder<any, GenericResultWrapper, false>);
|
|
318
449
|
initialize(): void;
|
|
319
450
|
getApplied(): Array<MigrationEntry>;
|
|
320
451
|
getUnapplied(): Array<Migration>;
|
|
321
452
|
apply(): Array<Migration>;
|
|
322
453
|
}
|
|
323
454
|
declare class asyncMigrationsBuilder<GenericResultWrapper> {
|
|
324
|
-
_builder: QueryBuilder<GenericResultWrapper, true>;
|
|
455
|
+
_builder: QueryBuilder<any, GenericResultWrapper, true>;
|
|
325
456
|
_migrations: Array<Migration>;
|
|
326
457
|
_tableName: string;
|
|
327
|
-
constructor(options: MigrationOptions, builder: QueryBuilder<GenericResultWrapper, true>);
|
|
458
|
+
constructor(options: MigrationOptions, builder: QueryBuilder<any, GenericResultWrapper, true>);
|
|
328
459
|
initialize(): Promise<void>;
|
|
329
460
|
getApplied(): Promise<Array<MigrationEntry>>;
|
|
330
461
|
getUnapplied(): Promise<Array<Migration>>;
|
|
@@ -336,7 +467,7 @@ interface D1Database {
|
|
|
336
467
|
batch: any;
|
|
337
468
|
exec: any;
|
|
338
469
|
}
|
|
339
|
-
declare class D1QB extends QueryBuilder<D1Result> {
|
|
470
|
+
declare class D1QB<Schema extends TableSchema = {}> extends QueryBuilder<Schema, D1Result, true> {
|
|
340
471
|
db: any;
|
|
341
472
|
constructor(db: D1Database, options?: QueryBuilderOptions);
|
|
342
473
|
migrations(options: MigrationOptions): asyncMigrationsBuilder<D1Result>;
|
|
@@ -344,23 +475,13 @@ declare class D1QB extends QueryBuilder<D1Result> {
|
|
|
344
475
|
batchExecute(queryArray: Query[]): Promise<any>;
|
|
345
476
|
}
|
|
346
477
|
|
|
347
|
-
declare class PGQB extends QueryBuilder<PGResult> {
|
|
348
|
-
db: any;
|
|
349
|
-
_migrationsBuilder: typeof asyncMigrationsBuilder;
|
|
350
|
-
constructor(db: any, options?: QueryBuilderOptions);
|
|
351
|
-
migrations(options: MigrationOptions): asyncMigrationsBuilder<PGResult>;
|
|
352
|
-
connect(): Promise<void>;
|
|
353
|
-
close(): Promise<void>;
|
|
354
|
-
execute(query: Query): Promise<any>;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
478
|
interface SqlStorage {
|
|
358
479
|
exec: any;
|
|
359
|
-
|
|
480
|
+
get databaseSize(): number;
|
|
360
481
|
Cursor: any;
|
|
361
482
|
Statement: any;
|
|
362
483
|
}
|
|
363
|
-
declare class DOQB extends QueryBuilder<DOResult, false> {
|
|
484
|
+
declare class DOQB<Schema extends TableSchema = {}> extends QueryBuilder<Schema, DOResult, false> {
|
|
364
485
|
db: SqlStorage;
|
|
365
486
|
loggerWrapper: typeof syncLoggerWrapper;
|
|
366
487
|
constructor(db: SqlStorage, options?: QueryBuilderOptions<false>);
|
|
@@ -369,4 +490,14 @@ declare class DOQB extends QueryBuilder<DOResult, false> {
|
|
|
369
490
|
lazyExecute(query: Query<any, false>): Iterable<any>;
|
|
370
491
|
}
|
|
371
492
|
|
|
372
|
-
|
|
493
|
+
declare class PGQB<Schema extends TableSchema = {}> extends QueryBuilder<Schema, PGResult, true> {
|
|
494
|
+
db: any;
|
|
495
|
+
_migrationsBuilder: typeof asyncMigrationsBuilder;
|
|
496
|
+
constructor(db: any, options?: QueryBuilderOptions);
|
|
497
|
+
migrations(options: MigrationOptions): asyncMigrationsBuilder<PGResult>;
|
|
498
|
+
connect(): Promise<void>;
|
|
499
|
+
close(): Promise<void>;
|
|
500
|
+
execute(query: Query): Promise<any>;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export { type ArrayResult, type AsyncType, type ColumnName, ConflictTypes, type ConflictUpsert, type CountResult, type D1Meta, D1QB, type D1Result, DOQB, type DOResult, type DefaultObject, type DefaultReturnObject, type Delete, type DeleteReturning, type DeleteWithoutReturning, FetchTypes, type FullArrayResult, type InferResult, type Insert, type InsertData, type InsertMultiple, type InsertOne, type InsertWithoutReturning, type IsEmptySchema, type IterableResult, type Join, JoinTypes, type MaybeAsync, type Migration, type MigrationEntry, type MigrationOptions, type OneResult, OrderTypes, PGQB, type PGResult, type Primitive, Query, QueryBuilder, type QueryBuilderOptions, type QueryLoggerMeta, QueryWithExtra, Raw, type RawQuery, type RawQueryFetchAll, type RawQueryFetchOne, type RawQueryWithoutFetching, type SchemaAware, type SelectAll, type SelectColumns, type SelectOne, type SyncType, type TableName, type TableSchema, type TypedDelete, type TypedInsert, type TypedSelectAll, type TypedSelectOne, type TypedUpdate, type Update, type UpdateData, type UpdateReturning, type UpdateWithoutReturning, type Where, asyncLoggerWrapper, asyncMigrationsBuilder, defaultLogger, syncLoggerWrapper, syncMigrationsBuilder, trimQuery };
|
package/dist/index.js
CHANGED
|
@@ -461,6 +461,8 @@ var QueryBuilder = class {
|
|
|
461
461
|
}
|
|
462
462
|
);
|
|
463
463
|
}
|
|
464
|
+
// Implementation signature - uses any for compatibility with both overloads
|
|
465
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
464
466
|
fetchOne(params) {
|
|
465
467
|
const queryArgs = [];
|
|
466
468
|
const countQueryArgs = [];
|
|
@@ -487,6 +489,7 @@ var QueryBuilder = class {
|
|
|
487
489
|
"ONE" /* ONE */
|
|
488
490
|
);
|
|
489
491
|
}
|
|
492
|
+
// Implementation signature - accepts any object with tableName
|
|
490
493
|
fetchAll(params) {
|
|
491
494
|
const queryArgs = [];
|
|
492
495
|
const countQueryArgs = [];
|
|
@@ -525,6 +528,8 @@ var QueryBuilder = class {
|
|
|
525
528
|
params.fetchType
|
|
526
529
|
);
|
|
527
530
|
}
|
|
531
|
+
// Implementation signature - accepts any object with tableName
|
|
532
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
528
533
|
insert(params) {
|
|
529
534
|
let args = [];
|
|
530
535
|
if (typeof params.onConflict === "object") {
|
|
@@ -552,6 +557,8 @@ var QueryBuilder = class {
|
|
|
552
557
|
fetchType
|
|
553
558
|
);
|
|
554
559
|
}
|
|
560
|
+
// Implementation signature - accepts any object with tableName
|
|
561
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
555
562
|
update(params) {
|
|
556
563
|
let args = this._parse_arguments(params.data);
|
|
557
564
|
if (typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params) {
|
|
@@ -570,6 +577,8 @@ var QueryBuilder = class {
|
|
|
570
577
|
"ALL" /* ALL */
|
|
571
578
|
);
|
|
572
579
|
}
|
|
580
|
+
// Implementation signature - accepts any object with tableName
|
|
581
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
573
582
|
delete(params) {
|
|
574
583
|
return new Query(
|
|
575
584
|
(q) => {
|
|
@@ -615,7 +624,8 @@ var QueryBuilder = class {
|
|
|
615
624
|
}
|
|
616
625
|
const columns = Object.keys(data[0]).join(", ");
|
|
617
626
|
let index = 1;
|
|
618
|
-
let orConflict = ""
|
|
627
|
+
let orConflict = "";
|
|
628
|
+
let onConflict = "";
|
|
619
629
|
if (params.onConflict && typeof params.onConflict === "object") {
|
|
620
630
|
onConflict = this._onConflict(params.onConflict);
|
|
621
631
|
if (typeof params.onConflict?.where === "object" && !Array.isArray(params.onConflict?.where) && params.onConflict?.where?.params) {
|
|
@@ -670,11 +680,12 @@ var QueryBuilder = class {
|
|
|
670
680
|
FROM ${params.tableName}` + this._where(params.where) + this._returning(params.returning) + this._orderBy(params.orderBy) + this._limit(params.limit) + this._offset(params.offset);
|
|
671
681
|
}
|
|
672
682
|
_select(params, queryArgs) {
|
|
683
|
+
let newQueryArgs = queryArgs;
|
|
673
684
|
const isTopLevelCall = queryArgs === void 0;
|
|
674
685
|
if (isTopLevelCall) {
|
|
675
|
-
|
|
686
|
+
newQueryArgs = [];
|
|
676
687
|
}
|
|
677
|
-
const currentQueryArgs =
|
|
688
|
+
const currentQueryArgs = newQueryArgs;
|
|
678
689
|
const context = {
|
|
679
690
|
subQueryPlaceholders: params.subQueryPlaceholders,
|
|
680
691
|
queryArgs: currentQueryArgs,
|
|
@@ -706,8 +717,9 @@ var QueryBuilder = class {
|
|
|
706
717
|
if (conditionStrings.length === 0) return "";
|
|
707
718
|
let primitiveParamIndex = 0;
|
|
708
719
|
const processedConditions = [];
|
|
720
|
+
const seenNumberedParams = {};
|
|
709
721
|
for (const conditionStr of conditionStrings) {
|
|
710
|
-
const parts = conditionStr.split(/(__SUBQUERY_TOKEN_\d+__
|
|
722
|
+
const parts = conditionStr.split(/(__SUBQUERY_TOKEN_\d+__|\?\d+|\?)/g).filter(Boolean);
|
|
711
723
|
let builtCondition = "";
|
|
712
724
|
for (const part of parts) {
|
|
713
725
|
if (part === "?") {
|
|
@@ -718,6 +730,18 @@ var QueryBuilder = class {
|
|
|
718
730
|
}
|
|
719
731
|
currentContext.queryArgs.push(primitiveParams[primitiveParamIndex++]);
|
|
720
732
|
builtCondition += "?";
|
|
733
|
+
} else if (/^\?\d+$/.test(part)) {
|
|
734
|
+
const paramNum = part.slice(1);
|
|
735
|
+
if (!seenNumberedParams[paramNum]) {
|
|
736
|
+
seenNumberedParams[paramNum] = true;
|
|
737
|
+
if (primitiveParamIndex >= primitiveParams.length) {
|
|
738
|
+
throw new Error(
|
|
739
|
+
"SQL generation error: Not enough primitive parameters for numbered placeholders in WHERE clause."
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
currentContext.queryArgs.push(primitiveParams[primitiveParamIndex++]);
|
|
743
|
+
}
|
|
744
|
+
builtCondition += part;
|
|
721
745
|
} else if (part.startsWith("__SUBQUERY_TOKEN_") && part.endsWith("__")) {
|
|
722
746
|
if (!currentContext.subQueryPlaceholders || !currentContext.toSQLCompiler) {
|
|
723
747
|
throw new Error("SQL generation error: Subquery context not provided for token processing.");
|
|
@@ -791,8 +815,9 @@ var QueryBuilder = class {
|
|
|
791
815
|
if (conditionStrings.length === 0) return "";
|
|
792
816
|
let primitiveParamIndex = 0;
|
|
793
817
|
const processedConditions = [];
|
|
818
|
+
const seenNumberedParams = {};
|
|
794
819
|
for (const conditionStr of conditionStrings) {
|
|
795
|
-
const parts = conditionStr.split(/(__SUBQUERY_TOKEN_\d+__
|
|
820
|
+
const parts = conditionStr.split(/(__SUBQUERY_TOKEN_\d+__|\?\d+|\?)/g).filter(Boolean);
|
|
796
821
|
let builtCondition = "";
|
|
797
822
|
for (const part of parts) {
|
|
798
823
|
if (part === "?") {
|
|
@@ -803,6 +828,18 @@ var QueryBuilder = class {
|
|
|
803
828
|
}
|
|
804
829
|
currentContext.queryArgs.push(primitiveParams[primitiveParamIndex++]);
|
|
805
830
|
builtCondition += "?";
|
|
831
|
+
} else if (/^\?\d+$/.test(part)) {
|
|
832
|
+
const paramNum = part.slice(1);
|
|
833
|
+
if (!seenNumberedParams[paramNum]) {
|
|
834
|
+
seenNumberedParams[paramNum] = true;
|
|
835
|
+
if (primitiveParamIndex >= primitiveParams.length) {
|
|
836
|
+
throw new Error(
|
|
837
|
+
"SQL generation error: Not enough primitive parameters for numbered placeholders in HAVING clause."
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
currentContext.queryArgs.push(primitiveParams[primitiveParamIndex++]);
|
|
841
|
+
}
|
|
842
|
+
builtCondition += part;
|
|
806
843
|
} else if (part.startsWith("__SUBQUERY_TOKEN_") && part.endsWith("__")) {
|
|
807
844
|
if (!currentContext.subQueryPlaceholders || !currentContext.toSQLCompiler) {
|
|
808
845
|
throw new Error("SQL generation error: Subquery context not provided for token processing.");
|
|
@@ -848,9 +885,8 @@ var QueryBuilder = class {
|
|
|
848
885
|
objs.push(`${key} ${item}`);
|
|
849
886
|
});
|
|
850
887
|
return objs.join(", ");
|
|
851
|
-
} else {
|
|
852
|
-
return obj;
|
|
853
888
|
}
|
|
889
|
+
return obj;
|
|
854
890
|
});
|
|
855
891
|
return ` ORDER BY ${result.join(", ")}`;
|
|
856
892
|
}
|
|
@@ -871,9 +907,11 @@ var QueryBuilder = class {
|
|
|
871
907
|
|
|
872
908
|
// src/migrations.ts
|
|
873
909
|
var syncMigrationsBuilder = class {
|
|
910
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
874
911
|
_builder;
|
|
875
912
|
_migrations;
|
|
876
913
|
_tableName;
|
|
914
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
877
915
|
constructor(options, builder) {
|
|
878
916
|
this._tableName = options.tableName || "migrations";
|
|
879
917
|
this._migrations = options.migrations;
|
|
@@ -924,9 +962,11 @@ var syncMigrationsBuilder = class {
|
|
|
924
962
|
}
|
|
925
963
|
};
|
|
926
964
|
var asyncMigrationsBuilder = class {
|
|
965
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
927
966
|
_builder;
|
|
928
967
|
_migrations;
|
|
929
968
|
_tableName;
|
|
969
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
930
970
|
constructor(options, builder) {
|
|
931
971
|
this._tableName = options.tableName || "migrations";
|
|
932
972
|
this._migrations = options.migrations;
|
|
@@ -1035,72 +1075,23 @@ var D1QB = class extends QueryBuilder {
|
|
|
1035
1075
|
success: resp.success,
|
|
1036
1076
|
results: queryArray[i]?.fetchType === "ONE" /* ONE */ ? resp.results?.[0] : resp.results
|
|
1037
1077
|
};
|
|
1038
|
-
} else {
|
|
1039
|
-
return {
|
|
1040
|
-
changes: resp.meta?.changes,
|
|
1041
|
-
duration: resp.meta?.duration,
|
|
1042
|
-
last_row_id: resp.meta?.last_row_id,
|
|
1043
|
-
served_by: resp.meta?.served_by,
|
|
1044
|
-
rowsRead: resp.meta?.rows_read,
|
|
1045
|
-
rowsWritten: resp.meta?.rows_written,
|
|
1046
|
-
meta: resp.meta,
|
|
1047
|
-
success: resp.success
|
|
1048
|
-
};
|
|
1049
1078
|
}
|
|
1079
|
+
return {
|
|
1080
|
+
changes: resp.meta?.changes,
|
|
1081
|
+
duration: resp.meta?.duration,
|
|
1082
|
+
last_row_id: resp.meta?.last_row_id,
|
|
1083
|
+
served_by: resp.meta?.served_by,
|
|
1084
|
+
rowsRead: resp.meta?.rows_read,
|
|
1085
|
+
rowsWritten: resp.meta?.rows_written,
|
|
1086
|
+
meta: resp.meta,
|
|
1087
|
+
success: resp.success
|
|
1088
|
+
};
|
|
1050
1089
|
}
|
|
1051
1090
|
);
|
|
1052
1091
|
});
|
|
1053
1092
|
}
|
|
1054
1093
|
};
|
|
1055
1094
|
|
|
1056
|
-
// src/databases/pg.ts
|
|
1057
|
-
var PGQB = class extends QueryBuilder {
|
|
1058
|
-
db;
|
|
1059
|
-
_migrationsBuilder = asyncMigrationsBuilder;
|
|
1060
|
-
constructor(db, options) {
|
|
1061
|
-
super(options);
|
|
1062
|
-
this.db = db;
|
|
1063
|
-
}
|
|
1064
|
-
migrations(options) {
|
|
1065
|
-
return new asyncMigrationsBuilder(options, this);
|
|
1066
|
-
}
|
|
1067
|
-
async connect() {
|
|
1068
|
-
await this.db.connect();
|
|
1069
|
-
}
|
|
1070
|
-
async close() {
|
|
1071
|
-
await this.db.end();
|
|
1072
|
-
}
|
|
1073
|
-
async execute(query) {
|
|
1074
|
-
return await this.loggerWrapper(query, this.options.logger, async () => {
|
|
1075
|
-
const queryString = query.query.replaceAll("?", "$");
|
|
1076
|
-
let result;
|
|
1077
|
-
if (query.arguments) {
|
|
1078
|
-
result = await this.db.query({
|
|
1079
|
-
values: query.arguments,
|
|
1080
|
-
text: queryString
|
|
1081
|
-
});
|
|
1082
|
-
} else {
|
|
1083
|
-
result = await this.db.query({
|
|
1084
|
-
text: queryString
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
if (query.fetchType === "ONE" /* ONE */ || query.fetchType === "ALL" /* ALL */) {
|
|
1088
|
-
return {
|
|
1089
|
-
command: result.command,
|
|
1090
|
-
lastRowId: result.oid,
|
|
1091
|
-
rowCount: result.rowCount,
|
|
1092
|
-
results: query.fetchType === "ONE" /* ONE */ ? result.rows[0] : result.rows
|
|
1093
|
-
};
|
|
1094
|
-
}
|
|
1095
|
-
return {
|
|
1096
|
-
command: result.command,
|
|
1097
|
-
lastRowId: result.oid,
|
|
1098
|
-
rowCount: result.rowCount
|
|
1099
|
-
};
|
|
1100
|
-
});
|
|
1101
|
-
}
|
|
1102
|
-
};
|
|
1103
|
-
|
|
1104
1095
|
// src/databases/do.ts
|
|
1105
1096
|
var DOQB = class extends QueryBuilder {
|
|
1106
1097
|
db;
|
|
@@ -1149,6 +1140,54 @@ var DOQB = class extends QueryBuilder {
|
|
|
1149
1140
|
});
|
|
1150
1141
|
}
|
|
1151
1142
|
};
|
|
1143
|
+
|
|
1144
|
+
// src/databases/pg.ts
|
|
1145
|
+
var PGQB = class extends QueryBuilder {
|
|
1146
|
+
db;
|
|
1147
|
+
_migrationsBuilder = asyncMigrationsBuilder;
|
|
1148
|
+
constructor(db, options) {
|
|
1149
|
+
super(options);
|
|
1150
|
+
this.db = db;
|
|
1151
|
+
}
|
|
1152
|
+
migrations(options) {
|
|
1153
|
+
return new asyncMigrationsBuilder(options, this);
|
|
1154
|
+
}
|
|
1155
|
+
async connect() {
|
|
1156
|
+
await this.db.connect();
|
|
1157
|
+
}
|
|
1158
|
+
async close() {
|
|
1159
|
+
await this.db.end();
|
|
1160
|
+
}
|
|
1161
|
+
async execute(query) {
|
|
1162
|
+
return await this.loggerWrapper(query, this.options.logger, async () => {
|
|
1163
|
+
const queryString = query.query.replaceAll("?", "$");
|
|
1164
|
+
let result;
|
|
1165
|
+
if (query.arguments) {
|
|
1166
|
+
result = await this.db.query({
|
|
1167
|
+
values: query.arguments,
|
|
1168
|
+
text: queryString
|
|
1169
|
+
});
|
|
1170
|
+
} else {
|
|
1171
|
+
result = await this.db.query({
|
|
1172
|
+
text: queryString
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
if (query.fetchType === "ONE" /* ONE */ || query.fetchType === "ALL" /* ALL */) {
|
|
1176
|
+
return {
|
|
1177
|
+
command: result.command,
|
|
1178
|
+
lastRowId: result.oid,
|
|
1179
|
+
rowCount: result.rowCount,
|
|
1180
|
+
results: query.fetchType === "ONE" /* ONE */ ? result.rows[0] : result.rows
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
return {
|
|
1184
|
+
command: result.command,
|
|
1185
|
+
lastRowId: result.oid,
|
|
1186
|
+
rowCount: result.rowCount
|
|
1187
|
+
};
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
};
|
|
1152
1191
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1153
1192
|
0 && (module.exports = {
|
|
1154
1193
|
ConflictTypes,
|