@holo-js/db-postgres 0.3.16 → 0.3.18

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.d.ts CHANGED
@@ -30,9 +30,7 @@ declare class PostgresAdapter<TConfig extends PoolConfig = PoolConfig> implement
30
30
  private readonly createPoolInstance?;
31
31
  private readonly config?;
32
32
  private connected;
33
- private transactionClient?;
34
- private leasedTransactionClient;
35
- private readonly transactionScope;
33
+ private readonly transactionClients;
36
34
  constructor(options?: PostgresAdapterOptions<TConfig>);
37
35
  initialize(): Promise<void>;
38
36
  isDatabaseMissingError(error: unknown): boolean;
@@ -50,10 +48,6 @@ declare class PostgresAdapter<TConfig extends PoolConfig = PoolConfig> implement
50
48
  releaseSavepoint(name: string): Promise<void>;
51
49
  private getQueryable;
52
50
  ensureDatabaseExists(): Promise<void>;
53
- private leaseTransactionClient;
54
- private requireTransactionClient;
55
- private releaseTransactionClient;
56
- private releaseScopedTransaction;
57
51
  private normalizeSavepointName;
58
52
  }
59
53
  declare function createPostgresAdapter<TConfig extends PoolConfig = PoolConfig>(options?: PostgresAdapterOptions<TConfig>): PostgresAdapter<TConfig>;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
- import { AsyncLocalStorage } from "async_hooks";
3
- import { Pool } from "pg";
2
+ import { holoRuntimeInternals } from "@holo-js/db";
3
+ import { Pool, types } from "pg";
4
4
  var TransactionError = class extends Error {
5
5
  };
