@ingram-tech/nk-db 2.0.1 → 2.1.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/README.md +36 -32
- package/dist/rls.d.ts +13 -2
- package/dist/rls.d.ts.map +1 -1
- package/dist/rls.js +20 -1
- package/dist/rls.js.map +1 -1
- package/package.json +2 -2
- package/src/rls.ts +25 -3
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# @ingram-tech/nk-db
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
A Postgres data layer: one TLS-aware `pg` pool, raw-SQL helpers, Drizzle
|
|
4
|
+
wiring, a drift-aware migration runner, and a PGlite (no-Docker) dev/test
|
|
5
|
+
harness. Design + rationale:
|
|
6
6
|
[`docs/db-package.md`](https://github.com/ingram-technologies/nextkit/blob/main/docs/db-package.md).
|
|
7
7
|
|
|
8
|
-
`pg` and `drizzle-orm` are
|
|
9
|
-
`@electric-sql/pglite` + `@electric-sql/pglite-socket` are
|
|
10
|
-
|
|
8
|
+
`pg` and `drizzle-orm` are peer dependencies (one copy in the app).
|
|
9
|
+
`@electric-sql/pglite` + `@electric-sql/pglite-socket` are optional peers: add
|
|
10
|
+
them as `devDependencies` only if you use `nk dev` / the test harness.
|
|
11
11
|
|
|
12
12
|
## Install
|
|
13
13
|
|
|
@@ -28,7 +28,7 @@ DATABASE_POOL_MAX=5 # optional; keep small on serverless
|
|
|
28
28
|
|
|
29
29
|
TLS is determined by the connection string and the CA cert, never by a flag: a
|
|
30
30
|
local host (`127.0.0.1`/`localhost`) gets no TLS and a `max: 1` pool (the
|
|
31
|
-
PGlite socket is single-connection
|
|
31
|
+
PGlite socket is single-connection; local detection also wins over a pulled
|
|
32
32
|
`DATABASE_POOL_MAX`/`DATABASE_CA_CERT`); with `DATABASE_CA_CERT` set the server
|
|
33
33
|
cert + hostname are verified; otherwise TLS runs without chain verification
|
|
34
34
|
(managed-provider certs aren't in Node's trust store).
|
|
@@ -36,7 +36,7 @@ cert + hostname are verified; otherwise TLS runs without chain verification
|
|
|
36
36
|
## The one barrel (`src/lib/db.ts`)
|
|
37
37
|
|
|
38
38
|
Create the pool once and share it across Drizzle, the raw helpers, and Better
|
|
39
|
-
Auth
|
|
39
|
+
Auth: exactly one pool per process.
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
42
|
import { createDb, createPool, createQueries } from "@ingram-tech/nk-db";
|
|
@@ -53,25 +53,29 @@ same pool: `betterAuth({ database: pool, … })`.
|
|
|
53
53
|
|
|
54
54
|
- **Drizzle** is the default: schema-first, `drizzle-kit` generates migrations
|
|
55
55
|
into `drizzle/`.
|
|
56
|
-
- **Raw helpers** (`createQueries(pool)`) are the escape hatch
|
|
57
|
-
functions (`select fn($1,…)`), `pgmq` draining, `pg_trgm`.
|
|
58
|
-
hand-rolled originals, so adopting is a find-and-replace of the import.
|
|
56
|
+
- **Raw helpers** (`createQueries(pool)`) are the escape hatch: Postgres
|
|
57
|
+
functions (`select fn($1,…)`), `pgmq` draining, `pg_trgm`.
|
|
59
58
|
- **`pgTimestampToIso(value)` / `pgNumericToNumber(value)`** — response-boundary
|
|
60
59
|
coercions for strict schemas. `pg`/Drizzle return `numeric` as a string and
|
|
61
60
|
`timestamp(..., { mode: "string" })` as Postgres' text form; these convert to
|
|
62
61
|
the `z.number()` / strict `z.iso.datetime()` shapes such schemas expect.
|
|
63
62
|
Offset-less timestamps (a `timestamp` *without* time zone column) are treated
|
|
64
|
-
as UTC. Presentation only
|
|
63
|
+
as UTC. Presentation only: keep money math on the decimal value. For string
|
|
65
64
|
timestamps prefer Drizzle's `timestamp(..., { mode: "string" })` per column.
|
|
66
65
|
|
|
67
66
|
## Keeping RLS on a direct connection (`withRls` / `withRlsTransaction`)
|
|
68
67
|
|
|
69
68
|
A plain `pg`/Drizzle connection runs as the connection's role with **no request
|
|
70
|
-
claims**, so `auth.uid()` policies can't fire
|
|
69
|
+
claims**, so `auth.uid()` policies can't fire: nothing populates the
|
|
71
70
|
`request.jwt.claims` they read. These helpers set the claims GUC + `SET LOCAL
|
|
72
|
-
ROLE`
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
ROLE` per transaction, so existing RLS policies keep working unchanged.
|
|
72
|
+
|
|
73
|
+
Claims are normalised at this boundary: any top-level string claim that is a
|
|
74
|
+
public prefixed id (`sub: "usr_…"`, `org_id: "org_…"`) is decoded to its uuid
|
|
75
|
+
before it is written to `request.jwt.claims`, so a policy written as
|
|
76
|
+
`user_id = auth.uid()` holds whether the app passes the raw uuid or the public
|
|
77
|
+
form (which it does once session ids come through nk-auth's helpers with a
|
|
78
|
+
registry). A public id is self-describing, so this needs no registry.
|
|
75
79
|
|
|
76
80
|
```ts
|
|
77
81
|
import { withRlsTransaction } from "@ingram-tech/nk-db";
|
|
@@ -85,9 +89,9 @@ const notes = await withRlsTransaction(db, { sub: session.user.id }, (tx) =>
|
|
|
85
89
|
);
|
|
86
90
|
```
|
|
87
91
|
|
|
88
|
-
The claims come
|
|
89
|
-
JWT minting, no JWKS issuer
|
|
90
|
-
|
|
92
|
+
The claims come straight from the Better Auth session (`sub` = `user.id`): no
|
|
93
|
+
JWT minting, no JWKS issuer. The raw helpers expose the same thing as `withRls`
|
|
94
|
+
(sibling of `withTx`):
|
|
91
95
|
|
|
92
96
|
```ts
|
|
93
97
|
const { withRls } = createQueries(pool);
|
|
@@ -96,9 +100,9 @@ const rows = await withRls({ sub: userId }, (tx) =>
|
|
|
96
100
|
);
|
|
97
101
|
```
|
|
98
102
|
|
|
99
|
-
Two requirements
|
|
103
|
+
Two requirements the library can't enforce for you:
|
|
100
104
|
|
|
101
|
-
- **Connect as a role that doesn't bypass RLS** for user-facing rows
|
|
105
|
+
- **Connect as a role that doesn't bypass RLS** for user-facing rows: not the
|
|
102
106
|
table owner, not a `BYPASSRLS` superuser. After `SET ROLE authenticated`, RLS
|
|
103
107
|
applies even when the underlying connection is a superuser. Service-role/admin
|
|
104
108
|
paths keep using plain `db` / `query` and bypass RLS as before.
|
|
@@ -107,15 +111,15 @@ Two requirements **you** own (they can't be enforced from the library):
|
|
|
107
111
|
|
|
108
112
|
Override the role / claims GUC when your DB role name differs from the JWT claim:
|
|
109
113
|
`withRlsTransaction(db, { sub }, fn, { role: "app_user" })`. Both helpers set the
|
|
110
|
-
GUCs
|
|
114
|
+
GUCs transaction-locally (`is_local = true`), so they reset at
|
|
111
115
|
commit/rollback and never leak across pooled connections. See
|
|
112
116
|
[`docs/db-package.md` §RLS](https://github.com/ingram-technologies/nextkit/blob/main/docs/db-package.md)
|
|
113
117
|
and the [`@ingram-tech/nk-auth`](https://github.com/ingram-technologies/nextkit/blob/main/packages/nk-auth) README.
|
|
114
118
|
|
|
115
119
|
## Migrations (`@ingram-tech/nk-db/migrate`, `nk-pg-migrate`)
|
|
116
120
|
|
|
117
|
-
A drop-in replacement for `drizzle-kit migrate` in
|
|
118
|
-
|
|
121
|
+
A drop-in replacement for `drizzle-kit migrate` in your `db:migrate` script
|
|
122
|
+
(you still *generate* migrations with `drizzle-kit generate`):
|
|
119
123
|
|
|
120
124
|
```bash
|
|
121
125
|
nk-pg-migrate # apply pending migrations
|
|
@@ -208,22 +212,22 @@ row.account_id; // "acct_…"
|
|
|
208
212
|
`polymorphicIdColumn` (an `entity_id` whose target is named by a sibling
|
|
209
213
|
`*_type` column) decodes any registered entity's id on the way in, but reads
|
|
210
214
|
stay raw: a column cannot see its sibling to know which prefix to put back.
|
|
211
|
-
Encode it in the query once the type is known
|
|
212
|
-
invoices.entity_id)`
|
|
215
|
+
Encode it in the query once the type is known (`encodedId("account",
|
|
216
|
+
invoices.entity_id)`), or with `entityOf` in code.
|
|
213
217
|
|
|
214
218
|
Raw SQL bypasses the column layer. Bind an incoming id with `sqlUuid(id)` /
|
|
215
219
|
`sqlUuidArray(ids)` (decoded in JS, nothing needed in the database), and select
|
|
216
|
-
a uuid as a public id with `encodedId(entity, column)
|
|
220
|
+
a uuid as a public id with `encodedId(entity, column)`, which emits
|
|
217
221
|
`id758_encode(…)` and therefore needs the functions below.
|
|
218
222
|
|
|
219
|
-
This subpath pulls only `drizzle-orm` and the codec
|
|
223
|
+
This subpath pulls only `drizzle-orm` and the codec, never `pg`, because it is
|
|
220
224
|
imported by `schema.ts`.
|
|
221
225
|
|
|
222
226
|
### The codec in Postgres (`@ingram-tech/nk-db/id/sql`)
|
|
223
227
|
|
|
224
228
|
`id758` ships the codec as plain plpgsql (`IMMUTABLE STRICT PARALLEL SAFE`, no
|
|
225
229
|
extensions, Postgres 14+), so the database itself accepts and produces either
|
|
226
|
-
form
|
|
230
|
+
form: in `psql`, in `createQueries` raw SQL, in an RPC, in an RLS policy:
|
|
227
231
|
|
|
228
232
|
```sql
|
|
229
233
|
select * from invoices where id = id758_decode('inv_1CGtMb233ezidDvSwDLNBn');
|
|
@@ -239,7 +243,7 @@ id758_decode($1)` uses the ordinary primary-key index.
|
|
|
239
243
|
The functions are text, not a migration chain: the codec is frozen, so there is
|
|
240
244
|
never a second version to journal, and the script is `create or replace`. The
|
|
241
245
|
PGlite harness installs them on every boot (`id758: false` opts out), so dev and
|
|
242
|
-
test have them for free. Production gets them once, through
|
|
246
|
+
test have them for free. Production gets them once, through your own
|
|
243
247
|
migrations:
|
|
244
248
|
|
|
245
249
|
```sh
|
|
@@ -249,8 +253,8 @@ bunx drizzle-kit generate --custom --name id758
|
|
|
249
253
|
and paste `id758Migration` (from `@ingram-tech/nk-db/id/sql`, the statements
|
|
250
254
|
joined by `--> statement-breakpoint`) into the generated file. Or, outside any
|
|
251
255
|
journal: `bunx id758 sql | psql "$DATABASE_URL"`. The `id758` bin also converts
|
|
252
|
-
at the shell
|
|
253
|
-
|
|
256
|
+
at the shell (`bunx id758 decode inv_…`), for a `psql` session that doesn't have
|
|
257
|
+
the functions installed.
|
|
254
258
|
|
|
255
259
|
## PGlite dev & test (`@ingram-tech/nk-db/pglite`)
|
|
256
260
|
|
package/dist/rls.d.ts
CHANGED
|
@@ -24,8 +24,9 @@
|
|
|
24
24
|
*/
|
|
25
25
|
export interface RlsClaims {
|
|
26
26
|
/**
|
|
27
|
-
* The user id. Becomes the `sub` claim → `auth.uid()`.
|
|
28
|
-
* `
|
|
27
|
+
* The user id. Becomes the `sub` claim → `auth.uid()`. Either form: a public
|
|
28
|
+
* id (`usr_…`) is decoded to its uuid before it reaches the database (see
|
|
29
|
+
* {@link decodeIdClaims}), so `auth.uid()::uuid` policies hold.
|
|
29
30
|
*/
|
|
30
31
|
sub: string;
|
|
31
32
|
/** The Postgres role to assume (the JWT `role` claim). Defaults to `"authenticated"`. */
|
|
@@ -59,6 +60,16 @@ export interface ResolvedRlsConfig {
|
|
|
59
60
|
}
|
|
60
61
|
/** Resolve claims + options into the role, GUC name, and serialized claims. */
|
|
61
62
|
export declare const resolveRlsConfig: (claims: RlsClaims, options?: RlsOptions) => ResolvedRlsConfig;
|
|
63
|
+
/**
|
|
64
|
+
* The claims as the database expects them: every string claim that is a
|
|
65
|
+
* public, prefixed id (`usr_…`, `org_…`) is decoded to its uuid, so a policy
|
|
66
|
+
* written as `user_id = auth.uid()` keeps working when the app passes the
|
|
67
|
+
* public form — which it does once session ids come through nk-auth's
|
|
68
|
+
* helpers with a registry. A public id is self-describing, so no registry is
|
|
69
|
+
* needed here; a raw uuid or any other string passes through unchanged. This
|
|
70
|
+
* is the database boundary doing the conversion, the same rule as `idColumn`.
|
|
71
|
+
*/
|
|
72
|
+
export declare const decodeIdClaims: (claims: RlsClaims) => RlsClaims;
|
|
62
73
|
/**
|
|
63
74
|
* The single parameterized statement that scopes a transaction: writes the
|
|
64
75
|
* claims GUC and the `role` GUC, **both transaction-local** (`is_local = true`,
|
package/dist/rls.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rls.d.ts","sourceRoot":"","sources":["../src/rls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;
|
|
1
|
+
{"version":3,"file":"rls.d.ts","sourceRoot":"","sources":["../src/rls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACzB;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,yFAAyF;IACzF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uEAAuE;IACvE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED,iFAAiF;AACjF,MAAM,WAAW,UAAU;IAC1B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iFAAiF;IACjF,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,6EAA6E;AAC7E,eAAO,MAAM,gBAAgB,kBAAkB,CAAC;AAChD,sEAAsE;AACtE,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAEvD,wDAAwD;AACxD,MAAM,WAAW,iBAAiB;IACjC,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,yCAAyC;IACzC,aAAa,EAAE,MAAM,CAAC;IACtB,8DAA8D;IAC9D,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,WACpB,SAAS,YACR,UAAU,KACjB,iBAID,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,WAAY,SAAS,KAAG,SAQlD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,WACf,iBAAiB,KACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAGjC,CAAC"}
|
package/dist/rls.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* 2. The connecting role must be allowed to `SET ROLE` to the target role
|
|
17
17
|
* (e.g. `GRANT app_user TO the_connecting_role`).
|
|
18
18
|
*/
|
|
19
|
+
import { decodeId, prefixOf } from "./id.js";
|
|
19
20
|
/** The role assumed when neither `options.role` nor `claims.role` is set. */
|
|
20
21
|
export const RLS_DEFAULT_ROLE = "authenticated";
|
|
21
22
|
/** The GUC `auth.uid()` / `auth.role()` policies read claims from. */
|
|
@@ -24,8 +25,26 @@ export const RLS_CLAIMS_SETTING = "request.jwt.claims";
|
|
|
24
25
|
export const resolveRlsConfig = (claims, options = {}) => ({
|
|
25
26
|
role: options.role ?? claims.role ?? RLS_DEFAULT_ROLE,
|
|
26
27
|
claimsSetting: options.claimsSetting ?? RLS_CLAIMS_SETTING,
|
|
27
|
-
claimsJson: JSON.stringify(claims),
|
|
28
|
+
claimsJson: JSON.stringify(decodeIdClaims(claims)),
|
|
28
29
|
});
|
|
30
|
+
/**
|
|
31
|
+
* The claims as the database expects them: every string claim that is a
|
|
32
|
+
* public, prefixed id (`usr_…`, `org_…`) is decoded to its uuid, so a policy
|
|
33
|
+
* written as `user_id = auth.uid()` keeps working when the app passes the
|
|
34
|
+
* public form — which it does once session ids come through nk-auth's
|
|
35
|
+
* helpers with a registry. A public id is self-describing, so no registry is
|
|
36
|
+
* needed here; a raw uuid or any other string passes through unchanged. This
|
|
37
|
+
* is the database boundary doing the conversion, the same rule as `idColumn`.
|
|
38
|
+
*/
|
|
39
|
+
export const decodeIdClaims = (claims) => {
|
|
40
|
+
const out = { ...claims };
|
|
41
|
+
for (const [key, value] of Object.entries(claims)) {
|
|
42
|
+
if (typeof value === "string" && prefixOf(value) !== null) {
|
|
43
|
+
out[key] = decodeId(value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
};
|
|
29
48
|
/**
|
|
30
49
|
* The single parameterized statement that scopes a transaction: writes the
|
|
31
50
|
* claims GUC and the `role` GUC, **both transaction-local** (`is_local = true`,
|
package/dist/rls.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rls.js","sourceRoot":"","sources":["../src/rls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;
|
|
1
|
+
{"version":3,"file":"rls.js","sourceRoot":"","sources":["../src/rls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAiC7C,6EAA6E;AAC7E,MAAM,CAAC,MAAM,gBAAgB,GAAG,eAAe,CAAC;AAChD,sEAAsE;AACtE,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AAYvD,+EAA+E;AAC/E,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC/B,MAAiB,EACjB,OAAO,GAAe,EAAE,EACJ,EAAE,CAAC,CAAC;IACxB,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,IAAI,gBAAgB;IACrD,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,kBAAkB;IAC1D,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;CAClD,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,MAAiB,EAAa,EAAE;IAC9D,MAAM,GAAG,GAAc,EAAE,GAAG,MAAM,EAAE,CAAC;IACrC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;YAC3D,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC;IACF,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAC1B,MAAyB,EACY,EAAE,CAAC,CAAC;IACzC,IAAI,EAAE,+DAA+D;IACrE,MAAM,EAAE,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC;CAC9D,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ingram-tech/nk-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "The Ingram Postgres data layer: one TLS-aware pg pool, raw-SQL helpers, Drizzle wiring, and a PGlite (no-Docker) dev/test harness for Next.js sites.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"@electric-sql/pglite": "^0.5.5",
|
|
80
80
|
"@electric-sql/pglite-socket": "^0.2.8",
|
|
81
|
-
"@ingram-tech/nk-dev": "0.
|
|
81
|
+
"@ingram-tech/nk-dev": "0.13.1",
|
|
82
82
|
"@types/node": "^26.2.0",
|
|
83
83
|
"@types/pg": "^8.23.1",
|
|
84
84
|
"drizzle-orm": "^0.45.2",
|
package/src/rls.ts
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
* (e.g. `GRANT app_user TO the_connecting_role`).
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
+
import { decodeId, prefixOf } from "./id.js";
|
|
21
|
+
|
|
20
22
|
/**
|
|
21
23
|
* The JWT-style claims to scope a transaction by. `sub` becomes `auth.uid()`;
|
|
22
24
|
* `role` (default `"authenticated"`) is the Postgres role assumed. Any further
|
|
@@ -25,8 +27,9 @@
|
|
|
25
27
|
*/
|
|
26
28
|
export interface RlsClaims {
|
|
27
29
|
/**
|
|
28
|
-
* The user id. Becomes the `sub` claim → `auth.uid()`.
|
|
29
|
-
* `
|
|
30
|
+
* The user id. Becomes the `sub` claim → `auth.uid()`. Either form: a public
|
|
31
|
+
* id (`usr_…`) is decoded to its uuid before it reaches the database (see
|
|
32
|
+
* {@link decodeIdClaims}), so `auth.uid()::uuid` policies hold.
|
|
30
33
|
*/
|
|
31
34
|
sub: string;
|
|
32
35
|
/** The Postgres role to assume (the JWT `role` claim). Defaults to `"authenticated"`. */
|
|
@@ -69,9 +72,28 @@ export const resolveRlsConfig = (
|
|
|
69
72
|
): ResolvedRlsConfig => ({
|
|
70
73
|
role: options.role ?? claims.role ?? RLS_DEFAULT_ROLE,
|
|
71
74
|
claimsSetting: options.claimsSetting ?? RLS_CLAIMS_SETTING,
|
|
72
|
-
claimsJson: JSON.stringify(claims),
|
|
75
|
+
claimsJson: JSON.stringify(decodeIdClaims(claims)),
|
|
73
76
|
});
|
|
74
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The claims as the database expects them: every string claim that is a
|
|
80
|
+
* public, prefixed id (`usr_…`, `org_…`) is decoded to its uuid, so a policy
|
|
81
|
+
* written as `user_id = auth.uid()` keeps working when the app passes the
|
|
82
|
+
* public form — which it does once session ids come through nk-auth's
|
|
83
|
+
* helpers with a registry. A public id is self-describing, so no registry is
|
|
84
|
+
* needed here; a raw uuid or any other string passes through unchanged. This
|
|
85
|
+
* is the database boundary doing the conversion, the same rule as `idColumn`.
|
|
86
|
+
*/
|
|
87
|
+
export const decodeIdClaims = (claims: RlsClaims): RlsClaims => {
|
|
88
|
+
const out: RlsClaims = { ...claims };
|
|
89
|
+
for (const [key, value] of Object.entries(claims)) {
|
|
90
|
+
if (typeof value === "string" && prefixOf(value) !== null) {
|
|
91
|
+
out[key] = decodeId(value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
};
|
|
96
|
+
|
|
75
97
|
/**
|
|
76
98
|
* The single parameterized statement that scopes a transaction: writes the
|
|
77
99
|
* claims GUC and the `role` GUC, **both transaction-local** (`is_local = true`,
|