@owlmeans/postgres 0.1.18-rc.16 → 0.1.18-rc.18

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/README.md CHANGED
@@ -10,7 +10,6 @@ provisioning, and an opt-in least-privilege bootstrap path.
10
10
  - Reads connection config from `context.cfg.dbs` entries whose `service` is `'postgres'`
11
11
  - Backs `@owlmeans/postgres-resource`; the resource layer owns all DDL
12
12
  - `bootstrap()` provisions a role, database and schema from a separate superuser config alias
13
- - `checkDbHealth()` / `pingDb()` answer a health endpoint through the registered service
14
13
 
15
14
  ## Installation
16
15
 
@@ -54,36 +53,6 @@ At `init()` the service opens a pool, runs a `SELECT 1` readiness probe (30 atte
54
53
  default — a Postgres sidecar routinely accepts TCP before it accepts queries), issues
55
54
  `CREATE SCHEMA IF NOT EXISTS`, and installs a `SIGTERM` handler that drains the pool.
56
55
 
57
- ### Health checks
58
-
59
- `pingDb` and `checkDbHealth` go through the **registered service**, looked up by alias exactly the
60
- way any other consumer looks it up — never a connection string of their own, so a probe can never
61
- report on a connection the app does not use.
62
-
63
- ```typescript
64
- import { checkDbHealth, pingDb, getLastDbHealth } from '@owlmeans/postgres'
65
-
66
- // Boot gate — after the context's init(), before the API server binds.
67
- const health = await checkDbHealth(context)
68
- if (!health.ok) throw new Error(health.error ?? health.summary)
69
- console.log(`[db-check] ${health.summary}`)
70
-
71
- // Request time — inside a health entrypoint handler, with the context it was given.
72
- const live = await pingDb(ctx)
73
- const boot = getLastDbHealth()
74
- return { db: { ok: live.ok && (boot?.ok ?? true), summary: boot?.summary ?? live.summary } }
75
- ```
76
-
77
- Both return `{ ok, summary, error? }` and never throw; a driver failure is formatted by
78
- `formatDbError`, which walks the `cause` chain and surfaces the Postgres `severity`/`code`/`detail`/
79
- `hint` a bare `.message` hides. `checkDbHealth` awaits `service.ready()` and caches its verdict for
80
- `getLastDbHealth(alias?, configAlias?)`; `pingDb` is a bare `SELECT 1` on the pool and caches
81
- nothing. Neither inspects the schema — structure is reconciled by the resources at `init()`, so a
82
- process that is serving at all has the tables it was built with.
83
-
84
- The context is a parameter, not a module import: a handler is given the context that actually
85
- served the request, and passing it keeps these functions out of the boot import cycle.
86
-
87
56
  ### Bootstrap (admin path)
88
57
 
89
58
  Hold the superuser connection under a **separate** config alias so the application's own entry never
@@ -131,14 +100,6 @@ Extends `PostgresDbService` from `@owlmeans/postgres-resource`:
131
100
  - `lock` / `unlock` — AES field encryption via `config.encryptionKey`
132
101
  - `bootstrap(configAlias, opts): Promise<BootstrapReport>`
133
102
 
134
- ### Health
135
-
136
- - `pingDb(context, alias?, configAlias?): Promise<DbHealth>` — liveness through the pool
137
- - `checkDbHealth(context, alias?, configAlias?): Promise<DbHealth>` — `ready()` + liveness, cached
138
- - `getLastDbHealth(alias?, configAlias?): DbHealth | undefined` — the last cached verdict
139
- - `formatDbError(error): string` — cause-chain formatter with `severity`/`code`/`detail`/`hint`
140
- - `DbHealth` — `{ ok: boolean, summary: string, error?: string }`
141
-
142
103
  ### Helpers
143
104
 
144
105
  - `parseUrl(url)` / `prepareConfig(config, overrides?)` / `poolDatabase(pool)`
@@ -168,7 +129,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
168
129
  your project's skill store (`.agents/skills/`):
169
130
 
170
131
  ```sh
171
- npx @owlmeans/agent-skills@^0.1.18-rc.14
132
+ npx @owlmeans/agent-skills@^0.1.18-rc.16
172
133
  ```
173
134
 
174
135
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 2,
3
3
  "package": "@owlmeans/postgres",
4
- "version": "0.1.18-rc.16",
5
- "generatedAt": "2026-09-10T23:12:07.520Z",
4
+ "version": "0.1.18-rc.18",
5
+ "generatedAt": "2026-09-12T12:32:46.384Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -8,7 +8,7 @@ user-invocable: false
8
8
  # @owlmeans/postgres
9
9
 
10
10
  **Layer:** Infra
