@dbcube/query-builder 5.1.5 → 5.1.6

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,22 @@
1
+ export interface PaginatedResult<T = DatabaseRecord> {
2
+ items: T[];
3
+ page: number;
4
+ perPage: number;
5
+ total: number;
6
+ totalPages: number;
7
+ hasNext: boolean;
8
+ hasPrev: boolean;
9
+ }
10
+ export interface RelationOptions {
11
+ /** Child/related table name (defaults to the relation name) */
12
+ table?: string;
13
+ /** FK column on the related table (hasMany) or on this table (belongsTo) */
14
+ foreignKey?: string;
15
+ /** Key on the parent table (defaults to 'id') */
16
+ localKey?: string;
17
+ /** 'many' attaches an array (hasMany), 'one' attaches a single record (belongsTo) */
18
+ type?: "many" | "one";
19
+ }
1
20
  export type WhereCallback = (query: Table) => void;
2
21
  export type DatabaseRecord = Record<string, any>;
3
22
  /**
@@ -9,7 +28,31 @@ export declare class Database {
9
28
  private engine;
10
29
  private computedFields;
11
30
  private triggers;
31
+ private txId;
12
32
  constructor(name: string);
33
+ /**
34
+ * Executes raw SQL (MySQL/PostgreSQL/SQLite) or a raw command document (MongoDB)
35
+ * with bound parameters. The escape hatch for anything the builder doesn't cover.
36
+ *
37
+ * @example
38
+ * const rows = await db.raw('SELECT * FROM users WHERE age > ?', [25]);
39
+ * await db.raw('CREATE INDEX idx_users_email ON users(email)');
40
+ */
41
+ raw(query: string, params?: any[]): Promise<any>;
42
+ /**
43
+ * Runs a callback inside a database transaction. Everything executed through
44
+ * the received connection is committed atomically; any thrown error rolls
45
+ * the whole transaction back.
46
+ *
47
+ * Requires daemon mode (enabled by default with an up-to-date query-engine).
48
+ *
49
+ * @example
50
+ * await db.transaction(async (trx) => {
51
+ * await trx.table('accounts').where('id', '=', 1).update({ balance: 50 });
52
+ * await trx.table('accounts').where('id', '=', 2).update({ balance: 150 });
53
+ * });
54
+ */
55
+ transaction<T>(callback: (trx: Database) => Promise<T>): Promise<T>;
13
56
  useComputes(): Promise<Database>;
14
57
  useTriggers(): Promise<Database>;
15
58
  connect(): Promise<void>;
