@groundbrick/db-core 0.2.2 → 0.2.4
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.
|
@@ -9,6 +9,7 @@ export declare abstract class BaseTransaction implements DatabaseTransaction {
|
|
|
9
9
|
protected readonly logger: Logger;
|
|
10
10
|
protected readonly dbType: 'postgresql' | 'mysql';
|
|
11
11
|
protected completed: boolean;
|
|
12
|
+
private cleanedUp;
|
|
12
13
|
constructor(logger: Logger, dbType: 'postgresql' | 'mysql');
|
|
13
14
|
abstract query<T = any>(text: string, params?: any[]): Promise<QueryResult<T>>;
|
|
14
15
|
/**
|
|
@@ -54,12 +55,18 @@ export declare abstract class BaseTransaction implements DatabaseTransaction {
|
|
|
54
55
|
*/
|
|
55
56
|
protected abstract doRollback(): Promise<void>;
|
|
56
57
|
/**
|
|
57
|
-
* Database-specific cleanup (connection release, etc.)
|
|
58
|
+
* Database-specific cleanup (connection release, etc.).
|
|
59
|
+
* @param erro - The error thrown by doCommit()/doRollback() itself, if any.
|
|
60
|
+
* Drivers may use it to discard a connection whose state is unknown instead
|
|
61
|
+
* of recycling it. A transaction that failed but rolled back cleanly passes
|
|
62
|
+
* nothing here: that connection is fine.
|
|
58
63
|
*/
|
|
59
|
-
protected abstract doCleanup(): Promise<void>;
|
|
64
|
+
protected abstract doCleanup(erro?: unknown): Promise<void>;
|
|
60
65
|
/**
|
|
61
|
-
* Ensure cleanup happens in both success and error cases
|
|
66
|
+
* Ensure cleanup happens in both success and error cases.
|
|
67
|
+
* Idempotent: commit() followed by rollback() releases the connection once,
|
|
68
|
+
* and a second release on the same pool client is an error in most drivers.
|
|
62
69
|
*/
|
|
63
|
-
protected ensureCleanup(): Promise<void>;
|
|
70
|
+
protected ensureCleanup(erro?: unknown): Promise<void>;
|
|
64
71
|
}
|
|
65
72
|
//# sourceMappingURL=BaseTransaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseTransaction.d.ts","sourceRoot":"","sources":["../../src/base/BaseTransaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE3D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C;;;GAGG;AACH,8BAAsB,eAAgB,YAAW,mBAAmB;
|
|
1
|
+
{"version":3,"file":"BaseTransaction.d.ts","sourceRoot":"","sources":["../../src/base/BaseTransaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE3D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C;;;GAGG;AACH,8BAAsB,eAAgB,YAAW,mBAAmB;IAK5D,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACjC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO;IALrD,SAAS,CAAC,SAAS,UAAS;IAC5B,OAAO,CAAC,SAAS,CAAS;gBAGH,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,YAAY,GAAG,OAAO;IAGrD,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAE9E;;OAEG;IACH,eAAe,IAAI,YAAY,GAAG,OAAO;IAIzC;;OAEG;IACH,iBAAiB,IAAI,OAAO;IAI5B;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAU7B;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM;IAc7D;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM;IAgB7D;;;OAGG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IA+B7B;;;OAGG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IA6B/B;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAE5C;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAE9C;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3D;;;;OAIG;cACa,aAAa,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAgB/D"}
|
|
@@ -7,6 +7,7 @@ export class BaseTransaction {
|
|
|
7
7
|
logger;
|
|
8
8
|
dbType;
|
|
9
9
|
completed = false;
|
|
10
|
+
cleanedUp = false;
|
|
10
11
|
constructor(logger, dbType // NEW: Database type awareness
|
|
11
12
|
) {
|
|
12
13
|
this.logger = logger;
|
|
@@ -72,19 +73,29 @@ export class BaseTransaction {
|
|
|
72
73
|
if (this.completed) {
|
|
73
74
|
throw new TransactionError('Transaction has already been completed');
|
|
74
75
|
}
|
|
76
|
+
let falha;
|
|
75
77
|
try {
|
|
76
78
|
this.logger.debug('Committing transaction', { dbType: this.dbType });
|
|
77
79
|
await this.doCommit();
|
|
78
|
-
this.completed = true;
|
|
79
80
|
this.logger.debug('Transaction committed successfully', { dbType: this.dbType });
|
|
80
81
|
}
|
|
81
82
|
catch (error) {
|
|
83
|
+
falha = error;
|
|
82
84
|
this.logger.error('Failed to commit transaction', error instanceof Error ? error : new Error(String(error)), {
|
|
83
85
|
error,
|
|
84
86
|
dbType: this.dbType
|
|
85
87
|
});
|
|
86
88
|
throw new TransactionError(`Failed to commit transaction: ${error.message}`, error);
|
|
87
89
|
}
|
|
90
|
+
finally {
|
|
91
|
+
// Cleanup lives here, not inside doCommit(): a COMMIT that throws
|
|
92
|
+
// must still return the connection. The transaction is marked
|
|
93
|
+
// completed either way — a failed COMMIT ends it just as surely as
|
|
94
|
+
// a successful one, and calling rollback() on it afterwards would
|
|
95
|
+
// only reach a connection that is already back in the pool.
|
|
96
|
+
this.completed = true;
|
|
97
|
+
await this.ensureCleanup(falha);
|
|
98
|
+
}
|
|
88
99
|
}
|
|
89
100
|
/**
|
|
90
101
|
* Template method for rolling back transaction
|
|
@@ -95,19 +106,26 @@ export class BaseTransaction {
|
|
|
95
106
|
this.logger.debug('Transaction already completed, nothing to rollback', { dbType: this.dbType });
|
|
96
107
|
return;
|
|
97
108
|
}
|
|
109
|
+
let falha;
|
|
98
110
|
try {
|
|
99
111
|
this.logger.debug('Rolling back transaction', { dbType: this.dbType });
|
|
100
112
|
await this.doRollback();
|
|
101
|
-
this.completed = true;
|
|
102
113
|
this.logger.debug('Transaction rolled back successfully', { dbType: this.dbType });
|
|
103
114
|
}
|
|
104
115
|
catch (error) {
|
|
116
|
+
falha = error;
|
|
105
117
|
this.logger.error('Failed to rollback transaction', error instanceof Error ? error : new Error(String(error)), {
|
|
106
118
|
error,
|
|
107
119
|
dbType: this.dbType
|
|
108
120
|
});
|
|
109
121
|
throw new TransactionError(`Failed to rollback transaction: ${error.message}`, error);
|
|
110
122
|
}
|
|
123
|
+
finally {
|
|
124
|
+
// A ROLLBACK that throws (a dead connection, say) must still release,
|
|
125
|
+
// otherwise the failure leaks the very connection it was cleaning up.
|
|
126
|
+
this.completed = true;
|
|
127
|
+
await this.ensureCleanup(falha);
|
|
128
|
+
}
|
|
111
129
|
}
|
|
112
130
|
/**
|
|
113
131
|
* Check if transaction is completed
|
|
@@ -116,11 +134,17 @@ export class BaseTransaction {
|
|
|
116
134
|
return this.completed;
|
|
117
135
|
}
|
|
118
136
|
/**
|
|
119
|
-
* Ensure cleanup happens in both success and error cases
|
|
137
|
+
* Ensure cleanup happens in both success and error cases.
|
|
138
|
+
* Idempotent: commit() followed by rollback() releases the connection once,
|
|
139
|
+
* and a second release on the same pool client is an error in most drivers.
|
|
120
140
|
*/
|
|
121
|
-
async ensureCleanup() {
|
|
141
|
+
async ensureCleanup(erro) {
|
|
142
|
+
if (this.cleanedUp) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this.cleanedUp = true;
|
|
122
146
|
try {
|
|
123
|
-
await this.doCleanup();
|
|
147
|
+
await this.doCleanup(erro);
|
|
124
148
|
}
|
|
125
149
|
catch (error) {
|
|
126
150
|
// Don't throw here as we don't want cleanup errors to mask the original error
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseTransaction.js","sourceRoot":"","sources":["../../src/base/BaseTransaction.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AAGzE;;;GAGG;AACH,MAAM,OAAgB,eAAe;
|
|
1
|
+
{"version":3,"file":"BaseTransaction.js","sourceRoot":"","sources":["../../src/base/BaseTransaction.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AAGzE;;;GAGG;AACH,MAAM,OAAgB,eAAe;IAKV;IACA;IALb,SAAS,GAAG,KAAK,CAAC;IACpB,SAAS,GAAG,KAAK,CAAC;IAE1B,YACuB,MAAc,EACd,MAA8B,CAAC,+BAA+B;;QAD9D,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAwB;IACjD,CAAC;IAIL;;OAEG;IACH,eAAe;QACX,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,iBAAiB;QACb,OAAO,IAAI,CAAC,MAAM,KAAK,YAAY,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,GAAW;QAChB,IAAI,I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,CAAC,SAAS,CAAC;IAC1B,CAAC;IAqBD;;;;OAIG;IACO,KAAK,CAAC,aAAa,CAAC,IAAc;QACxC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QAEtB,IAAI,CAAC;YACD,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,8EAA8E;YAC9E,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE;gBACpH,KAAK;gBACL,MAAM,EAAE,IAAI,CAAC,MAAM;aACtB,CAAC,CAAC;QACP,CAAC;IACL,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groundbrick/db-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Core database interfaces and types for the TypeScript microframework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,14 +17,6 @@
|
|
|
17
17
|
"README.md",
|
|
18
18
|
"LICENSE"
|
|
19
19
|
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc",
|
|
22
|
-
"dev": "tsc --watch",
|
|
23
|
-
"clean": "rm -rf dist *.tsbuildinfo",
|
|
24
|
-
"prepublishOnly": "npm run clean && npm run build",
|
|
25
|
-
"lint": "eslint src/**/*.ts",
|
|
26
|
-
"lint:fix": "eslint src/**/*.ts --fix"
|
|
27
|
-
},
|
|
28
20
|
"keywords": [
|
|
29
21
|
"typescript",
|
|
30
22
|
"database",
|
|
@@ -42,7 +34,7 @@
|
|
|
42
34
|
"directory": "packages/db-core"
|
|
43
35
|
},
|
|
44
36
|
"dependencies": {
|
|
45
|
-
"@groundbrick/logger": "^0.4.
|
|
37
|
+
"@groundbrick/logger": "^0.4.3"
|
|
46
38
|
},
|
|
47
39
|
"devDependencies": {
|
|
48
40
|
"@types/node": "^20.10.0",
|
|
@@ -53,5 +45,12 @@
|
|
|
53
45
|
},
|
|
54
46
|
"publishConfig": {
|
|
55
47
|
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc",
|
|
51
|
+
"dev": "tsc --watch",
|
|
52
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
53
|
+
"lint": "eslint src/**/*.ts",
|
|
54
|
+
"lint:fix": "eslint src/**/*.ts --fix"
|
|
56
55
|
}
|
|
57
|
-
}
|
|
56
|
+
}
|