forge-sql-orm 2.1.1 → 2.1.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.
@@ -22,27 +22,35 @@ import {
22
22
  } from "./SystemTables";
23
23
  import { ForgeSQLCacheOperations } from "./ForgeSQLCacheOperations";
24
24
  import {
25
- DeleteAndEvictCacheType, ExecuteQuery, ExecuteQueryCacheable,
26
- InsertAndEvictCacheType,
27
- SelectAliasedCacheableType,
28
- SelectAliasedDistinctCacheableType,
29
- SelectAliasedDistinctType,
30
- SelectAliasedType, SelectAllDistinctFromAliasedType,
31
- SelectAllDistinctFromCacheableAliasedType, SelectAllFromAliasedType, SelectAllFromCacheableAliasedType,
32
- UpdateAndEvictCacheType,
25
+ DeleteAndEvictCacheType,
26
+ ExecuteQuery,
27
+ ExecuteQueryCacheable,
28
+ InsertAndEvictCacheType,
29
+ SelectAliasedCacheableType,
30
+ SelectAliasedDistinctCacheableType,
31
+ SelectAliasedDistinctType,
32
+ SelectAliasedType,
33
+ SelectAllDistinctFromAliasedType,
34
+ SelectAllDistinctFromCacheableAliasedType,
35
+ SelectAllFromAliasedType,
36
+ SelectAllFromCacheableAliasedType,
37
+ UpdateAndEvictCacheType,
33
38
  } from "..";
34
39
  import {
35
40
  MySqlDeleteBase,
36
41
  MySqlInsertBuilder,
37
- MySqlSelectBase,
42
+ MySqlSelectBase,
38
43
  MySqlUpdateBuilder,
39
44
  } from "drizzle-orm/mysql-core/query-builders";
40
45
  import { MySqlRemoteQueryResultHKT } from "drizzle-orm/mysql-proxy";
41
- import {GetSelectTableName, GetSelectTableSelection} from "drizzle-orm/query-builders/select.types";
42
- import {SQLWrapper} from "drizzle-orm/sql/sql";
43
- import type {MySqlQueryResultKind} from "drizzle-orm/mysql-core/session";
44
- import type {WithBuilder} from "drizzle-orm/mysql-core/subquery";
45
- import {WithSubquery} from "drizzle-orm/subquery";
46
+ import {
47
+ GetSelectTableName,
48
+ GetSelectTableSelection,
49
+ } from "drizzle-orm/query-builders/select.types";
50
+ import { SQLWrapper } from "drizzle-orm/sql/sql";
51
+ import type { MySqlQueryResultKind } from "drizzle-orm/mysql-core/session";
52
+ import type { WithBuilder } from "drizzle-orm/mysql-core/subquery";
53
+ import { WithSubquery } from "drizzle-orm/subquery";
46
54
 
47
55
  /**
48
56
  * Core interface for ForgeSQL operations.
@@ -62,20 +70,20 @@ export interface ForgeSqlOperation extends QueryBuilderForgeSql {
62
70
  getDrizzleQueryBuilder(): MySqlRemoteDatabase<Record<string, unknown>> & {
63
71
  selectAliased: SelectAliasedType;
64
72
  selectAliasedDistinct: SelectAliasedDistinctType;
65
- executeQuery: ExecuteQuery;
66
- selectAliasedCacheable: SelectAliasedCacheableType;
67
- selectAliasedDistinctCacheable: SelectAliasedDistinctCacheableType;
68
- executeQueryCacheable: ExecuteQueryCacheable;
69
- insertWithCacheContext: InsertAndEvictCacheType;
70
- insertAndEvictCache: InsertAndEvictCacheType;
71
- updateAndEvictCache: UpdateAndEvictCacheType;
72
- updateWithCacheContext: UpdateAndEvictCacheType;
73
- deleteAndEvictCache: DeleteAndEvictCacheType;
74
- deleteWithCacheContext: DeleteAndEvictCacheType;
75
- selectFrom: SelectAllFromAliasedType;
76
- selectDistinctFrom: SelectAllDistinctFromAliasedType;
77
- selectFromCacheable: SelectAllFromCacheableAliasedType;
78
- selectDistinctFromCacheable: SelectAllDistinctFromCacheableAliasedType;
73
+ executeQuery: ExecuteQuery;
74
+ selectAliasedCacheable: SelectAliasedCacheableType;
75
+ selectAliasedDistinctCacheable: SelectAliasedDistinctCacheableType;
76
+ executeQueryCacheable: ExecuteQueryCacheable;
77
+ insertWithCacheContext: InsertAndEvictCacheType;
78
+ insertAndEvictCache: InsertAndEvictCacheType;
79
+ updateAndEvictCache: UpdateAndEvictCacheType;
80
+ updateWithCacheContext: UpdateAndEvictCacheType;
81
+ deleteAndEvictCache: DeleteAndEvictCacheType;
82
+ deleteWithCacheContext: DeleteAndEvictCacheType;
83
+ selectFrom: SelectAllFromAliasedType;
84
+ selectDistinctFrom: SelectAllDistinctFromAliasedType;
85
+ selectFromCacheable: SelectAllFromCacheableAliasedType;
86
+ selectDistinctFromCacheable: SelectAllDistinctFromCacheableAliasedType;
79
87
  };
80
88
 
81
89
  /**
@@ -128,10 +136,10 @@ export interface QueryBuilderForgeSql {
128
136
  getDrizzleQueryBuilder(): MySqlRemoteDatabase<Record<string, unknown>> & {
129
137
  selectAliased: SelectAliasedType;
130
138
  selectAliasedDistinct: SelectAliasedDistinctType;
131
- executeQuery: ExecuteQuery;
139
+ executeQuery: ExecuteQuery;
132
140
  selectAliasedCacheable: SelectAliasedCacheableType;
133
141
  selectAliasedDistinctCacheable: SelectAliasedDistinctCacheableType;
134
- executeQueryCacheable: ExecuteQueryCacheable;
142
+ executeQueryCacheable: ExecuteQueryCacheable;
135
143
  insertWithCacheContext: InsertAndEvictCacheType;
136
144
  insertAndEvictCache: InsertAndEvictCacheType;
137
145
  updateAndEvictCache: UpdateAndEvictCacheType;
@@ -173,10 +181,19 @@ export interface QueryBuilderForgeSql {
173
181
  * ```
174
182
  */
