@atscript/db-postgres 0.1.153 → 0.1.154

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.cjs CHANGED
@@ -2361,6 +2361,10 @@ function createCustomTypes(pgTypes) {
2361
2361
  return pgTypes.getTypeParser(oid, format);
2362
2362
  } };
2363
2363
  }
2364
+ /** The warning logged for a pool client whose connection broke (`57P01`, `ECONNRESET`, …). */
2365
+ function lostConnectionMessage(err, where) {
2366
+ return `[atscript/db-postgres] ${where} pool connection lost (${err.code ?? err.name}): ${err.message}`;
2367
+ }
2364
2368
  /**
2365
2369
  * {@link TPgDriver} implementation backed by `pg` (node-postgres).
2366
2370
  *
@@ -2387,6 +2391,12 @@ function createCustomTypes(pgTypes) {
2387
2391
  * const driver = new PgDriver(pool)
2388
2392
  * ```
2389
2393
  *
2394
+ * A broken pool connection (a terminated backend, failover, network reset) is
2395
+ * logged through `options.logger` instead of crashing the process (since
2396
+ * 0.1.154): idle clients of a pool the driver creates, and clients checked out
2397
+ * for a transaction. A pre-created `pg.Pool` is the caller's — attach your own
2398
+ * `pool.on('error', …)` listener, or an idle client's error crashes the process.
2399
+ *
2390
2400
  * Requires `pg` to be installed:
2391
2401
  * ```bash
2392
2402
  * pnpm add pg
@@ -2395,7 +2405,9 @@ function createCustomTypes(pgTypes) {
2395
2405
  var PgDriver = class {
2396
2406
  pool;
2397
2407
  poolInit;
2398
- constructor(poolOrConfig) {
2408
+ logger;
2409
+ constructor(poolOrConfig, options = {}) {
2410
+ this.logger = options.logger ?? console;
2399
2411
  if (typeof poolOrConfig === "object" && typeof poolOrConfig.query === "function") this.pool = poolOrConfig;
2400
2412
  else this.poolInit = import("pg").then((pg) => {
2401
2413
  const Pool = pg.default?.Pool ?? pg.Pool;
@@ -2409,6 +2421,7 @@ var PgDriver = class {
2409
2421
  ...poolOrConfig,
2410
2422
  types: customTypes
2411
2423
  });
2424
+ this.pool.on("error", (err) => this.logger.warn(lostConnectionMessage(err, "idle")));
2412
2425
  return this.pool;
2413
2426
  });
2414
2427
  }
@@ -2433,6 +2446,13 @@ var PgDriver = class {
2433
2446
  }
2434
2447
  async getConnection() {
2435
2448
  const client = await (await this.getPool()).connect();
2449
+ let lost;
2450
+ const onError = (err) => {
2451
+ if (lost) return;
2452
+ lost = err;
2453
+ this.logger.warn(lostConnectionMessage(err, "checked-out"));
2454
+ };
2455
+ client.on("error", onError);
2436
2456
  return {
2437
2457
  async run(sql, params) {
2438
2458
  const result = await client.query(sql, sanitizeParams(params));
@@ -2451,7 +2471,8 @@ var PgDriver = class {
2451
2471
  await client.query(sql);
2452
2472
  },
2453
2473
  release() {
2454
- client.release();
2474
+ client.removeListener("error", onError);
2475
+ client.release(lost);
2455
2476
  }
2456
2477
  };
2457
2478
  }
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TColumnDiff, TDbDefaultFn, TDbDeleteResult, TDbFieldMeta, TDbInsertIgnoreOptions, TDbInsertIgnoreSlot, TDbInsertManyResult, TDbInsertResult, TDbObjectKind, TDbUpdateOptions, TDbUpdateResult, TEnsureTableOptions, TExistingColumn, TFieldOps, TPrimaryKeyChange, TReferencingForeignKey, TSearchIndexInfo, TSyncColumnResult, TValueFormatterPair, TViewCapability } from "@atscript/db";
1
+ import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TColumnDiff, TDbDefaultFn, TDbDeleteResult, TDbFieldMeta, TDbInsertIgnoreOptions, TDbInsertIgnoreSlot, TDbInsertManyResult, TDbInsertResult, TDbObjectKind, TDbUpdateOptions, TDbUpdateResult, TEnsureTableOptions, TExistingColumn, TFieldOps, TGenericLogger, TPrimaryKeyChange, TReferencingForeignKey, TSearchIndexInfo, TSyncColumnResult, TValueFormatterPair, TViewCapability } from "@atscript/db";
2
2
  import { TMetadataMap } from "@atscript/typescript/utils";
3
3
  import { TFilterVisitorOptions, TSqlFragment } from "@atscript/db-sql-tools";
4
4
 
@@ -442,6 +442,14 @@ declare class PostgresAdapter extends BaseDbAdapter {
442
442
  }
443
443
  //#endregion
444
444
  //#region src/pg-driver.d.ts
445
+ /** Options of {@link PgDriver}. */
446
+ interface TPgDriverOptions {
447
+ /**
448
+ * Receives a warning when a pool connection breaks — a terminated backend,
449
+ * failover or network reset (default `console`). See {@link PgDriver}.
450
+ */
451
+ logger?: Pick<TGenericLogger, "warn">;
452
+ }
445
453
  /**
446
454
  * {@link TPgDriver} implementation backed by `pg` (node-postgres).
447
455
  *
@@ -468,6 +476,12 @@ declare class PostgresAdapter extends BaseDbAdapter {
468
476
  * const driver = new PgDriver(pool)
469
477
  * ```
470
478
  *
479
+ * A broken pool connection (a terminated backend, failover, network reset) is
480
+ * logged through `options.logger` instead of crashing the process (since
481
+ * 0.1.154): idle clients of a pool the driver creates, and clients checked out
482
+ * for a transaction. A pre-created `pg.Pool` is the caller's — attach your own
483
+ * `pool.on('error', …)` listener, or an idle client's error crashes the process.
484
+ *
471
485
  * Requires `pg` to be installed:
472
486
  * ```bash
473
487
  * pnpm add pg
@@ -476,7 +490,8 @@ declare class PostgresAdapter extends BaseDbAdapter {
476
490
  declare class PgDriver implements TPgDriver {
477
491
  private pool;
478
492
  private poolInit;
479
- constructor(poolOrConfig: string | import("pg").Pool | import("pg").PoolConfig);
493
+ private readonly logger;
494
+ constructor(poolOrConfig: string | import("pg").Pool | import("pg").PoolConfig, options?: TPgDriverOptions);
480
495
  private getPool;
481
496
  run(sql: string, params?: unknown[]): Promise<TPgRunResult>;
482
497
  all<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
@@ -517,4 +532,4 @@ declare function buildWhere(filter: FilterExpr, opts?: TFilterVisitorOptions): T
517
532
  */
