forge-sql-orm 2.0.30 → 2.1.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 +1410 -81
- package/dist/ForgeSQLORM.js +1456 -60
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +1440 -61
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLAnalyseOperations.d.ts +1 -1
- package/dist/core/ForgeSQLAnalyseOperations.d.ts.map +1 -1
- package/dist/core/ForgeSQLCacheOperations.d.ts +119 -0
- package/dist/core/ForgeSQLCacheOperations.d.ts.map +1 -0
- package/dist/core/ForgeSQLCrudOperations.d.ts +38 -22
- package/dist/core/ForgeSQLCrudOperations.d.ts.map +1 -1
- package/dist/core/ForgeSQLORM.d.ts +248 -13
- package/dist/core/ForgeSQLORM.d.ts.map +1 -1
- package/dist/core/ForgeSQLQueryBuilder.d.ts +394 -19
- package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/lib/drizzle/extensions/additionalActions.d.ts +90 -0
- package/dist/lib/drizzle/extensions/additionalActions.d.ts.map +1 -0
- package/dist/utils/cacheContextUtils.d.ts +123 -0
- package/dist/utils/cacheContextUtils.d.ts.map +1 -0
- package/dist/utils/cacheUtils.d.ts +56 -0
- package/dist/utils/cacheUtils.d.ts.map +1 -0
- package/dist/utils/sqlUtils.d.ts +8 -0
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/dist/webtriggers/clearCacheSchedulerTrigger.d.ts +46 -0
- package/dist/webtriggers/clearCacheSchedulerTrigger.d.ts.map +1 -0
- package/dist/webtriggers/index.d.ts +1 -0
- package/dist/webtriggers/index.d.ts.map +1 -1
- package/package.json +15 -12
- package/src/core/ForgeSQLAnalyseOperations.ts +1 -1
- package/src/core/ForgeSQLCacheOperations.ts +195 -0
- package/src/core/ForgeSQLCrudOperations.ts +49 -40
- package/src/core/ForgeSQLORM.ts +743 -34
- package/src/core/ForgeSQLQueryBuilder.ts +456 -20
- package/src/index.ts +1 -1
- package/src/lib/drizzle/extensions/additionalActions.ts +852 -0
- package/src/lib/drizzle/extensions/types.d.ts +99 -10
- package/src/utils/cacheContextUtils.ts +212 -0
- package/src/utils/cacheUtils.ts +403 -0
- package/src/utils/sqlUtils.ts +42 -0
- package/src/webtriggers/clearCacheSchedulerTrigger.ts +79 -0
- package/src/webtriggers/index.ts +1 -0
- package/dist/lib/drizzle/extensions/selectAliased.d.ts +0 -9
- package/dist/lib/drizzle/extensions/selectAliased.d.ts.map +0 -1
- package/src/lib/drizzle/extensions/selectAliased.ts +0 -72
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { ForgeSqlOrmOptions } from "..";
|
|
2
|
-
import {
|
|
3
|
-
import { AnyMySqlTable } from "drizzle-orm/mysql-core
|
|
4
|
-
import { AnyColumn, InferInsertModel } from "drizzle-orm";
|
|
5
|
-
import { eq, and } from "drizzle-orm";
|
|
6
|
-
import { SQL } from "drizzle-orm";
|
|
2
|
+
import { VerioningModificationForgeSQL, ForgeSqlOperation } from "./ForgeSQLQueryBuilder";
|
|
3
|
+
import { AnyMySqlTable } from "drizzle-orm/mysql-core";
|
|
4
|
+
import { and, AnyColumn, eq, InferInsertModel, SQL } from "drizzle-orm";
|
|
7
5
|
import { getPrimaryKeys, getTableMetadata } from "../utils/sqlUtils";
|
|
6
|
+
import { saveTableIfInsideCacheContext } from "../utils/cacheContextUtils";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
|
-
* Class implementing
|
|
9
|
+
* Class implementing Modification operations for ForgeSQL ORM.
|
|
11
10
|
* Provides methods for inserting, updating, and deleting records with support for optimistic locking.
|
|
12
11
|
*/
|
|
13
|
-
export class ForgeSQLCrudOperations implements
|
|
12
|
+
export class ForgeSQLCrudOperations implements VerioningModificationForgeSQL {
|
|
14
13
|
private readonly forgeOperations: ForgeSqlOperation;
|
|
15
14
|
private readonly options: ForgeSqlOrmOptions;
|
|
16
15
|
|
|
@@ -28,12 +27,17 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
28
27
|
* Inserts records into the database with optional versioning support.
|
|
29
28
|
* If a version field exists in the schema, versioning is applied.
|
|
30
29
|
*
|
|
30
|
+
* This method automatically handles:
|
|
31
|
+
* - Version field initialization for optimistic locking
|
|
32
|
+
* - Batch insertion for multiple records
|
|
33
|
+
* - Duplicate key handling with optional updates
|
|
34
|
+
*
|
|
31
35
|
* @template T - The type of the table schema
|
|
32
|
-
* @param
|
|
33
|
-
* @param
|
|
34
|
-
* @param
|
|
35
|
-
* @returns
|
|
36
|
-
* @throws
|
|
36
|
+
* @param schema - The entity schema
|
|
37
|
+
* @param models - Array of entities to insert
|
|
38
|
+
* @param updateIfExists - Whether to update existing records (default: false)
|
|
39
|
+
* @returns Promise that resolves to the number of inserted rows
|
|
40
|
+
* @throws Error if the insert operation fails
|
|
37
41
|
*/
|
|
38
42
|
async insert<T extends AnyMySqlTable>(
|
|
39
43
|
schema: T,
|
|
@@ -51,10 +55,7 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
51
55
|
);
|
|
52
56
|
|
|
53
57
|
// Build insert query
|
|
54
|
-
const queryBuilder = this.forgeOperations
|
|
55
|
-
.getDrizzleQueryBuilder()
|
|
56
|
-
.insert(schema)
|
|
57
|
-
.values(preparedModels);
|
|
58
|
+
const queryBuilder = this.forgeOperations.insert(schema).values(preparedModels);
|
|
58
59
|
|
|
59
60
|
// Add onDuplicateKeyUpdate if needed
|
|
60
61
|
const finalQuery = updateIfExists
|
|
@@ -67,6 +68,7 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
67
68
|
|
|
68
69
|
// Execute query
|
|
69
70
|
const result = await finalQuery;
|
|
71
|
+
await saveTableIfInsideCacheContext(schema);
|
|
70
72
|
return result[0].insertId;
|
|
71
73
|
}
|
|
72
74
|
|
|
@@ -74,12 +76,18 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
74
76
|
* Deletes a record by its primary key with optional version check.
|
|
75
77
|
* If versioning is enabled, ensures the record hasn't been modified since last read.
|
|
76
78
|
*
|
|
79
|
+
* This method automatically handles:
|
|
80
|
+
* - Single primary key validation
|
|
81
|
+
* - Optimistic locking checks if versioning is enabled
|
|
82
|
+
* - Version field validation before deletion
|
|
83
|
+
*
|
|
77
84
|
* @template T - The type of the table schema
|
|
78
|
-
* @param
|
|
79
|
-
* @param
|
|
80
|
-
* @returns
|
|
81
|
-
* @throws
|
|
82
|
-
* @throws
|
|
85
|
+
* @param id - The ID of the record to delete
|
|
86
|
+
* @param schema - The entity schema
|
|
87
|
+
* @returns Promise that resolves to the number of affected rows
|
|
88
|
+
* @throws Error if the delete operation fails
|
|
89
|
+
* @throws Error if multiple primary keys are found
|
|
90
|
+
* @throws Error if optimistic locking check fails
|
|
83
91
|
*/
|
|
84
92
|
async deleteById<T extends AnyMySqlTable>(id: unknown, schema: T): Promise<number> {
|
|
85
93
|
const { tableName, columns } = getTableMetadata(schema);
|
|
@@ -108,13 +116,13 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
// Execute delete query
|
|
111
|
-
const queryBuilder = this.forgeOperations
|
|
112
|
-
.getDrizzleQueryBuilder()
|
|
113
|
-
.delete(schema)
|
|
114
|
-
.where(and(...conditions));
|
|
119
|
+
const queryBuilder = this.forgeOperations.delete(schema).where(and(...conditions));
|
|
115
120
|
|
|
116
121
|
const result = await queryBuilder;
|
|
117
|
-
|
|
122
|
+
if (versionMetadata && result[0].affectedRows === 0) {
|
|
123
|
+
throw new Error(`Optimistic locking failed: record with primary key ${id} has been modified`);
|
|
124
|
+
}
|
|
125
|
+
await saveTableIfInsideCacheContext(schema);
|
|
118
126
|
return result[0].affectedRows;
|
|
119
127
|
}
|
|
120
128
|
|
|
@@ -125,13 +133,19 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
125
133
|
* - Checks for concurrent modifications
|
|
126
134
|
* - Increments the version on successful update
|
|
127
135
|
*
|
|
136
|
+
* This method automatically handles:
|
|
137
|
+
* - Primary key validation
|
|
138
|
+
* - Version field retrieval and validation
|
|
139
|
+
* - Optimistic locking conflict detection
|
|
140
|
+
* - Version field incrementation
|
|
141
|
+
*
|
|
128
142
|
* @template T - The type of the table schema
|
|
129
|
-
* @param
|
|
130
|
-
* @param
|
|
131
|
-
* @returns
|
|
132
|
-
* @throws
|
|
133
|
-
* @throws
|
|
134
|
-
* @throws
|
|
143
|
+
* @param entity - The entity with updated values (must include primary key)
|
|
144
|
+
* @param schema - The entity schema
|
|
145
|
+
* @returns Promise that resolves to the number of affected rows
|
|
146
|
+
* @throws Error if the primary key is not provided
|
|
147
|
+
* @throws Error if optimistic locking check fails
|
|
148
|
+
* @throws Error if multiple primary keys are found
|
|
135
149
|
*/
|
|
136
150
|
async updateById<T extends AnyMySqlTable>(
|
|
137
151
|
entity: Partial<InferInsertModel<T>>,
|
|
@@ -177,7 +191,6 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
177
191
|
|
|
178
192
|
// Execute update query
|
|
179
193
|
const queryBuilder = this.forgeOperations
|
|
180
|
-
.getDrizzleQueryBuilder()
|
|
181
194
|
.update(schema)
|
|
182
195
|
.set(updateData)
|
|
183
196
|
.where(and(...conditions));
|
|
@@ -189,7 +202,7 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
189
202
|
`Optimistic locking failed: record with primary key ${entity[primaryKeyName as keyof typeof entity]} has been modified`,
|
|
190
203
|
);
|
|
191
204
|
}
|
|
192
|
-
|
|
205
|
+
await saveTableIfInsideCacheContext(schema);
|
|
193
206
|
return result[0].affectedRows;
|
|
194
207
|
}
|
|
195
208
|
|
|
@@ -214,13 +227,10 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
214
227
|
throw new Error("WHERE conditions must be provided");
|
|
215
228
|
}
|
|
216
229
|
|
|
217
|
-
const queryBuilder = this.forgeOperations
|
|
218
|
-
.getDrizzleQueryBuilder()
|
|
219
|
-
.update(schema)
|
|
220
|
-
.set(updateData)
|
|
221
|
-
.where(where);
|
|
230
|
+
const queryBuilder = this.forgeOperations.update(schema).set(updateData).where(where);
|
|
222
231
|
|
|
223
232
|
const result = await queryBuilder;
|
|
233
|
+
await saveTableIfInsideCacheContext(schema);
|
|
224
234
|
return result[0].affectedRows;
|
|
225
235
|
}
|
|
226
236
|
|
|
@@ -421,7 +431,6 @@ export class ForgeSQLCrudOperations implements CRUDForgeSQL {
|
|
|
421
431
|
const [primaryKeyName, primaryKeyColumn] = primaryKeys[0];
|
|
422
432
|
|
|
423
433
|
const resultQuery = this.forgeOperations
|
|
424
|
-
.getDrizzleQueryBuilder()
|
|
425
434
|
.select({
|
|
426
435
|
[primaryKeyName]: primaryKeyColumn as any,
|
|
427
436
|
[versionFieldName]: versionFieldColumn as any,
|