@dbcube/query-builder 5.1.5 → 5.2.1
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 +122 -1700
- package/dist/index.cjs +478 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +233 -35
- package/dist/index.d.ts +233 -35
- package/dist/index.js +478 -38
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
- package/.npmignore +0 -51
- package/CONTRIBUTING.md +0 -9
- package/bun.lockb +0 -0
- package/pnpm-workspace.yaml +0 -2
- package/tsup.config.ts +0 -14
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
import { QueryEngine } from '@dbcube/core';
|
|
2
|
+
|
|
3
|
+
export interface PaginatedResult<T = DatabaseRecord> {
|
|
4
|
+
items: T[];
|
|
5
|
+
page: number;
|
|
6
|
+
perPage: number;
|
|
7
|
+
total: number;
|
|
8
|
+
totalPages: number;
|
|
9
|
+
hasNext: boolean;
|
|
10
|
+
hasPrev: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface RelationOptions {
|
|
13
|
+
/** Child/related table name (defaults to the relation name) */
|
|
14
|
+
table?: string;
|
|
15
|
+
/** FK column on the related table (hasMany) or on this table (belongsTo) */
|
|
16
|
+
foreignKey?: string;
|
|
17
|
+
/** Key on the parent table (defaults to 'id') */
|
|
18
|
+
localKey?: string;
|
|
19
|
+
/** 'many' attaches an array (hasMany), 'one' attaches a single record (belongsTo) */
|
|
20
|
+
type?: "many" | "one";
|
|
21
|
+
}
|
|
22
|
+
export type WhereCallback = (query: Table<DatabaseRecord>) => void;
|
|
2
23
|
export type DatabaseRecord = Record<string, any>;
|
|
24
|
+
/** Result row returned by write operations (update/delete/truncate/upsert). */
|
|
25
|
+
export interface MutationInfo {
|
|
26
|
+
affectedRows?: number;
|
|
27
|
+
affected_rows?: number;
|
|
28
|
+
lastInsertId?: number | string;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
/** A computed-field definition loaded from dbcube_computes_config. */
|
|
32
|
+
export interface ComputedFieldConfig {
|
|
33
|
+
column: string;
|
|
34
|
+
type: string;
|
|
35
|
+
instruction: string;
|
|
36
|
+
table_ref?: string;
|
|
37
|
+
database_ref?: string;
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
/** A runtime-trigger definition loaded from dbcube_triggers_config. */
|
|
41
|
+
export interface TriggerConfig {
|
|
42
|
+
type: string;
|
|
43
|
+
table_ref: string;
|
|
44
|
+
database_ref: string;
|
|
45
|
+
name?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
}
|
|
3
49
|
/**
|
|
4
50
|
* Main class to handle MySQL database connections and queries.
|
|
5
51
|
* Implements the Singleton pattern to ensure a single instance of the connection pool.
|
|
@@ -9,7 +55,31 @@ export declare class Database {
|
|
|
9
55
|
private engine;
|
|
10
56
|
private computedFields;
|
|
11
57
|
private triggers;
|
|
58
|
+
private txId;
|
|
12
59
|
constructor(name: string);
|
|
60
|
+
/**
|
|
61
|
+
* Executes raw SQL (MySQL/PostgreSQL/SQLite) or a raw command document (MongoDB)
|
|
62
|
+
* with bound parameters. The escape hatch for anything the builder doesn't cover.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const rows = await db.raw('SELECT * FROM users WHERE age > ?', [25]);
|
|
66
|
+
* await db.raw('CREATE INDEX idx_users_email ON users(email)');
|
|
67
|
+
*/
|
|
68
|
+
raw<R = DatabaseRecord>(query: string, params?: unknown[]): Promise<R[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Runs a callback inside a database transaction. Everything executed through
|
|
71
|
+
* the received connection is committed atomically; any thrown error rolls
|
|
72
|
+
* the whole transaction back.
|
|
73
|
+
*
|
|
74
|
+
* Requires daemon mode (enabled by default with an up-to-date query-engine).
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* await db.transaction(async (trx) => {
|
|
78
|
+
* await trx.table('accounts').where('id', '=', 1).update({ balance: 50 });
|
|
79
|
+
* await trx.table('accounts').where('id', '=', 2).update({ balance: 150 });
|
|
80
|
+
* });
|
|
81
|
+
*/
|
|
82
|
+
transaction<T>(callback: (trx: Database) => Promise<T>): Promise<T>;
|
|
13
83
|
useComputes(): Promise<Database>;
|
|
14
84
|
useTriggers(): Promise<Database>;
|
|
15
85
|
connect(): Promise<void>;
|
|
@@ -51,22 +121,30 @@ export declare class Database {
|
|
|
51
121
|
* // Access column management
|
|
52
122
|
* const columns = await db.table('users').columns().get();
|
|
53
123
|
*/
|
|
54
|
-
table(tableName: string): Table
|
|
124
|
+
table<T extends DatabaseRecord = DatabaseRecord>(tableName: string): Table<T>;
|
|
55
125
|
private setComputedFields;
|
|
56
126
|
private setTriggers;
|
|
57
127
|
}
|
|
58
128
|
/**
|
|
59
129
|
* Class to build and execute SQL queries for a specific table.
|
|
60
130
|
* Supports operations like SELECT, INSERT, UPDATE, DELETE, and more.
|
|
131
|
+
*
|
|
132
|
+
* Generic over the row shape: `db.table<User>('users').get()` resolves to
|
|
133
|
+
* `Promise<User[]>`. Pair it with the interfaces emitted by `dbcube generate`.
|
|
61
134
|
*/
|
|
62
|
-
export declare class Table {
|
|
135
|
+
export declare class Table<T extends DatabaseRecord = DatabaseRecord> {
|
|
63
136
|
private engine;
|
|
64
137
|
private nextType;
|
|
65
138
|
private dml;
|
|
66
139
|
private computedFields;
|
|
67
140
|
private trigger;
|
|
68
141
|
private triggers;
|
|
69
|
-
|
|
142
|
+
private txId;
|
|
143
|
+
private relations;
|
|
144
|
+
/** Builders de grupo mutan en sitio (ver clone()) */
|
|
145
|
+
private _mutable;
|
|
146
|
+
private instance;
|
|
147
|
+
constructor(instance: Database, databaseName: string, tableName: string, engine: QueryEngine, computedFields?: ComputedFieldConfig[], triggers?: TriggerConfig[], txId?: string | null);
|
|
70
148
|
/**
|
|
71
149
|
* Specifies the columns to select in a SELECT query.
|
|
72
150
|
*
|
|
@@ -77,7 +155,7 @@ export declare class Table {
|
|
|
77
155
|
* const users = await db.table('users').select(['id', 'name']).get();
|
|
78
156
|
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
79
157
|
*/
|
|
80
|
-
select(fields?: string[]): Table
|
|
158
|
+
select(fields?: string[]): Table<T>;
|
|
81
159
|
/**
|
|
82
160
|
* Adds a WHERE condition to the query.
|
|
83
161
|
*
|
|
@@ -91,8 +169,8 @@ export declare class Table {
|
|
|
91
169
|
* const nullUsers = await db.table('users').where('email', 'IS NULL').get();
|
|
92
170
|
* console.log(users); // [{ id: 1, name: 'John', age: 30 }]
|
|
93
171
|
*/
|
|
94
|
-
where(column: string, operator: "IS NULL" | "IS NOT NULL"): Table
|
|
95
|
-
where(column: string, operator: "=" | "!=" | "<>" | ">" | "<" | ">=" | "<=" | "LIKE" | "NOT LIKE" | "IN" | "NOT IN" | "BETWEEN" | "NOT BETWEEN", value: any): Table
|
|
172
|
+
where(column: string, operator: "IS NULL" | "IS NOT NULL"): Table<T>;
|
|
173
|
+
where(column: string, operator: "=" | "!=" | "<>" | ">" | "<" | ">=" | "<=" | "LIKE" | "NOT LIKE" | "IN" | "NOT IN" | "BETWEEN" | "NOT BETWEEN", value: any): Table<T>;
|
|
96
174
|
/**
|
|
97
175
|
* Adds an OR WHERE condition to the query.
|
|
98
176
|
*
|
|
@@ -106,8 +184,8 @@ export declare class Table {
|
|
|
106
184
|
* const nullUsers = await db.table('users').where('active', '=', true).orWhere('email', 'IS NULL').get();
|
|
107
185
|
* console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
|
|
108
186
|
*/
|
|
109
|
-
orWhere(column: string, operator: "IS NULL" | "IS NOT NULL"): Table
|
|
110
|
-
orWhere(column: string, operator: "=" | "!=" | "<>" | ">" | "<" | ">=" | "<=" | "LIKE" | "NOT LIKE" | "IN" | "NOT IN" | "BETWEEN" | "NOT BETWEEN", value: any): Table
|
|
187
|
+
orWhere(column: string, operator: "IS NULL" | "IS NOT NULL"): Table<T>;
|
|
188
|
+
orWhere(column: string, operator: "=" | "!=" | "<>" | ">" | "<" | ">=" | "<=" | "LIKE" | "NOT LIKE" | "IN" | "NOT IN" | "BETWEEN" | "NOT BETWEEN", value: any): Table<T>;
|
|
111
189
|
/**
|
|
112
190
|
* Adds a grouped WHERE condition to the query.
|
|
113
191
|
*
|
|
@@ -120,9 +198,9 @@ export declare class Table {
|
|
|
120
198
|
* }).get();
|
|
121
199
|
* console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
|
|
122
200
|
*/
|
|
123
|
-
whereGroup(callback: WhereCallback): Table
|
|
124
|
-
or(): Table
|
|
125
|
-
and(): Table
|
|
201
|
+
whereGroup(callback: WhereCallback): Table<T>;
|
|
202
|
+
or(): Table<T>;
|
|
203
|
+
and(): Table<T>;
|
|
126
204
|
/**
|
|
127
205
|
* Adds a WHERE BETWEEN condition to the query.
|
|
128
206
|
*
|
|
@@ -137,7 +215,7 @@ export declare class Table {
|
|
|
137
215
|
whereBetween(column: string, values: [
|
|
138
216
|
any,
|
|
139
217
|
any
|
|
140
|
-
]): Table
|
|
218
|
+
]): Table<T>;
|
|
141
219
|
/**
|
|
142
220
|
* Adds a WHERE IN condition to the query.
|
|
143
221
|
*
|
|
@@ -149,7 +227,7 @@ export declare class Table {
|
|
|
149
227
|
* const users = await db.table('users').whereIn('id', [1, 2]).get();
|
|
150
228
|
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
151
229
|
*/
|
|
152
|
-
whereIn(column: string, values: any[]): Table
|
|
230
|
+
whereIn(column: string, values: any[]): Table<T>;
|
|
153
231
|
/**
|
|
154
232
|
* Adds a WHERE IS NULL condition to the query.
|
|
155
233
|
*
|
|
@@ -160,7 +238,7 @@ export declare class Table {
|
|
|
160
238
|
* const users = await db.table('users').whereNull('email').get();
|
|
161
239
|
* console.log(users); // [{ id: 3, name: 'Alice', email: null }]
|
|
162
240
|
*/
|
|
163
|
-
whereNull(column: string): Table
|
|
241
|
+
whereNull(column: string): Table<T>;
|
|
164
242
|
/**
|
|
165
243
|
* Adds a WHERE IS NOT NULL condition to the query.
|
|
166
244
|
*
|
|
@@ -171,7 +249,7 @@ export declare class Table {
|
|
|
171
249
|
* const users = await db.table('users').whereNotNull('email').get();
|
|
172
250
|
* console.log(users); // [{ id: 1, name: 'John', email: 'john@example.com' }]
|
|
173
251
|
*/
|
|
174
|
-
whereNotNull(column: string): Table
|
|
252
|
+
whereNotNull(column: string): Table<T>;
|
|
175
253
|
/**
|
|
176
254
|
* Adds a JOIN clause to the query.
|
|
177
255
|
*
|
|
@@ -185,7 +263,7 @@ export declare class Table {
|
|
|
185
263
|
* const users = await db.table('users').join('orders', 'users.id', '=', 'orders.user_id').get();
|
|
186
264
|
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }]
|
|
187
265
|
*/
|
|
188
|
-
join(table: string, column1: string, operator: string, column2: string): Table
|
|
266
|
+
join(table: string, column1: string, operator: string, column2: string): Table<T>;
|
|
189
267
|
/**
|
|
190
268
|
* Adds a LEFT JOIN clause to the query.
|
|
191
269
|
*
|
|
@@ -199,7 +277,7 @@ export declare class Table {
|
|
|
199
277
|
* const users = await db.table('users').leftJoin('orders', 'users.id', '=', 'orders.user_id').get();
|
|
200
278
|
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: 2, name: 'Jane', order_id: null }]
|
|
201
279
|
*/
|
|
202
|
-
leftJoin(table: string, column1: string, operator: string, column2: string): Table
|
|
280
|
+
leftJoin(table: string, column1: string, operator: string, column2: string): Table<T>;
|
|
203
281
|
/**
|
|
204
282
|
* Adds a RIGHT JOIN clause to the query.
|
|
205
283
|
*
|
|
@@ -213,7 +291,7 @@ export declare class Table {
|
|
|
213
291
|
* const users = await db.table('users').rightJoin('orders', 'users.id', '=', 'orders.user_id').get();
|
|
214
292
|
* console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: null, name: null, order_id: 102 }]
|
|
215
293
|
*/
|
|
216
|
-
rightJoin(table: string, column1: string, operator: string, column2: string): Table
|
|
294
|
+
rightJoin(table: string, column1: string, operator: string, column2: string): Table<T>;
|
|
217
295
|
/**
|
|
218
296
|
* Adds an ORDER BY clause to the query.
|
|
219
297
|
*
|
|
@@ -225,7 +303,7 @@ export declare class Table {
|
|
|
225
303
|
* const users = await db.table('users').orderBy('name', 'ASC').get();
|
|
226
304
|
* console.log(users); // [{ id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
|
|
227
305
|
*/
|
|
228
|
-
orderBy(column: string, direction?: "ASC" | "DESC"): Table
|
|
306
|
+
orderBy(column: string, direction?: "ASC" | "DESC"): Table<T>;
|
|
229
307
|
/**
|
|
230
308
|
* Adds a GROUP BY clause to the query.
|
|
231
309
|
*
|
|
@@ -236,7 +314,7 @@ export declare class Table {
|
|
|
236
314
|
* const users = await db.table('users').groupBy('age').get();
|
|
237
315
|
* console.log(users); // [{ age: 30, count: 1 }, { age: 25, count: 1 }]
|
|
238
316
|
*/
|
|
239
|
-
groupBy(column: string): Table
|
|
317
|
+
groupBy(column: string): Table<T>;
|
|
240
318
|
/**
|
|
241
319
|
* Adds a DISTINCT clause to the query.
|
|
242
320
|
*
|
|
@@ -246,7 +324,7 @@ export declare class Table {
|
|
|
246
324
|
* const users = await db.table('users').distinct().select(['name']).get();
|
|
247
325
|
* console.log(users); // [{ name: 'John' }, { name: 'Jane' }]
|
|
248
326
|
*/
|
|
249
|
-
distinct(): Table
|
|
327
|
+
distinct(): Table<T>;
|
|
250
328
|
/**
|
|
251
329
|
* Adds a COUNT clause to the query.
|
|
252
330
|
*
|
|
@@ -257,7 +335,7 @@ export declare class Table {
|
|
|
257
335
|
* const count = await db.table('users').count().first();
|
|
258
336
|
* console.log(count); // { count: 2 }
|
|
259
337
|
*/
|
|
260
|
-
count(column?: string): Promise<
|
|
338
|
+
count(column?: string): Promise<number>;
|
|
261
339
|
/**
|
|
262
340
|
* Adds a SUM clause to the query.
|
|
263
341
|
*
|
|
@@ -268,7 +346,7 @@ export declare class Table {
|
|
|
268
346
|
* const totalAge = await db.table('users').sum('age').first();
|
|
269
347
|
* console.log(totalAge); // { sum: 55 }
|
|
270
348
|
*/
|
|
271
|
-
sum(column: string): Promise<
|
|
349
|
+
sum(column: string): Promise<number>;
|
|
272
350
|
/**
|
|
273
351
|
* Adds an AVG clause to the query.
|
|
274
352
|
*
|
|
@@ -279,7 +357,7 @@ export declare class Table {
|
|
|
279
357
|
* const avgAge = await db.table('users').avg('age').first();
|
|
280
358
|
* console.log(avgAge); // { avg: 27.5 }
|
|
281
359
|
*/
|
|
282
|
-
avg(column: string): Promise<
|
|
360
|
+
avg(column: string): Promise<number>;
|
|
283
361
|
/**
|
|
284
362
|
* Adds a MAX clause to the query.
|
|
285
363
|
*
|
|
@@ -290,7 +368,7 @@ export declare class Table {
|
|
|
290
368
|
* const maxAge = await db.table('users').max('age').first();
|
|
291
369
|
* console.log(maxAge); // { max: 30 }
|
|
292
370
|
*/
|
|
293
|
-
max(column: string): Promise<
|
|
371
|
+
max(column: string): Promise<number>;
|
|
294
372
|
/**
|
|
295
373
|
* Adds a MIN clause to the query.
|
|
296
374
|
*
|
|
@@ -301,7 +379,7 @@ export declare class Table {
|
|
|
301
379
|
* const minAge = await db.table('users').min('age').first();
|
|
302
380
|
* console.log(minAge); // { min: 25 }
|
|
303
381
|
*/
|
|
304
|
-
min(column: string): Promise<
|
|
382
|
+
min(column: string): Promise<number>;
|
|
305
383
|
/**
|
|
306
384
|
* Gets the column names of the table.
|
|
307
385
|
* For SQL databases (MySQL, PostgreSQL, SQLite), returns a simple array of column names.
|
|
@@ -340,7 +418,7 @@ export declare class Table {
|
|
|
340
418
|
* const users = await db.table('users').limit(1).get();
|
|
341
419
|
* console.log(users); // [{ id: 1, name: 'John', age: 30 }]
|
|
342
420
|
*/
|
|
343
|
-
limit(number: number): Table
|
|
421
|
+
limit(number: number): Table<T>;
|
|
344
422
|
/**
|
|
345
423
|
* Adds pagination to the query using LIMIT and OFFSET.
|
|
346
424
|
*
|
|
@@ -351,7 +429,7 @@ export declare class Table {
|
|
|
351
429
|
* const users = await db.table('users').limit(1).page(2).get();
|
|
352
430
|
* console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
|
|
353
431
|
*/
|
|
354
|
-
page(number: number): Table
|
|
432
|
+
page(number: number): Table<T>;
|
|
355
433
|
/**
|
|
356
434
|
* Executes the query and returns all matching rows.
|
|
357
435
|
*
|
|
@@ -361,7 +439,7 @@ export declare class Table {
|
|
|
361
439
|
* const users = await db.table('users').get();
|
|
362
440
|
* console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
|
|
363
441
|
*/
|
|
364
|
-
get(): Promise<
|
|
442
|
+
get(): Promise<T[]>;
|
|
365
443
|
/**
|
|
366
444
|
* Executes the query and returns the first matching row.
|
|
367
445
|
*
|
|
@@ -371,7 +449,7 @@ export declare class Table {
|
|
|
371
449
|
* const user = await db.table('users').first();
|
|
372
450
|
* console.log(user); // { id: 1, name: 'John' }
|
|
373
451
|
*/
|
|
374
|
-
first(): Promise<
|
|
452
|
+
first(): Promise<T | null>;
|
|
375
453
|
/**
|
|
376
454
|
* Finds a row by a specific column value.
|
|
377
455
|
*
|
|
@@ -383,7 +461,7 @@ export declare class Table {
|
|
|
383
461
|
* const user = await db.table('users').find(1);
|
|
384
462
|
* console.log(user); // { id: 1, name: 'John' }
|
|
385
463
|
*/
|
|
386
|
-
find(value:
|
|
464
|
+
find(value: string | number, column?: string): Promise<T | null>;
|
|
387
465
|
/**
|
|
388
466
|
* Inserts one or more rows into the table.
|
|
389
467
|
*
|
|
@@ -397,7 +475,7 @@ export declare class Table {
|
|
|
397
475
|
* ]);
|
|
398
476
|
* console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
|
|
399
477
|
*/
|
|
400
|
-
insert(data:
|
|
478
|
+
insert(data: Partial<T>[]): Promise<T[]>;
|
|
401
479
|
/**
|
|
402
480
|
* Updates rows in the table based on the defined conditions.
|
|
403
481
|
*
|
|
@@ -410,7 +488,7 @@ export declare class Table {
|
|
|
410
488
|
* .update({ name: 'John Updated', age: 31 });
|
|
411
489
|
* console.log(result); // { affectedRows: 1 }
|
|
412
490
|
*/
|
|
413
|
-
update(data:
|
|
491
|
+
update(data: Partial<T>): Promise<MutationInfo[]>;
|
|
414
492
|
/**
|
|
415
493
|
* Deletes rows from the table based on the defined conditions.
|
|
416
494
|
*
|
|
@@ -420,7 +498,127 @@ export declare class Table {
|
|
|
420
498
|
* const result = await db.table('users').where('id', '=', 1).delete();
|
|
421
499
|
* console.log(result); // { affectedRows: 1 }
|
|
422
500
|
*/
|
|
423
|
-
delete(): Promise<
|
|
501
|
+
delete(): Promise<MutationInfo[]>;
|
|
502
|
+
/**
|
|
503
|
+
* Adds a WHERE NOT IN condition to the query.
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* const users = await db.table('users').whereNotIn('status', ['banned', 'deleted']).get();
|
|
507
|
+
*/
|
|
508
|
+
whereNotIn(column: string, values: any[]): Table<T>;
|
|
509
|
+
/**
|
|
510
|
+
* Sets an explicit OFFSET for the query (alternative to page()).
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* const rows = await db.table('logs').orderBy('id', 'ASC').limit(50).offset(100).get();
|
|
514
|
+
*/
|
|
515
|
+
offset(number: number): Table<T>;
|
|
516
|
+
/**
|
|
517
|
+
* Appends raw expressions to the SELECT list (aggregates, functions, aliases).
|
|
518
|
+
* Combine with groupBy() and having() for per-group metrics.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* const stats = await db.table('orders')
|
|
522
|
+
* .select(['user_id'])
|
|
523
|
+
* .selectRaw(['COUNT(*) AS order_count', 'SUM(amount) AS total'])
|
|
524
|
+
* .groupBy('user_id')
|
|
525
|
+
* .having('order_count', '>', 5)
|
|
526
|
+
* .get();
|
|
527
|
+
*/
|
|
528
|
+
selectRaw(expressions: string[]): Table<T>;
|
|
529
|
+
/**
|
|
530
|
+
* Adds a HAVING condition (filters grouped results).
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* .groupBy('user_id').having('COUNT(*)', '>', 5)
|
|
534
|
+
*/
|
|
535
|
+
having(column: string, operator: string, value?: any): Table<T>;
|
|
536
|
+
/**
|
|
537
|
+
* Checks if at least one row matches the current conditions.
|
|
538
|
+
* Cheaper than first(): selects a constant with LIMIT 1.
|
|
539
|
+
*
|
|
540
|
+
* @example
|
|
541
|
+
* const taken = await db.table('users').where('email', '=', email).exists();
|
|
542
|
+
*/
|
|
543
|
+
exists(): Promise<boolean>;
|
|
544
|
+
/**
|
|
545
|
+
* Fetches one page of results plus pagination metadata in a single call.
|
|
546
|
+
* Runs the page query and the total count with the same conditions.
|
|
547
|
+
*
|
|
548
|
+
* @example
|
|
549
|
+
* const { items, total, totalPages, hasNext } = await db.table('products')
|
|
550
|
+
* .where('published', '=', true)
|
|
551
|
+
* .orderBy('id', 'ASC')
|
|
552
|
+
* .paginate(2, 25);
|
|
553
|
+
*/
|
|
554
|
+
paginate(page?: number, perPage?: number): Promise<PaginatedResult<T>>;
|
|
555
|
+
/**
|
|
556
|
+
* Processes all matching rows in batches of `size`, keeping memory flat.
|
|
557
|
+
* Return `false` from the callback to stop early.
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* await db.table('logs').orderBy('id', 'ASC').chunk(500, async (rows) => {
|
|
561
|
+
* await processBatch(rows);
|
|
562
|
+
* });
|
|
563
|
+
*/
|
|
564
|
+
chunk(size: number, callback: (rows: T[], page: number) => Promise<void | boolean> | void | boolean): Promise<void>;
|
|
565
|
+
/**
|
|
566
|
+
* Atomically increments a numeric column: `SET col = col + amount`.
|
|
567
|
+
* Requires at least one WHERE condition. Extra fields can be updated in the same statement.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* await db.table('products').where('id', '=', 5).increment('stock', 3);
|
|
571
|
+
* await db.table('posts').where('id', '=', 1).increment('views', 1, { last_viewed_at: new Date().toISOString() });
|
|
572
|
+
*/
|
|
573
|
+
increment(column: string, amount?: number, extra?: Partial<T>): Promise<MutationInfo[]>;
|
|
574
|
+
/**
|
|
575
|
+
* Atomically decrements a numeric column: `SET col = col - amount`.
|
|
576
|
+
*
|
|
577
|
+
* @example
|
|
578
|
+
* await db.table('products').where('id', '=', 5).decrement('stock', 1);
|
|
579
|
+
*/
|
|
580
|
+
decrement(column: string, amount?: number, extra?: Partial<T>): Promise<MutationInfo[]>;
|
|
581
|
+
/**
|
|
582
|
+
* Deletes ALL rows from the table. The only write operation allowed
|
|
583
|
+
* without a WHERE — the destructive intent is explicit in the name.
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* await db.table('temp_imports').truncate();
|
|
587
|
+
*/
|
|
588
|
+
truncate(): Promise<MutationInfo[]>;
|
|
589
|
+
/**
|
|
590
|
+
* Inserts rows, updating them instead when a conflict occurs on the given keys.
|
|
591
|
+
* MySQL → ON DUPLICATE KEY UPDATE · PostgreSQL/SQLite → ON CONFLICT ... DO UPDATE.
|
|
592
|
+
*
|
|
593
|
+
* @param data Rows to insert.
|
|
594
|
+
* @param conflictKeys Column(s) with the UNIQUE/PK constraint that triggers the update.
|
|
595
|
+
* @param updateColumns Columns to overwrite on conflict (defaults to every non-conflict column).
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* await db.table('settings').upsert(
|
|
599
|
+
* [{ key: 'theme', value: 'dark' }],
|
|
600
|
+
* ['key']
|
|
601
|
+
* );
|
|
602
|
+
*/
|
|
603
|
+
upsert(data: Partial<T>[], conflictKeys: string[], updateColumns?: string[]): Promise<MutationInfo[]>;
|
|
604
|
+
/**
|
|
605
|
+
* Declares a relation to eager-load with the results of get().
|
|
606
|
+
* Relations are resolved from `foreign` definitions in your .cube files,
|
|
607
|
+
* or explicitly via options. Loads each relation with ONE batched query
|
|
608
|
+
* (whereIn) — no N+1.
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* // hasMany: orders.user_id → users.id (auto-detected from orders.table.cube)
|
|
612
|
+
* const users = await db.table('users').with('orders').get();
|
|
613
|
+
* // users[0].orders === [{...}, {...}]
|
|
614
|
+
*
|
|
615
|
+
* // belongsTo: posts.author_id → users.id, attached as a single object
|
|
616
|
+
* const posts = await db.table('posts')
|
|
617
|
+
* .with('author', { table: 'users', foreignKey: 'author_id', type: 'one' })
|
|
618
|
+
* .get();
|
|
619
|
+
*/
|
|
620
|
+
with(relation: string, options?: RelationOptions): Table<T>;
|
|
621
|
+
private attachRelations;
|
|
424
622
|
private getResponse;
|
|
425
623
|
private clone;
|
|
426
624
|
}
|