518
533
  declare function createAdapter(uri: string, options?: Record<string, unknown>): DbSpace;
519
534
  //#endregion
520
- export { PgDriver, PostgresAdapter, type TPgConnection, type TPgDriver, type TPgRunResult, type TSqlFragment, buildWhere, createAdapter };
535
+ export { PgDriver, PostgresAdapter, type TPgConnection, type TPgDriver, type TPgDriverOptions, type TPgRunResult, type TSqlFragment, buildWhere, createAdapter };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TColumnDiff, TDbDefaultFn, TDbDeleteResult, TDbFieldMeta, TDbInsertIgnoreOptions, TDbInsertIgnoreSlot, TDbInsertManyResult, TDbInsertResult, TDbObjectKind, TDbUpdateOptions, TDbUpdateResult, TEnsureTableOptions, TExistingColumn, TFieldOps, TPrimaryKeyChange, TReferencingForeignKey, TSearchIndexInfo, TSyncColumnResult, TValueFormatterPair, TViewCapability } from "@atscript/db";
1
+ import { AggregateFn, BaseDbAdapter, BucketUnit, DbQuery, DbSpace, FilterExpr, TColumnDiff, TDbDefaultFn, TDbDeleteResult, TDbFieldMeta, TDbInsertIgnoreOptions, TDbInsertIgnoreSlot, TDbInsertManyResult, TDbInsertResult, TDbObjectKind, TDbUpdateOptions, TDbUpdateResult, TEnsureTableOptions, TExistingColumn, TFieldOps, TGenericLogger, TPrimaryKeyChange, TReferencingForeignKey, TSearchIndexInfo, TSyncColumnResult, TValueFormatterPair, TViewCapability } from "@atscript/db";
2
2
  import { TFilterVisitorOptions, TSqlFragment } from "@atscript/db-sql-tools";