175
183
  selectFrom<T extends MySqlTable>(
176
- table: T,
177
- ): MySqlSelectBase<GetSelectTableName<T>, T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"], T["_"]["columns"] extends undefined ? "single" : "partial", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, any>;
184
+ table: T,
185
+ ): MySqlSelectBase<
186
+ GetSelectTableName<T>,
187
+ T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"],
188
+ T["_"]["columns"] extends undefined ? "single" : "partial",
189
+ MySqlRemotePreparedQueryHKT,
190
+ GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {},
191
+ false,
192
+ never,
193
+ any
194
+ >;
178
195
 
179
- /**
196
+ /**
180
197
  * Creates a distinct select query with unique field aliases to prevent field name collisions in joins.
181
198
  * This is particularly useful when working with Atlassian Forge SQL, which collapses fields with the same name in joined tables.
182
199
  *
@@ -195,21 +212,30 @@ export interface QueryBuilderForgeSql {
195
212
  selectDistinct<TSelection extends SelectedFields>(
196
213
  fields: TSelection,
197
214
  ): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
198
- /**
199
- * Creates a select distinct query builder for all columns from a table with field aliasing support.
200
- * This is a convenience method that automatically selects all distinct columns from the specified table.
201
- *
202
- * @template T - The type of the table
203
- * @param table - The table to select from
204
- * @returns Select distinct query builder with all table columns and field aliasing support
205
- * @example
206
- * ```typescript
207
- * const uniqueUsers = await forgeSQL.selectDistinctFrom(userTable).where(eq(userTable.status, 'active'));
208
- * ```
209
- */
210
- selectDistinctFrom<T extends MySqlTable>(
211
- table: T,
212
- ): MySqlSelectBase<GetSelectTableName<T>, T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"], T["_"]["columns"] extends undefined ? "single" : "partial", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, any>;
215
+ /**
216
+ * Creates a select distinct query builder for all columns from a table with field aliasing support.
217
+ * This is a convenience method that automatically selects all distinct columns from the specified table.
218
+ *
219
+ * @template T - The type of the table
220
+ * @param table - The table to select from
221
+ * @returns Select distinct query builder with all table columns and field aliasing support
222
+ * @example
223
+ * ```typescript
224
+ * const uniqueUsers = await forgeSQL.selectDistinctFrom(userTable).where(eq(userTable.status, 'active'));
225
+ * ```
226
+ */
227
+ selectDistinctFrom<T extends MySqlTable>(
228
+ table: T,
229
+ ): MySqlSelectBase<
230
+ GetSelectTableName<T>,
231
+ T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"],
232
+ T["_"]["columns"] extends undefined ? "single" : "partial",
233
+ MySqlRemotePreparedQueryHKT,
234
+ GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {},
235
+ false,
236
+ never,
237
+ any
238
+ >;
213
239
 
