@dascompany/database 4.0.4 → 4.0.6
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/Connection.d.ts +4 -3
- package/dist/Connection.js +21 -10
- package/package.json +1 -1
package/dist/Connection.d.ts
CHANGED
|
@@ -2,10 +2,11 @@ import pg from 'pg';
|
|
|
2
2
|
import Query from './query/Query';
|
|
3
3
|
export default class Connection {
|
|
4
4
|
private pgPool;
|
|
5
|
+
private client;
|
|
5
6
|
constructor(pgPool: pg.Pool);
|
|
6
7
|
createQuery(queryString: string): Query;
|
|
7
|
-
beginTransaction(): Promise<
|
|
8
|
-
commitTransaction(
|
|
9
|
-
rollBackTransaction(
|
|
8
|
+
beginTransaction(): Promise<void>;
|
|
9
|
+
commitTransaction(): Promise<void>;
|
|
10
|
+
rollBackTransaction(): Promise<void>;
|
|
10
11
|
end(): Promise<void>;
|
|
11
12
|
}
|
package/dist/Connection.js
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
1
|
import Query from './query/Query';
|
|
2
2
|
export default class Connection {
|
|
3
3
|
pgPool;
|
|
4
|
+
client = null;
|
|
4
5
|
constructor(pgPool) {
|
|
5
6
|
this.pgPool = pgPool;
|
|
6
7
|
}
|
|
7
8
|
createQuery(queryString) {
|
|
8
|
-
return new Query(this.pgPool, queryString);
|
|
9
|
+
return new Query(this.client ?? this.pgPool, queryString);
|
|
9
10
|
}
|
|
10
11
|
async beginTransaction() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
if (this.client) {
|
|
13
|
+
throw new Error('Transaction has already been started.');
|
|
14
|
+
}
|
|
15
|
+
this.client = await this.pgPool.connect();
|
|
16
|
+
await this.client.query('BEGIN');
|
|
14
17
|
}
|
|
15
|
-
async commitTransaction(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
async commitTransaction() {
|
|
19
|
+
if (!this.client) {
|
|
20
|
+
throw new Error('No transaction started to commit.');
|
|
21
|
+
}
|
|
22
|
+
await this.client.query('COMMIT');
|
|
23
|
+
this.client.release();
|
|
24
|
+
this.client = null;
|
|
18
25
|
}
|
|
19
|
-
async rollBackTransaction(
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
async rollBackTransaction() {
|
|
27
|
+
if (!this.client) {
|
|
28
|
+
throw new Error('No transaction started to roll back.');
|
|
29
|
+
}
|
|
30
|
+
await this.client.query('ROLLBACK');
|
|
31
|
+
this.client.release();
|
|
32
|
+
this.client = null;
|
|
22
33
|
}
|
|
23
34
|
async end() {
|
|
24
35
|
await this.pgPool.end();
|