@forinda/kickjs-db 6.0.0 → 6.1.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.
@@ -1,196 +0,0 @@
1
-
2
- //#region src/snapshot/types.d.ts
3
- type Dialect = 'postgres' | 'sqlite' | 'mysql';
4
- type FkAction = 'cascade' | 'restrict' | 'set_null' | 'set_default' | 'no_action';
5
- interface ColumnSnapshot {
6
- name: string;
7
- type: string;
8
- nullable: boolean;
9
- default: string | null;
10
- primaryKey: boolean;
11
- }
12
- interface IndexSnapshot {
13
- name: string;
14
- columns: string[];
15
- unique: boolean;
16
- }
17
- interface ForeignKeySnapshot {
18
- name: string;
19
- columns: string[];
20
- refTable: string;
21
- refColumns: string[];
22
- onDelete: FkAction;
23
- onUpdate: FkAction;
24
- }
25
- interface CheckSnapshot {
26
- name: string;
27
- expression: string;
28
- }
29
- interface TableSnapshot {
30
- name: string;
31
- columns: Record<string, ColumnSnapshot>;
32
- indexes: IndexSnapshot[];
33
- foreignKeys: ForeignKeySnapshot[];
34
- checks: CheckSnapshot[];
35
- }
36
- /**
37
- * PostgreSQL ENUM type declaration. Currently PG-only; the field is
38
- * optional on `SchemaSnapshot` so other dialects don't have to carry
39
- * a phantom `enums: {}`.
40
- */
41
- interface EnumSnapshot {
42
- name: string;
43
- /** Allowed values, in declaration order. PG preserves the order. */
44
- values: readonly string[];
45
- }
46
- /**
47
- * Resolved relation graph attached as a sidecar on the snapshot.
48
- * Lives on `SchemaSnapshot.relations` (optional). Keyed
49
- * sourceTable → relationName → resolved entry. Consumed by the
50
- * relational-query compiler in `packages/db/src/query/`. The
51
- * migration pipeline does not read this field — relations are
52
- * query-time sugar, not DDL.
53
- *
54
- * Shaped this way (rather than re-using the runtime `RelationsDecl`)
55
- * so the snapshot stays JSON-serializable: no function thunks, no
56
- * back-references to table objects.
57
- */
58
- interface RelationSnapshot {
59
- kind: 'one' | 'many';
60
- /** Target table name. */
61
- target: string;
62
- /** Columns on the source table that participate in the join. */
63
- sourceColumns: readonly string[];
64
- /** Columns on the target table that participate in the join. */
65
- targetColumns: readonly string[];
66
- /**
67
- * Optional pairing tag from `relationName: 'foo'` on both sides of
68
- * the relation. Disambiguates multi-FK schemas. See
69
- * docs/db/spec-relation-name.md (M4.B).
70
- */
71
- relationName?: string;
72
- }
73
- interface SchemaSnapshot {
74
- version: 1;
75
- dialect: Dialect;
76
- tables: Record<string, TableSnapshot>;
77
- /** ENUM types declared via `pgEnum()`. PG-only; absent on other dialects. */
78
- enums?: Record<string, EnumSnapshot>;
79
- /**
80
- * Optional relation sidecar populated when the schema includes
81
- * `relations()` declarations. Absent when no relations are
82
- * declared so M0/M1 callers see the same snapshot shape they
83
- * always did.
84
- */
85
- relations?: Record<string, Record<string, RelationSnapshot>>;
86
- }
87
- //#endregion
88
- //#region src/dsl/columns/types.d.ts
89
- /**
90
- * Runtime + type representation of a column reference (e.g. `users.id`).
91
- * Carries the parent table name, the column's own name, plus runtime
92
- * accessors used by FK thunks and constraint builders.
93
- *
94
- * Exported so adopters can annotate self-referencing thunks without
95
- * spelling the shape inline:
96
- *
97
- * parentId: uuid().references((): ColumnRef => categories.id)
98
- *
99
- * Without the annotation the initializer of a self-referencing const
100
- * trips TS7022 — TS needs to infer the const while the initializer
101
- * already references it.
102
- */
103
- interface ColumnRef {
104
- __tableName: string;
105
- __name: string;
106
- __builder: ColumnBuilder;
107
- __state: () => ReturnType<ColumnBuilder['__state']>;
108
- }
109
- /**
110
- * Resolved FK spec — `table` / `column` are pulled by invoking `thunk()`
111
- * lazily. The thunk pattern lets self-referencing tables work:
112
- *
113
- * const categories = table('categories', {
114
- * id: uuid().primaryKey().defaultRandom(),
115
- * parentId: uuid().references((): ColumnRef => categories.id),
116
- * })
117
- *
118
- * If we resolved at `.references()` call time, `categories` would TDZ-fail
119
- * inside its own initializer. Storing the thunk and resolving on read
120
- * (extract / render / emit) defers until after the const binding lands.
121
- */
122
- interface FkSpec {
123
- thunk: () => ColumnRef;
124
- onDelete: FkAction;
125
- onUpdate: FkAction;
126
- }
127
- interface ColumnState {
128
- type: string;
129
- nullable: boolean;
130
- default: string | null;
131
- primaryKey: boolean;
132
- unique: boolean;
133
- references: FkSpec | null;
134
- }
135
- /**
136
- * Type-only brand attached to a column when it's auto-assigned by the
137
- * database (serial / bigserial / smallserial), has a runtime default
138
- * (`.default(...)`), or carries an expression default
139
- * (`uuid().defaultRandom()`, `timestamp().defaultNow()`). `SchemaToTypes<S>`
140
- * reads this marker and wraps the column type in Kysely's `Generated<T>` so
141
- * adopters can omit the column on insert.
142
- *
143
- * Runtime is a no-op — the symbol never reaches a value.
144
- */
145
- declare const KICK_GENERATED: unique symbol;
146
- type GeneratedBrand = {
147
- readonly [KICK_GENERATED]?: true;
148
- };
149
- /**
150
- * Type-only brand attached to a column when `.notNull()` or `.primaryKey()`
151
- * is called. `SchemaToTypes<S>` reads this brand to decide whether the
152
- * column type is `T` (NOT NULL) or `T | null` (default nullable).
153
- *
154
- * Why a brand instead of a class generic: chained methods (notNull,
155
- * primaryKey) can return `this & NotNullBrand` to narrow nullability while
156
- * preserving the subclass identity — so `uuid().primaryKey().defaultRandom()`
157
- * still resolves `defaultRandom()` on `UuidBuilder`. A class generic
158
- * `<TNullable>` would collapse the subclass back to the parent.
159
- */
160
- declare const KICK_NOT_NULL: unique symbol;
161
- type NotNullBrand = {
162
- readonly [KICK_NOT_NULL]?: true;
163
- };
164
- /**
165
- * Phantom-typed column builder. The `T` generic carries the column's TS
166
- * value type (number / string / Date / etc.); nullability is tracked via
167
- * the `NotNullBrand` intersection rather than a class generic so chain
168
- * methods preserve subclass identity.
169
- *
170
- * Both `T` and the brand are erased at runtime; they exist purely so
171
- * `SchemaToTypes<S>` can pull them out per column.
172
- */
173
- declare class ColumnBuilder<T = unknown> {
174
- readonly __t?: T;
175
- protected state: ColumnState;
176
- constructor(type: string, defaults?: Partial<ColumnState>);
177
- notNull(): this & NotNullBrand;
178
- primaryKey(): this & NotNullBrand;
179
- unique(): this;
180
- array(): ColumnBuilder<T[]>;
181
- references(target: () => ColumnRef, opts?: {
182
- onDelete?: FkAction;
183
- onUpdate?: FkAction;
184
- }): this;
185
- /**
186
- * Mark the column as having a runtime / DB-assigned default. The
187
- * `GeneratedBrand` flows into `SchemaToTypes<S>` so the column wraps
188
- * in `Generated<T>` — adopters can `INSERT` without specifying it.
189
- */
190
- default(value: string): this & GeneratedBrand;
191
- toJSON(name: string): ColumnSnapshot;
192
- __state(): Readonly<ColumnState>;
193
- }
194
- //#endregion
195
- export { SchemaSnapshot as _, GeneratedBrand as a, NotNullBrand as c, Dialect as d, EnumSnapshot as f, RelationSnapshot as g, IndexSnapshot as h, FkSpec as i, CheckSnapshot as l, ForeignKeySnapshot as m, ColumnRef as n, KICK_GENERATED as o, FkAction as p, ColumnState as r, KICK_NOT_NULL as s, ColumnBuilder as t, ColumnSnapshot as u, TableSnapshot as v };
196
- //# sourceMappingURL=types-C1VwEYWD.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-C1VwEYWD.d.mts","names":[],"sources":["../src/snapshot/types.ts","../src/dsl/columns/types.ts"],"mappings":";;KAAY,OAAA;AAAA,KAEA,QAAA;AAAA,UAEK,cAAA;EACf,IAAA;EACA,IAAA;EACA,QAAA;EACA,OAAA;EACA,UAAA;AAAA;AAAA,UAGe,aAAA;EACf,IAAA;EACA,OAAA;EACA,MAAA;AAAA;AAAA,UAGe,kBAAA;EACf,IAAA;EACA,OAAA;EACA,QAAA;EACA,UAAA;EACA,QAAA,EAAU,QAAA;EACV,QAAA,EAAU,QAAQ;AAAA;AAAA,UAGH,aAAA;EACf,IAAA;EACA,UAAU;AAAA;AAAA,UAGK,aAAA;EACf,IAAA;EACA,OAAA,EAAS,MAAA,SAAe,cAAA;EACxB,OAAA,EAAS,aAAA;EACT,WAAA,EAAa,kBAAA;EACb,MAAA,EAAQ,aAAA;AAAA;;;;;;UAQO,YAAA;EACf,IAAA;EAvBU;EAyBV,MAAM;AAAA;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AAA,GAAW,QAAA;IAAU,QAAA,GAAW,QAAA;EAAA;EDrCf;;;;;ECsD3B,OAAA,CAAQ,KAAA,kBAAuB,cAAA;EAK/B,MAAA,CAAO,IAAA,WAAe,cAAA;EAWtB,OAAA,IAAW,QAAA,CAAS,WAAA;AAAA"}
@@ -1,12 +0,0 @@
1
- /**
2
- * @forinda/kickjs-db v6.0.0
3
- *
4
- * Copyright (c) Felix Orinda
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- const KICK_GENERATED=Symbol.for(`@forinda/kickjs-db/Generated`),KICK_NOT_NULL=Symbol.for(`@forinda/kickjs-db/NotNull`);var ColumnBuilder=class{state;constructor(type,defaults={}){this.state={type,nullable:defaults.nullable??!0,default:defaults.default??null,primaryKey:defaults.primaryKey??!1,unique:defaults.unique??!1,references:defaults.references??null}}notNull(){return this.state.nullable=!1,this}primaryKey(){return this.state.primaryKey=!0,this.state.nullable=!1,this}unique(){return this.state.unique=!0,this}array(){return this.state.type=`${this.state.type}[]`,this}references(target,opts={}){return this.state.references={thunk:target,onDelete:opts.onDelete??`no_action`,onUpdate:opts.onUpdate??`no_action`},this}default(value){return this.state.default=value,this}toJSON(name){return{name,type:this.state.type,nullable:this.state.nullable,default:this.state.default,primaryKey:this.state.primaryKey}}__state(){return this.state}};export{KICK_GENERATED as n,KICK_NOT_NULL as r,ColumnBuilder as t};
12
- //# sourceMappingURL=types-CnOQetrD.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-CnOQetrD.mjs","names":[],"sources":["../src/dsl/columns/types.ts"],"sourcesContent":["import type { ColumnSnapshot, FkAction } from '../../snapshot/types'\n\n/**\n * Runtime + type representation of a column reference (e.g. `users.id`).\n * Carries the parent table name, the column's own name, plus runtime\n * accessors used by FK thunks and constraint builders.\n *\n * Exported so adopters can annotate self-referencing thunks without\n * spelling the shape inline:\n *\n * parentId: uuid().references((): ColumnRef => categories.id)\n *\n * Without the annotation the initializer of a self-referencing const\n * trips TS7022 — TS needs to infer the const while the initializer\n * already references it.\n */\nexport interface ColumnRef {\n __tableName: string\n __name: string\n __builder: ColumnBuilder\n __state: () => ReturnType<ColumnBuilder['__state']>\n}\n\n/**\n * Resolved FK spec — `table` / `column` are pulled by invoking `thunk()`\n * lazily. The thunk pattern lets self-referencing tables work:\n *\n * const categories = table('categories', {\n * id: uuid().primaryKey().defaultRandom(),\n * parentId: uuid().references((): ColumnRef => categories.id),\n * })\n *\n * If we resolved at `.references()` call time, `categories` would TDZ-fail\n * inside its own initializer. Storing the thunk and resolving on read\n * (extract / render / emit) defers until after the const binding lands.\n */\nexport interface FkSpec {\n thunk: () => ColumnRef\n onDelete: FkAction\n onUpdate: FkAction\n}\n\nexport interface ColumnState {\n type: string\n nullable: boolean\n default: string | null\n primaryKey: boolean\n unique: boolean\n references: FkSpec | null\n}\n\n/**\n * Type-only brand attached to a column when it's auto-assigned by the\n * database (serial / bigserial / smallserial), has a runtime default\n * (`.default(...)`), or carries an expression default\n * (`uuid().defaultRandom()`, `timestamp().defaultNow()`). `SchemaToTypes<S>`\n * reads this marker and wraps the column type in Kysely's `Generated<T>` so\n * adopters can omit the column on insert.\n *\n * Runtime is a no-op — the symbol never reaches a value.\n */\nexport const KICK_GENERATED = Symbol.for('@forinda/kickjs-db/Generated')\nexport type GeneratedBrand = { readonly [KICK_GENERATED]?: true }\n\n/**\n * Type-only brand attached to a column when `.notNull()` or `.primaryKey()`\n * is called. `SchemaToTypes<S>` reads this brand to decide whether the\n * column type is `T` (NOT NULL) or `T | null` (default nullable).\n *\n * Why a brand instead of a class generic: chained methods (notNull,\n * primaryKey) can return `this & NotNullBrand` to narrow nullability while\n * preserving the subclass identity — so `uuid().primaryKey().defaultRandom()`\n * still resolves `defaultRandom()` on `UuidBuilder`. A class generic\n * `<TNullable>` would collapse the subclass back to the parent.\n */\nexport const KICK_NOT_NULL = Symbol.for('@forinda/kickjs-db/NotNull')\nexport type NotNullBrand = { readonly [KICK_NOT_NULL]?: true }\n\n/**\n * Phantom-typed column builder. The `T` generic carries the column's TS\n * value type (number / string / Date / etc.); nullability is tracked via\n * the `NotNullBrand` intersection rather than a class generic so chain\n * methods preserve subclass identity.\n *\n * Both `T` and the brand are erased at runtime; they exist purely so\n * `SchemaToTypes<S>` can pull them out per column.\n */\nexport class ColumnBuilder<T = unknown> {\n // Phantom param shows up in the public surface so the type system sees\n // it. Reading it at runtime is intentionally not supported.\n declare readonly __t?: T\n\n protected state: ColumnState\n\n constructor(type: string, defaults: Partial<ColumnState> = {}) {\n this.state = {\n type,\n nullable: defaults.nullable ?? true,\n default: defaults.default ?? null,\n primaryKey: defaults.primaryKey ?? false,\n unique: defaults.unique ?? false,\n references: defaults.references ?? null,\n }\n }\n\n notNull(): this & NotNullBrand {\n this.state.nullable = false\n return this as this & NotNullBrand\n }\n\n primaryKey(): this & NotNullBrand {\n this.state.primaryKey = true\n this.state.nullable = false\n return this as this & NotNullBrand\n }\n\n unique(): this {\n this.state.unique = true\n return this\n }\n\n array(): ColumnBuilder<T[]> {\n this.state.type = `${this.state.type}[]`\n return this as unknown as ColumnBuilder<T[]>\n }\n\n references(\n target: () => ColumnRef,\n opts: { onDelete?: FkAction; onUpdate?: FkAction } = {},\n ): this {\n // Store the thunk — do NOT invoke it here. Self-referencing tables\n // pass `() => self.id` whose binding does not exist yet.\n this.state.references = {\n thunk: target,\n onDelete: opts.onDelete ?? 'no_action',\n onUpdate: opts.onUpdate ?? 'no_action',\n }\n return this\n }\n\n /**\n * Mark the column as having a runtime / DB-assigned default. The\n * `GeneratedBrand` flows into `SchemaToTypes<S>` so the column wraps\n * in `Generated<T>` — adopters can `INSERT` without specifying it.\n */\n default(value: string): this & GeneratedBrand {\n this.state.default = value\n return this as this & GeneratedBrand\n }\n\n toJSON(name: string): ColumnSnapshot {\n return {\n name,\n type: this.state.type,\n nullable: this.state.nullable,\n default: this.state.default,\n primaryKey: this.state.primaryKey,\n }\n }\n\n // Internal accessor for table()/diff to read full state including unique/references.\n __state(): Readonly<ColumnState> {\n return this.state\n }\n}\n"],"mappings":";;;;;;;;;;AA6DA,MAAa,eAAiB,OAAO,IAAI,8BAA8B,EAc1D,cAAgB,OAAO,IAAI,4BAA4B,EAYpE,IAAa,cAAb,KAAwC,CAKtC,MAEA,YAAY,KAAc,SAAiC,CAAC,EAAG,CAC7D,KAAK,MAAQ,CACX,KACA,SAAU,SAAS,UAAY,GAC/B,QAAS,SAAS,SAAW,KAC7B,WAAY,SAAS,YAAc,GACnC,OAAQ,SAAS,QAAU,GAC3B,WAAY,SAAS,YAAc,IACrC,CACF,CAEA,SAA+B,CAE7B,MADA,MAAK,MAAM,SAAW,GACf,IACT,CAEA,YAAkC,CAGhC,MAFA,MAAK,MAAM,WAAa,GACxB,KAAK,MAAM,SAAW,GACf,IACT,CAEA,QAAe,CAEb,MADA,MAAK,MAAM,OAAS,GACb,IACT,CAEA,OAA4B,CAE1B,MADA,MAAK,MAAM,KAAO,GAAG,KAAK,MAAM,KAAK,IAC9B,IACT,CAEA,WACE,OACA,KAAqD,CAAC,EAChD,CAQN,MALA,MAAK,MAAM,WAAa,CACtB,MAAO,OACP,SAAU,KAAK,UAAY,YAC3B,SAAU,KAAK,UAAY,WAC7B,EACO,IACT,CAOA,QAAQ,MAAsC,CAE5C,MADA,MAAK,MAAM,QAAU,MACd,IACT,CAEA,OAAO,KAA8B,CACnC,MAAO,CACL,KACA,KAAM,KAAK,MAAM,KACjB,SAAU,KAAK,MAAM,SACrB,QAAS,KAAK,MAAM,QACpB,WAAY,KAAK,MAAM,UACzB,CACF,CAGA,SAAiC,CAC/B,OAAO,KAAK,KACd,CACF"}