@owlmeans/postgres 0.1.18-rc.11 → 0.1.18-rc.13
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 +2 -2
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/postgres/SKILL.md +4 -4
- package/build/bootstrap.d.ts.map +1 -1
- package/build/middleware.d.ts.map +1 -1
- package/build/middleware.js.map +1 -1
- package/build/service.d.ts.map +1 -1
- package/build/service.js +5 -32
- package/build/service.js.map +1 -1
- package/build/utils/config.d.ts.map +1 -1
- package/build/utils/connection.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/service.ts +9 -42
- package/tests/crud.spec.ts +68 -18
- package/tests/custom-sql.spec.ts +3 -2
package/README.md
CHANGED
|
@@ -46,8 +46,8 @@ A whole connection string works too, and wins over `host`/`user`/`secret`:
|
|
|
46
46
|
cfg.dbs = [{ service: 'postgres', alias: 'postgres', schema: 'app', meta: { url: process.env.DATABASE_URL } }]
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
`schema` is the Postgres **schema**, not the database — `dbName()`
|
|
50
|
-
|
|
49
|
+
`schema` is the Postgres **schema**, not the database — `dbName()` resolves it as
|
|
50
|
+
`config.schema ?? config.alias ?? service.alias`. The database comes from `meta.database`.
|
|
51
51
|
|
|
52
52
|
At `init()` the service opens a pool, runs a `SELECT 1` readiness probe (30 attempts, 2s apart by
|
|
53
53
|
default — a Postgres sidecar routinely accepts TCP before it accepts queries), issues
|
package/agent-meta/manifest.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 2,
|
|
3
3
|
"package": "@owlmeans/postgres",
|
|
4
|
-
"version": "0.1.18-rc.
|
|
5
|
-
"generatedAt": "2026-09-
|
|
4
|
+
"version": "0.1.18-rc.13",
|
|
5
|
+
"generatedAt": "2026-09-01T17:39:40.769Z",
|
|
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.
|
|
11
|
+
**Install:** `"@owlmeans/postgres": "^0.1.18-rc.13"` 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]] —
|
|
@@ -50,9 +50,9 @@ cfg.dbs = [{
|
|
|
50
50
|
for a `DATABASE_URL` env var. Because a leading `/` is auto-read, moving to file-mounted secrets
|
|
51
51
|
later is a config change, not a code change.
|
|
52
52
|
|
|
53
|
-
**`schema` is the SCHEMA, not the database.** `dbName()`
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
**`schema` is the SCHEMA, not the database.** `dbName()` returns it as given
|
|
54
|
+
(`config.schema ?? config.alias ?? service.alias`), so a separate namespace costs a `CREATE SCHEMA`
|
|
55
|
+
inside a shared database rather than a `CREATE DATABASE`. The database comes from `meta.database`.
|
|
56
56
|
|
|
57
57
|
Other `meta` keys: `autoSync` (see [[postgres-resource]]), `ssl`, `max`, `idleTimeoutMillis`,
|
|
58
58
|
`connectionTimeoutMillis`, `statementTimeoutMillis`, `retries`, `retryDelayMillis`.
|
package/build/bootstrap.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AASpF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AASpF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,GACtB,SAAS,eAAe,EAAE,aAAa,MAAM,EAAE,MAAM,gBAAgB,KACpE,OAAO,CAAC,eAAe,CAwFzB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAKnD;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAKnD;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,QAAO,MAAsB,KAAG,UAW9D,CAAA"}
|
package/build/middleware.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAGnE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAG3C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAGnE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAG3C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,QAAgB,aAAa,EAAc,EAAE,CAAC,CAAC;IAC7E,IAAI,EAAE,cAAc,CAAC,OAAO;IAC5B,KAAK,EAAE,eAAe,CAAC,OAAO;IAE9B,KAAK,EAAE,KAAK,EAAC,OAAO,EAAC,EAAE;QACrB,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/B,OAAM;QACR,CAAC;QAED,MAAM,OAAO,CAAC,OAAO,CAAkB,KAAK,CAAC,CAAC,KAAK,EAAE,CAAA;IACvD,CAAC;CACF,CAAC,CAAA"}
|
package/build/service.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAS3E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAIjD,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,UAAU,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEzE,eAAO,MAAM,qBAAqB,GAAI,QAAO,MAAsB,KAAG,eA2NrE,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAChF,SAAS,CAAC,EAAE,QAAO,MAAsB,KACxC,CAOF,CAAA"}
|
package/build/service.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { makeKeyPairModel } from '@owlmeans/basic-keys';
|
|
2
|
-
import { assertContext
|
|
2
|
+
import { assertContext } from '@owlmeans/context';
|
|
3
3
|
import { makeTx, pgErrorToResourceError, pgIdentifier, refOf, resolvePlaceholders } from '@owlmeans/postgres-resource';
|
|
4
4
|
import { createDbService } from '@owlmeans/resource';
|
|
5
5
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
@@ -14,7 +14,7 @@ export const makePostgresDbService = (alias = DEFAULT_ALIAS) => {
|
|
|
14
14
|
/**
|
|
15
15
|
* Handles are cached per config alias rather than rebuilt per call: `db()` runs on every
|
|
16
16
|
* single resource operation, and both the Drizzle binding and the schema name are fixed
|
|
17
|
-
* for the lifetime of
|
|
17
|
+
* for the lifetime of the service — one instance per alias, for the whole process.
|
|
18
18
|
*/
|
|
19
19
|
const handles = new Map();
|
|
20
20
|
const deferred = new Map();
|
|
@@ -28,9 +28,9 @@ export const makePostgresDbService = (alias = DEFAULT_ALIAS) => {
|
|
|
28
28
|
drizzle: drizzle(pool),
|
|
29
29
|
pool,
|
|
30
30
|
/**
|
|
31
|
-
* `name()` is the
|
|
32
|
-
* reads the same value as a database name.
|
|
33
|
-
*
|
|
31
|
+
* `name()` is the configured namespace — the Postgres SCHEMA here, where Mongo
|
|
32
|
+
* reads the same value as a database name. `pgIdentifier` holds it inside the
|
|
33
|
+
* 63 byte limit Postgres would otherwise truncate silently.
|
|
34
34
|
*/
|
|
35
35
|
schema: pgIdentifier(service.name(configAlias)),
|
|
36
36
|
database: poolDatabase(pool)
|
|
@@ -45,20 +45,6 @@ export const makePostgresDbService = (alias = DEFAULT_ALIAS) => {
|
|
|
45
45
|
if (service.clients[configAlias] != null) {
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
48
|
-
if (service.layers == null) {
|
|
49
|
-
service.layers = [Layer.Global];
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Sensitivity *adds* a layer the service is willing to serve. The Mongo copy of this
|
|
53
|
-
* block tests `includes` where it means `!includes`, so it only ever appends a layer
|
|
54
|
-
* that is already there — a latent no-op, corrected here.
|
|
55
|
-
*/
|
|
56
|
-
if (config.serviceSensitive === true && !service.layers.includes(Layer.Service)) {
|
|
57
|
-
service.layers.push(Layer.Service);
|
|
58
|
-
}
|
|
59
|
-
if (config.entitySensitive === true && !service.layers.includes(Layer.Entity)) {
|
|
60
|
-
service.layers.push(Layer.Entity);
|
|
61
|
-
}
|
|
62
48
|
const meta = (config.meta ?? {});
|
|
63
49
|
const pool = new Pool(prepareConfig(config));
|
|
64
50
|
/**
|
|
@@ -188,19 +174,6 @@ export const makePostgresDbService = (alias = DEFAULT_ALIAS) => {
|
|
|
188
174
|
/** A value that was never locked decrypts to itself rather than failing. */
|
|
189
175
|
fields.includes(field) ? await key.decrypt(value).catch(() => value) : value
|
|
190
176
|
])));
|
|
191
|
-
},
|
|
192
|
-
reinitializeContext: (context) => {
|
|
193
|
-
const _service = makePostgresDbService(alias);
|
|
194
|
-
_service.ctx = context;
|
|
195
|
-
_service.layers = service.layers;
|
|
196
|
-
/**
|
|
197
|
-
* Pools carry over instead of being reopened. A layer switch changes the Postgres
|
|
198
|
-
* *schema*, not the server, and `max_connections` is a cluster wide budget — a pool
|
|
199
|
-
* per tenant exhausts it long before the tenants run out. The handle cache is
|
|
200
|
-
* deliberately *not* carried: its schema name is what the switch just changed.
|
|
201
|
-
*/
|
|
202
|
-
Object.assign(_service.clients, service.clients);
|
|
203
|
-
return _service;
|
|
204
177
|
}
|
|
205
178
|
}, service => async () => {
|
|
206
179
|
const context = assertContext(service.ctx, location);
|
package/build/service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EACL,MAAM,EAAE,sBAAsB,EAAE,YAAY,EAAE,KAAK,EAAE,mBAAmB,E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"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAIpC;;;;;;;;;GASG;AACH,eAAO,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAIpC;;;;;;;;;GASG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,KAAG,UAsCtC,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,QAAQ,EAAE,YAAY,UAAU,KAAG,UAuCxE,CAAA;AAED,+DAA+D;AAC/D,eAAO,MAAM,YAAY,GAAI,MAAM;IAAE,OAAO,CAAC,EAAE,UAAU,CAAA;CAAE,KAAG,MACV,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/utils/connection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC/D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAA;AAkB9B;;;;;;;;GAQG;AACH,eAAO,MAAM,KAAK,
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/utils/connection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC/D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAA;AAkB9B;;;;;;;;GAQG;AACH,eAAO,MAAM,KAAK,GAAU,MAAM,IAAI,EAAE,MAAM,YAAY,EAAE,UAAU,MAAM,KAAG,OAAO,CAAC,IAAI,CAyB1F,CAAA;AAED,4DAA4D;AAC5D,eAAO,MAAM,YAAY,GAAU,MAAM,IAAI,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,IAAI,CAM3E,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/postgres",
|
|
3
|
-
"version": "0.1.18-rc.
|
|
3
|
+
"version": "0.1.18-rc.13",
|
|
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.
|
|
26
|
-
"@owlmeans/context": "^0.1.18-rc.
|
|
27
|
-
"@owlmeans/postgres-resource": "^0.1.18-rc.
|
|
28
|
-
"@owlmeans/resource": "^0.1.18-rc.
|
|
29
|
-
"@owlmeans/server-context": "^0.1.18-rc.
|
|
25
|
+
"@owlmeans/basic-keys": "^0.1.18-rc.11",
|
|
26
|
+
"@owlmeans/context": "^0.1.18-rc.7",
|
|
27
|
+
"@owlmeans/postgres-resource": "^0.1.18-rc.12",
|
|
28
|
+
"@owlmeans/resource": "^0.1.18-rc.8",
|
|
29
|
+
"@owlmeans/server-context": "^0.1.18-rc.11",
|
|
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.
|
|
35
|
+
"@owlmeans/test-integration": "^0.1.18-rc.7",
|
|
36
36
|
"@types/bun": "^1.4.0",
|
|
37
37
|
"@types/node": "^26.1.0",
|
|
38
38
|
"@types/pg": "^8.20.4",
|
package/src/service.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { makeKeyPairModel } from '@owlmeans/basic-keys'
|
|
2
|
-
import { assertContext
|
|
3
|
-
import type { BasicContext } from '@owlmeans/context'
|
|
2
|
+
import { assertContext } from '@owlmeans/context'
|
|
4
3
|
import {
|
|
5
4
|
makeTx, pgErrorToResourceError, pgIdentifier, refOf, resolvePlaceholders
|
|
6
5
|
} from '@owlmeans/postgres-resource'
|
|
@@ -28,7 +27,7 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
28
27
|
/**
|
|
29
28
|
* Handles are cached per config alias rather than rebuilt per call: `db()` runs on every
|
|
30
29
|
* single resource operation, and both the Drizzle binding and the schema name are fixed
|
|
31
|
-
* for the lifetime of
|
|
30
|
+
* for the lifetime of the service — one instance per alias, for the whole process.
|
|
32
31
|
*/
|
|
33
32
|
const handles: Map<string, PostgresDb> = new Map()
|
|
34
33
|
const deferred: Map<string, Array<() => Promise<void>>> = new Map()
|
|
@@ -44,9 +43,9 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
44
43
|
drizzle: drizzle(pool) as NodePgDatabase<Record<string, never>>,
|
|
45
44
|
pool,
|
|
46
45
|
/**
|
|
47
|
-
* `name()` is the
|
|
48
|
-
* reads the same value as a database name.
|
|
49
|
-
*
|
|
46
|
+
* `name()` is the configured namespace — the Postgres SCHEMA here, where Mongo
|
|
47
|
+
* reads the same value as a database name. `pgIdentifier` holds it inside the
|
|
48
|
+
* 63 byte limit Postgres would otherwise truncate silently.
|
|
50
49
|
*/
|
|
51
50
|
schema: pgIdentifier(service.name(configAlias)),
|
|
52
51
|
database: poolDatabase(pool)
|
|
@@ -65,21 +64,6 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
65
64
|
return
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
if (service.layers == null) {
|
|
69
|
-
service.layers = [Layer.Global]
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Sensitivity *adds* a layer the service is willing to serve. The Mongo copy of this
|
|
73
|
-
* block tests `includes` where it means `!includes`, so it only ever appends a layer
|
|
74
|
-
* that is already there — a latent no-op, corrected here.
|
|
75
|
-
*/
|
|
76
|
-
if (config.serviceSensitive === true && !service.layers.includes(Layer.Service)) {
|
|
77
|
-
service.layers.push(Layer.Service)
|
|
78
|
-
}
|
|
79
|
-
if (config.entitySensitive === true && !service.layers.includes(Layer.Entity)) {
|
|
80
|
-
service.layers.push(Layer.Entity)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
67
|
const meta = (config.meta ?? {}) as PostgresMeta
|
|
84
68
|
const pool = new Pool(prepareConfig(config))
|
|
85
69
|
|
|
@@ -125,7 +109,7 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
125
109
|
qualify: resourceAlias => {
|
|
126
110
|
const context = assertContext<Config, Context>(service.ctx as Context, location)
|
|
127
111
|
|
|
128
|
-
return refOf(context
|
|
112
|
+
return refOf(context, null, resourceAlias)
|
|
129
113
|
},
|
|
130
114
|
|
|
131
115
|
query: async <Row extends QueryResultRow = QueryResultRow>(
|
|
@@ -135,7 +119,7 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
135
119
|
const context = assertContext<Config, Context>(service.ctx as Context, location)
|
|
136
120
|
try {
|
|
137
121
|
const result = await db.pool.query<Row>(
|
|
138
|
-
resolvePlaceholders(text, context
|
|
122
|
+
resolvePlaceholders(text, context, null), params as never[]
|
|
139
123
|
)
|
|
140
124
|
|
|
141
125
|
return result.rows
|
|
@@ -152,8 +136,8 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
152
136
|
await client.query('BEGIN')
|
|
153
137
|
const result = await fn(makeTx(
|
|
154
138
|
client,
|
|
155
|
-
text => resolvePlaceholders(text, context
|
|
156
|
-
resourceAlias => refOf(context
|
|
139
|
+
text => resolvePlaceholders(text, context, null),
|
|
140
|
+
resourceAlias => refOf(context, null, resourceAlias)
|
|
157
141
|
))
|
|
158
142
|
await client.query('COMMIT')
|
|
159
143
|
|
|
@@ -243,23 +227,6 @@ export const makePostgresDbService = (alias: string = DEFAULT_ALIAS): PostgresSe
|
|
|
243
227
|
]
|
|
244
228
|
))
|
|
245
229
|
)
|
|
246
|
-
},
|
|
247
|
-
|
|
248
|
-
reinitializeContext: <T>(context: BasicContext<ServerConfig>) => {
|
|
249
|
-
const _service = makePostgresDbService(alias)
|
|
250
|
-
|
|
251
|
-
_service.ctx = context
|
|
252
|
-
_service.layers = service.layers
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Pools carry over instead of being reopened. A layer switch changes the Postgres
|
|
256
|
-
* *schema*, not the server, and `max_connections` is a cluster wide budget — a pool
|
|
257
|
-
* per tenant exhausts it long before the tenants run out. The handle cache is
|
|
258
|
-
* deliberately *not* carried: its schema name is what the switch just changed.
|
|
259
|
-
*/
|
|
260
|
-
Object.assign(_service.clients, service.clients)
|
|
261
|
-
|
|
262
|
-
return _service as T
|
|
263
230
|
}
|
|
264
231
|
}, service => async () => {
|
|
265
232
|
const context = assertContext<Config, Context>(service.ctx as Context, location)
|
package/tests/crud.spec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
-
import { makePostgresResource } from '@owlmeans/postgres-resource'
|
|
2
|
+
import { DEFAULT_PAGE_SIZE, makePostgresResource } from '@owlmeans/postgres-resource'
|
|
3
3
|
import type { PostgresResource } from '@owlmeans/postgres-resource'
|
|
4
|
-
import { RecordExists, UnknownRecordError } from '@owlmeans/resource'
|
|
4
|
+
import { RecordExists, UnknownRecordError, UnsupportedArgumentError } from '@owlmeans/resource'
|
|
5
5
|
import type { ResourceRecord } from '@owlmeans/resource'
|
|
6
6
|
|
|
7
7
|
import { gate, makeSuite, shapeOf } from './context.js'
|
|
@@ -146,16 +146,23 @@ describe('@owlmeans/postgres — resource CRUD against a real context', () => {
|
|
|
146
146
|
expect(loaded.createdAt).toBeInstanceOf(Date)
|
|
147
147
|
})
|
|
148
148
|
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
/**
|
|
150
|
+
* One call, several fields — the shape a list-then-take-the-first was standing in for.
|
|
151
|
+
* `load` answers with a record or `null`; `get` answers or raises.
|
|
152
|
+
*/
|
|
153
|
+
it('reads one record by criteria rather than by id, and refuses an unknown one', async () => {
|
|
154
|
+
const byEmail = await users.load({ email: 'a@b.c' })
|
|
151
155
|
expect(byEmail?.id).toBe(seeded.id as string)
|
|
152
156
|
|
|
153
|
-
expect(await users.load('
|
|
154
|
-
await
|
|
157
|
+
expect(await users.load({ email: 'a@b.c', status: 'active', age: 21 })).not.toBeNull()
|
|
158
|
+
expect(await users.load({ email: 'a@b.c', status: 'banned' })).toBeNull()
|
|
159
|
+
|
|
160
|
+
expect(await users.load({ email: 'nobody@nowhere' })).toBeNull()
|
|
161
|
+
await expect(users.get({ email: 'nobody@nowhere' })).rejects.toThrow(UnknownRecordError)
|
|
155
162
|
})
|
|
156
163
|
|
|
157
164
|
it('patches by merge where update replaces the whole record', async () => {
|
|
158
|
-
const patched = await users.patch
|
|
165
|
+
const patched = await users.patch({ id: seeded.id, age: 22 })
|
|
159
166
|
expect(patched.age).toBe(22)
|
|
160
167
|
expect(patched.email).toBe('a@b.c')
|
|
161
168
|
|
|
@@ -170,21 +177,55 @@ describe('@owlmeans/postgres — resource CRUD against a real context', () => {
|
|
|
170
177
|
await users.create({ email: 'third@b.c', status: 'active' })
|
|
171
178
|
|
|
172
179
|
const banned = await users.list({ status: 'banned' })
|
|
173
|
-
expect(banned.
|
|
180
|
+
expect(banned.total).toBe(2)
|
|
174
181
|
expect(banned.items.every(item => item.status === 'banned')).toBe(true)
|
|
175
182
|
|
|
176
|
-
const paged = await users.list({}, {
|
|
183
|
+
const paged = await users.list({}, { page: 0, size: 2 })
|
|
177
184
|
expect(paged.items).toHaveLength(2)
|
|
178
|
-
expect(paged.
|
|
185
|
+
expect(paged.total).toBe(3)
|
|
186
|
+
expect(paged.page).toBe(0)
|
|
187
|
+
expect(paged.size).toBe(2)
|
|
188
|
+
|
|
189
|
+
/** A page past the end is empty, and the total still describes the whole match. */
|
|
190
|
+
const beyond = await users.list({}, { page: 5, size: 2 })
|
|
191
|
+
expect(beyond.items).toHaveLength(0)
|
|
192
|
+
expect(beyond.total).toBe(3)
|
|
179
193
|
|
|
180
|
-
const sorted = await users.list({}, {
|
|
194
|
+
const sorted = await users.list({}, { sort: ['email'] })
|
|
181
195
|
expect(sorted.items.map(item => item.email)).toEqual(['a@b.c', 'second@b.c', 'third@b.c'])
|
|
196
|
+
|
|
197
|
+
const descending = await users.list({}, { sort: [{ field: 'email', order: 'desc' }] })
|
|
198
|
+
expect(descending.items.map(item => item.email)).toEqual(['third@b.c', 'second@b.c', 'a@b.c'])
|
|
199
|
+
|
|
200
|
+
/** The same sort narrows a multi-record criteria down to one record in a single read. */
|
|
201
|
+
const last = await users.get({ status: 'banned' }, { sort: [{ field: 'email', order: 'desc' }] })
|
|
202
|
+
expect(last.email).toBe('second@b.c')
|
|
182
203
|
})
|
|
183
204
|
|
|
184
|
-
|
|
205
|
+
/**
|
|
206
|
+
* A relational table is unbounded, so an unasked-for read is capped. Reading everything
|
|
207
|
+
* stays possible, but only by saying so.
|
|
208
|
+
*/
|
|
209
|
+
it('caps an unpaged list at the default size and lifts the cap on size 0', async () => {
|
|
210
|
+
const capped = await users.list()
|
|
211
|
+
expect(capped.size).toBe(DEFAULT_PAGE_SIZE)
|
|
212
|
+
expect(capped.page).toBe(0)
|
|
213
|
+
expect(capped.items).toHaveLength(3)
|
|
214
|
+
|
|
215
|
+
const everything = await users.list({}, { size: 0 })
|
|
216
|
+
expect(everything.items).toHaveLength(3)
|
|
217
|
+
expect(everything.total).toBe(3)
|
|
218
|
+
expect(everything.size).toBeUndefined()
|
|
219
|
+
expect(everything.page).toBeUndefined()
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('counts and purges by criteria, and refuses to purge everything', async () => {
|
|
185
223
|
expect(await users.count()).toBe(3)
|
|
186
224
|
expect(await users.count({ status: 'banned' })).toBe(2)
|
|
187
225
|
|
|
226
|
+
/** An empty criteria object would truncate the table — it has to be said in SQL instead. */
|
|
227
|
+
await expect(users.purge({})).rejects.toThrow(UnsupportedArgumentError)
|
|
228
|
+
|
|
188
229
|
expect(await users.purge({ email: 'third@b.c' })).toBe(1)
|
|
189
230
|
expect(await users.count()).toBe(2)
|
|
190
231
|
})
|
|
@@ -193,15 +234,22 @@ describe('@owlmeans/postgres — resource CRUD against a real context', () => {
|
|
|
193
234
|
await expect(users.create({ id: '00000000-0000-4000-8000-000000000001', email: 'no@b.c' }))
|
|
194
235
|
.rejects.toThrow(RecordExists)
|
|
195
236
|
|
|
196
|
-
const inserted = await users.insert
|
|
237
|
+
const inserted = await users.insert({
|
|
197
238
|
id: '00000000-0000-4000-8000-000000000002', email: 'explicit@b.c'
|
|
198
239
|
})
|
|
199
240
|
expect(inserted.id).toBe('00000000-0000-4000-8000-000000000002')
|
|
200
241
|
await users.delete(inserted.id as string)
|
|
201
242
|
})
|
|
202
243
|
|
|
244
|
+
it('refuses a TTL rather than dropping it — Postgres has no row expiry', async () => {
|
|
245
|
+
await expect(users.create({ email: 'ttl@b.c' }, { ttl: 60 }))
|
|
246
|
+
.rejects.toThrow(UnsupportedArgumentError)
|
|
247
|
+
await expect(users.save({ id: seeded.id, email: 'a@b.c' }, { ttl: 60 }))
|
|
248
|
+
.rejects.toThrow(UnsupportedArgumentError)
|
|
249
|
+
})
|
|
250
|
+
|
|
203
251
|
it('upserts on an arbiter that is not the primary key', async () => {
|
|
204
|
-
const upserted = await users.upsert
|
|
252
|
+
const upserted = await users.upsert({ email: 'a@b.c', age: 44 }, ['email'])
|
|
205
253
|
|
|
206
254
|
expect(upserted.id).toBe(seeded.id as string)
|
|
207
255
|
expect(upserted.age).toBe(44)
|
|
@@ -212,16 +260,18 @@ describe('@owlmeans/postgres — resource CRUD against a real context', () => {
|
|
|
212
260
|
await expect(users.create({ email: 'a@b.c' })).rejects.toThrow(RecordExists)
|
|
213
261
|
})
|
|
214
262
|
|
|
215
|
-
it('
|
|
263
|
+
it('takes a record by deleting it, atomically', async () => {
|
|
216
264
|
const before = await users.count()
|
|
217
265
|
expect(await posts.count()).toBe(1)
|
|
218
266
|
|
|
219
|
-
const
|
|
267
|
+
const taken = await users.take(seeded.id as string)
|
|
220
268
|
|
|
221
|
-
expect(
|
|
269
|
+
expect(taken.email).toBe('a@b.c')
|
|
222
270
|
expect(await users.count()).toBe(before - 1)
|
|
223
271
|
/** The cascade is the database's, not the resource's — the post goes with its owner. */
|
|
224
272
|
expect(await posts.count()).toBe(0)
|
|
225
|
-
|
|
273
|
+
/** `delete` reports absence, `take` raises on it. */
|
|
274
|
+
expect(await users.delete(seeded.id as string)).toBeNull()
|
|
275
|
+
await expect(users.take(seeded.id as string)).rejects.toThrow(UnknownRecordError)
|
|
226
276
|
})
|
|
227
277
|
})
|
package/tests/custom-sql.spec.ts
CHANGED
|
@@ -140,12 +140,13 @@ describe('@owlmeans/postgres — custom SQL with alias placeholders', () => {
|
|
|
140
140
|
expect(raw?.email_address).toBe('jrr@shire.me')
|
|
141
141
|
expect(raw?.email).toBeUndefined()
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
/** The resource is typed once, at the call to `resource()` — never per method. */
|
|
144
|
+
const record = await authors.selectOne('SELECT * FROM {{}} WHERE "name" = $1', ['Tolkien'])
|
|
144
145
|
expect(record?.email).toBe('jrr@shire.me')
|
|
145
146
|
expect(record?.id).toBe(tolkien.id as string)
|
|
146
147
|
|
|
147
148
|
/** jsonb comes back parsed, not as a string, on both routes. */
|
|
148
|
-
const [book] = await books.select
|
|
149
|
+
const [book] = await books.select(`SELECT * FROM {{}} WHERE "title" = $1`, ['The Silmarillion'])
|
|
149
150
|
expect(book.meta).toEqual({ posthumous: true })
|
|
150
151
|
})
|
|
151
152
|
|