3
3
  import { TMetadataMap } from "@atscript/typescript/utils";
4
4
 
@@ -442,6 +442,14 @@ declare class PostgresAdapter extends BaseDbAdapter {
442
442
  }
443
443
  //#endregion
444
444
  //#region src/pg-driver.d.ts
445
+ /** Options of {@link PgDriver}. */
446
+ interface TPgDriverOptions {
447
+ /**
448
+ * Receives a warning when a pool connection breaks — a terminated backend,
449
+ * failover or network reset (default `console`). See {@link PgDriver}.
450
+ */
451
+ logger?: Pick<TGenericLogger, "warn">;
452
+ }
445
453
  /**
446
454
  * {@link TPgDriver} implementation backed by `pg` (node-postgres).
447
455
  *
@@ -468,6 +476,12 @@ declare class PostgresAdapter extends BaseDbAdapter {
468
476
  * const driver = new PgDriver(pool)
469
477
  * ```
470
478
  *
479
+ * A broken pool connection (a terminated backend, failover, network reset) is
480
+ * logged through `options.logger` instead of crashing the process (since
481
+ * 0.1.154): idle clients of a pool the driver creates, and clients checked out
482
+ * for a transaction. A pre-created `pg.Pool` is the caller's — attach your own
483
+ * `pool.on('error', …)` listener, or an idle client's error crashes the process.
484
+ *
471
485
  * Requires `pg` to be installed:
472
486
  * ```bash
473
487
  * pnpm add pg
@@ -476,7 +490,8 @@ declare class PostgresAdapter extends BaseDbAdapter {
476
490
  declare class PgDriver implements TPgDriver {
477
491
  private pool;
478
492
  private poolInit;
479
- constructor(poolOrConfig: string | import("pg").Pool | import("pg").PoolConfig);
493
+ private readonly logger;
494
+ constructor(poolOrConfig: string | import("pg").Pool | import("pg").PoolConfig, options?: TPgDriverOptions);
480
495
  private getPool;
481
496
  run(sql: string, params?: unknown[]): Promise<TPgRunResult>;
482
497
  all<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>;
@@ -517,4 +532,4 @@ declare function buildWhere(filter: FilterExpr, opts?: TFilterVisitorOptions): T
517
532
  */
518
533
  declare function createAdapter(uri: string, options?: Record<string, unknown>): DbSpace;
519
534
  //#endregion
520
- export { PgDriver, PostgresAdapter, type TPgConnection, type TPgDriver, type TPgRunResult, type TSqlFragment, buildWhere, createAdapter };
535
+ export { PgDriver, PostgresAdapter, type TPgConnection, type TPgDriver, type TPgDriverOptions, type TPgRunResult, type TSqlFragment, buildWhere, createAdapter };
package/dist/index.mjs CHANGED
@@ -2360,6 +2360,10 @@ function createCustomTypes(pgTypes) {
2360
2360
  return pgTypes.getTypeParser(oid, format);
2361
2361
  } };