214
240
  /**
215
241
  * Creates a cacheable select query with unique field aliases to prevent field name collisions in joins.
@@ -247,9 +273,18 @@ export interface QueryBuilderForgeSql {
247
273
  * ```
248
274
  */
249
275
  selectCacheableFrom<T extends MySqlTable>(
250
- table: T,
251
- cacheTTL?: number,
252
- ): MySqlSelectBase<GetSelectTableName<T>, T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"], T["_"]["columns"] extends undefined ? "single" : "partial", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, any>;
276
+ table: T,
277
+ cacheTTL?: number,
278
+ ): MySqlSelectBase<
279
+ GetSelectTableName<T>,
280
+ T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"],
281
+ T["_"]["columns"] extends undefined ? "single" : "partial",
282
+ MySqlRemotePreparedQueryHKT,
283
+ GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {},
284
+ false,
285
+ never,
286
+ any
287
+ >;
253
288
 
254
289
  /**
255
290
  * Creates a cacheable distinct select query with unique field aliases to prevent field name collisions in joins.
@@ -273,25 +308,34 @@ export interface QueryBuilderForgeSql {
273
308
  cacheTTL?: number,
274
309
  ): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
275
310
 
276
- /**
277
- * Creates a cacheable select distinct query builder for all columns from a table with field aliasing and caching support.
278
- * This is a convenience method that automatically selects all distinct columns from the specified table with caching enabled.
279
- *
280
- * @template T - The type of the table
281
- * @param table - The table to select from
282
- * @param cacheTTL - Optional cache TTL override (defaults to global cache TTL)
283
- * @returns Select distinct query builder with all table columns, field aliasing, and caching support
284
- * @example
285
- * ```typescript
286
- * const uniqueUsers = await forgeSQL.selectDistinctCacheableFrom(userTable, 300).where(eq(userTable.status, 'active'));
287
- * ```
288
- */
289
- selectDistinctCacheableFrom<T extends MySqlTable>(
290
- table: T,
291
- cacheTTL?: number,
292
- ): MySqlSelectBase<GetSelectTableName<T>, T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"], T["_"]["columns"] extends undefined ? "single" : "partial", MySqlRemotePreparedQueryHKT, GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {}, false, never, any>;
293
-
294
- /**
311
+ /**
312
+ * Creates a cacheable select distinct query builder for all columns from a table with field aliasing and caching support.
313
+ * This is a convenience method that automatically selects all distinct columns from the specified table with caching enabled.
314
+ *
315
+ * @template T - The type of the table
316
+ * @param table - The table to select from
317
+ * @param cacheTTL - Optional cache TTL override (defaults to global cache TTL)
318
+ * @returns Select distinct query builder with all table columns, field aliasing, and caching support
319
+ * @example
320
+ * ```typescript
321
+ * const uniqueUsers = await forgeSQL.selectDistinctCacheableFrom(userTable, 300).where(eq(userTable.status, 'active'));
322
+ * ```
323
+ */
324
+ selectDistinctCacheableFrom<T extends MySqlTable>(
325
+ table: T,
326
+ cacheTTL?: number,
327
+ ): MySqlSelectBase<
328
+ GetSelectTableName<T>,
329
+ T["_"]["columns"] extends undefined ? GetSelectTableSelection<T> : T["_"]["columns"],
330
+ T["_"]["columns"] extends undefined ? "single" : "partial",
331
+ MySqlRemotePreparedQueryHKT,
332
+ GetSelectTableName<T> extends string ? Record<string & GetSelectTableName<T>, "not-null"> : {},
333
+ false,
334
+ never,
335
+ any
336
+ >;
337
+
338
+ /**
295
339
  * Creates an insert query builder.
296
340
  *
297
341
  * ⚠️ **IMPORTANT**: This method does NOT support optimistic locking/versioning.
@@ -460,12 +504,14 @@ export interface QueryBuilderForgeSql {
460
504
  * ```typescript
461
505
  * // Using SQLWrapper
462
506
  * const result = await forgeSQL.execute(sql`SELECT * FROM users WHERE id = ${userId}`);
463
- *
507
+ *
464
508
  * // Using string
465
509
  * const result = await forgeSQL.execute("SELECT * FROM users WHERE status = 'active'");
466
510
  * ```
467
511
  */
468
- execute(query: SQLWrapper | string): Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, unknown>>;
512
+ execute(
513
+ query: SQLWrapper | string,
514
+ ): Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, unknown>>;
469
515
 
