@ingram-tech/nk-db 1.5.0 → 2.0.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 +81 -26
- package/dist/id-drizzle.d.ts +47 -31
- package/dist/id-drizzle.d.ts.map +1 -1
- package/dist/id-drizzle.js +5 -2
- package/dist/id-drizzle.js.map +1 -1
- package/dist/id-sql.d.ts +8 -0
- package/dist/id-sql.d.ts.map +1 -0
- package/dist/id-sql.js +31 -0
- package/dist/id-sql.js.map +1 -0
- package/dist/id.d.ts +18 -131
- package/dist/id.d.ts.map +1 -1
- package/dist/id.js +17 -200
- package/dist/id.js.map +1 -1
- package/dist/pglite/index.d.ts +8 -0
- package/dist/pglite/index.d.ts.map +1 -1
- package/dist/pglite/index.js +3 -0
- package/dist/pglite/index.js.map +1 -1
- package/package.json +16 -8
- package/src/coerce.ts +50 -0
- package/src/drizzle.ts +51 -0
- package/src/errors.ts +35 -0
- package/src/id-drizzle.ts +130 -0
- package/src/id-sql.ts +32 -0
- package/src/id.ts +21 -0
- package/src/index.ts +27 -0
- package/src/keys.ts +78 -0
- package/src/migrate-bin.ts +87 -0
- package/src/migrate.ts +454 -0
- package/src/pglite/dev.ts +63 -0
- package/src/pglite/index.ts +255 -0
- package/src/pglite/reset.ts +31 -0
- package/src/pool.ts +148 -0
- package/src/queries.ts +244 -0
- package/src/rls.ts +90 -0
package/src/rls.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row-Level Security for a **direct Postgres connection** (`pg` / Drizzle).
|
|
3
|
+
*
|
|
4
|
+
* A plain connection runs as the connection's role with **no request claims**, so
|
|
5
|
+
* RLS is either bypassed (privileged role) or denies everything. These helpers
|
|
6
|
+
* scope a transaction the way a JWT-claims RLS setup needs: per transaction they
|
|
7
|
+
* set the `request.jwt.claims` GUC and `SET LOCAL ROLE`, so policies written
|
|
8
|
+
* against `auth.uid()` (i.e. `current_setting('request.jwt.claims') ->> 'sub'`)
|
|
9
|
+
* fire correctly. It is pure Postgres. See `docs/db-package.md` (§RLS).
|
|
10
|
+
*
|
|
11
|
+
* Two requirements the caller owns (documented, not enforceable here):
|
|
12
|
+
* 1. The pool must connect as a role that **does not bypass RLS** for the rows
|
|
13
|
+
* it touches — i.e. not the table owner and not a `BYPASSRLS` superuser for
|
|
14
|
+
* user-facing reads. (After `SET ROLE authenticated`, RLS applies even if the
|
|
15
|
+
* underlying connection is a superuser.)
|
|
16
|
+
* 2. The connecting role must be allowed to `SET ROLE` to the target role
|
|
17
|
+
* (e.g. `GRANT app_user TO the_connecting_role`).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The JWT-style claims to scope a transaction by. `sub` becomes `auth.uid()`;
|
|
22
|
+
* `role` (default `"authenticated"`) is the Postgres role assumed. Any further
|
|
23
|
+
* keys are written into the claims GUC for policies that read them
|
|
24
|
+
* (`request.jwt.claims ->> 'org_id'`, etc.).
|
|
25
|
+
*/
|
|
26
|
+
export interface RlsClaims {
|
|
27
|
+
/**
|
|
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
|
+
*/
|
|
31
|
+
sub: string;
|
|
32
|
+
/** The Postgres role to assume (the JWT `role` claim). Defaults to `"authenticated"`. */
|
|
33
|
+
role?: string;
|
|
34
|
+
/** Any further claims your policies read from `request.jwt.claims`. */
|
|
35
|
+
[claim: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Overrides for {@link resolveRlsConfig} (and the helpers that build on it). */
|
|
39
|
+
export interface RlsOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Force the Postgres role to `SET LOCAL`, ignoring `claims.role`. Use when the
|
|
42
|
+
* DB role name differs from the JWT `role` claim (e.g. a dedicated `app_user`
|
|
43
|
+
* on DO while the claim stays `"authenticated"`).
|
|
44
|
+
*/
|
|
45
|
+
role?: string;
|
|
46
|
+
/** The GUC the claims JSON is written to. Defaults to `"request.jwt.claims"`. */
|
|
47
|
+
claimsSetting?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The role assumed when neither `options.role` nor `claims.role` is set. */
|
|
51
|
+
export const RLS_DEFAULT_ROLE = "authenticated";
|
|
52
|
+
/** The GUC `auth.uid()` / `auth.role()` policies read claims from. */
|
|
53
|
+
export const RLS_CLAIMS_SETTING = "request.jwt.claims";
|
|
54
|
+
|
|
55
|
+
/** The concrete values a transaction is scoped with. */
|
|
56
|
+
export interface ResolvedRlsConfig {
|
|
57
|
+
/** Postgres role to assume for the transaction. */
|
|
58
|
+
role: string;
|
|
59
|
+
/** GUC the claims JSON is written to. */
|
|
60
|
+
claimsSetting: string;
|
|
61
|
+
/** The claims serialized for `set_config` (a JSON string). */
|
|
62
|
+
claimsJson: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Resolve claims + options into the role, GUC name, and serialized claims. */
|
|
66
|
+
export const resolveRlsConfig = (
|
|
67
|
+
claims: RlsClaims,
|
|
68
|
+
options: RlsOptions = {},
|
|
69
|
+
): ResolvedRlsConfig => ({
|
|
70
|
+
role: options.role ?? claims.role ?? RLS_DEFAULT_ROLE,
|
|
71
|
+
claimsSetting: options.claimsSetting ?? RLS_CLAIMS_SETTING,
|
|
72
|
+
claimsJson: JSON.stringify(claims),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The single parameterized statement that scopes a transaction: writes the
|
|
77
|
+
* claims GUC and the `role` GUC, **both transaction-local** (`is_local = true`,
|
|
78
|
+
* so they reset at `commit`/`rollback` and never leak across pooled
|
|
79
|
+
* connections). Everything — including the GUC name and role — is bound, never
|
|
80
|
+
* interpolated, so it's injection-safe.
|
|
81
|
+
*
|
|
82
|
+
* Exposed for callers that manage their own connection/transaction (e.g. a
|
|
83
|
+
* framework middleware): run it as the first statement after `begin`.
|
|
84
|
+
*/
|
|
85
|
+
export const rlsPreamble = (
|
|
86
|
+
config: ResolvedRlsConfig,
|
|
87
|
+
): { text: string; values: string[] } => ({
|
|
88
|
+
text: "select set_config($1, $2, true), set_config('role', $3, true)",
|
|
89
|
+
values: [config.claimsSetting, config.claimsJson, config.role],
|
|
90
|
+
});
|