2362
2362
  }
2363
+ /** The warning logged for a pool client whose connection broke (`57P01`, `ECONNRESET`, …). */
2364
+ function lostConnectionMessage(err, where) {
2365
+ return `[atscript/db-postgres] ${where} pool connection lost (${err.code ?? err.name}): ${err.message}`;
2366
+ }
2363
2367
  /**
2364
2368
  * {@link TPgDriver} implementation backed by `pg` (node-postgres).
2365
2369
  *
@@ -2386,6 +2390,12 @@ function createCustomTypes(pgTypes) {
2386
2390
  * const driver = new PgDriver(pool)
2387
2391
  * ```
2388
2392
  *
2393
+ * A broken pool connection (a terminated backend, failover, network reset) is
2394
+ * logged through `options.logger` instead of crashing the process (since
2395
+ * 0.1.154): idle clients of a pool the driver creates, and clients checked out
2396
+ * for a transaction. A pre-created `pg.Pool` is the caller's — attach your own
2397
+ * `pool.on('error', …)` listener, or an idle client's error crashes the process.
2398
+ *
2389
2399
  * Requires `pg` to be installed:
2390
2400
  * ```bash
2391
2401
  * pnpm add pg
@@ -2394,7 +2404,9 @@ function createCustomTypes(pgTypes) {
2394
2404
  var PgDriver = class {
2395
2405
  pool;
2396
2406
  poolInit;
2397
- constructor(poolOrConfig) {
2407
+ logger;
2408
+ constructor(poolOrConfig, options = {}) {
2409
+ this.logger = options.logger ?? console;
2398
2410
  if (typeof poolOrConfig === "object" && typeof poolOrConfig.query === "function") this.pool = poolOrConfig;
2399
2411
  else this.poolInit = import("pg").then((pg) => {
2400
2412
  const Pool = pg.default?.Pool ?? pg.Pool;
@@ -2408,6 +2420,7 @@ var PgDriver = class {
2408
2420
  ...poolOrConfig,
2409
2421
  types: customTypes
2410
2422
  });
2423
+ this.pool.on("error", (err) => this.logger.warn(lostConnectionMessage(err, "idle")));
2411
2424
  return this.pool;
2412
2425
  });
2413
2426
  }
@@ -2432,6 +2445,13 @@ var PgDriver = class {
2432
2445
  }
2433
2446
  async getConnection() {
2434
2447
  const client = await (await this.getPool()).connect();
2448
+ let lost;
2449
+ const onError = (err) => {
2450
+ if (lost) return;
2451
+ lost = err;
2452
+ this.logger.warn(lostConnectionMessage(err, "checked-out"));
2453
+ };
2454
+ client.on("error", onError);
2435
2455
  return {
2436
2456
  async run(sql, params) {
2437
2457
  const result = await client.query(sql, sanitizeParams(params));
@@ -2450,7 +2470,8 @@ var PgDriver = class {
2450
2470
  await client.query(sql);
2451
2471
  },
2452
2472
  release() {
2453
- client.release();
2473
+ client.removeListener("error", onError);
2474
+ client.release(lost);
2454
2475
  }
2455
2476
  };
2456
2477
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-postgres",
3
- "version": "0.1.153",
3
+ "version": "0.1.154",
4
4
  "description": "PostgreSQL adapter for @atscript/db with pg (node-postgres) driver support.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -55,8 +55,8 @@
55
55
  "@atscript/typescript": "^0.1.103",
56
56
  "@uniqu/core": "^0.1.15",
57
57
  "pg": ">=8.0.0",
58
- "@atscript/db-sql-tools": "^0.1.153",
59
- "@atscript/db": "^0.1.153"
58
+ "@atscript/db": "^0.1.154",
59
+ "@atscript/db-sql-tools": "^0.1.154"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "pg": {