@ingram-tech/nk-db 2.0.1 → 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 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
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()`. For policies that cast
28
- * `sub` to `uuid`, this MUST be a valid UUID.
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;AAEH;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACzB;;;OAGG;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;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,WACf,iBAAiB,KACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAGjC,CAAC"}
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;AAgCH,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;IAC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E,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.0.1",
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.1",
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/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()`. For policies that cast
29
- * `sub` to `uuid`, this MUST be a valid UUID.
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`,