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.
- package/dist/ForgeSQLORM.js +64 -50
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +64 -50
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLORM.d.ts.map +1 -1
- package/dist/core/ForgeSQLQueryBuilder.d.ts +23 -23
- package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
- package/dist/lib/drizzle/extensions/additionalActions.d.ts.map +1 -1
- package/dist/utils/cacheContextUtils.d.ts +4 -2
- package/dist/utils/cacheContextUtils.d.ts.map +1 -1
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/ForgeSQLORM.ts +29 -23
- package/src/core/ForgeSQLQueryBuilder.ts +172 -119
- package/src/lib/drizzle/extensions/additionalActions.ts +123 -74
- package/src/lib/drizzle/extensions/types.d.ts +16 -11
- package/src/utils/cacheContextUtils.ts +16 -4
- package/src/utils/sqlUtils.ts +27 -28
|
@@ -22,27 +22,35 @@ import {
|
|
|
22
22
|
} from "./SystemTables";
|
|
23
23
|
import { ForgeSQLCacheOperations } from "./ForgeSQLCacheOperations";
|
|
24
24
|
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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 {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
import {
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
139
|
+
executeQuery: ExecuteQuery;
|
|
132
140
|
selectAliasedCacheable: SelectAliasedCacheableType;
|
|
133
141
|
selectAliasedDistinctCacheable: SelectAliasedDistinctCacheableType;
|
|
134
|
-
|
|
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
|
-
|
|
177
|
-
):
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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(
|
|
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(
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
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
|
/**
|