@ingram-tech/nk-db 2.1.0 → 2.1.2

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.
Files changed (2) hide show
  1. package/README.md +29 -32
  2. package/package.json +7 -7
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # @ingram-tech/nk-db
2
2
 
3
- The Ingram **Postgres data layer**: one TLS-aware `pg` pool, raw-SQL helpers,
4
- Drizzle wiring, a drift-aware migration runner, and a **PGlite** (no-Docker)
5
- dev/test harness. Design + rationale:
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 **peer dependencies** (one copy in the app).
9
- `@electric-sql/pglite` + `@electric-sql/pglite-socket` are **optional** peers —
10
- add them as `devDependencies` only if you use `nk dev` / the test harness.
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 — local detection also wins over a pulled
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 — exactly one pool per process.
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,22 @@ 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 — Postgres
57
- functions (`select fn($1,…)`), `pgmq` draining, `pg_trgm`. Signatures match the
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 — keep money math on the decimal value. For string
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 — nothing populates the
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` **per transaction**, so your **existing RLS policies keep working
73
- unchanged**, wherever the cluster lives. It's pure Postgres and behaves
74
- identically everywhere.
71
+ ROLE` per transaction, so existing RLS policies keep working unchanged.
75
72
 
76
73
  Claims are normalised at this boundary: any top-level string claim that is a
77
74
  public prefixed id (`sub: "usr_…"`, `org_id: "org_…"`) is decoded to its uuid
@@ -92,9 +89,9 @@ const notes = await withRlsTransaction(db, { sub: session.user.id }, (tx) =>
92
89
  );
93
90
  ```
94
91
 
95
- The claims come **straight from the Better Auth session** (`sub` = `user.id`) — no
96
- JWT minting, no JWKS issuer, no third-party auth bridge. The raw helpers expose the same
97
- thing as `withRls` (sibling of `withTx`):
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`):
98
95
 
99
96
  ```ts
100
97
  const { withRls } = createQueries(pool);
@@ -103,9 +100,9 @@ const rows = await withRls({ sub: userId }, (tx) =>
103
100
  );
104
101
  ```
105
102
 
