@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/README.md
CHANGED
|
@@ -144,54 +144,109 @@ The same surface is available programmatically:
|
|
|
144
144
|
|
|
145
145
|
## Prefixed ids (`@ingram-tech/nk-db/id`)
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
(
|
|
149
|
-
|
|
147
|
+
The codec is the standalone [`id758`](https://github.com/ingram-technologies/id758)
|
|
148
|
+
package: `uuidv7()`, `encodeId` / `decodeId` / `mintId`, and `createIdRegistry`
|
|
149
|
+
for typed per-entity helpers. This subpath re-exports all of it, plus the
|
|
150
|
+
pre-extraction names (`uuidGenerateId`, `toPrefixedId`, `fromPrefixedId`,
|
|
151
|
+
`base58Id`) as deprecated aliases, so either import path works.
|
|
150
152
|
|
|
151
|
-
**
|
|
153
|
+
**The module is isomorphic** (no imports, randomness from Web Crypto), so a
|
|
152
154
|
Drizzle `schema.ts`, a client component or an edge runtime can all use it. Keep
|
|
153
155
|
it that way: a single `node:crypto` import makes every module that touches an id
|
|
154
156
|
node-only (a test enforces this).
|
|
155
157
|
|
|
156
|
-
Store the raw `uuid
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
codec bugs permanently on disk.
|
|
158
|
+
Store the raw `uuid`; the prefixed id is presentation, not identity. Inside the
|
|
159
|
+
DB, "which entity is this?" is already carried by the column, so storing the
|
|
160
|
+
prefix duplicates schema metadata into every row.
|
|
160
161
|
|
|
161
|
-
**
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
**The rule: application code only ever sees public ids.** An id is `inv_…` in
|
|
163
|
+
a URL, a form, a log line, a row read through Drizzle and a value passed to a
|
|
164
|
+
query. The conversion to and from `uuid` happens in exactly two places — the
|
|
165
|
+
column (`idColumn`, below) and the database (`id758_encode` / `id758_decode`,
|
|
166
|
+
below) — and nowhere else. Code that calls `ids.invoice.decode(param)` before a
|
|
167
|
+
query, or `ids.invoice.encode(row.id)` after one, is converting at the wrong
|
|
168
|
+
layer; the `nextkit/no-id-codec-in-app-code` lint flags it. Validate untrusted
|
|
169
|
+
input with `ids.invoice.is(param)` (a 404 on a wrong-entity id) and pass it
|
|
170
|
+
straight through.
|
|
166
171
|
|
|
167
172
|
### Drizzle bindings (`@ingram-tech/nk-db/id/drizzle`)
|
|
168
173
|
|
|
169
|
-
|
|
170
|
-
|
|
174
|
+
A column declared with `idColumn` is a plain `uuid` in Postgres that speaks
|
|
175
|
+
public ids in TypeScript, typed as the entity's branded `Id<E>`:
|
|
171
176
|
|
|
172
177
|
```ts
|
|
178
|
+
// ids.ts
|
|
173
179
|
export const ids = createIdRegistry({ invoice: "inv", account: "acct" });
|
|
174
|
-
export const { idColumn, polymorphicIdColumn, sqlUuid, sqlUuidArray } =
|
|
180
|
+
export const { idColumn, polymorphicIdColumn, encodedId, sqlUuid, sqlUuidArray } =
|
|
175
181
|
createIdColumns(ids);
|
|
176
182
|
|
|
183
|
+
// schema.ts
|
|
177
184
|
export const invoices = pgTable("invoices", {
|
|
178
|
-
id: idColumn("invoice")().primaryKey(),
|
|
179
|
-
account_id: idColumn("account")().notNull(),
|
|
180
|
-
entity_id: polymorphicIdColumn(),
|
|
185
|
+
id: idColumn("invoice")().primaryKey().default(sql`uuidv7()`), // Id<"invoice">
|
|
186
|
+
account_id: idColumn("account")().notNull(), // Id<"account">
|
|
187
|
+
entity_id: polymorphicIdColumn(), // any entity, raw on read
|
|
181
188
|
});
|
|
182
|
-
```
|
|
183
189
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
const [row] = await db.select().from(invoices).where(eq(invoices.id, "inv_…"));
|
|
191
|
+
row.id; // "inv_…" — encoded on read
|
|
192
|
+
row.account_id; // "acct_…"
|
|
193
|
+
```
|
|
188
194
|
|
|
189
|
-
|
|
190
|
-
`
|
|
195
|
+
- `toDriver` runs on insert/update `SET` values **and** WHERE values (`eq`,
|
|
196
|
+
`inArray`, …) and decodes the public id; a raw uuid (a database default,
|
|
197
|
+
Better Auth's `generateId`) passes through.
|
|
198
|
+
- `fromDriver` runs on every selected value (`select`, `returning()`, the
|
|
199
|
+
relational `db.query`) and encodes it.
|
|
200
|
+
- `dataType` stays `uuid`, so there is **no DDL and no migration** and
|
|
201
|
+
`drizzle-kit generate` reports no diff.
|
|
202
|
+
|
|
203
|
+
`polymorphicIdColumn` (an `entity_id` whose target is named by a sibling
|
|
204
|
+
`*_type` column) decodes any registered entity's id on the way in, but reads
|
|
205
|
+
stay raw: a column cannot see its sibling to know which prefix to put back.
|
|
206
|
+
Encode it in the query once the type is known — `encodedId("account",
|
|
207
|
+
invoices.entity_id)` — or with `entityOf` in code.
|
|
208
|
+
|
|
209
|
+
Raw SQL bypasses the column layer. Bind an incoming id with `sqlUuid(id)` /
|
|
210
|
+
`sqlUuidArray(ids)` (decoded in JS, nothing needed in the database), and select
|
|
211
|
+
a uuid as a public id with `encodedId(entity, column)` — which emits
|
|
212
|
+
`id758_encode(…)` and therefore needs the functions below.
|
|
191
213
|
|
|
192
214
|
This subpath pulls only `drizzle-orm` and the codec — never `pg` — because it is
|
|
193
215
|
imported by `schema.ts`.
|
|
194
216
|
|
|
217
|
+
### The codec in Postgres (`@ingram-tech/nk-db/id/sql`)
|
|
218
|
+
|
|
219
|
+
`id758` ships the codec as plain plpgsql (`IMMUTABLE STRICT PARALLEL SAFE`, no
|
|
220
|
+
extensions, Postgres 14+), so the database itself accepts and produces either
|
|
221
|
+
form — in `psql`, in `createQueries` raw SQL, in an RPC, in an RLS policy:
|
|
222
|
+
|
|
223
|
+
```sql
|
|
224
|
+
select * from invoices where id = id758_decode('inv_1CGtMb233ezidDvSwDLNBn');
|
|
225
|
+
select id758_encode('inv', id) as id, total from invoices;
|
|
226
|
+
create policy … using (account_id = id758_decode(auth.uid()));
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`id758_decode(id)` validates the shape and accepts any prefix; `id758_decode(id,
|
|
230
|
+
prefix)` also requires that prefix; `id758_prefix(id)` names it. An immutable
|
|
231
|
+
call on a bound literal is folded at plan time, so `where id =
|
|
232
|
+
id758_decode($1)` uses the ordinary primary-key index.
|
|
233
|
+
|
|
234
|
+
The functions are text, not a migration chain: the codec is frozen, so there is
|
|
235
|
+
never a second version to journal, and the script is `create or replace`. The
|
|
236
|
+
PGlite harness installs them on every boot (`id758: false` opts out), so dev and
|
|
237
|
+
test have them for free. Production gets them once, through the site's own
|
|
238
|
+
migrations:
|
|
239
|
+
|
|
240
|
+
```sh
|
|
241
|
+
bunx drizzle-kit generate --custom --name id758
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
and paste `id758Migration` (from `@ingram-tech/nk-db/id/sql`, the statements
|
|
245
|
+
joined by `--> statement-breakpoint`) into the generated file. Or, outside any
|
|
246
|
+
journal: `bunx id758 sql | psql "$DATABASE_URL"`. The `id758` bin also converts
|
|
247
|
+
at the shell — `bunx id758 decode inv_…` — for the moments a `psql` session
|
|
248
|
+
without the functions hands you the other form.
|
|
249
|
+
|
|
195
250
|
## PGlite dev & test (`@ingram-tech/nk-db/pglite`)
|
|
196
251
|
|
|
197
252
|
`nk dev` runs the `nk-pglite-dev` bin automatically when this package is
|
package/dist/id-drizzle.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type SQL } from "drizzle-orm";
|
|
1
|
+
import { type AnyColumn, type SQL } from "drizzle-orm";
|
|
2
2
|
import { customType } from "drizzle-orm/pg-core";
|
|
3
|
-
import { type IdHelper } from "./id.js";
|
|
3
|
+
import { type Id, type IdHelper } from "./id.js";
|
|
4
4
|
/**
|
|
5
5
|
* Drizzle bindings for the id codec (`@ingram-tech/nk-db/id/drizzle`).
|
|
6
6
|
*
|
|
@@ -8,47 +8,62 @@ import { type IdHelper } from "./id.js";
|
|
|
8
8
|
* site's `schema.ts`, so it pulls only `drizzle-orm` and the (isomorphic) codec
|
|
9
9
|
* — never `pg`, the pool, or the RLS helpers that the root export carries.
|
|
10
10
|
*
|
|
11
|
-
* The
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* The invariant these bindings hold: **application code only ever sees public
|
|
12
|
+
* ids**. A column declared with `idColumn` decodes a public id to its raw uuid
|
|
13
|
+
* on the way *into* Postgres and encodes the uuid on the way *out*, so a row
|
|
14
|
+
* read through Drizzle carries `inv_…`, a `where eq(invoices.id, "inv_…")`
|
|
15
|
+
* just works, and nothing above the schema converts by hand. Raw SQL crosses
|
|
16
|
+
* the same boundary with `id758_encode` / `id758_decode` in the query (see
|
|
17
|
+
* `@ingram-tech/nk-db/id/sql`) or with {@link IdColumnBindings.encodedId} /
|
|
18
|
+
* {@link IdColumnBindings.sqlUuid} here.
|
|
15
19
|
*/
|
|
16
20
|
type IdColumnBindings<K extends string> = {
|
|
17
21
|
/**
|
|
18
|
-
* A `uuid` column that
|
|
19
|
-
*
|
|
22
|
+
* A `uuid` column that speaks public ids. Its TypeScript type is the
|
|
23
|
+
* entity's branded `Id<E>`, so an invoice id can't be handed to an account
|
|
24
|
+
* column.
|
|
20
25
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
26
|
+
* - `toDriver` (insert/update `SET` values **and** WHERE-clause comparison
|
|
27
|
+
* values: `eq`, `inArray`, …) decodes the public id to its uuid. Tolerant:
|
|
28
|
+
* a raw uuid passes through unchanged, so a value minted by the database
|
|
29
|
+
* or by Better Auth's `generateId` still inserts.
|
|
30
|
+
* - `fromDriver` (every selected value, including `returning()` and the
|
|
31
|
+
* relational `db.query`) encodes the uuid, so a read yields `inv_…`.
|
|
25
32
|
*
|
|
26
33
|
* `dataType` is unchanged (`uuid`), so this is a TypeScript-only mapping: no
|
|
27
34
|
* DDL, no migration, and `drizzle-kit generate` reports no schema diff.
|
|
28
|
-
*
|
|
29
|
-
* Decode is tolerant — a raw uuid or a non-id sentinel passes through, so
|
|
30
|
-
* adopting it never regresses a working call site. There is intentionally no
|
|
31
|
-
* `fromDriver`: reads stay raw uuids. Encoding is the caller's job, because
|
|
32
|
-
* (unlike decode) it cannot be inferred from the value — see `entityOf`.
|
|
33
35
|
*/
|
|
34
|
-
idColumn: (entity:
|
|
36
|
+
idColumn: <E extends K>(entity: E) => ReturnType<typeof customType<IdColumnConfig<Id<E>>>>;
|
|
35
37
|
/**
|
|
36
38
|
* A `uuid` column for a **polymorphic** FK — an `entity_id` whose target is
|
|
37
|
-
* named by a sibling `*_type` column — where no single-entity
|
|
38
|
-
* Decodes a
|
|
39
|
-
* self-describing)
|
|
39
|
+
* named by a sibling `*_type` column — where no single-entity codec applies.
|
|
40
|
+
* Decodes a public id of any entity in the registry (ids are
|
|
41
|
+
* self-describing) on the way in; reads stay raw uuids, because a column
|
|
42
|
+
* cannot see its sibling to know which prefix to put back. Encode such a
|
|
43
|
+
* value in the query with {@link IdColumnBindings.encodedId} once the type
|
|
44
|
+
* is known, or with `entityOf` in code.
|
|
40
45
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
46
|
+
* The same blindness means it accepts an id of the *wrong* entity. Where
|
|
47
|
+
* both halves are in hand, check them (`entityOf(registry, id) ===
|
|
48
|
+
* expectedType`) before writing, or the row is silently mislabelled.
|
|
49
|
+
*/
|
|
50
|
+
polymorphicIdColumn: ReturnType<typeof customType<IdColumnConfig<string>>>;
|
|
51
|
+
/**
|
|
52
|
+
* A raw-SQL selection of a uuid column or expression as the entity's public
|
|
53
|
+
* id, for the queries the column layer cannot reach (a `sql` select, an
|
|
54
|
+
* aggregate, a polymorphic column whose type this row has resolved):
|
|
55
|
+
* `db.select({ id: encodedId("invoice", invoices.id) })`. Emits
|
|
56
|
+
* `id758_encode('inv', …)`, so the database must have the functions
|
|
57
|
+
* (`@ingram-tech/nk-db/id/sql`).
|
|
44
58
|
*/
|
|
45
|
-
|
|
59
|
+
encodedId: <E extends K>(entity: E, column: AnyColumn | SQL) => SQL<Id<E>>;
|
|
46
60
|
/**
|
|
47
61
|
* Bind an id into raw SQL as a `uuid`. Raw SQL and RPC args bypass the column
|
|
48
62
|
* layer entirely, so this is the sanctioned binding for them:
|
|
49
63
|
* `sql\`select f(${sqlUuid(id)})\``. Needs no entity because ids are
|
|
50
|
-
* self-describing; a raw uuid
|
|
51
|
-
*
|
|
64
|
+
* self-describing; a raw uuid passes through, and null/undefined binds as
|
|
65
|
+
* `null::uuid`. Decodes in JS, so it needs nothing installed in the
|
|
66
|
+
* database — the SQL-side alternative is `id758_decode(${id})`.
|
|
52
67
|
*/
|
|
53
68
|
sqlUuid: (value: string | null | undefined) => SQL;
|
|
54
69
|
/**
|
|
@@ -58,8 +73,8 @@ type IdColumnBindings<K extends string> = {
|
|
|
58
73
|
*/
|
|
59
74
|
sqlUuidArray: (values: readonly string[]) => SQL;
|
|
60
75
|
};
|
|
61
|
-
type IdColumnConfig = {
|
|
62
|
-
data:
|
|
76
|
+
type IdColumnConfig<TData extends string> = {
|
|
77
|
+
data: TData;
|
|
63
78
|
driverData: string;
|
|
64
79
|
};
|
|
65
80
|
/**
|
|
@@ -68,11 +83,12 @@ type IdColumnConfig = {
|
|
|
68
83
|
* ```ts
|
|
69
84
|
* // ids.ts
|
|
70
85
|
* export const ids = createIdRegistry({ invoice: "inv", account: "acct" });
|
|
71
|
-
* export const { idColumn, polymorphicIdColumn, sqlUuid } =
|
|
86
|
+
* export const { idColumn, polymorphicIdColumn, encodedId, sqlUuid } =
|
|
87
|
+
* createIdColumns(ids);
|
|
72
88
|
*
|
|
73
89
|
* // schema.ts
|
|
74
90
|
* export const invoices = pgTable("invoices", {
|
|
75
|
-
* id: idColumn("invoice")().primaryKey(),
|
|
91
|
+
* id: idColumn("invoice")().primaryKey(), // Id<"invoice">, "inv_…" in and out
|
|
76
92
|
* account_id: idColumn("account")().notNull(),
|
|
77
93
|
* });
|
|
78
94
|
* ```
|
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,EAAO,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;
|
|
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,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE9D;;;;;;;;;;;;;;;GAeG;AAEH,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;IACzC;;;;;;;;;;;;;;OAcG;IACH,QAAQ,EAAE,CAAC,CAAC,SAAS,CAAC,EACrB,MAAM,EAAE,CAAC,KACL,UAAU,CAAC,OAAO,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D;;;;;;;;;;;;OAYG;IACH,mBAAmB,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC3E;;;;;;;OAOG;IACH,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,GAAG,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3E;;;;;;;OAOG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,KAAK,GAAG,CAAC;IACnD;;;;OAIG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,KAAK,GAAG,CAAC;CACjD,CAAC;AAEF,KAAK,cAAc,CAAC,KAAK,SAAS,MAAM,IAAI;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,UAAU,EAAE,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
|
@@ -7,11 +7,12 @@ import { decodeAnyId } from "./id.js";
|
|
|
7
7
|
* ```ts
|
|
8
8
|
* // ids.ts
|
|
9
9
|
* export const ids = createIdRegistry({ invoice: "inv", account: "acct" });
|
|
10
|
-
* export const { idColumn, polymorphicIdColumn, sqlUuid } =
|
|
10
|
+
* export const { idColumn, polymorphicIdColumn, encodedId, sqlUuid } =
|
|
11
|
+
* createIdColumns(ids);
|
|
11
12
|
*
|
|
12
13
|
* // schema.ts
|
|
13
14
|
* export const invoices = pgTable("invoices", {
|
|
14
|
-
* id: idColumn("invoice")().primaryKey(),
|
|
15
|
+
* id: idColumn("invoice")().primaryKey(), // Id<"invoice">, "inv_…" in and out
|
|
15
16
|
* account_id: idColumn("account")().notNull(),
|
|
16
17
|
* });
|
|
17
18
|
* ```
|
|
@@ -25,11 +26,13 @@ export function createIdColumns(registry) {
|
|
|
25
26
|
toDriver: (value) => typeof value === "string"
|
|
26
27
|
? (registry[entity].decodeOrNull(value) ?? value)
|
|
27
28
|
: value,
|
|
29
|
+
fromDriver: (value) => registry[entity].encode(value),
|
|
28
30
|
}),
|
|
29
31
|
polymorphicIdColumn: customType({
|
|
30
32
|
dataType: () => "uuid",
|
|
31
33
|
toDriver: (value) => (typeof value === "string" ? decodeAny(value) : value),
|
|
32
34
|
}),
|
|
35
|
+
encodedId: (entity, column) => sql `id758_encode(${registry[entity].prefix}, ${column})`,
|
|
33
36
|
sqlUuid,
|
|
34
37
|
sqlUuidArray: (values) => sql `array[${sql.join(values.map((value) => sqlUuid(value)), sql `, `)}]::uuid[]`,
|
|
35
38
|
};
|
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,
|
|
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;AAgF9D;;;;;;;;;;;;;;;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,GAAG,CAAA,GAAG,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC;IAEvD,OAAO;QACN,QAAQ,EAAE,CAAc,MAAS,EAAE,EAAE,CACpC,UAAU,CAAwB;YACjC,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM;YACtB,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CACnB,OAAO,KAAK,KAAK,QAAQ;gBACxB,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;gBACjD,CAAC,CAAC,KAAK;YACT,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAU;SAC9D,CAAC;QACH,mBAAmB,EAAE,UAAU,CAAyB;YACvD,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM;YACtB,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;SAC3E,CAAC;QACF,SAAS,EAAE,CAAc,MAAS,EAAE,MAAuB,EAAE,EAAE,CAC9D,GAAG,CAAO,gBAAgB,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,MAAM,GAAG;QAChE,OAAO;QACP,YAAY,EAAE,CAAC,MAAM,EAAE,EAAE,CACxB,GAAG,CAAA,SAAS,GAAG,CAAC,IAAI,CACnB,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EACrC,GAAG,CAAA,IAAI,CACP,WAAW;KACb,CAAC;AACH,CAAC"}
|
package/dist/id-sql.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ID758_SQL, ID758_SQL_STATEMENTS } from "id758/sql";
|
|
2
|
+
/**
|
|
3
|
+
* The functions as the body of a Drizzle migration file: one statement per
|
|
4
|
+
* `--> statement-breakpoint`, which the migrator needs to run a multi-statement
|
|
5
|
+
* file (it sends each chunk as its own query).
|
|
6
|
+
*/
|
|
7
|
+
export declare const id758Migration: string;
|
|
8
|
+
//# sourceMappingURL=id-sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-sql.d.ts","sourceRoot":"","sources":["../src/id-sql.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAE5D;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAmE,CAAC"}
|
package/dist/id-sql.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@ingram-tech/nk-db/id/sql` — the id codec as Postgres functions, from
|
|
3
|
+
* `id758/sql`, plus the form a site pastes into a Drizzle migration.
|
|
4
|
+
*
|
|
5
|
+
* The functions (`id758_encode`, `id758_decode`, `id758_prefix`) let the
|
|
6
|
+
* database speak both forms of an id, so raw SQL, `psql` and RPCs need no
|
|
7
|
+
* conversion in application code:
|
|
8
|
+
*
|
|
9
|
+
* select * from invoices where id = id758_decode('inv_…');
|
|
10
|
+
* select id758_encode('inv', id) as id from invoices;
|
|
11
|
+
*
|
|
12
|
+
* They ship as text, not as a migration chain: the codec is frozen, so there
|
|
13
|
+
* is never a second version to journal, and the script is `create or replace`
|
|
14
|
+
* (idempotent). The PGlite harness applies it on every boot, so dev and test
|
|
15
|
+
* have the functions for free; production gets them once, through the site's
|
|
16
|
+
* own migrations:
|
|
17
|
+
*
|
|
18
|
+
* bunx drizzle-kit generate --custom --name id758
|
|
19
|
+
* # paste `id758Migration` into the generated drizzle/NNNN_id758.sql
|
|
20
|
+
*
|
|
21
|
+
* Or on the fly, outside any journal: `bunx id758 sql | psql "$DATABASE_URL"`.
|
|
22
|
+
*/
|
|
23
|
+
import { ID758_SQL_STATEMENTS } from "id758/sql";
|
|
24
|
+
export { ID758_SQL, ID758_SQL_STATEMENTS } from "id758/sql";
|
|
25
|
+
/**
|
|
26
|
+
* The functions as the body of a Drizzle migration file: one statement per
|
|
27
|
+
* `--> statement-breakpoint`, which the migrator needs to run a multi-statement
|
|
28
|
+
* file (it sends each chunk as its own query).
|
|
29
|
+
*/
|
|
30
|
+
export const id758Migration = `${ID758_SQL_STATEMENTS.join("\n--> statement-breakpoint\n")}\n`;
|
|
31
|
+
//# sourceMappingURL=id-sql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-sql.js","sourceRoot":"","sources":["../src/id-sql.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEjD,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAE5D;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,oBAAoB,CAAC,IAAI,CAAC,8BAA8B,CAAC,IAAI,CAAC"}
|
package/dist/id.d.ts
CHANGED
|
@@ -1,134 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* (
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* `@ingram-tech/nk-db/id` — the fleet's id codec, now published standalone as
|
|
3
|
+
* [`id758`](https://github.com/ingram-technologies/id758). This subpath stays
|
|
4
|
+
* as the nextkit-facing entry point: it re-exports the whole `id758` surface
|
|
5
|
+
* plus the names nextkit sites adopted before the extraction, so nothing that
|
|
6
|
+
* imports from here needs to change. New code may import `id758` directly.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* the vectors in lockstep.
|
|
21
|
-
*
|
|
22
|
-
* Store the hyphenated UUIDv7 at rest (uuid columns stay native and time-ordered
|
|
23
|
-
* for index locality) and use {@link toPrefixedId} to skin it as a prefixed
|
|
24
|
-
* base58 id for the wire / display, {@link fromPrefixedId} to recover it.
|
|
25
|
-
* {@link base58Id} mints a fresh one directly; {@link createIdRegistry} builds a
|
|
26
|
-
* typed, prefix-validated set of helpers for a project's entities.
|
|
27
|
-
*/
|
|
28
|
-
/**
|
|
29
|
-
* Mint a **UUIDv7** (RFC 9562): a 48-bit Unix-ms timestamp prefix + random tail,
|
|
30
|
-
* version `7`, variant `10`. UUID-shaped (drops straight into a `uuid` column)
|
|
31
|
-
* while staying time-ordered for index locality. Used as Better Auth's
|
|
32
|
-
* `advanced.database.generateId`. Node/Bun's `randomUUID` is v4-only, so we lay
|
|
33
|
-
* the bytes out by hand.
|
|
34
|
-
*/
|
|
35
|
-
export declare const uuidGenerateId: () => Uuid;
|
|
36
|
-
/** Skin a stored hyphenated UUIDv7 as a prefixed base58 id, e.g. `team_…`. */
|
|
37
|
-
export declare function toPrefixedId(uuid: string, prefix: string): string;
|
|
38
|
-
/** Inverse of {@link toPrefixedId}: recover the hyphenated UUIDv7. */
|
|
39
|
-
export declare function fromPrefixedId(id: string): string;
|
|
40
|
-
/** Mint a fresh prefixed base58 id (UUIDv7 core), for text-id sites / API parity. */
|
|
41
|
-
export declare function base58Id(prefix: string): string;
|
|
42
|
-
declare const ID_BRAND: unique symbol;
|
|
43
|
-
declare const UUID_BRAND: unique symbol;
|
|
44
|
-
/**
|
|
45
|
-
* A prefixed base58 public id for entity `E` (e.g. `org_…`), branded by entity
|
|
46
|
-
* so ids of different entities — and plain strings — can't be mixed silently. A
|
|
47
|
-
* plain `string` at runtime; the brand is minted by a {@link createIdRegistry}
|
|
48
|
-
* helper and erased on the wire.
|
|
49
|
-
*/
|
|
50
|
-
export type Id<E extends string> = string & {
|
|
51
|
-
readonly [ID_BRAND]: E;
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* A hyphenated UUIDv7 at rest, branded distinct from a public {@link Id} so a
|
|
55
|
-
* skinned id can't be fed into a uuid slot (or a uuid into an id slot) by
|
|
56
|
-
* accident. Recovered from an {@link Id} by a registry helper's `decode`.
|
|
57
|
-
*/
|
|
58
|
-
export type Uuid = string & {
|
|
59
|
-
readonly [UUID_BRAND]: "Uuid";
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* Whether `value` is a hyphenated uuid — the sanctioned string→{@link Uuid}
|
|
63
|
-
* bless: narrowing through the shape check is never a blind cast. Use at trust
|
|
64
|
-
* boundaries (env/config, external payloads, `crypto.randomUUID()`).
|
|
65
|
-
*/
|
|
66
|
-
export declare const isUuid: (value: string | null | undefined) => value is Uuid;
|
|
67
|
-
/** The throwing {@link isUuid}: bless a string you require to be a uuid. */
|
|
68
|
-
export declare function asUuid(value: string): Uuid;
|
|
69
|
-
/** Typed helpers for one entity's prefixed ids — built by {@link createIdRegistry}. */
|
|
70
|
-
export interface IdHelper<E extends string = string> {
|
|
71
|
-
/** This entity's prefix, e.g. `"agt"` (no trailing underscore). */
|
|
72
|
-
readonly prefix: string;
|
|
73
|
-
/** Mint a fresh prefixed id (new UUIDv7 core). */
|
|
74
|
-
mint(): Id<E>;
|
|
75
|
-
/** Skin a stored hyphenated UUIDv7 as this entity's prefixed id. */
|
|
76
|
-
encode(uuid: string): Id<E>;
|
|
77
|
-
/** Recover the hyphenated UUIDv7; throws if the prefix / shape doesn't match. */
|
|
78
|
-
decode(id: string): Uuid;
|
|
79
|
-
/**
|
|
80
|
-
* Recover the hyphenated UUIDv7, or `null` if `id` isn't a well-formed
|
|
81
|
-
* prefixed id for this entity. The throw-free {@link decode}, for validating
|
|
82
|
-
* untrusted input (a path param, a query string) without a try/catch.
|
|
83
|
-
*/
|
|
84
|
-
decodeOrNull(id: string): Uuid | null;
|
|
85
|
-
/** Whether `id` is a well-formed prefixed id for this entity (narrows to {@link Id}). */
|
|
86
|
-
is(id: string): id is Id<E>;
|
|
87
|
-
}
|
|
88
|
-
/** The map returned by {@link createIdRegistry}: one {@link IdHelper} per key. */
|
|
89
|
-
export type IdRegistry<K extends string> = {
|
|
90
|
-
[E in K]: IdHelper<E>;
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* Build a typed, prefix-validated id registry for a project's entities — one
|
|
94
|
-
* place to declare every prefix instead of hand-rolling encode/decode wrappers:
|
|
95
|
-
*
|
|
96
|
-
* ```ts
|
|
97
|
-
* const ids = createIdRegistry({ org: "org", agent: "agt", session: "cs" });
|
|
98
|
-
* ids.org.mint(); // "org_3k9…"
|
|
99
|
-
* ids.org.encode(uuid); // "org_…"
|
|
100
|
-
* ids.org.decode("org_…"); // uuid — throws on a wrong / missing prefix
|
|
101
|
-
* ids.agent.is(someId); // boolean
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
export declare function createIdRegistry<const T extends Record<string, string>>(prefixes: T): {
|
|
105
|
-
[K in keyof T]: IdHelper;
|
|
106
|
-
};
|
|
107
|
-
/**
|
|
108
|
-
* Which entity a prefixed id belongs to, or `null` if no helper in `registry`
|
|
109
|
-
* recognises it (a raw uuid, a sentinel, a foreign prefix).
|
|
110
|
-
*
|
|
111
|
-
* A public id is **self-describing**: its prefix names its entity, so resolving
|
|
112
|
-
* one needs no surrounding context. That is the property behind every
|
|
113
|
-
* entity-agnostic decode — a polymorphic FK whose target is named by a sibling
|
|
114
|
-
* `*_type` column, a raw-SQL binding that only has a value, a webhook payload.
|
|
115
|
-
* The inverse does **not** hold: a bare uuid carries no entity, so encoding
|
|
116
|
-
* always needs to know which entity you meant.
|
|
117
|
-
*
|
|
118
|
-
* Standalone rather than a registry method: the registry is keyed by entity
|
|
119
|
-
* name, so a method would collide with an entity literally called `entityOf`.
|
|
120
|
-
*/
|
|
121
|
-
export declare function entityOf<K extends string>(registry: Record<K, IdHelper>, value: string): K | null;
|
|
122
|
-
/**
|
|
123
|
-
* Decode a prefixed id belonging to **any** entity in `registry` (see
|
|
124
|
-
* {@link entityOf}), for the boundaries that can't name one: polymorphic FKs,
|
|
125
|
-
* raw-SQL id bindings, generic audit/event payloads.
|
|
126
|
-
*
|
|
127
|
-
* Tolerant by design — a value no helper recognises is returned unchanged, so an
|
|
128
|
-
* already-decoded uuid passes straight through and this is safe to apply to a
|
|
129
|
-
* value that may or may not be skinned. Use a helper's `decode`/`decodeOrNull`
|
|
130
|
-
* when you know the entity and want a wrong prefix rejected.
|
|
131
|
-
*/
|
|
132
|
-
export declare function decodeAnyId<K extends string>(registry: Record<K, IdHelper>, value: string): Uuid;
|
|
133
|
-
export {};
|
|
8
|
+
* Like `id758` itself, this module must stay isomorphic (no node-only imports):
|
|
9
|
+
* it is pulled into Drizzle schemas, client components and edge runtimes.
|
|
10
|
+
*/
|
|
11
|
+
export * from "id758";
|
|
12
|
+
import { decodeId, encodeId, mintId, uuidv7 } from "id758";
|
|
13
|
+
/** @deprecated Use `uuidv7` from `id758`. */
|
|
14
|
+
export declare const uuidGenerateId: typeof uuidv7;
|
|
15
|
+
/** @deprecated Use `encodeId` from `id758`. */
|
|
16
|
+
export declare const toPrefixedId: typeof encodeId;
|
|
17
|
+
/** @deprecated Use `decodeId` from `id758`. */
|
|
18
|
+
export declare const fromPrefixedId: typeof decodeId;
|
|
19
|
+
/** @deprecated Use `mintId` from `id758`. */
|
|
20
|
+
export declare const base58Id: typeof mintId;
|
|
134
21
|
//# sourceMappingURL=id.d.ts.map
|
package/dist/id.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../src/id.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,cAAc,OAAO,CAAC;AACtB,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE3D,6CAA6C;AAC7C,eAAO,MAAM,cAAc,eAAS,CAAC;AACrC,+CAA+C;AAC/C,eAAO,MAAM,YAAY,iBAAW,CAAC;AACrC,+CAA+C;AAC/C,eAAO,MAAM,cAAc,iBAAW,CAAC;AACvC,6CAA6C;AAC7C,eAAO,MAAM,QAAQ,eAAS,CAAC"}
|