@holo-js/db-postgres 0.5.6 → 0.6.1

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.
@@ -1 +1 @@
1
- a4c0450c49008f4867504feccefde0816babb55423e251023debdae980000972
1
+ e8a722848313f32a5d5f1f3898d76d9604d7f39c5329554fe9612e116e4ccc92
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { PoolConfig } from 'pg';
2
- import { DriverAdapter, CompiledStatement, DriverQueryResult, DriverExecutionResult, DatabaseDriverFactory } from '@holo-js/db';
1
+ import { DriverAdapter, CompiledStatement, holoRuntimeInternals, DriverQueryResult, DriverExecutionResult, DatabaseDriverFactory } from '@holo-js/db';
3
2
  export { DriverAdapter, DriverExecutionResult, DriverQueryResult } from '@holo-js/db';
3
+ import { PoolConfig } from 'pg';
4
4
 
5
5
  type PostgresQueryResult = {
6
6
  rows: Record<string, unknown>[];
@@ -40,12 +40,14 @@ declare class PostgresAdapter<TConfig extends PoolConfig = PoolConfig> implement
40
40
  private readonly config?;
41
41
  private connected;
42
42
  private readonly transactionClients;
43
+ private readonly managedPool;
43
44
  constructor(options?: PostgresAdapterOptions<TConfig>);
44
45
  initialize(): Promise<void>;
45
46
  isDatabaseMissingError(error: unknown): boolean;
46
47
  disconnect(): Promise<void>;
47
48
  isConnected(): boolean;
48
49
  getMutationReturningPermissionQuery(tableName: string): CompiledStatement;
50
+ get [holoRuntimeInternals.databaseSessionCacheEligibleKey](): boolean;
49
51
  runWithTransactionScope<T>(callback: () => Promise<T>): Promise<T>;
50
52
  query<TRow extends Record<string, unknown> = Record<string, unknown>>(sql: string, bindings?: readonly unknown[]): Promise<DriverQueryResult<TRow>>;
51
53
  introspect<TRow extends Record<string, unknown> = Record<string, unknown>>(sql: string, bindings?: readonly unknown[]): Promise<DriverQueryResult<TRow>>;
package/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  // src/index.ts
2
2
  import { holoRuntimeInternals } from "@holo-js/db";
3
- import { Pool, types } from "pg";
4
- var { formatWithOffset, parseCalendarDate, parseWallClockTimestamp } = holoRuntimeInternals;
3
+ import { Client, Pool, types } from "pg";
4
+ import { parse as parseConnectionString } from "pg-connection-string";
5
+ var { SERVER_WALL_CLOCK_AMBIGUITY, formatWithOffset, parseCalendarDate, parseWallClockTimestamp } = holoRuntimeInternals;
5
6
  var TransactionError = class extends Error {
6
7
  };
7
8
  function finalQueryResult(result) {
@@ -21,7 +22,7 @@ function applicationTypes(base, timeZone) {
21
22
  return (value) => parseCalendarDate(value, timeZone) ?? base.getTypeParser(oid, format)(value);
22
23
  }
23
24
  if (format === "text" && oid === 1114) {
24
- return (value) => parseWallClockTimestamp(value, timeZone, "later") ?? base.getTypeParser(oid, format)(value);
25
+ return (value) => parseWallClockTimestamp(value, timeZone, SERVER_WALL_CLOCK_AMBIGUITY.postgres) ?? base.getTypeParser(oid, format)(value);
25
26
  }
26
27
  return base.getTypeParser(oid, format);
27
28
  }
@@ -30,18 +31,30 @@ function applicationTypes(base, timeZone) {
30
31
  function quoteDatabaseIdentifier(database) {
31
32
  return `"${database.replaceAll('"', '""')}"`;
32
33
  }
33
- function stripDatabaseFromConnectionString(connectionString) {
34
- const parsed = new URL(connectionString);
35
- const database = parsed.pathname.replace(/^\/+/, "");
36
- parsed.pathname = "/postgres";
37
- return {
38
- database: database ? decodeURIComponent(database) : void 0,
39
- connectionString: parsed.toString()
40
- };
34
+ function escapeStartupOptionValue(value) {
35
+ return value.replace(/[\\\s]/g, (character) => `\\${character}`);
36
+ }
37
+ function sessionStartupOptions(schema) {
38
+ const settings = [["TimeZone", holoRuntimeInternals.getApplicationTimezone()]];
39
+ if (schema) {
40
+ settings.push(["search_path", quoteDatabaseIdentifier(schema)]);
41
+ }
42
+ return settings.map(([name, value]) => `-c ${name}=${escapeStartupOptionValue(value)}`).join(" ");
43
+ }
44
+ function withSessionStartupClient(base, schema) {
45
+ class HoloClient extends base {
46
+ constructor(config) {
47
+ super(config);
48
+ if (this.connectionParameters) {
49
+ this.connectionParameters.options = [sessionStartupOptions(schema), this.connectionParameters.options].filter(Boolean).join(" ");
50
+ }
51
+ }
52
+ }
53
+ return HoloClient;
41
54
  }
42
55
  var openPoolSessions = /* @__PURE__ */ new WeakMap();
43
- function createHoloPool(config) {
44
- const pool = new Pool({ ...config });
56
+ function createHoloPool(config, schema) {
57
+ const pool = new Pool({ ...config, Client: withSessionStartupClient(config?.Client ?? Client, schema) });
45
58
  const sessions = /* @__PURE__ */ new Set();
46
59
  openPoolSessions.set(pool, sessions);
47
60
  pool.on("connect", (client) => {
@@ -65,34 +78,37 @@ function resolveBootstrapTarget(config) {
65
78
  if (!config) {
66
79
  return void 0;
67
80
  }
68
- if (typeof config.database === "string" && config.database.trim().length > 0) {
69
- const { database, ...bootstrapConfig } = config;
70
- if (database === "postgres") {
81
+ if (typeof config.connectionString === "string" && config.connectionString.trim().length > 0) {
82
+ const { connectionString, database: _database, ...bootstrapConfig } = config;
83
+ const resolved = parseConnectionString(connectionString);
84
+ if (!resolved.database || resolved.database === "postgres") {
71
85
  return void 0;
72
86
  }
73
87
  return {
74
- database,
88
+ database: resolved.database,
75
89
  config: {
76
90
  ...bootstrapConfig,
91
+ ...resolved,
77
92
  database: "postgres"
78
93
  }
79
94
  };
80
95
  }
81
- if (typeof config.connectionString === "string" && config.connectionString.trim().length > 0) {
82
- const stripped = stripDatabaseFromConnectionString(config.connectionString);
83
- if (!stripped.database || stripped.database === "postgres") {
96
+ if (typeof config.database === "string" && config.database.trim().length > 0) {
97
+ const { database, ...bootstrapConfig } = config;
98
+ if (database === "postgres") {
84
99
  return void 0;
85
100
  }
86
101
  return {
87
- database: stripped.database,
102
+ database,
88
103
  config: {
89
- ...config,
90
- connectionString: stripped.connectionString
104
+ ...bootstrapConfig,
105
+ database: "postgres"
91
106
  }
92
107
  };
93
108
  }
94
109
  return void 0;
95
110
  }
111
+ var managedAdapters = /* @__PURE__ */ new WeakSet();
96
112
  var PostgresAdapter = class {
97
113
  supportsConcurrentTransactionScopes;
98
114
  pool;
@@ -101,7 +117,9 @@ var PostgresAdapter = class {
101
117
  config;
102
118
  connected;
103
119
  transactionClients;
120
+ managedPool;
104
121
  constructor(options = {}) {
122
+ this.managedPool = !options.client && !options.pool && !options.createPool;
105
123
  this.supportsConcurrentTransactionScopes = !options.client;
106
124
  this.directClient = options.client;
107
125
  this.pool = options.pool;
@@ -154,10 +172,13 @@ var PostgresAdapter = class {
154
172
  getMutationReturningPermissionQuery(tableName) {
155
173
  return {
156
174
  sql: "SELECT has_table_privilege(current_user, $1, 'SELECT') AND NOT row_security_active($1) AS allowed",
157
- bindings: [quoteDatabaseIdentifier(tableName)],
175
+ bindings: [tableName.split(".").map(quoteDatabaseIdentifier).join(".")],
158
176
  source: "schema"
159
177
  };
160
178
  }
179
+ get [holoRuntimeInternals.databaseSessionCacheEligibleKey]() {
180
+ return (this.managedPool || managedAdapters.has(this)) && !this.transactionClients.current;
181
+ }
161
182
  runWithTransactionScope(callback) {
162
183
  return this.transactionClients.run(callback);
163
184
  }
@@ -265,19 +286,24 @@ var postgresDatabaseDriverFactory = Object.freeze({
265
286
  driver: "postgres",
266
287
  supportsConcurrentTransactionScopes: true,
267
288
  create(connection) {
268
- return createPostgresAdapter({ config: {
269
- connectionString: connection.url,
270
- host: connection.host,
271
- port: connection.port,
272
- user: connection.username,
273
- password: connection.password,
274
- database: connection.database,
275
- ssl: connection.ssl,
276
- max: connection.max,
277
- connectionTimeoutMillis: connection.connectionTimeoutMillis,
278
- idleTimeoutMillis: connection.idleTimeoutMillis,
279
- application_name: connection.application_name
280
- } });
289
+ const adapter = createPostgresAdapter({
290
+ config: {
291
+ connectionString: connection.url,
292
+ host: connection.host,
293
+ port: connection.port,
294
+ user: connection.username,
295
+ password: connection.password,
296
+ database: connection.database,
297
+ ssl: connection.ssl,
298
+ max: connection.max,
299
+ connectionTimeoutMillis: connection.connectionTimeoutMillis,
300
+ idleTimeoutMillis: connection.idleTimeoutMillis,
301
+ application_name: connection.application_name
302
+ },
303
+ createPool: (config) => createHoloPool(config, connection.schema)
304
+ });
305
+ managedAdapters.add(adapter);
306
+ return adapter;
281
307
  }
282
308
  });
283
309
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/db-postgres",
3
- "version": "0.5.6",
3
+ "version": "0.6.1",
4
4
  "description": "Holo-JS Framework - Postgres database adapter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -30,13 +30,14 @@
30
30
  "test:integration": "HOLO_POSTGRES_INTEGRATION=1 vitest --run tests/postgres.test.ts"
31
31
  },
32
32
  "peerDependencies": {
33
- "@holo-js/db": "^0.5.6"
33
+ "@holo-js/db": "^0.6.1"
34
34
  },
35
35
  "dependencies": {
36
- "pg": "^8.13.0"
36
+ "pg": "^8.13.0",
37
+ "pg-connection-string": "^2.14.1"
37
38
  },
38
39
  "devDependencies": {
39
- "@holo-js/db": "^0.5.6",
40
+ "@holo-js/db": "^0.6.1",
40
41
  "@types/pg": "^8.11.0",
41
42
  "tsup": "^9.0.0",
42
43
  "typescript": "^6.0.3",