@ingram-tech/nk-db 2.0.0 → 2.0.1
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 +8 -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/package.json +2 -2
- package/src/id-drizzle.ts +13 -10
package/README.md
CHANGED
|
@@ -172,7 +172,7 @@ straight through.
|
|
|
172
172
|
### Drizzle bindings (`@ingram-tech/nk-db/id/drizzle`)
|
|
173
173
|
|
|
174
174
|
A column declared with `idColumn` is a plain `uuid` in Postgres that speaks
|
|
175
|
-
public ids in TypeScript
|
|
175
|
+
public ids in TypeScript:
|
|
176
176
|
|
|
177
177
|
```ts
|
|
178
178
|
// ids.ts
|
|
@@ -182,8 +182,8 @@ export const { idColumn, polymorphicIdColumn, encodedId, sqlUuid, sqlUuidArray }
|
|
|
182
182
|
|
|
183
183
|
// schema.ts
|
|
184
184
|
export const invoices = pgTable("invoices", {
|
|
185
|
-
id: idColumn("invoice")().primaryKey().default(sql`uuidv7()`),
|
|
186
|
-
account_id: idColumn("account")().notNull(),
|
|
185
|
+
id: idColumn("invoice")().primaryKey().default(sql`uuidv7()`),
|
|
186
|
+
account_id: idColumn("account")().notNull(),
|
|
187
187
|
entity_id: polymorphicIdColumn(), // any entity, raw on read
|
|
188
188
|
});
|
|
189
189
|
|
|
@@ -199,6 +199,11 @@ row.account_id; // "acct_…"
|
|
|
199
199
|
relational `db.query`) and encodes it.
|
|
200
200
|
- `dataType` stays `uuid`, so there is **no DDL and no migration** and
|
|
201
201
|
`drizzle-kit generate` reports no diff.
|
|
202
|
+
- The column is typed `string`, not `Id<E>`: Drizzle has one type for both
|
|
203
|
+
directions and the write side accepts a raw uuid, so a branded type would
|
|
204
|
+
reject every database- or client-minted uuid at the insert. The read side is
|
|
205
|
+
a public id at runtime; brand it where it leaves the data layer
|
|
206
|
+
(`ids.invoice.is()`, a typed response schema).
|
|
202
207
|
|
|
203
208
|
`polymorphicIdColumn` (an `entity_id` whose target is named by a sibling
|
|
204
209
|
`*_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,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE9D;;;;;;;;;;;;;;;GAeG;AAEH,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;IACzC
|
|
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;;;;;;;;;;;;;;;;;;;OAmBG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,UAAU,CAAC,OAAO,UAAU,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC/E;;;;;;;;;;;;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
|
@@ -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":"AA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AAQ,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ingram-tech/nk-db",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
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.1",
|
|
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",
|