470
516
  /**
471
517
  * Executes a raw SQL query with both local and global cache support.
@@ -480,57 +526,64 @@ export interface QueryBuilderForgeSql {
480
526
  * ```typescript
481
527
  * // Using SQLWrapper with custom TTL
482
528
  * const result = await forgeSQL.executeCacheable(sql`SELECT * FROM users WHERE id = ${userId}`, 300);
483
- *
529
+ *
484
530
  * // Using string with default TTL
485
531
  * const result = await forgeSQL.executeCacheable("SELECT * FROM users WHERE status = 'active'");
486
532
  * ```
487
533
  */
488
- executeCacheable(query: SQLWrapper | string, cacheTtl?: number): Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, unknown>>;
489
- /**
490
- * Creates a Common Table Expression (CTE) builder for complex queries.
491
- * CTEs allow you to define temporary named result sets that exist within the scope of a single query.
492
- *
493
- * @returns WithBuilder for creating CTEs
494
- * @example
495
- * ```typescript
496
- * const withQuery = forgeSQL.$with('userStats').as(
497
- * forgeSQL.select({ userId: users.id, count: sql<number>`count(*)` })
498
- * .from(users)
499
- * .groupBy(users.id)
500
- * );
501
- * ```
502
- */
503
- $with: WithBuilder;
504
-
505
- /**
506
- * Creates a query builder that uses Common Table Expressions (CTEs).
507
- * CTEs allow you to define temporary named result sets that exist within the scope of a single query.
508
- *
509
- * @param queries - Array of CTE queries created with $with()
510
- * @returns Query builder with CTE support
511
- * @example
512
- * ```typescript
513
- * const withQuery = forgeSQL.$with('userStats').as(
514
- * forgeSQL.select({ userId: users.id, count: sql<number>`count(*)` })
515
- * .from(users)
516
- * .groupBy(users.id)
517
- * );
518
- *
519
- * const result = await forgeSQL.with(withQuery)
520
- * .select({ userId: withQuery.userId, count: withQuery.count })
521
- * .from(withQuery);
522
- * ```
523
- */
524
- with(...queries: WithSubquery[]): {
525
- select: {
526
- (): MySqlSelectBuilder<undefined, MySqlRemotePreparedQueryHKT>;
527
- <TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
528
- };
529
- selectDistinct: {
530
- (): MySqlSelectBuilder<undefined, MySqlRemotePreparedQueryHKT>;
531
- <TSelection extends SelectedFields>(fields: TSelection): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
532
- };
533
- }
534
+ executeCacheable(
535
+ query: SQLWrapper | string,
536
+ cacheTtl?: number,
537
+ ): Promise<MySqlQueryResultKind<MySqlRemoteQueryResultHKT, unknown>>;
538
+ /**
539
+ * Creates a Common Table Expression (CTE) builder for complex queries.
540
+ * CTEs allow you to define temporary named result sets that exist within the scope of a single query.
541
+ *
542
+ * @returns WithBuilder for creating CTEs
543
+ * @example
544
+ * ```typescript
545
+ * const withQuery = forgeSQL.$with('userStats').as(
546
+ * forgeSQL.select({ userId: users.id, count: sql<number>`count(*)` })
547
+ * .from(users)
548
+ * .groupBy(users.id)
549
+ * );
550
+ * ```
551
+ */
552
+ $with: WithBuilder;
553
+
554
+ /**
555
+ * Creates a query builder that uses Common Table Expressions (CTEs).
556
+ * CTEs allow you to define temporary named result sets that exist within the scope of a single query.
557
+ *
558
+ * @param queries - Array of CTE queries created with $with()
559
+ * @returns Query builder with CTE support
560
+ * @example
561
+ * ```typescript
562
+ * const withQuery = forgeSQL.$with('userStats').as(
563
+ * forgeSQL.select({ userId: users.id, count: sql<number>`count(*)` })
564
+ * .from(users)
565
+ * .groupBy(users.id)
566
+ * );
567
+ *
568
+ * const result = await forgeSQL.with(withQuery)
569
+ * .select({ userId: withQuery.userId, count: withQuery.count })
570
+ * .from(withQuery);
571
+ * ```
572
+ */
573
+ with(...queries: WithSubquery[]): {
574
+ select: {
575
+ (): MySqlSelectBuilder<undefined, MySqlRemotePreparedQueryHKT>;
576
+ <TSelection extends SelectedFields>(
577
+ fields: TSelection,
578
+ ): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
579
+ };
580
+ selectDistinct: {
581
+ (): MySqlSelectBuilder<undefined, MySqlRemotePreparedQueryHKT>;
582
+ <TSelection extends SelectedFields>(
583
+ fields: TSelection,
584
+ ): MySqlSelectBuilder<TSelection, MySqlRemotePreparedQueryHKT>;
585
+ };
586
+ };
534
587
  }
535
588
 
536
589
  /**