6
6
  function quoteDatabaseIdentifier(database) {
@@ -54,15 +54,35 @@ var PostgresAdapter = class {
54
54
  createPoolInstance;
55
55
  config;
56
56
  connected;
57
- transactionClient;
58
- leasedTransactionClient = false;
59
- transactionScope = new AsyncLocalStorage();
57
+ transactionClients;
60
58
  constructor(options = {}) {
61
59
  this.directClient = options.client;
62
60
  this.pool = options.pool;
63
- this.createPoolInstance = options.createPool ?? (options.client || options.pool ? void 0 : (config) => new Pool(config));
61
+ this.createPoolInstance = options.createPool ?? (options.client || options.pool ? void 0 : (config) => new Pool({
62
+ ...config,
63
+ types: config?.types ?? {
64
+ getTypeParser(oid, format = "text") {
65
+ if (oid === 1114 && format === "text") return (value) => /* @__PURE__ */ new Date(`${value}Z`);
66
+ return types.getTypeParser(oid, format);
67
+ }
68
+ }
69
+ }));
64
70
  this.config = options.config ?? (options.connectionString ? { connectionString: options.connectionString } : void 0);
65
71
  this.connected = !!(options.client || options.pool);
72
+ this.transactionClients = new holoRuntimeInternals.PooledTransactionClients(
73
+ "Postgres",
74
+ this.directClient,
75
+ async () => {
76
+ await this.initialize();
77
+ if (this.directClient) {
78
+ return this.directClient;
79
+ }
80
+ if (!this.pool) {
81
+ throw new TransactionError("Postgres adapter is not initialized with a pool or client.");
82
+ }
83
+ return this.pool.connect();
84
+ }
85
+ );
66
86
  }
67
87
  async initialize() {
68
88
  if (this.connected) {
@@ -80,11 +100,7 @@ var PostgresAdapter = class {
80
100
  if (!this.connected) {
81
101
  return;
82
102
  }
83
- if (this.transactionClient && this.leasedTransactionClient) {
84
- this.transactionClient.release?.();
85
- this.transactionClient = void 0;
86
- this.leasedTransactionClient = false;
87
- }
103
+ this.transactionClients.disconnect();
88
104
  if (this.pool) {
89
105
  await this.pool.end();
90
106
  this.pool = void 0;
@@ -96,30 +112,8 @@ var PostgresAdapter = class {
96
112
  isConnected() {
97
113
  return this.connected;
98
114
  }
99
- async runWithTransactionScope(callback) {
100
- const active = this.transactionScope.getStore();
101
- if (active) {
102
- return callback();
103
- }
104
- await this.initialize();
105
- if (this.directClient) {
106
- return this.transactionScope.run({
107
- client: this.directClient
108
- }, callback);
109
- }
110
- if (!this.pool) {
111
- throw new TransactionError("Postgres adapter is not initialized with a pool or client.");
112
- }
113
- const state = {
114
- client: await this.pool.connect()
115
- };
116
- return this.transactionScope.run(state, async () => {
117
- try {
118
- return await callback();
119
- } finally {
120
- this.releaseScopedTransaction(state);
121
- }
122
- });
115
+ runWithTransactionScope(callback) {
116
+ return this.transactionClients.run(callback);
123
117
  }
124
118
  async query(sql, bindings = []) {
125
119
  const client = await this.getQueryable();
@@ -143,35 +137,32 @@ var PostgresAdapter = class {
143
137
  };
144
138
  }
145
139
  async beginTransaction() {
146
- const client = await this.leaseTransactionClient();
140
+ const client = await this.transactionClients.lease();
147
141
  await client.query("BEGIN");
148
142
  }
149
143
  async commit() {
150
- const client = this.requireTransactionClient();
144
+ const client = this.transactionClients.require();
151
145
  await client.query("COMMIT");
152
- this.releaseTransactionClient();
146
+ this.transactionClients.release();
153
147
  }
154
148
  async rollback() {
155
- const client = this.requireTransactionClient();
149
+ const client = this.transactionClients.require();
156
150
  await client.query("ROLLBACK");
157
- this.releaseTransactionClient();
151
+ this.transactionClients.release();
158
152
  }
159
153
  async createSavepoint(name) {
160
- await this.requireTransactionClient().query(`SAVEPOINT ${this.normalizeSavepointName(name)}`);
154
+ await this.transactionClients.require().query(`SAVEPOINT ${this.normalizeSavepointName(name)}`);
161
155
  }
162
156
  async rollbackToSavepoint(name) {
163
- await this.requireTransactionClient().query(`ROLLBACK TO SAVEPOINT ${this.normalizeSavepointName(name)}`);
157
+ await this.transactionClients.require().query(`ROLLBACK TO SAVEPOINT ${this.normalizeSavepointName(name)}`);
164
158
  }
165
159
  async releaseSavepoint(name) {
166
- await this.requireTransactionClient().query(`RELEASE SAVEPOINT ${this.normalizeSavepointName(name)}`);
160
+ await this.transactionClients.require().query(`RELEASE SAVEPOINT ${this.normalizeSavepointName(name)}`);
167
161
  }
168
162
  async getQueryable() {
169
- const scoped = this.transactionScope.getStore();
170
- if (scoped) {
171
- return scoped.client;
172
- }
173
- if (this.transactionClient) {
174
- return this.transactionClient;
163
+ const active = this.transactionClients.current;
164
+ if (active) {
165
+ return active;
175
166
  }
176
167
  await this.initialize();
177
168
  if (this.directClient) {
@@ -206,47 +197,6 @@ var PostgresAdapter = class {
206
197
  await bootstrapPool.end();
207
198
  }
208
199
  }
209
- async leaseTransactionClient() {
210
- const scoped = this.transactionScope.getStore();
211
- if (scoped) {
212
- return scoped.client;
213
- }
214
- if (this.transactionClient) {
215
- return this.transactionClient;
216
- }
217
- await this.initialize();
218
- if (this.directClient) {
219
- this.transactionClient = this.directClient;
220
- this.leasedTransactionClient = false;
221
- return this.transactionClient;
222
- }
223
- if (!this.pool) {
224
- throw new TransactionError("Postgres adapter is not initialized with a pool or client.");
225
- }
226
- this.transactionClient = await this.pool.connect();
227
- this.leasedTransactionClient = true;
228
- return this.transactionClient;
229
- }
230
- requireTransactionClient() {
231
- const scoped = this.transactionScope.getStore();
232
- if (scoped) {
233
- return scoped.client;
234
- }
235
- if (!this.transactionClient) {
236
- throw new TransactionError("No active Postgres transaction client is available.");
237
- }
238
- return this.transactionClient;
239
- }
240
- releaseTransactionClient() {
241
- if (this.transactionClient && this.leasedTransactionClient) {
242
- this.transactionClient.release?.();
243
- }
244
- this.transactionClient = void 0;
245
- this.leasedTransactionClient = false;
246
- }
247
- releaseScopedTransaction(state) {
248
- state.client.release?.();
249
- }
250
200
  normalizeSavepointName(name) {
251
201
  if (!/^[A-Z_]\w*$/i.test(name)) {
252
202
  throw new TransactionError(`Invalid savepoint name "${name}".`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/db-postgres",
3
- "version": "0.3.16",
3
+ "version": "0.3.18",
4
4
  "description": "Holo-JS Framework - Postgres database adapter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -24,13 +24,13 @@
24
24
  "test:integration": "HOLO_POSTGRES_INTEGRATION=1 vitest --run tests/postgres.test.ts"
25
25
  },
26
26
  "peerDependencies": {
27
- "@holo-js/db": "^0.3.16"
27
+ "@holo-js/db": "^0.3.18"
28
28
  },
29
29
  "dependencies": {
30
30
  "pg": "^8.13.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@holo-js/db": "^0.3.16",
33
+ "@holo-js/db": "^0.3.18",
34
34
  "@types/pg": "^8.11.0",
35
35
  "tsup": "^8.3.5",
36
36
  "typescript": "^5.7.2",