@holo-js/db-postgres 0.1.5 → 0.1.7

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
@@ -47,6 +47,7 @@ declare class PostgresAdapter implements DriverAdapter {
47
47
  rollbackToSavepoint(name: string): Promise<void>;
48
48
  releaseSavepoint(name: string): Promise<void>;
49
49
  private getQueryable;
50
+ private ensureDatabaseExists;
50
51
  private leaseTransactionClient;
51
52
  private requireTransactionClient;
52
53
  private releaseTransactionClient;
package/dist/index.mjs CHANGED
@@ -3,6 +3,50 @@ import { AsyncLocalStorage } from "async_hooks";
3
3
  import { Pool } from "pg";
4
4
  var TransactionError = class extends Error {
5
5
  };
6
+ function quoteDatabaseIdentifier(database) {
7
+ return `"${database.replaceAll('"', '""')}"`;
8
+ }
9
+ function stripDatabaseFromConnectionString(connectionString) {
10
+ const parsed = new URL(connectionString);
11
+ const database = parsed.pathname.replace(/^\/+/, "");
12
+ parsed.pathname = "/postgres";
13
+ return {
14
+ database: database ? decodeURIComponent(database) : void 0,
15
+ connectionString: parsed.toString()
16
+ };
17
+ }
18
+ function resolveBootstrapTarget(config) {
19
+ if (!config) {
20
+ return void 0;
21
+ }
22
+ if (typeof config.database === "string" && config.database.trim().length > 0) {
23
+ const { database, ...bootstrapConfig } = config;
24
+ if (database === "postgres") {
25
+ return void 0;
26
+ }
27
+ return {
28
+ database,
29
+ config: {
30
+ ...bootstrapConfig,
31
+ database: "postgres"
32
+ }
33
+ };
34
+ }
35
+ if (typeof config.connectionString === "string" && config.connectionString.trim().length > 0) {
36
+ const stripped = stripDatabaseFromConnectionString(config.connectionString);
37
+ if (!stripped.database || stripped.database === "postgres") {
38
+ return void 0;
39
+ }
40
+ return {
41
+ database: stripped.database,
42
+ config: {
43
+ ...config,
44
+ connectionString: stripped.connectionString
45
+ }
46
+ };
47
+ }
48
+ return void 0;
49
+ }
6
50
  var PostgresAdapter = class {
7
51
  pool;
8
52
  directClient;
@@ -24,6 +68,7 @@ var PostgresAdapter = class {
24
68
  return;
25
69
  }
26
70
  if (this.createPoolInstance) {
71
+ await this.ensureDatabaseExists();
27
72
  this.pool = this.createPoolInstance(this.config);
28
73
  }
29
74
  this.connected = true;
@@ -138,6 +183,27 @@ var PostgresAdapter = class {
138
183
  }
139
184
  return this.pool;
140
185
  }
186
+ async ensureDatabaseExists() {
187
+ if (!this.createPoolInstance) {
188
+ return;
189
+ }
190
+ const target = resolveBootstrapTarget(this.config);
191
+ if (!target) {
192
+ return;
193
+ }
194
+ const bootstrapPool = this.createPoolInstance(target.config);
195
+ try {
196
+ const existing = await bootstrapPool.query("select 1 from pg_database where datname = $1", [target.database]);
197
+ if (existing.rows.length === 0) {
198
+ await bootstrapPool.query(`create database ${quoteDatabaseIdentifier(target.database)}`);
199
+ }
200
+ } catch (error) {
201
+ const message = error instanceof Error ? error.message : String(error);
202
+ throw new Error(`Unable to ensure Postgres database "${target.database}" exists: ${message}`, { cause: error });
203
+ } finally {
204
+ await bootstrapPool.end();
205
+ }
206
+ }
141
207
  async leaseTransactionClient() {
142
208
  const scoped = this.transactionScope.getStore();
143
209
  if (scoped) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/db-postgres",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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.1.5"
27
+ "@holo-js/db": "^0.1.7"
28
28
  },
29
29
  "dependencies": {
30
30
  "pg": "^8.13.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@holo-js/db": "^0.1.5",
33
+ "@holo-js/db": "^0.1.7",
34
34
  "@types/pg": "^8.11.0",
35
35
  "tsup": "^8.3.5",
36
36
  "typescript": "^5.7.2",