@dbcube/query-builder 5.1.6 → 5.2.2

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/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { QueryEngine } from '@dbcube/core';
2
+
1
3
  export interface PaginatedResult<T = DatabaseRecord> {
2
4
  items: T[];
3
5
  page: number;
@@ -17,8 +19,33 @@ export interface RelationOptions {
17
19
  /** 'many' attaches an array (hasMany), 'one' attaches a single record (belongsTo) */
18
20
  type?: "many" | "one";
19
21
  }
20
- export type WhereCallback = (query: Table) => void;
22
+ export type WhereCallback = (query: Table<DatabaseRecord>) => void;
21
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
+ }
22
49
  /**
23
50
  * Main class to handle MySQL database connections and queries.
24
51
  * Implements the Singleton pattern to ensure a single instance of the connection pool.
@@ -38,7 +65,7 @@ export declare class Database {
38
65
  * const rows = await db.raw('SELECT * FROM users WHERE age > ?', [25]);
39
66
  * await db.raw('CREATE INDEX idx_users_email ON users(email)');
40
67
  */
41
- raw(query: string, params?: any[]): Promise<any>;
68
+ raw<R = DatabaseRecord>(query: string, params?: unknown[]): Promise<R[]>;
42
69
  /**
43
70
  * Runs a callback inside a database transaction. Everything executed through
44
71
  * the received connection is committed atomically; any thrown error rolls
@@ -94,15 +121,18 @@ export declare class Database {
94
121
  * // Access column management
95
122
  * const columns = await db.table('users').columns().get();
96
123
  */
97
- table(tableName: string): Table;
124
+ table<T extends DatabaseRecord = DatabaseRecord>(tableName: string): Table<T>;
98
125
  private setComputedFields;
99
126
  private setTriggers;
100
127
  }
101
128
  /**
102
129
  * Class to build and execute SQL queries for a specific table.
103
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`.
104
134
  */