@@ -66,7 +109,9 @@ export declare class Table {
66
109
  private computedFields;
67
110
  private trigger;
68
111
  private triggers;
69
- constructor(instance: any, databaseName: string, tableName: string, engine?: any, computedFields?: any[], triggers?: any[]);
112
+ private txId;
113
+ private relations;
114
+ constructor(instance: any, databaseName: string, tableName: string, engine?: any, computedFields?: any[], triggers?: any[], txId?: string | null);
70
115
  /**
71
116
  * Specifies the columns to select in a SELECT query.
72
117
  *
@@ -421,6 +466,126 @@ export declare class Table {
421
466
  * console.log(result); // { affectedRows: 1 }
422
467
  */
423
468
  delete(): Promise<any>;
469
+ /**
470
+ * Adds a WHERE NOT IN condition to the query.
471
+ *
472
+ * @example
473
+ * const users = await db.table('users').whereNotIn('status', ['banned', 'deleted']).get();
474
+ */
475
+ whereNotIn(column: string, values: any[]): Table;
476
+ /**
477
+ * Sets an explicit OFFSET for the query (alternative to page()).
478
+ *
479
+ * @example
480
+ * const rows = await db.table('logs').orderBy('id', 'ASC').limit(50).offset(100).get();
481
+ */
482
+ offset(number: number): Table;
483
+ /**
484
+ * Appends raw expressions to the SELECT list (aggregates, functions, aliases).
485
+ * Combine with groupBy() and having() for per-group metrics.
486
+ *
487
+ * @example
488
+ * const stats = await db.table('orders')
489
+ * .select(['user_id'])
490
+ * .selectRaw(['COUNT(*) AS order_count', 'SUM(amount) AS total'])
491
+ * .groupBy('user_id')
492
+ * .having('order_count', '>', 5)
493
+ * .get();
494
+ */
495
+ selectRaw(expressions: string[]): Table;
496
+ /**
497
+ * Adds a HAVING condition (filters grouped results).
498
+ *
499
+ * @example
500
+ * .groupBy('user_id').having('COUNT(*)', '>', 5)
501
+ */
502
+ having(column: string, operator: string, value?: any): Table;
503
+ /**
504
+ * Checks if at least one row matches the current conditions.
505
+ * Cheaper than first(): selects a constant with LIMIT 1.
506
+ *
507
+ * @example
508
+ * const taken = await db.table('users').where('email', '=', email).exists();
509
+ */
510
+ exists(): Promise<boolean>;
511
+ /**
512
+ * Fetches one page of results plus pagination metadata in a single call.
513
+ * Runs the page query and the total count with the same conditions.
514
+ *
515
+ * @example
516
+ * const { items, total, totalPages, hasNext } = await db.table('products')
517
+ * .where('published', '=', true)
518
+ * .orderBy('id', 'ASC')
519
+ * .paginate(2, 25);
520
+ */
521
+ paginate(page?: number, perPage?: number): Promise<PaginatedResult>;
522
+ /**
523
+ * Processes all matching rows in batches of `size`, keeping memory flat.
524
+ * Return `false` from the callback to stop early.
525
+ *
526
+ * @example
527
+ * await db.table('logs').orderBy('id', 'ASC').chunk(500, async (rows) => {
528
+ * await processBatch(rows);
529
+ * });
530
+ */
531
+ chunk(size: number, callback: (rows: DatabaseRecord[], page: number) => Promise<void | boolean> | void | boolean): Promise<void>;
532
+ /**
533
+ * Atomically increments a numeric column: `SET col = col + amount`.
534
+ * Requires at least one WHERE condition. Extra fields can be updated in the same statement.
535
+ *
536
+ * @example
537
+ * await db.table('products').where('id', '=', 5).increment('stock', 3);
538
+ * await db.table('posts').where('id', '=', 1).increment('views', 1, { last_viewed_at: new Date().toISOString() });
539
+ */
540
+ increment(column: string, amount?: number, extra?: DatabaseRecord): Promise<any>;
541
+ /**
542
+ * Atomically decrements a numeric column: `SET col = col - amount`.
543
+ *
544
+ * @example
545
+ * await db.table('products').where('id', '=', 5).decrement('stock', 1);
546
+ */
547
+ decrement(column: string, amount?: number, extra?: DatabaseRecord): Promise<any>;
548
+ /**
549
+ * Deletes ALL rows from the table. The only write operation allowed
550
+ * without a WHERE — the destructive intent is explicit in the name.
551
+ *
552
+ * @example
553
+ * await db.table('temp_imports').truncate();
554
+ */
555
+ truncate(): Promise<any>;
556
+ /**
557
+ * Inserts rows, updating them instead when a conflict occurs on the given keys.
558
+ * MySQL → ON DUPLICATE KEY UPDATE · PostgreSQL/SQLite → ON CONFLICT ... DO UPDATE.
559
+ *
560
+ * @param data Rows to insert.
561
+ * @param conflictKeys Column(s) with the UNIQUE/PK constraint that triggers the update.
562
+ * @param updateColumns Columns to overwrite on conflict (defaults to every non-conflict column).
563
+ *
564
+ * @example
565
+ * await db.table('settings').upsert(
566
+ * [{ key: 'theme', value: 'dark' }],
567
+ * ['key']
568
+ * );
569
+ */
570
+ upsert(data: DatabaseRecord[], conflictKeys: string[], updateColumns?: string[]): Promise<any>;
571
+ /**
572
+ * Declares a relation to eager-load with the results of get().
573
+ * Relations are resolved from `foreign` definitions in your .cube files,
574
+ * or explicitly via options. Loads each relation with ONE batched query
575
+ * (whereIn) — no N+1.
576
+ *
577
+ * @example
578
+ * // hasMany: orders.user_id → users.id (auto-detected from orders.table.cube)
579
+ * const users = await db.table('users').with('orders').get();
580
+ * // users[0].orders === [{...}, {...}]
581
+ *
582
+ * // belongsTo: posts.author_id → users.id, attached as a single object
583
+ * const posts = await db.table('posts')
584
+ * .with('author', { table: 'users', foreignKey: 'author_id', type: 'one' })
585
+ * .get();
586
+ */
587
+ with(relation: string, options?: RelationOptions): Table;
588
+ private attachRelations;
424
589
  private getResponse;
425
590
  private clone;
426
591
  }