rado 1.3.0-preview.4 → 1.3.0-preview.5
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/driver/pg.js +15 -4
- package/package.json +1 -1
package/dist/driver/pg.js
CHANGED
|
@@ -3,6 +3,17 @@ import { AsyncDatabase } from "../core/Database.js";
|
|
|
3
3
|
import { postgresDialect } from "../postgres/dialect.js";
|
|
4
4
|
import { postgresDiff } from "../postgres/diff.js";
|
|
5
5
|
import { setTransaction } from "../postgres/transactions.js";
|
|
6
|
+
function isPool(client) {
|
|
7
|
+
return "connect" in client && "totalCount" in client && "idleCount" in client && "waitingCount" in client;
|
|
8
|
+
}
|
|
9
|
+
async function acquireClient(client, depth) {
|
|
10
|
+
if (depth > 0 || !isPool(client)) return { client };
|
|
11
|
+
const acquired = await client.connect();
|
|
12
|
+
return {
|
|
13
|
+
client: acquired,
|
|
14
|
+
release: () => acquired.release()
|
|
15
|
+
};
|
|
16
|
+
}
|
|
6
17
|
var PreparedStatement = class {
|
|
7
18
|
constructor(client, sql, name) {
|
|
8
19
|
this.client = client;
|
|
@@ -59,8 +70,9 @@ var PgDriver = class _PgDriver {
|
|
|
59
70
|
return new PreparedStatement(this.client, sql, options?.name);
|
|
60
71
|
}
|
|
61
72
|
async close() {
|
|
62
|
-
if (
|
|
73
|
+
if (this.depth > 0) throw new Error("Cannot close a transaction");
|
|
63
74
|
if ("release" in this.client) return this.client.release();
|
|
75
|
+
if ("end" in this.client) return this.client.end();
|
|
64
76
|
}
|
|
65
77
|
async batch(queries) {
|
|
66
78
|
return this.transaction(async (tx) => {
|
|
@@ -71,8 +83,7 @@ var PgDriver = class _PgDriver {
|
|
|
71
83
|
}, {});
|
|
72
84
|
}
|
|
73
85
|
async transaction(run, options) {
|
|
74
|
-
const
|
|
75
|
-
const client = acquiredClient ?? this.client;
|
|
86
|
+
const { client, release } = await acquireClient(this.client, this.depth);
|
|
76
87
|
try {
|
|
77
88
|
await client.query(this.depth > 0 ? `savepoint d${this.depth}` : "begin");
|
|
78
89
|
if (this.depth === 0) await client.query(setTransaction(options));
|
|
@@ -87,7 +98,7 @@ var PgDriver = class _PgDriver {
|
|
|
87
98
|
);
|
|
88
99
|
throw error;
|
|
89
100
|
} finally {
|
|
90
|
-
|
|
101
|
+
release?.();
|
|
91
102
|
}
|
|
92
103
|
}
|
|
93
104
|
};
|