@ingram-tech/nk-db 2.0.0 → 2.1.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/README.md +15 -3
- package/dist/id-drizzle.d.ts +10 -5
- package/dist/id-drizzle.d.ts.map +1 -1
- package/dist/id-drizzle.js +1 -1
- package/dist/id-drizzle.js.map +1 -1
- 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/id-drizzle.ts +13 -10
- package/src/rls.ts +25 -3
package/README.md
CHANGED
|
@@ -73,6 +73,13 @@ ROLE` **per transaction**, so your **existing RLS policies keep working
|
|
|
73
73
|
unchanged**, wherever the cluster lives. It's pure Postgres and behaves
|
|
74
74
|
identically everywhere.
|
|
75
75
|
|
|
76
|
+
Claims are normalised at this boundary: any top-level string claim that is a
|
|
77
|
+
public prefixed id (`sub: "usr_…"`, `org_id: "org_…"`) is decoded to its uuid
|
|
78
|
+
before it is written to `request.jwt.claims`, so a policy written as
|
|
79
|
+
`user_id = auth.uid()` holds whether the app passes the raw uuid or the public
|
|
80
|
+
form (which it does once session ids come through nk-auth's helpers with a
|
|
81
|
+
registry). A public id is self-describing, so this needs no registry.
|
|
82
|
+
|
|
76
83
|
```ts
|
|
77
84
|
import { withRlsTransaction } from "@ingram-tech/nk-db";
|
|
78
85
|
import { auth } from "@/lib/auth"; // your Better Auth instance
|
|
@@ -172,7 +179,7 @@ straight through.
|
|
|
172
179
|
### Drizzle bindings (`@ingram-tech/nk-db/id/drizzle`)
|
|
173
180
|
|
|
174
181
|
A column declared with `idColumn` is a plain `uuid` in Postgres that speaks
|
|
175
|
-
public ids in TypeScript
|
|
182
|
+
public ids in TypeScript:
|
|
176
183
|
|
|
177
184
|
```ts
|
|
178
185
|
// ids.ts
|
|
@@ -182,8 +189,8 @@ export const { idColumn, polymorphicIdColumn, encodedId, sqlUuid, sqlUuidArray }
|
|
|
182
189
|
|
|
183
190
|
// schema.ts
|
|
184
191
|
export const invoices = pgTable("invoices", {
|
|
185
|
-
id: idColumn("invoice")().primaryKey().default(sql`uuidv7()`),
|
|
186
|
-
account_id: idColumn("account")().notNull(),
|
|
192
|
+
id: idColumn("invoice")().primaryKey().default(sql`uuidv7()`),
|
|
193
|
+
account_id: idColumn("account")().notNull(),
|
|
187
194
|
entity_id: polymorphicIdColumn(), // any entity, raw on read
|
|
188
195
|
});
|
|
189
196
|
|
|
@@ -199,6 +206,11 @@ row.account_id; // "acct_…"
|
|
|
199
206
|
relational `db.query`) and encodes it.
|
|
200
207
|
- `dataType` stays `uuid`, so there is **no DDL and no migration** and
|
|
201
208
|
`drizzle-kit generate` reports no diff.
|
|
209
|
+
- The column is typed `string`, not `Id<E>`: Drizzle has one type for both
|
|
210
|
+
directions and the write side accepts a raw uuid, so a branded type would
|
|
211
|
+
reject every database- or client-minted uuid at the insert. The read side is
|
|
212
|
+
a public id at runtime; brand it where it leaves the data layer
|
|
213
|
+
(`ids.invoice.is()`, a typed response schema).
|
|
202
214
|
|
|
203
215
|
`polymorphicIdColumn` (an `entity_id` whose target is named by a sibling
|
|
204
216
|
`*_type` column) decodes any registered entity's id on the way in, but reads
|
package/dist/id-drizzle.d.ts
CHANGED
|
@@ -19,9 +19,7 @@ import { type Id, type IdHelper } from "./id.js";
|
|
|
19
19
|
*/
|
|
20
20
|
type IdColumnBindings<K extends string> = {
|
|
21
21
|
/**
|
|
22
|
-
* A `uuid` column that speaks public ids.
|
|
23
|
-
* entity's branded `Id<E>`, so an invoice id can't be handed to an account
|
|
24
|
-
* column.
|
|
22
|
+
* A `uuid` column that speaks public ids.
|
|
25
23
|
*
|
|
26
24
|
* - `toDriver` (insert/update `SET` values **and** WHERE-clause comparison
|
|
27
25
|
* values: `eq`, `inArray`, …) decodes the public id to its uuid. Tolerant:
|
|
@@ -30,10 +28,17 @@ type IdColumnBindings<K extends string> = {
|
|
|
30
28
|
* - `fromDriver` (every selected value, including `returning()` and the
|
|
31
29
|
* relational `db.query`) encodes the uuid, so a read yields `inv_…`.
|
|
32
30
|
*
|
|
31
|
+
* Typed `string`, not `Id<E>`: Drizzle's `customType` has one `data` type
|
|
32
|
+
* for both directions, and the write side genuinely accepts a raw uuid, so
|
|
33
|
+
* a branded read type would make every database-minted or client-minted
|
|
34
|
+
* uuid a type error at the insert. The read-side guarantee is a runtime one
|
|
35
|
+
* (a selected value is always the public id); brand it at the seam where
|
|
36
|
+
* it leaves the data layer (`ids.x.is()`, or a typed response schema).
|
|
37
|
+
*
|
|
33
38
|
* `dataType` is unchanged (`uuid`), so this is a TypeScript-only mapping: no
|
|
34
39
|
* DDL, no migration, and `drizzle-kit generate` reports no schema diff.
|
|
35
40
|
*/
|
|
36
|
-
idColumn:
|
|
41
|
+
idColumn: (entity: K) => ReturnType<typeof customType<IdColumnConfig<string>>>;
|
|
37
42
|
/**
|
|
38
43
|
* A `uuid` column for a **polymorphic** FK — an `entity_id` whose target is
|
|
39
44
|
* named by a sibling `*_type` column — where no single-entity codec applies.
|
|
@@ -88,7 +93,7 @@ type IdColumnConfig<TData extends string> = {
|
|
|
88
93
|
*
|
|
89
94
|
* // schema.ts
|
|
90
95
|
* export const invoices = pgTable("invoices", {
|
|
91
|
-
* id: idColumn("invoice")().primaryKey(), //
|
|
96
|
+
* id: idColumn("invoice")().primaryKey(), // "inv_…" in and out
|
|
92
97
|
* account_id: idColumn("account")().notNull(),
|
|
93
98
|
* });
|
|
94
99
|
* ```
|
package/dist/id-drizzle.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id-drizzle.d.ts","sourceRoot":"","sources":["../src/id-drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAO,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAe,KAAK,EAAE,EAAE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E,MAAM,CAAA;CAAE,CAAC;AAEhF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAC/C,QAAQ,EAAE,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAC3B,gBAAgB,CAAC,CAAC,CAAC,CA6BrB"}
|
package/dist/id-drizzle.js
CHANGED
|
@@ -12,7 +12,7 @@ import { decodeAnyId } from "./id.js";
|
|
|
12
12
|
*
|
|
13
13
|
* // schema.ts
|
|
14
14
|
* export const invoices = pgTable("invoices", {
|
|
15
|
-
* id: idColumn("invoice")().primaryKey(), //
|
|
15
|
+
* id: idColumn("invoice")().primaryKey(), // "inv_…" in and out
|
|
16
16
|
* account_id: idColumn("account")().notNull(),
|
|
17
17
|
* });
|
|
18
18
|
* ```
|
package/dist/id-drizzle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id-drizzle.js","sourceRoot":"","sources":["../src/id-drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,GAAG,EAAY,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,WAAW,EAA0B,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"id-drizzle.js","sourceRoot":"","sources":["../src/id-drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,GAAG,EAAY,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,WAAW,EAA0B,MAAM,SAAS,CAAC;AAmF9D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,eAAe,CAC9B,QAA6B;IAE7B,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAElE,MAAM,OAAO,GAAG,CAAC,KAAgC,EAAO,EAAE,CACzD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*/
|
|
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":["../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,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.0",
|
|
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.12.
|
|
81
|
+
"@ingram-tech/nk-dev": "0.12.2",
|
|
82
82
|
"@types/node": "^26.2.0",
|
|
83
83
|
"@types/pg": "^8.23.1",
|
|
84
84
|
"drizzle-orm": "^0.45.2",
|
package/src/id-drizzle.ts
CHANGED
|
@@ -21,9 +21,7 @@ import { decodeAnyId, type Id, type IdHelper } from "./id.js";
|
|
|
21
21
|
|
|
22
22
|
type IdColumnBindings<K extends string> = {
|
|
23
23
|
/**
|
|
24
|
-
* A `uuid` column that speaks public ids.
|
|
25
|
-
* entity's branded `Id<E>`, so an invoice id can't be handed to an account
|
|
26
|
-
* column.
|
|
24
|
+
* A `uuid` column that speaks public ids.
|
|
27
25
|
*
|
|
28
26
|
* - `toDriver` (insert/update `SET` values **and** WHERE-clause comparison
|
|
29
27
|
* values: `eq`, `inArray`, …) decodes the public id to its uuid. Tolerant:
|
|
@@ -32,12 +30,17 @@ type IdColumnBindings<K extends string> = {
|
|
|
32
30
|
* - `fromDriver` (every selected value, including `returning()` and the
|
|
33
31
|
* relational `db.query`) encodes the uuid, so a read yields `inv_…`.
|
|
34
32
|
*
|
|
33
|
+
* Typed `string`, not `Id<E>`: Drizzle's `customType` has one `data` type
|
|
34
|
+
* for both directions, and the write side genuinely accepts a raw uuid, so
|
|
35
|
+
* a branded read type would make every database-minted or client-minted
|
|
36
|
+
* uuid a type error at the insert. The read-side guarantee is a runtime one
|
|
37
|
+
* (a selected value is always the public id); brand it at the seam where
|
|
38
|
+
* it leaves the data layer (`ids.x.is()`, or a typed response schema).
|
|
39
|
+
*
|
|
35
40
|
* `dataType` is unchanged (`uuid`), so this is a TypeScript-only mapping: no
|
|
36
41
|
* DDL, no migration, and `drizzle-kit generate` reports no schema diff.
|
|
37
42
|
*/
|
|
38
|
-
idColumn: <
|
|
39
|
-
entity: E,
|
|
40
|
-
) => ReturnType<typeof customType<IdColumnConfig<Id<E>>>>;
|
|
43
|
+
idColumn: (entity: K) => ReturnType<typeof customType<IdColumnConfig<string>>>;
|
|
41
44
|
/**
|
|
42
45
|
* A `uuid` column for a **polymorphic** FK — an `entity_id` whose target is
|
|
43
46
|
* named by a sibling `*_type` column — where no single-entity codec applies.
|
|
@@ -91,7 +94,7 @@ type IdColumnConfig<TData extends string> = { data: TData; driverData: string };
|
|
|
91
94
|
*
|
|
92
95
|
* // schema.ts
|
|
93
96
|
* export const invoices = pgTable("invoices", {
|
|
94
|
-
* id: idColumn("invoice")().primaryKey(), //
|
|
97
|
+
* id: idColumn("invoice")().primaryKey(), // "inv_…" in and out
|
|
95
98
|
* account_id: idColumn("account")().notNull(),
|
|
96
99
|
* });
|
|
97
100
|
* ```
|
|
@@ -105,14 +108,14 @@ export function createIdColumns<K extends string>(
|
|
|
105
108
|
sql`${value == null ? null : decodeAny(value)}::uuid`;
|
|
106
109
|
|
|
107
110
|
return {
|
|
108
|
-
idColumn:
|
|
109
|
-
customType<IdColumnConfig<
|
|
111
|
+
idColumn: (entity) =>
|
|
112
|
+
customType<IdColumnConfig<string>>({
|
|
110
113
|
dataType: () => "uuid",
|
|
111
114
|
toDriver: (value) =>
|
|
112
115
|
typeof value === "string"
|
|
113
116
|
? (registry[entity].decodeOrNull(value) ?? value)
|
|
114
117
|
: value,
|
|
115
|
-
fromDriver: (value) => registry[entity].encode(value)
|
|
118
|
+
fromDriver: (value) => registry[entity].encode(value),
|
|
116
119
|
}),
|
|
117
120
|
polymorphicIdColumn: customType<IdColumnConfig<string>>({
|
|
118
121
|
dataType: () => "uuid",
|
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`,
|