@dyrected/db-mysql 2.5.30 → 2.5.33
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.cjs +27 -6
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +27 -6
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -53,6 +53,7 @@ var MysqlAdapter = class {
|
|
|
53
53
|
pool;
|
|
54
54
|
config;
|
|
55
55
|
initPromise = null;
|
|
56
|
+
inTransaction = false;
|
|
56
57
|
constructor(config) {
|
|
57
58
|
this.config = config;
|
|
58
59
|
this.ensureInitialized().catch((err) => {
|
|
@@ -184,7 +185,7 @@ FIX INSTRUCTIONS:
|
|
|
184
185
|
}
|
|
185
186
|
}
|
|
186
187
|
async find(args) {
|
|
187
|
-
await this.ensureTable(args.collection);
|
|
188
|
+
if (!this.inTransaction) await this.ensureTable(args.collection);
|
|
188
189
|
const tableName = this.getTableName(args.collection);
|
|
189
190
|
const limit = args.limit ?? 10;
|
|
190
191
|
const page = args.page ?? 1;
|
|
@@ -237,9 +238,10 @@ FIX INSTRUCTIONS:
|
|
|
237
238
|
};
|
|
238
239
|
}
|
|
239
240
|
async findOne(params) {
|
|
240
|
-
await this.ensureTable(params.collection);
|
|
241
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
241
242
|
const tableName = this.getTableName(params.collection);
|
|
242
|
-
const
|
|
243
|
+
const lock = this.inTransaction ? " FOR UPDATE" : "";
|
|
244
|
+
const [rows] = await this.query(`SELECT * FROM \`${tableName}\` WHERE id = ?${lock}`, [params.id]);
|
|
243
245
|
const row = rows[0];
|
|
244
246
|
if (!row) return null;
|
|
245
247
|
return {
|
|
@@ -250,7 +252,7 @@ FIX INSTRUCTIONS:
|
|
|
250
252
|
};
|
|
251
253
|
}
|
|
252
254
|
async create(params) {
|
|
253
|
-
await this.ensureTable(params.collection);
|
|
255
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
254
256
|
const tableName = this.getTableName(params.collection);
|
|
255
257
|
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
256
258
|
const existingCols = cols.map((c) => c.Field);
|
|
@@ -274,7 +276,7 @@ FIX INSTRUCTIONS:
|
|
|
274
276
|
return { id, ...data, createdAt: now, updatedAt: now };
|
|
275
277
|
}
|
|
276
278
|
async update(params) {
|
|
277
|
-
await this.ensureTable(params.collection);
|
|
279
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
278
280
|
const tableName = this.getTableName(params.collection);
|
|
279
281
|
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
280
282
|
const existingCols = cols.map((c) => c.Field);
|
|
@@ -302,7 +304,7 @@ FIX INSTRUCTIONS:
|
|
|
302
304
|
}
|
|
303
305
|
}
|
|
304
306
|
async delete(params) {
|
|
305
|
-
await this.ensureTable(params.collection);
|
|
307
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
306
308
|
const tableName = this.getTableName(params.collection);
|
|
307
309
|
await this.query(`DELETE FROM \`${tableName}\` WHERE id = ?`, [params.id]);
|
|
308
310
|
}
|
|
@@ -321,6 +323,25 @@ FIX INSTRUCTIONS:
|
|
|
321
323
|
);
|
|
322
324
|
return params.data;
|
|
323
325
|
}
|
|
326
|
+
async transaction(callback) {
|
|
327
|
+
await this.ensureInitialized();
|
|
328
|
+
const connection = await this.pool.getConnection();
|
|
329
|
+
try {
|
|
330
|
+
await connection.beginTransaction();
|
|
331
|
+
const scoped = Object.create(this);
|
|
332
|
+
scoped.pool = connection;
|
|
333
|
+
scoped.initPromise = Promise.resolve();
|
|
334
|
+
scoped.inTransaction = true;
|
|
335
|
+
const result = await callback(scoped);
|
|
336
|
+
await connection.commit();
|
|
337
|
+
return result;
|
|
338
|
+
} catch (error) {
|
|
339
|
+
await connection.rollback();
|
|
340
|
+
throw error;
|
|
341
|
+
} finally {
|
|
342
|
+
connection.release();
|
|
343
|
+
}
|
|
344
|
+
}
|
|
324
345
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
325
346
|
async close() {
|
|
326
347
|
if (this.pool) {
|
package/dist/index.d.cts
CHANGED
|
@@ -14,6 +14,7 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
14
14
|
private pool;
|
|
15
15
|
private config;
|
|
16
16
|
private initPromise;
|
|
17
|
+
private inTransaction;
|
|
17
18
|
constructor(config: MysqlAdapterConfig);
|
|
18
19
|
private handleConnectionError;
|
|
19
20
|
private ensureInitialized;
|
|
@@ -54,6 +55,7 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
54
55
|
slug: string;
|
|
55
56
|
data: any;
|
|
56
57
|
}): Promise<any>;
|
|
58
|
+
transaction<T>(callback: (db: DatabaseAdapter) => Promise<T>): Promise<T>;
|
|
57
59
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
58
60
|
close(): Promise<void>;
|
|
59
61
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
14
14
|
private pool;
|
|
15
15
|
private config;
|
|
16
16
|
private initPromise;
|
|
17
|
+
private inTransaction;
|
|
17
18
|
constructor(config: MysqlAdapterConfig);
|
|
18
19
|
private handleConnectionError;
|
|
19
20
|
private ensureInitialized;
|
|
@@ -54,6 +55,7 @@ declare class MysqlAdapter implements DatabaseAdapter {
|
|
|
54
55
|
slug: string;
|
|
55
56
|
data: any;
|
|
56
57
|
}): Promise<any>;
|
|
58
|
+
transaction<T>(callback: (db: DatabaseAdapter) => Promise<T>): Promise<T>;
|
|
57
59
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
58
60
|
close(): Promise<void>;
|
|
59
61
|
}
|
package/dist/index.js
CHANGED
|
@@ -18,6 +18,7 @@ var MysqlAdapter = class {
|
|
|
18
18
|
pool;
|
|
19
19
|
config;
|
|
20
20
|
initPromise = null;
|
|
21
|
+
inTransaction = false;
|
|
21
22
|
constructor(config) {
|
|
22
23
|
this.config = config;
|
|
23
24
|
this.ensureInitialized().catch((err) => {
|
|
@@ -149,7 +150,7 @@ FIX INSTRUCTIONS:
|
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
async find(args) {
|
|
152
|
-
await this.ensureTable(args.collection);
|
|
153
|
+
if (!this.inTransaction) await this.ensureTable(args.collection);
|
|
153
154
|
const tableName = this.getTableName(args.collection);
|
|
154
155
|
const limit = args.limit ?? 10;
|
|
155
156
|
const page = args.page ?? 1;
|
|
@@ -202,9 +203,10 @@ FIX INSTRUCTIONS:
|
|
|
202
203
|
};
|
|
203
204
|
}
|
|
204
205
|
async findOne(params) {
|
|
205
|
-
await this.ensureTable(params.collection);
|
|
206
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
206
207
|
const tableName = this.getTableName(params.collection);
|
|
207
|
-
const
|
|
208
|
+
const lock = this.inTransaction ? " FOR UPDATE" : "";
|
|
209
|
+
const [rows] = await this.query(`SELECT * FROM \`${tableName}\` WHERE id = ?${lock}`, [params.id]);
|
|
208
210
|
const row = rows[0];
|
|
209
211
|
if (!row) return null;
|
|
210
212
|
return {
|
|
@@ -215,7 +217,7 @@ FIX INSTRUCTIONS:
|
|
|
215
217
|
};
|
|
216
218
|
}
|
|
217
219
|
async create(params) {
|
|
218
|
-
await this.ensureTable(params.collection);
|
|
220
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
219
221
|
const tableName = this.getTableName(params.collection);
|
|
220
222
|
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
221
223
|
const existingCols = cols.map((c) => c.Field);
|
|
@@ -239,7 +241,7 @@ FIX INSTRUCTIONS:
|
|
|
239
241
|
return { id, ...data, createdAt: now, updatedAt: now };
|
|
240
242
|
}
|
|
241
243
|
async update(params) {
|
|
242
|
-
await this.ensureTable(params.collection);
|
|
244
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
243
245
|
const tableName = this.getTableName(params.collection);
|
|
244
246
|
const [cols] = await this.query(`SHOW COLUMNS FROM \`${tableName}\``);
|
|
245
247
|
const existingCols = cols.map((c) => c.Field);
|
|
@@ -267,7 +269,7 @@ FIX INSTRUCTIONS:
|
|
|
267
269
|
}
|
|
268
270
|
}
|
|
269
271
|
async delete(params) {
|
|
270
|
-
await this.ensureTable(params.collection);
|
|
272
|
+
if (!this.inTransaction) await this.ensureTable(params.collection);
|
|
271
273
|
const tableName = this.getTableName(params.collection);
|
|
272
274
|
await this.query(`DELETE FROM \`${tableName}\` WHERE id = ?`, [params.id]);
|
|
273
275
|
}
|
|
@@ -286,6 +288,25 @@ FIX INSTRUCTIONS:
|
|
|
286
288
|
);
|
|
287
289
|
return params.data;
|
|
288
290
|
}
|
|
291
|
+
async transaction(callback) {
|
|
292
|
+
await this.ensureInitialized();
|
|
293
|
+
const connection = await this.pool.getConnection();
|
|
294
|
+
try {
|
|
295
|
+
await connection.beginTransaction();
|
|
296
|
+
const scoped = Object.create(this);
|
|
297
|
+
scoped.pool = connection;
|
|
298
|
+
scoped.initPromise = Promise.resolve();
|
|
299
|
+
scoped.inTransaction = true;
|
|
300
|
+
const result = await callback(scoped);
|
|
301
|
+
await connection.commit();
|
|
302
|
+
return result;
|
|
303
|
+
} catch (error) {
|
|
304
|
+
await connection.rollback();
|
|
305
|
+
throw error;
|
|
306
|
+
} finally {
|
|
307
|
+
connection.release();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
289
310
|
/** Gracefully close the connection pool. Call on process exit. */
|
|
290
311
|
async close() {
|
|
291
312
|
if (this.pool) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dyrected/db-mysql",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.33",
|
|
4
4
|
"description": "MySQL adapter for Dyrected CMS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"mysql2": "^3.22.3",
|
|
21
|
-
"@dyrected/core": "^2.5.
|
|
21
|
+
"@dyrected/core": "^2.5.33"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"tsup": "^8.0.0",
|