105
- export declare class Table {
135
+ export declare class Table<T extends DatabaseRecord = DatabaseRecord> {
106
136
  private engine;
107
137
  private nextType;
108
138
  private dml;
@@ -111,7 +141,10 @@ export declare class Table {
111
141
  private triggers;
112
142
  private txId;
113
143
  private relations;
114
- constructor(instance: any, databaseName: string, tableName: string, engine?: any, computedFields?: any[], triggers?: any[], txId?: string | null);
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);
115
148
  /**
116
149
  * Specifies the columns to select in a SELECT query.
117
150
  *
@@ -122,7 +155,7 @@ export declare class Table {
122
155
  * const users = await db.table('users').select(['id', 'name']).get();
123
156
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
124
157
  */
125
- select(fields?: string[]): Table;
158
+ select(fields?: string[]): Table<T>;
126
159
  /**
127
160
  * Adds a WHERE condition to the query.
128
161
  *
@@ -136,8 +169,8 @@ export declare class Table {
136
169
  * const nullUsers = await db.table('users').where('email', 'IS NULL').get();
137
170
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
138
171
  */
139
- where(column: string, operator: "IS NULL" | "IS NOT NULL"): Table;
140
- 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>;
141
174
  /**
142
175
  * Adds an OR WHERE condition to the query.
143
176
  *
@@ -151,8 +184,8 @@ export declare class Table {
151
184
  * const nullUsers = await db.table('users').where('active', '=', true).orWhere('email', 'IS NULL').get();
152
185
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
153
186
  */
154
- orWhere(column: string, operator: "IS NULL" | "IS NOT NULL"): Table;
155
- 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>;
156
189
  /**
157
190
  * Adds a grouped WHERE condition to the query.
158
191
  *
@@ -165,9 +198,9 @@ export declare class Table {
165
198
  * }).get();
166
199
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }, { id: 2, name: 'Jane', age: 25 }]
167
200
  */
168
- whereGroup(callback: WhereCallback): Table;
169
- or(): Table;
170
- and(): Table;
201
+ whereGroup(callback: WhereCallback): Table<T>;
202
+ or(): Table<T>;
203
+ and(): Table<T>;
171
204
  /**
172
205
  * Adds a WHERE BETWEEN condition to the query.
173
206
  *
@@ -182,7 +215,7 @@ export declare class Table {
182
215
  whereBetween(column: string, values: [
183
216
  any,
184
217
  any
185
- ]): Table;
218
+ ]): Table<T>;
186
219
  /**
187
220
  * Adds a WHERE IN condition to the query.
188
221
  *
@@ -194,7 +227,7 @@ export declare class Table {
194
227
  * const users = await db.table('users').whereIn('id', [1, 2]).get();
195
228
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
196
229
  */
197
- whereIn(column: string, values: any[]): Table;
230
+ whereIn(column: string, values: any[]): Table<T>;
198
231
  /**
199
232
  * Adds a WHERE IS NULL condition to the query.
200
233
  *
@@ -205,7 +238,7 @@ export declare class Table {
205
238
  * const users = await db.table('users').whereNull('email').get();
206
239
  * console.log(users); // [{ id: 3, name: 'Alice', email: null }]
207
240
  */
208
- whereNull(column: string): Table;
241
+ whereNull(column: string): Table<T>;
209
242
  /**
210
243
  * Adds a WHERE IS NOT NULL condition to the query.
211
244
  *
@@ -216,7 +249,7 @@ export declare class Table {
216
249
  * const users = await db.table('users').whereNotNull('email').get();
217
250
  * console.log(users); // [{ id: 1, name: 'John', email: 'john@example.com' }]
218
251
  */
219
- whereNotNull(column: string): Table;
252
+ whereNotNull(column: string): Table<T>;
220
253
  /**
221
254
  * Adds a JOIN clause to the query.
222
255
  *
@@ -230,7 +263,7 @@ export declare class Table {
230
263
  * const users = await db.table('users').join('orders', 'users.id', '=', 'orders.user_id').get();
231
264
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }]
232
265
  */
233
- join(table: string, column1: string, operator: string, column2: string): Table;
266
+ join(table: string, column1: string, operator: string, column2: string): Table<T>;
234
267
  /**
235
268
  * Adds a LEFT JOIN clause to the query.
236
269
  *
@@ -244,7 +277,7 @@ export declare class Table {
244
277
  * const users = await db.table('users').leftJoin('orders', 'users.id', '=', 'orders.user_id').get();
245
278
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: 2, name: 'Jane', order_id: null }]
246
279
  */
247
- leftJoin(table: string, column1: string, operator: string, column2: string): Table;
280
+ leftJoin(table: string, column1: string, operator: string, column2: string): Table<T>;
248
281
  /**
249
282
  * Adds a RIGHT JOIN clause to the query.
250
283
  *
@@ -258,7 +291,7 @@ export declare class Table {
258
291
  * const users = await db.table('users').rightJoin('orders', 'users.id', '=', 'orders.user_id').get();
259
292
  * console.log(users); // [{ id: 1, name: 'John', order_id: 101 }, { id: null, name: null, order_id: 102 }]
260
293
  */
261
- rightJoin(table: string, column1: string, operator: string, column2: string): Table;
294
+ rightJoin(table: string, column1: string, operator: string, column2: string): Table<T>;
262
295
  /**
263
296
  * Adds an ORDER BY clause to the query.
264
297
  *
@@ -270,7 +303,7 @@ export declare class Table {
270
303
  * const users = await db.table('users').orderBy('name', 'ASC').get();
271
304
  * console.log(users); // [{ id: 2, name: 'Jane' }, { id: 1, name: 'John' }]
272
305
  */
273
- orderBy(column: string, direction?: "ASC" | "DESC"): Table;
306
+ orderBy(column: string, direction?: "ASC" | "DESC"): Table<T>;
274
307
  /**
275
308
  * Adds a GROUP BY clause to the query.
276
309
  *
@@ -281,7 +314,7 @@ export declare class Table {
281
314
  * const users = await db.table('users').groupBy('age').get();
282
315
  * console.log(users); // [{ age: 30, count: 1 }, { age: 25, count: 1 }]
283
316
  */
284
- groupBy(column: string): Table;
317
+ groupBy(column: string): Table<T>;
285
318
  /**
286
319
  * Adds a DISTINCT clause to the query.
287
320
  *
@@ -291,7 +324,7 @@ export declare class Table {
291
324
  * const users = await db.table('users').distinct().select(['name']).get();
292
325
  * console.log(users); // [{ name: 'John' }, { name: 'Jane' }]
293
326
  */
294
- distinct(): Table;
327
+ distinct(): Table<T>;
295
328
  /**
296
329
  * Adds a COUNT clause to the query.
297
330
  *
@@ -302,7 +335,7 @@ export declare class Table {
302
335
  * const count = await db.table('users').count().first();
303
336
  * console.log(count); // { count: 2 }
304
337
  */
305
- count(column?: string): Promise<Number>;
338
+ count(column?: string): Promise<number>;
306
339
  /**
307
340
  * Adds a SUM clause to the query.
308
341
  *
@@ -313,7 +346,7 @@ export declare class Table {
313
346
  * const totalAge = await db.table('users').sum('age').first();
314
347
  * console.log(totalAge); // { sum: 55 }
315
348
  */
316
- sum(column: string): Promise<Number>;
349
+ sum(column: string): Promise<number>;
317
350
  /**
318
351
  * Adds an AVG clause to the query.
319
352
  *
@@ -324,7 +357,7 @@ export declare class Table {
324
357
  * const avgAge = await db.table('users').avg('age').first();
325
358
  * console.log(avgAge); // { avg: 27.5 }
326
359
  */
327
- avg(column: string): Promise<Number>;
360
+ avg(column: string): Promise<number>;
328
361
  /**
329
362
  * Adds a MAX clause to the query.
330
363
  *
@@ -335,7 +368,7 @@ export declare class Table {
335
368
  * const maxAge = await db.table('users').max('age').first();
336
369
  * console.log(maxAge); // { max: 30 }
337
370
  */
338
- max(column: string): Promise<Number>;
371
+ max(column: string): Promise<number>;
339
372
  /**
340
373
  * Adds a MIN clause to the query.
341
374
  *
@@ -346,7 +379,7 @@ export declare class Table {
346
379
  * const minAge = await db.table('users').min('age').first();
347
380
  * console.log(minAge); // { min: 25 }
348
381
  */
349
- min(column: string): Promise<Number>;
382
+ min(column: string): Promise<number>;
350
383
  /**
351
384
  * Gets the column names of the table.
352
385
  * For SQL databases (MySQL, PostgreSQL, SQLite), returns a simple array of column names.
@@ -385,7 +418,7 @@ export declare class Table {
385
418
  * const users = await db.table('users').limit(1).get();
386
419
  * console.log(users); // [{ id: 1, name: 'John', age: 30 }]
387
420
  */
388
- limit(number: number): Table;
421
+ limit(number: number): Table<T>;
389
422
  /**
390
423
  * Adds pagination to the query using LIMIT and OFFSET.
391
424
  *
@@ -396,7 +429,7 @@ export declare class Table {
396
429
  * const users = await db.table('users').limit(1).page(2).get();
397
430
  * console.log(users); // [{ id: 2, name: 'Jane', age: 25 }]
398
431
  */
399
- page(number: number): Table;
432
+ page(number: number): Table<T>;
400
433
  /**
401
434
  * Executes the query and returns all matching rows.
402
435
  *
@@ -406,7 +439,7 @@ export declare class Table {
406
439
  * const users = await db.table('users').get();
407
440
  * console.log(users); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
408
441
  */
409
- get(): Promise<DatabaseRecord[]>;
442
+ get(): Promise<T[]>;
410
443
  /**
411
444
  * Executes the query and returns the first matching row.
412
445
  *
@@ -416,7 +449,7 @@ export declare class Table {
416
449
  * const user = await db.table('users').first();
417
450
  * console.log(user); // { id: 1, name: 'John' }
418
451
  */
419
- first(): Promise<DatabaseRecord | null>;
452
+ first(): Promise<T | null>;
420
453
  /**
421
454
  * Finds a row by a specific column value.
422
455
  *
@@ -428,7 +461,7 @@ export declare class Table {
428
461
  * const user = await db.table('users').find(1);
429
462
  * console.log(user); // { id: 1, name: 'John' }
430
463
  */
431
- find(value: any, column?: string): Promise<DatabaseRecord | null>;
464
+ find(value: string | number, column?: string): Promise<T | null>;
432
465
  /**
433
466
  * Inserts one or more rows into the table.
434
467
  *
@@ -442,7 +475,7 @@ export declare class Table {
442
475
  * ]);
443
476
  * console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
444
477
  */
445
- insert(data: DatabaseRecord[]): Promise<DatabaseRecord[]>;
478
+ insert(data: Partial<T>[]): Promise<T[]>;
446
479
  /**
447
480
  * Updates rows in the table based on the defined conditions.
448
481
  *
@@ -455,7 +488,7 @@ export declare class Table {
455
488
  * .update({ name: 'John Updated', age: 31 });
456
489
  * console.log(result); // { affectedRows: 1 }
457
490
  */
458
- update(data: DatabaseRecord): Promise<any>;
491
+ update(data: Partial<T>): Promise<MutationInfo[]>;
459
492
  /**
460
493
  * Deletes rows from the table based on the defined conditions.
461
494
  *
@@ -465,21 +498,21 @@ export declare class Table {
465
498
  * const result = await db.table('users').where('id', '=', 1).delete();
466
499
  * console.log(result); // { affectedRows: 1 }
467
500
  */
468
- delete(): Promise<any>;
501
+ delete(): Promise<MutationInfo[]>;
469
502
  /**
470
503
  * Adds a WHERE NOT IN condition to the query.
471
504
  *
472
505
  * @example
473
506
  * const users = await db.table('users').whereNotIn('status', ['banned', 'deleted']).get();
474
507
  */
475
- whereNotIn(column: string, values: any[]): Table;
508
+ whereNotIn(column: string, values: any[]): Table<T>;
476
509
  /**
477
510
  * Sets an explicit OFFSET for the query (alternative to page()).
478
511
  *
479
512
  * @example
480
513
  * const rows = await db.table('logs').orderBy('id', 'ASC').limit(50).offset(100).get();
481
514
  */
482
- offset(number: number): Table;
515
+ offset(number: number): Table<T>;
483
516
  /**
484
517
  * Appends raw expressions to the SELECT list (aggregates, functions, aliases).
485
518
  * Combine with groupBy() and having() for per-group metrics.
@@ -492,14 +525,14 @@ export declare class Table {
492
525
  * .having('order_count', '>', 5)
493
526
  * .get();
494
527
  */
495
- selectRaw(expressions: string[]): Table;
528
+ selectRaw(expressions: string[]): Table<T>;
496
529
  /**
497
530
  * Adds a HAVING condition (filters grouped results).
498
531
  *
499
532
  * @example
500
533
  * .groupBy('user_id').having('COUNT(*)', '>', 5)
501
534
  */
502
- having(column: string, operator: string, value?: any): Table;
535
+ having(column: string, operator: string, value?: any): Table<T>;
503
536
  /**
504
537
  * Checks if at least one row matches the current conditions.
505
538
  * Cheaper than first(): selects a constant with LIMIT 1.
@@ -518,7 +551,7 @@ export declare class Table {
518
551
  * .orderBy('id', 'ASC')
519
552
  * .paginate(2, 25);
520
553
  */
521
- paginate(page?: number, perPage?: number): Promise<PaginatedResult>;
554
+ paginate(page?: number, perPage?: number): Promise<PaginatedResult<T>>;
522
555
  /**
523
556
  * Processes all matching rows in batches of `size`, keeping memory flat.
524
557
  * Return `false` from the callback to stop early.
@@ -528,7 +561,7 @@ export declare class Table {
528
561
  * await processBatch(rows);
529
562
  * });
530
563
  */
531
- chunk(size: number, callback: (rows: DatabaseRecord[], page: number) => Promise<void | boolean> | void | boolean): Promise<void>;
564
+ chunk(size: number, callback: (rows: T[], page: number) => Promise<void | boolean> | void | boolean): Promise<void>;
532
565
  /**
533
566
  * Atomically increments a numeric column: `SET col = col + amount`.
534
567
  * Requires at least one WHERE condition. Extra fields can be updated in the same statement.
@@ -537,14 +570,14 @@ export declare class Table {
537
570
  * await db.table('products').where('id', '=', 5).increment('stock', 3);
538
571
  * await db.table('posts').where('id', '=', 1).increment('views', 1, { last_viewed_at: new Date().toISOString() });
539
572
  */
540
- increment(column: string, amount?: number, extra?: DatabaseRecord): Promise<any>;
573
+ increment(column: string, amount?: number, extra?: Partial<T>): Promise<MutationInfo[]>;
541
574
  /**
542
575
  * Atomically decrements a numeric column: `SET col = col - amount`.
543
576
  *
544
577
  * @example
545
578
  * await db.table('products').where('id', '=', 5).decrement('stock', 1);
546
579
  */
547
- decrement(column: string, amount?: number, extra?: DatabaseRecord): Promise<any>;
580
+ decrement(column: string, amount?: number, extra?: Partial<T>): Promise<MutationInfo[]>;
548
581
  /**
549
582
  * Deletes ALL rows from the table. The only write operation allowed
550
583
  * without a WHERE — the destructive intent is explicit in the name.
@@ -552,7 +585,7 @@ export declare class Table {
552
585
  * @example
553
586
  * await db.table('temp_imports').truncate();
554
587
  */
555
- truncate(): Promise<any>;
588
+ truncate(): Promise<MutationInfo[]>;
556
589
  /**
557
590
  * Inserts rows, updating them instead when a conflict occurs on the given keys.
558
591
  * MySQL → ON DUPLICATE KEY UPDATE · PostgreSQL/SQLite → ON CONFLICT ... DO UPDATE.
@@ -567,7 +600,7 @@ export declare class Table {
567
600
  * ['key']
568
601
  * );
569
602
  */
570
- upsert(data: DatabaseRecord[], conflictKeys: string[], updateColumns?: string[]): Promise<any>;
603
+ upsert(data: Partial<T>[], conflictKeys: string[], updateColumns?: string[]): Promise<MutationInfo[]>;
571
604
  /**
572
605
  * Declares a relation to eager-load with the results of get().
573
606
  * Relations are resolved from `foreign` definitions in your .cube files,
@@ -584,7 +617,7 @@ export declare class Table {
584
617
  * .with('author', { table: 'users', foreignKey: 'author_id', type: 'one' })
585
618
  * .get();
586
619
  */
587
- with(relation: string, options?: RelationOptions): Table;
620
+ with(relation: string, options?: RelationOptions): Table<T>;
588
621
  private attachRelations;
589
622
  private getResponse;
590
623
  private clone;