@kubun/db-postgres 0.11.0 → 0.12.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.
package/lib/index.d.ts CHANGED
@@ -4,6 +4,8 @@ import { type Options, type PostgresType } from 'postgres';
4
4
  export type AdapterParams = {
5
5
  url: string;
6
6
  options?: Options<Record<string, PostgresType>>;
7
+ /** @see CLOSE_TIMEOUT_SECONDS */
8
+ closeTimeoutSeconds?: number;
7
9
  };
8
10
  export declare class PostgresAdapter extends AbstractPostgresAdapter {
9
11
  #private;
package/lib/index.js CHANGED
@@ -1,6 +1,42 @@
1
1
  import { AbstractPostgresAdapter } from '@kubun/db-adapter';
2
2
  import { PostgresJSDialect } from 'kysely-postgres-js';
3
3
  import postgres from 'postgres';
4
+ /**
5
+ * How long `close()` gives in-flight queries before terminating the connections
6
+ * out from under them.
7
+ *
8
+ * A timeout is not a nicety here. kysely-postgres-js acquires EVERY query
9
+ * through `postgres.reserve()`, and postgres.js `end()` waits on a reserved
10
+ * connection for a resolver only `terminate()` ever calls — releasing the
11
+ * reservation afterwards returns the connection to the pool without ending it.
12
+ * So a single query overlapping the close wedges the close PERMANENTLY, and any
13
+ * unawaited background query hangs a process that is shutting down cleanly.
14
+ * Shutdown has to be able to finish; a component with work worth finishing
15
+ * drains it before closing the database, and does not rely on this.
16
+ */ const CLOSE_TIMEOUT_SECONDS = 5;
17
+ class ClosingPostgresJSDialect {
18
+ #dialect;
19
+ #end;
20
+ constructor(params){
21
+ this.#dialect = params.dialect;
22
+ this.#end = params.end;
23
+ }
24
+ createAdapter() {
25
+ return this.#dialect.createAdapter();
26
+ }
27
+ createDriver() {
28
+ const driver = this.#dialect.createDriver();
29
+ // Shadows the prototype's `destroy`, which calls `end()` with no deadline.
30
+ driver.destroy = this.#end;
31
+ return driver;
32
+ }
33
+ createIntrospector(db) {
34
+ return this.#dialect.createIntrospector(db);
35
+ }
36
+ createQueryCompiler() {
37
+ return this.#dialect.createQueryCompiler();
38
+ }
39
+ }
4
40
  export class PostgresAdapter extends AbstractPostgresAdapter {
5
41
  #dialect;
6
42
  constructor(params){
@@ -29,8 +65,17 @@ export class PostgresAdapter extends AbstractPostgresAdapter {
29
65
  ...params.options?.types
30
66
  }
31
67
  };
32
- this.#dialect = new PostgresJSDialect({
33
- postgres: postgres(params.url, options)
68
+ const client = postgres(params.url, options);
69
+ const timeout = params.closeTimeoutSeconds ?? CLOSE_TIMEOUT_SECONDS;
70
+ this.#dialect = new ClosingPostgresJSDialect({
71
+ dialect: new PostgresJSDialect({
72
+ postgres: client
73
+ }),
74
+ end: async ()=>{
75
+ await client.end({
76
+ timeout
77
+ });
78
+ }
34
79
  });
35
80
  }
36
81
  get dialect() {
package/package.json CHANGED
@@ -1,33 +1,36 @@
1
1
  {
2
2
  "name": "@kubun/db-postgres",
3
- "version": "0.11.0",
4
- "license": "see LICENSE.md",
3
+ "version": "0.12.1",
5
4
  "keywords": [],
5
+ "license": "see LICENSE.md",
6
+ "sideEffects": false,
6
7
  "type": "module",
7
- "main": "lib/index.js",
8
- "types": "lib/index.d.ts",
9
8
  "exports": {
10
9
  ".": "./lib/index.js"
11
10
  },
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
12
13
  "files": [
13
14
  "lib/*",
14
15
  "LICENSE.md"
15
16
  ],
16
- "sideEffects": false,
17
17
  "dependencies": {
18
- "kysely": "^0.29.2",
18
+ "kysely": "^0.29.5",
19
19
  "kysely-postgres-js": "^3.0.0",
20
20
  "postgres": "^3.4.8",
21
- "@kubun/db-adapter": "^0.11.0"
21
+ "@kubun/db-adapter": "^0.13.0"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
22
25
  },
23
26
  "scripts": {
27
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
24
28
  "build:clean": "del lib",
25
29
  "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
26
30
  "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
27
31
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
28
- "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
32
+ "test": "pnpm run test:types && pnpm run test:unit",
29
33
  "test:types": "tsc --noEmit",
30
- "test:unit": "vitest run",
31
- "test": "pnpm run test:types && pnpm run test:unit"
34
+ "test:unit": "vitest run"
32
35
  }
33
36
  }