@kubun/db-postgres 0.10.0 → 0.12.0

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 +1,84 @@
1
- import{AbstractPostgresAdapter as t}from"@kubun/db-adapter";import{PostgresJSDialect as r}from"kysely-postgres-js";import e from"postgres";export class PostgresAdapter extends t{#t;constructor(t){super(),this.#t=new r({postgres:e(t.url,t.options)})}get dialect(){return this.#t}}
1
+ import { AbstractPostgresAdapter } from '@kubun/db-adapter';
2
+ import { PostgresJSDialect } from 'kysely-postgres-js';
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
+ }
40
+ export class PostgresAdapter extends AbstractPostgresAdapter {
41
+ #dialect;
42
+ constructor(params){
43
+ super();
44
+ // postgres.js returns int8/bigint columns as strings by default to avoid
45
+ // precision loss. Our bigint columns hold ms-epoch values (well under
46
+ // 2^53), and the table types declare them as `number`, so parse them back
47
+ // to numbers on read. Caller-supplied `types` still take precedence.
48
+ //
49
+ // NOTE: this parser is connection-global — it applies to EVERY int8 read
50
+ // (including `COUNT()` results), not just the hub ms-epoch columns. Safe
51
+ // today because all int8 values in use are < 2^53 (Number.MAX_SAFE_INTEGER).
52
+ // A future column holding genuinely large bigints (e.g. a snowflake ID)
53
+ // would lose precision here and must opt out via a `BigInt`-returning type.
54
+ const options = {
55
+ ...params.options,
56
+ types: {
57
+ int8: {
58
+ to: 20,
59
+ from: [
60
+ 20
61
+ ],
62
+ serialize: (value)=>String(value),
63
+ parse: (value)=>Number(value)
64
+ },
65
+ ...params.options?.types
66
+ }
67
+ };
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
+ }
79
+ });
80
+ }
81
+ get dialect() {
82
+ return this.#dialect;
83
+ }
84
+ }
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
1
  {
2
2
  "name": "@kubun/db-postgres",
3
- "version": "0.10.0",
4
- "license": "see LICENSE.md",
3
+ "version": "0.12.0",
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.4",
19
19
  "kysely-postgres-js": "^3.0.0",
20
20
  "postgres": "^3.4.8",
21
- "@kubun/db-adapter": "^0.10.0"
21
+ "@kubun/db-adapter": "^0.12.0"
22
22
  },
23
23
  "scripts": {
24
+ "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
24
25
  "build:clean": "del lib",
25
- "build:js": "swc src -d ./lib --config-file ../../swc.json --strip-leading-paths",
26
+ "build:js": "swc src -d ./lib --config-file ../../node_modules/@kigu/dev/swc.json --strip-leading-paths",
26
27
  "build:types": "tsc --emitDeclarationOnly --skipLibCheck",
27
28
  "build:types:ci": "tsc --emitDeclarationOnly --declarationMap false",
28
- "build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
29
+ "test": "pnpm run test:types && pnpm run test:unit",
29
30
  "test:types": "tsc --noEmit",
30
- "test:unit": "vitest run",
31
- "test": "pnpm run test:types && pnpm run test:unit"
31
+ "test:unit": "vitest run"
32
32
  }
33
33
  }