11
- **Install:** `"@owlmeans/postgres": "^0.1.18-rc.16"` in `dependencies`
11
+ **Install:** `"@owlmeans/postgres": "^0.1.18-rc.18"` in `dependencies`
12
12
 
13
13
  Pooled `pg` connections for a server context, plus the admin path that provisions the role, database
14
14
  and schema an app connects with. All DDL for application tables belongs to [[postgres-resource]] —
package/build/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export type * from './types.js';
2
2
  export * from './consts.js';
3
3
  export * from './bootstrap.js';
4
- export * from './health.js';
5
4
  export * from './middleware.js';
6
5
  export * from './service.js';
7
6
  export * from './utils/config.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA"}
package/build/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  export * from './consts.js';
2
2
  export * from './bootstrap.js';
3
- export * from './health.js';
4
3
  export * from './middleware.js';
5
4
  export * from './service.js';
6
5
  export * from './utils/config.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/postgres",
3
- "version": "0.1.18-rc.16",
3
+ "version": "0.1.18-rc.18",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -22,17 +22,17 @@
22
22
  }
23
23
  },
24
24
  "dependencies": {
25
- "@owlmeans/basic-keys": "^0.1.18-rc.14",
26
- "@owlmeans/context": "^0.1.18-rc.10",
27
- "@owlmeans/postgres-resource": "^0.1.18-rc.15",
28
- "@owlmeans/resource": "^0.1.18-rc.11",
29
- "@owlmeans/server-context": "^0.1.18-rc.14",
25
+ "@owlmeans/basic-keys": "^0.1.18-rc.16",
26
+ "@owlmeans/context": "^0.1.18-rc.12",
27
+ "@owlmeans/postgres-resource": "^0.1.18-rc.17",
28
+ "@owlmeans/resource": "^0.1.18-rc.13",
29
+ "@owlmeans/server-context": "^0.1.18-rc.16",
30
30
  "drizzle-orm": "~0.45.2",
31
31
  "pg": "^8.22.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@owlmeans/dep-config": "workspace:*",
35
- "@owlmeans/test-integration": "^0.1.18-rc.10",
35
+ "@owlmeans/test-integration": "^0.1.18-rc.12",
36
36
  "@types/bun": "^1.4.0",
37
37
  "@types/node": "^26.1.0",
38
38
  "@types/pg": "^8.20.4",
package/src/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export type * from './types.js'
2
2
  export * from './consts.js'
3
3
  export * from './bootstrap.js'
4
- export * from './health.js'
5
4
  export * from './middleware.js'
6
5
  export * from './service.js'
7
6
  export * from './utils/config.js'
package/build/health.d.ts DELETED
@@ -1,40 +0,0 @@
1
- import type { BasicConfig, BasicContext } from '@owlmeans/context';
2
- export interface DbHealth {
3
- ok: boolean;
4
- /** One line fit for a boot log — states what was checked, not only whether it passed. */
5
- summary: string;
6
- error?: string;
7
- }
8
- /**
9
- * The outcome of the last {@link checkDbHealth} for the same pair of aliases.
10
- *
11
- * A request time handler wants the verdict of the boot check without paying for it again: the
12
- * structure was reconciled once, at init, and re-asserting it per request buys nothing.
13
- */
14
- export declare const getLastDbHealth: (alias?: string, configAlias?: string) => DbHealth | undefined;
15
- /**
16
- * Surface the Postgres fields (code/severity/detail/hint) an error carries — a bare `.message`
17
- * hides the real FATAL/permission reason, and the cause chain is where the driver's error ends up
18
- * once a resource wraps it.
19
- */
20
- export declare const formatDbError: (error: unknown) => string;
21
- /**
22
- * Liveness only, through the context's own pool — no private client, no connection string, no
23
- * migration journal. The service is looked up by alias exactly as any other consumer looks it up,
24
- * so a health probe cannot end up connected differently from the app it reports on.
25
- *
26
- * The context arrives as an argument rather than as a module import: a handler is given the
27
- * context that actually served the request (possibly an entity scoped derivative), and taking it
28
- * as a parameter keeps this module out of the boot import cycle.
29
- */
30
- export declare const pingDb: (context: BasicContext<BasicConfig>, alias?: string, configAlias?: string) => Promise<DbHealth>;
31
- /**
32
- * The boot gate check: wait for the service to be ready, then assert the connection still works.
33
- *
34
- * Structure is reconciled by the resources during the context's `init()`; if that succeeded the
35
- * tables are correct by construction, and if it failed the process never got here. There is
36
- * nothing left to assert beyond "the connection works", which is why this is a `SELECT 1` and not
37
- * a schema walk. The result is cached for {@link getLastDbHealth}.
38
- */
39
- export declare const checkDbHealth: (context: BasicContext<BasicConfig>, alias?: string, configAlias?: string) => Promise<DbHealth>;
40
- //# sourceMappingURL=health.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAKlE,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,OAAO,CAAA;IACX,yFAAyF;IACzF,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAMD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,WACnB,MAAM,gBAAgC,MAAM,KAClD,QAAQ,GAAG,SAAsD,CAAA;AAEpE;;;;GAIG;AACH,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,MAmB9C,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,MAAM,YACR,YAAY,CAAC,WAAW,CAAC,UAAS,MAAM,gBAAgC,MAAM,KACtF,OAAO,CAAC,QAAQ,CASlB,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,YACf,YAAY,CAAC,WAAW,CAAC,UAAS,MAAM,gBAAgC,MAAM,KACtF,OAAO,CAAC,QAAQ,CAelB,CAAA"}
package/build/health.js DELETED
@@ -1,80 +0,0 @@
1
- import { DEFAULT_ALIAS } from './consts.js';
2
- const lastHealth = new Map();
3
- const keyOf = (alias, configAlias) => `${alias}:${configAlias ?? ''}`;
4
- /**
5
- * The outcome of the last {@link checkDbHealth} for the same pair of aliases.
6
- *
7
- * A request time handler wants the verdict of the boot check without paying for it again: the
8
- * structure was reconciled once, at init, and re-asserting it per request buys nothing.
9
- */
10
- export const getLastDbHealth = (alias = DEFAULT_ALIAS, configAlias) => lastHealth.get(keyOf(alias, configAlias));
11
- /**
12
- * Surface the Postgres fields (code/severity/detail/hint) an error carries — a bare `.message`
13
- * hides the real FATAL/permission reason, and the cause chain is where the driver's error ends up
14
- * once a resource wraps it.
15
- */
16
- export const formatDbError = (error) => {
17
- const parts = [];
18
- const seen = new Set();
19
- let current = error;
20
- while (current != null && !seen.has(current)) {
21
- seen.add(current);
22
- const meta = [];
23
- if (current.severity != null)
24
- meta.push(`severity=${current.severity}`);
25
- if (current.code != null)
26
- meta.push(`code=${current.code}`);
27
- if (current.detail != null)
28
- meta.push(`detail=${current.detail}`);
29
- if (current.hint != null)
30
- meta.push(`hint=${current.hint}`);
31
- const message = current.message ?? String(current);
32
- parts.push(meta.length > 0 ? `${message} (${meta.join(', ')})` : message);
33
- current = current.cause;
34
- }
35
- const joined = parts.join('; caused by: ');
36
- return joined !== '' ? joined : String(error);
37
- };
38
- /**
39
- * Liveness only, through the context's own pool — no private client, no connection string, no
40
- * migration journal. The service is looked up by alias exactly as any other consumer looks it up,
41
- * so a health probe cannot end up connected differently from the app it reports on.
42
- *
43
- * The context arrives as an argument rather than as a module import: a handler is given the
44
- * context that actually served the request (possibly an entity scoped derivative), and taking it
45
- * as a parameter keeps this module out of the boot import cycle.
46
- */
47
- export const pingDb = async (context, alias = DEFAULT_ALIAS, configAlias) => {
48
- try {
49
- const pool = await context.service(alias).client(configAlias);
50
- await pool.query('SELECT 1');
51
- return { ok: true, summary: 'DB reachable' };
52
- }
53
- catch (error) {
54
- return { ok: false, summary: 'DB unreachable', error: formatDbError(error) };
55
- }
56
- };
57
- /**
58
- * The boot gate check: wait for the service to be ready, then assert the connection still works.
59
- *
60
- * Structure is reconciled by the resources during the context's `init()`; if that succeeded the
61
- * tables are correct by construction, and if it failed the process never got here. There is
62
- * nothing left to assert beyond "the connection works", which is why this is a `SELECT 1` and not
63
- * a schema walk. The result is cached for {@link getLastDbHealth}.
64
- */
65
- export const checkDbHealth = async (context, alias = DEFAULT_ALIAS, configAlias) => {
66
- let result;
67
- try {
68
- const service = context.service(alias);
69
- await service.ready();
70
- const pool = await service.client(configAlias);
71
- await pool.query('SELECT 1');
72
- result = { ok: true, summary: 'DB OK — connected; structure reconciled at init' };
73
- }
74
- catch (error) {
75
- result = { ok: false, summary: 'DB check failed', error: formatDbError(error) };
76
- }
77
- lastHealth.set(keyOf(alias, configAlias), result);
78
- return result;
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C,OAAkC,EAAE,KAAK,GAAW,aAAa,EAAE,WAAoB,EACpE,EAAE;IACrB,IAAI,MAAgB,CAAA;IACpB,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAkB,KAAK,CAAC,CAAA;QACvD,MAAM,OAAO,CAAC,KAAK,EAAE,CAAA;QACrB,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;QAC9C,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;QAC5B,MAAM,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,iDAAiD,EAAE,CAAA;IACnF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,EAAE,CAAA;IACjF,CAAC;IAED,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,MAAM,CAAC,CAAA;IAEjD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
package/src/health.ts DELETED
@@ -1,100 +0,0 @@
1
- import type { BasicConfig, BasicContext } from '@owlmeans/context'
2
-
3
- import { DEFAULT_ALIAS } from './consts.js'
4
- import type { PostgresService } from './types.js'
5
-
6
- export interface DbHealth {
7
- ok: boolean
8
- /** One line fit for a boot log — states what was checked, not only whether it passed. */
9
- summary: string
10
- error?: string
11
- }
12
-
13
- const lastHealth: Map<string, DbHealth> = new Map()
14
-
15
- const keyOf = (alias: string, configAlias?: string): string => `${alias}:${configAlias ?? ''}`
16
-
17
- /**
18
- * The outcome of the last {@link checkDbHealth} for the same pair of aliases.
19
- *
20
- * A request time handler wants the verdict of the boot check without paying for it again: the
21
- * structure was reconciled once, at init, and re-asserting it per request buys nothing.
22
- */
23
- export const getLastDbHealth = (
24
- alias: string = DEFAULT_ALIAS, configAlias?: string
25
- ): DbHealth | undefined => lastHealth.get(keyOf(alias, configAlias))
26
-
27
- /**
28
- * Surface the Postgres fields (code/severity/detail/hint) an error carries — a bare `.message`
29
- * hides the real FATAL/permission reason, and the cause chain is where the driver's error ends up
30
- * once a resource wraps it.
31
- */
32
- export const formatDbError = (error: unknown): string => {
33
- const parts: string[] = []
34
- const seen = new Set<unknown>()
35
- let current = error as (Record<string, any> | null | undefined)
36
- while (current != null && !seen.has(current)) {
37
- seen.add(current)
38
- const meta: string[] = []
39
- if (current.severity != null) meta.push(`severity=${current.severity}`)
40
- if (current.code != null) meta.push(`code=${current.code}`)
41
- if (current.detail != null) meta.push(`detail=${current.detail}`)
42
- if (current.hint != null) meta.push(`hint=${current.hint}`)
43
- const message = current.message ?? String(current)
44
- parts.push(meta.length > 0 ? `${message} (${meta.join(', ')})` : message)
45
- current = current.cause
46
- }
47
-
48
- const joined = parts.join('; caused by: ')
49
-
50
- return joined !== '' ? joined : String(error)
51
- }
52
-
53
- /**
54
- * Liveness only, through the context's own pool — no private client, no connection string, no
55
- * migration journal. The service is looked up by alias exactly as any other consumer looks it up,
56
- * so a health probe cannot end up connected differently from the app it reports on.
57
- *
58
- * The context arrives as an argument rather than as a module import: a handler is given the
59
- * context that actually served the request (possibly an entity scoped derivative), and taking it
60
- * as a parameter keeps this module out of the boot import cycle.
61
- */
62
- export const pingDb = async (
63
- context: BasicContext<BasicConfig>, alias: string = DEFAULT_ALIAS, configAlias?: string
64
- ): Promise<DbHealth> => {
65
- try {
66
- const pool = await context.service<PostgresService>(alias).client(configAlias)
67
- await pool.query('SELECT 1')
68
-
69
- return { ok: true, summary: 'DB reachable' }
70
- } catch (error) {
71
- return { ok: false, summary: 'DB unreachable', error: formatDbError(error) }
72
- }
73
- }
74
-
75
- /**
76
- * The boot gate check: wait for the service to be ready, then assert the connection still works.
77
- *
78
- * Structure is reconciled by the resources during the context's `init()`; if that succeeded the
79
- * tables are correct by construction, and if it failed the process never got here. There is
80
- * nothing left to assert beyond "the connection works", which is why this is a `SELECT 1` and not
81
- * a schema walk. The result is cached for {@link getLastDbHealth}.
82
- */
83
- export const checkDbHealth = async (
84
- context: BasicContext<BasicConfig>, alias: string = DEFAULT_ALIAS, configAlias?: string
85
- ): Promise<DbHealth> => {
86
- let result: DbHealth
87
- try {
88
- const service = context.service<PostgresService>(alias)
89
- await service.ready()
90
- const pool = await service.client(configAlias)
91
- await pool.query('SELECT 1')
92
- result = { ok: true, summary: 'DB OK — connected; structure reconciled at init' }
93
- } catch (error) {
94
- result = { ok: false, summary: 'DB check failed', error: formatDbError(error) }
95
- }
96
-
97
- lastHealth.set(keyOf(alias, configAlias), result)
98
-
99
- return result
100
- }