106
- Two requirements **you** own (they can't be enforced from the library):
103
+ Two requirements the library can't enforce for you:
107
104
 
108
- - **Connect as a role that doesn't bypass RLS** for user-facing rows — not the
105
+ - **Connect as a role that doesn't bypass RLS** for user-facing rows: not the
109
106
  table owner, not a `BYPASSRLS` superuser. After `SET ROLE authenticated`, RLS
110
107
  applies even when the underlying connection is a superuser. Service-role/admin
111
108
  paths keep using plain `db` / `query` and bypass RLS as before.
@@ -114,15 +111,15 @@ Two requirements **you** own (they can't be enforced from the library):
114
111
 
115
112
  Override the role / claims GUC when your DB role name differs from the JWT claim:
116
113
  `withRlsTransaction(db, { sub }, fn, { role: "app_user" })`. Both helpers set the
117
- GUCs **transaction-locally** (`is_local = true`), so they reset at
114
+ GUCs transaction-locally (`is_local = true`), so they reset at
118
115
  commit/rollback and never leak across pooled connections. See
119
116
  [`docs/db-package.md` §RLS](https://github.com/ingram-technologies/nextkit/blob/main/docs/db-package.md)
120
117
  and the [`@ingram-tech/nk-auth`](https://github.com/ingram-technologies/nextkit/blob/main/packages/nk-auth) README.
121
118
 
122
119
  ## Migrations (`@ingram-tech/nk-db/migrate`, `nk-pg-migrate`)
123
120
 
124
- A drop-in replacement for `drizzle-kit migrate` in the site's `db:migrate`
125
- script (you still *generate* migrations with `drizzle-kit generate`):
121
+ A drop-in replacement for `drizzle-kit migrate` in your `db:migrate` script
122
+ (you still *generate* migrations with `drizzle-kit generate`):
126
123
 
127
124
  ```bash
128
125
  nk-pg-migrate # apply pending migrations
@@ -215,22 +212,22 @@ row.account_id; // "acct_…"
215
212
  `polymorphicIdColumn` (an `entity_id` whose target is named by a sibling
216
213
  `*_type` column) decodes any registered entity's id on the way in, but reads
217
214
  stay raw: a column cannot see its sibling to know which prefix to put back.
218
- Encode it in the query once the type is known — `encodedId("account",
219
- invoices.entity_id)` — or with `entityOf` in code.
215
+ Encode it in the query once the type is known (`encodedId("account",
216
+ invoices.entity_id)`), or with `entityOf` in code.
220
217
 
221
218
  Raw SQL bypasses the column layer. Bind an incoming id with `sqlUuid(id)` /
222
219
  `sqlUuidArray(ids)` (decoded in JS, nothing needed in the database), and select
223
- a uuid as a public id with `encodedId(entity, column)` — which emits
220
+ a uuid as a public id with `encodedId(entity, column)`, which emits
224
221
  `id758_encode(…)` and therefore needs the functions below.
225
222
 
226
- This subpath pulls only `drizzle-orm` and the codec — never `pg` — because it is
223
+ This subpath pulls only `drizzle-orm` and the codec, never `pg`, because it is
227
224
  imported by `schema.ts`.
228
225
 
229
226
  ### The codec in Postgres (`@ingram-tech/nk-db/id/sql`)
230
227
 
231
228
  `id758` ships the codec as plain plpgsql (`IMMUTABLE STRICT PARALLEL SAFE`, no
232
229
  extensions, Postgres 14+), so the database itself accepts and produces either
233
- form — in `psql`, in `createQueries` raw SQL, in an RPC, in an RLS policy:
230
+ form: in `psql`, in `createQueries` raw SQL, in an RPC, in an RLS policy:
234
231
 
235
232
  ```sql
236
233
  select * from invoices where id = id758_decode('inv_1CGtMb233ezidDvSwDLNBn');
@@ -246,7 +243,7 @@ id758_decode($1)` uses the ordinary primary-key index.
246
243
  The functions are text, not a migration chain: the codec is frozen, so there is
247
244
  never a second version to journal, and the script is `create or replace`. The
248
245
  PGlite harness installs them on every boot (`id758: false` opts out), so dev and
249
- test have them for free. Production gets them once, through the site's own
246
+ test have them for free. Production gets them once, through your own
250
247
  migrations:
251
248
 
252
249
  ```sh
@@ -256,8 +253,8 @@ bunx drizzle-kit generate --custom --name id758
256
253
  and paste `id758Migration` (from `@ingram-tech/nk-db/id/sql`, the statements
257
254
  joined by `--> statement-breakpoint`) into the generated file. Or, outside any
258
255
  journal: `bunx id758 sql | psql "$DATABASE_URL"`. The `id758` bin also converts
259
- at the shell — `bunx id758 decode inv_…` — for the moments a `psql` session
260
- without the functions hands you the other form.
256
+ at the shell (`bunx id758 decode inv_…`), for a `psql` session that doesn't have
257
+ the functions installed.
261
258
 
262
259
  ## PGlite dev & test (`@ingram-tech/nk-db/pglite`)
263
260
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ingram-tech/nk-db",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
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",
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "id758": "^1.1.0",
62
- "zod": "^4.4.3"
62
+ "zod": "^4.5.4"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "@electric-sql/pglite": ">=0.5.0",
@@ -76,14 +76,14 @@
76
76
  }
77
77
  },
78
78
  "devDependencies": {
79
- "@electric-sql/pglite": "^0.5.5",
80
- "@electric-sql/pglite-socket": "^0.2.8",
81
- "@ingram-tech/nk-dev": "0.12.2",
82
- "@types/node": "^26.2.0",
79
+ "@electric-sql/pglite": "^0.5.8",
80
+ "@electric-sql/pglite-socket": "^0.2.11",
81
+ "@ingram-tech/nk-dev": "0.14.1",
82
+ "@types/node": "^26.4.0",
83
83
  "@types/pg": "^8.23.1",
84
84
  "drizzle-orm": "^0.45.2",
85
85
  "pg": "^8.23.0",
86
86
  "typescript": "^7.0.2",
87
- "vitest": "^4.1.10"
87
+ "vitest": "^4.1.11"
88
88
  }
89
89
  }