appflare 0.2.41 → 0.2.43
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/Documentation.md +898 -758
- package/cli/commands/index.ts +247 -247
- package/cli/generate.ts +360 -360
- package/cli/index.ts +120 -120
- package/cli/load-config.ts +184 -184
- package/cli/schema-compiler.ts +1363 -1361
- package/cli/templates/auth/README.md +156 -156
- package/cli/templates/auth/config.ts +61 -61
- package/cli/templates/auth/route-config.ts +1 -1
- package/cli/templates/auth/route-handler.ts +1 -1
- package/cli/templates/auth/route-request-utils.ts +5 -5
- package/cli/templates/auth/route.config.ts +18 -18
- package/cli/templates/auth/route.handler.ts +18 -18
- package/cli/templates/auth/route.request-utils.ts +55 -55
- package/cli/templates/auth/route.ts +14 -14
- package/cli/templates/core/README.md +266 -266
- package/cli/templates/core/app-creation.ts +19 -19
- package/cli/templates/core/client/appflare.ts +112 -112
- package/cli/templates/core/client/handlers/index.ts +763 -755
- package/cli/templates/core/client/handlers.ts +1 -1
- package/cli/templates/core/client/index.ts +7 -7
- package/cli/templates/core/client/storage.ts +195 -195
- package/cli/templates/core/client/types.ts +187 -186
- package/cli/templates/core/client-modules/appflare.ts +1 -1
- package/cli/templates/core/client-modules/handlers.ts +1 -1
- package/cli/templates/core/client-modules/index.ts +1 -1
- package/cli/templates/core/client-modules/storage.ts +1 -1
- package/cli/templates/core/client-modules/types.ts +1 -1
- package/cli/templates/core/client.artifacts.ts +39 -39
- package/cli/templates/core/client.ts +4 -4
- package/cli/templates/core/drizzle.ts +15 -15
- package/cli/templates/core/export.ts +14 -14
- package/cli/templates/core/handlers.route.ts +24 -24
- package/cli/templates/core/handlers.ts +1 -1
- package/cli/templates/core/imports.ts +9 -9
- package/cli/templates/core/server.ts +38 -38
- package/cli/templates/core/types.ts +6 -6
- package/cli/templates/core/wrangler.ts +109 -109
- package/cli/templates/dashboard/builders/functions/index.ts +17 -17
- package/cli/templates/dashboard/builders/functions/render-page/header.ts +20 -20
- package/cli/templates/dashboard/builders/functions/render-page/index.ts +33 -33
- package/cli/templates/dashboard/builders/functions/render-page/request-panel.ts +171 -171
- package/cli/templates/dashboard/builders/functions/render-page/result-panel.ts +85 -85
- package/cli/templates/dashboard/builders/functions/render-page/scripts.ts +554 -554
- package/cli/templates/dashboard/builders/functions/tree-builder.ts +47 -47
- package/cli/templates/dashboard/builders/navigation.ts +155 -155
- package/cli/templates/dashboard/builders/storage/index.ts +13 -13
- package/cli/templates/dashboard/builders/storage/routes/create-directory-route.ts +29 -29
- package/cli/templates/dashboard/builders/storage/routes/delete-route.ts +18 -18
- package/cli/templates/dashboard/builders/storage/routes/download-route.ts +23 -23
- package/cli/templates/dashboard/builders/storage/routes/index.ts +22 -22
- package/cli/templates/dashboard/builders/storage/routes/list-route.ts +25 -25
- package/cli/templates/dashboard/builders/storage/routes/preview-route.ts +21 -21
- package/cli/templates/dashboard/builders/storage/routes/upload-route.ts +21 -21
- package/cli/templates/dashboard/builders/storage/runtime/helpers.ts +72 -72
- package/cli/templates/dashboard/builders/storage/runtime/storage-page.ts +130 -130
- package/cli/templates/dashboard/builders/table-routes/common/drawer-panel.ts +27 -27
- package/cli/templates/dashboard/builders/table-routes/common/pagination.ts +30 -30
- package/cli/templates/dashboard/builders/table-routes/common/search-bar.ts +23 -23
- package/cli/templates/dashboard/builders/table-routes/fragments.ts +217 -217
- package/cli/templates/dashboard/builders/table-routes/helpers.ts +45 -45
- package/cli/templates/dashboard/builders/table-routes/index.ts +8 -8
- package/cli/templates/dashboard/builders/table-routes/table/actions-cell.ts +71 -71
- package/cli/templates/dashboard/builders/table-routes/table/get-route.ts +291 -291
- package/cli/templates/dashboard/builders/table-routes/table/index.ts +80 -80
- package/cli/templates/dashboard/builders/table-routes/table/post-routes.ts +163 -163
- package/cli/templates/dashboard/builders/table-routes/table-route.ts +7 -7
- package/cli/templates/dashboard/builders/table-routes/users/get-route.ts +69 -69
- package/cli/templates/dashboard/builders/table-routes/users/html/modals.ts +57 -57
- package/cli/templates/dashboard/builders/table-routes/users/html/page.ts +27 -27
- package/cli/templates/dashboard/builders/table-routes/users/html/table.ts +128 -128
- package/cli/templates/dashboard/builders/table-routes/users/index.ts +32 -32
- package/cli/templates/dashboard/builders/table-routes/users/post-routes.ts +150 -150
- package/cli/templates/dashboard/builders/table-routes/users/redirect.ts +14 -14
- package/cli/templates/dashboard/builders/table-routes/users-route.ts +10 -10
- package/cli/templates/dashboard/components/dashboard-home.ts +23 -23
- package/cli/templates/dashboard/components/layout.ts +420 -420
- package/cli/templates/dashboard/components/login-page.ts +65 -65
- package/cli/templates/dashboard/index.ts +61 -61
- package/cli/templates/dashboard/types.ts +9 -9
- package/cli/templates/handlers/README.md +353 -353
- package/cli/templates/handlers/auth.ts +37 -37
- package/cli/templates/handlers/execution.ts +42 -42
- package/cli/templates/handlers/generators/context/context-creation.ts +101 -101
- package/cli/templates/handlers/generators/context/error-helpers.ts +11 -11
- package/cli/templates/handlers/generators/context/scheduler.ts +24 -24
- package/cli/templates/handlers/generators/context/storage-api.ts +82 -82
- package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
- package/cli/templates/handlers/generators/context/types.ts +40 -40
- package/cli/templates/handlers/generators/context.ts +43 -43
- package/cli/templates/handlers/generators/execution.ts +15 -15
- package/cli/templates/handlers/generators/handlers.ts +14 -14
- package/cli/templates/handlers/generators/registration/modules/cron.ts +26 -26
- package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -75
- package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -144
- package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +14 -14
- package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +102 -102
- package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +164 -164
- package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -30
- package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +510 -510
- package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -56
- package/cli/templates/handlers/generators/registration/modules/storage.ts +199 -199
- package/cli/templates/handlers/generators/registration/sections.ts +210 -210
- package/cli/templates/handlers/generators/types/context.ts +121 -121
- package/cli/templates/handlers/generators/types/core.ts +106 -106
- package/cli/templates/handlers/generators/types/operations.ts +135 -135
- package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +291 -271
- package/cli/templates/handlers/generators/types/query-definitions/query-api-types.ts +135 -135
- package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +1382 -1382
- package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -278
- package/cli/templates/handlers/generators/types/query-definitions.ts +13 -13
- package/cli/templates/handlers/generators/types/query-runtime/handled-error.ts +13 -13
- package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts +174 -174
- package/cli/templates/handlers/generators/types/query-runtime/runtime-read.ts +157 -157
- package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
- package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +958 -697
- package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
- package/cli/templates/handlers/index.ts +47 -47
- package/cli/templates/handlers/operations.ts +116 -116
- package/cli/templates/handlers/registration.ts +91 -91
- package/cli/templates/handlers/types.ts +17 -17
- package/cli/templates/handlers/utils.ts +48 -48
- package/cli/types.ts +110 -110
- package/cli/utils/handler-discovery.ts +466 -466
- package/cli/utils/json-utils.ts +24 -24
- package/cli/utils/path-utils.ts +19 -19
- package/cli/utils/schema-discovery.ts +399 -399
- package/dist/cli/index.js +320 -31
- package/dist/cli/index.mjs +320 -31
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/react/index.d.mts +2 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +1 -1
- package/dist/react/index.mjs +1 -1
- package/index.ts +18 -18
- package/package.json +58 -58
- package/react/index.ts +5 -5
- package/react/use-infinite-query.ts +255 -252
- package/react/use-mutation.ts +89 -89
- package/react/use-query.ts +210 -207
- package/schema.ts +641 -641
- package/test-better-auth-hash.ts +2 -2
- package/tsconfig.json +6 -6
- package/tsup.config.ts +82 -82
package/schema.ts
CHANGED
|
@@ -1,641 +1,641 @@
|
|
|
1
|
-
export type ColumnType = "int" | "string" | "boolean" | "date" | "enum" | "json";
|
|
2
|
-
|
|
3
|
-
export type JsonShape =
|
|
4
|
-
| { kind: "array"; element: JsonShape }
|
|
5
|
-
| { kind: "object"; shape: Record<string, JsonShape> }
|
|
6
|
-
| { kind: "string" }
|
|
7
|
-
| { kind: "number" }
|
|
8
|
-
| { kind: "boolean" }
|
|
9
|
-
| { kind: "date" }
|
|
10
|
-
| { kind: "unknown" };
|
|
11
|
-
|
|
12
|
-
export type EnumDefinition = {
|
|
13
|
-
kind: "enum";
|
|
14
|
-
name: string;
|
|
15
|
-
sqlName?: string;
|
|
16
|
-
values: readonly string[];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export type ColumnBuilderOptions = {
|
|
20
|
-
sqlName?: string;
|
|
21
|
-
length?: number;
|
|
22
|
-
enumValues?: readonly string[];
|
|
23
|
-
enumRef?: string;
|
|
24
|
-
isArray?: boolean;
|
|
25
|
-
jsonShape?: JsonShape;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type PrimaryKeyOptions = {
|
|
29
|
-
autoIncrement?: boolean;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type ForeignKeyAction =
|
|
33
|
-
| "cascade"
|
|
34
|
-
| "set null"
|
|
35
|
-
| "set default"
|
|
36
|
-
| "restrict"
|
|
37
|
-
| "no action";
|
|
38
|
-
|
|
39
|
-
export type RelationOneOptions = {
|
|
40
|
-
referenceField?: string;
|
|
41
|
-
field?: string;
|
|
42
|
-
fkType?: ColumnType;
|
|
43
|
-
sqlName?: string;
|
|
44
|
-
notNull?: boolean;
|
|
45
|
-
nullable?: boolean;
|
|
46
|
-
onDelete?: ForeignKeyAction;
|
|
47
|
-
onUpdate?: ForeignKeyAction;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export type RelationManyOptions = {
|
|
51
|
-
referenceField?: string;
|
|
52
|
-
field?: string;
|
|
53
|
-
fkType?: ColumnType;
|
|
54
|
-
sqlName?: string;
|
|
55
|
-
notNull?: boolean;
|
|
56
|
-
nullable?: boolean;
|
|
57
|
-
onDelete?: ForeignKeyAction;
|
|
58
|
-
onUpdate?: ForeignKeyAction;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type RelationManyToManyOptions = {
|
|
62
|
-
referenceField?: string;
|
|
63
|
-
targetReferenceField?: string;
|
|
64
|
-
junctionTable?: string;
|
|
65
|
-
sourceField?: string;
|
|
66
|
-
targetField?: string;
|
|
67
|
-
sourceSqlName?: string;
|
|
68
|
-
targetSqlName?: string;
|
|
69
|
-
onDelete?: ForeignKeyAction;
|
|
70
|
-
onUpdate?: ForeignKeyAction;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export type ColumnReference = {
|
|
74
|
-
table: string;
|
|
75
|
-
column: string;
|
|
76
|
-
onDelete?: ForeignKeyAction;
|
|
77
|
-
onUpdate?: ForeignKeyAction;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export type ColumnDefinition = {
|
|
81
|
-
kind: "column";
|
|
82
|
-
type: ColumnType;
|
|
83
|
-
sqlName?: string;
|
|
84
|
-
length?: number;
|
|
85
|
-
notNull?: boolean;
|
|
86
|
-
nullable?: boolean;
|
|
87
|
-
primaryKey?: boolean;
|
|
88
|
-
autoIncrement?: boolean;
|
|
89
|
-
/** When true the column stores a UUID generated at runtime (string type). */
|
|
90
|
-
uuidPrimaryKey?: boolean;
|
|
91
|
-
unique?: true | { name?: string };
|
|
92
|
-
index?: true | { name?: string };
|
|
93
|
-
sqlDefault?: unknown;
|
|
94
|
-
/** When true, the SQL column gets DEFAULT CURRENT_TIMESTAMP and runtime inserts use `new Date()`. */
|
|
95
|
-
nowDefault?: boolean;
|
|
96
|
-
runtimeDefaultFn?: () => unknown;
|
|
97
|
-
references?: ColumnReference;
|
|
98
|
-
/** For enum columns: the allowed values. */
|
|
99
|
-
enumValues?: readonly string[];
|
|
100
|
-
/** For enum columns: reference to a named enum definition. */
|
|
101
|
-
enumRef?: string;
|
|
102
|
-
/** When true, the column stores an array of enum values. */
|
|
103
|
-
isArray?: boolean;
|
|
104
|
-
/** For JSON columns: the shape of the stored data. */
|
|
105
|
-
jsonShape?: JsonShape;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export type OneRelationDefinition = {
|
|
109
|
-
kind: "relation";
|
|
110
|
-
relation: "one";
|
|
111
|
-
targetTable: string;
|
|
112
|
-
field?: string;
|
|
113
|
-
referenceField?: string;
|
|
114
|
-
fkType?: ColumnType;
|
|
115
|
-
sqlName?: string;
|
|
116
|
-
notNull?: boolean;
|
|
117
|
-
nullable?: boolean;
|
|
118
|
-
onDelete?: ForeignKeyAction;
|
|
119
|
-
onUpdate?: ForeignKeyAction;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
export type ManyRelationDefinition = {
|
|
123
|
-
kind: "relation";
|
|
124
|
-
relation: "many";
|
|
125
|
-
targetTable: string;
|
|
126
|
-
field?: string;
|
|
127
|
-
referenceField?: string;
|
|
128
|
-
fkType?: ColumnType;
|
|
129
|
-
sqlName?: string;
|
|
130
|
-
notNull?: boolean;
|
|
131
|
-
nullable?: boolean;
|
|
132
|
-
onDelete?: ForeignKeyAction;
|
|
133
|
-
onUpdate?: ForeignKeyAction;
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export type ManyToManyRelationDefinition = {
|
|
137
|
-
kind: "relation";
|
|
138
|
-
relation: "manyToMany";
|
|
139
|
-
targetTable: string;
|
|
140
|
-
referenceField?: string;
|
|
141
|
-
targetReferenceField?: string;
|
|
142
|
-
junctionTable?: string;
|
|
143
|
-
sourceField?: string;
|
|
144
|
-
targetField?: string;
|
|
145
|
-
sourceSqlName?: string;
|
|
146
|
-
targetSqlName?: string;
|
|
147
|
-
onDelete?: ForeignKeyAction;
|
|
148
|
-
onUpdate?: ForeignKeyAction;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
export type RelationDefinition =
|
|
152
|
-
| OneRelationDefinition
|
|
153
|
-
| ManyRelationDefinition
|
|
154
|
-
| ManyToManyRelationDefinition;
|
|
155
|
-
|
|
156
|
-
export type TableOptions = {
|
|
157
|
-
sqlName?: string;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export type TableShape = Record<string, ColumnBuilder | RelationDefinition>;
|
|
161
|
-
|
|
162
|
-
export type TableDefinition = {
|
|
163
|
-
kind: "table";
|
|
164
|
-
sqlName?: string;
|
|
165
|
-
columns: Record<string, ColumnDefinition>;
|
|
166
|
-
relations: Record<string, RelationDefinition>;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
export type SchemaDefinition = {
|
|
170
|
-
kind: "schema";
|
|
171
|
-
tables: Record<string, TableDefinition>;
|
|
172
|
-
enums: Record<string, EnumDefinition>;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
export class EnumBuilder<T extends string> {
|
|
176
|
-
private def: EnumDefinition;
|
|
177
|
-
|
|
178
|
-
public constructor(name: string, values: readonly T[]) {
|
|
179
|
-
this.def = {
|
|
180
|
-
kind: "enum",
|
|
181
|
-
name,
|
|
182
|
-
values: values as readonly string[],
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
public sql(name: string): EnumBuilder<T> {
|
|
187
|
-
return new EnumBuilder<T>(name, this.def.values as readonly T[]);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
public toDefinition(): EnumDefinition {
|
|
191
|
-
return { ...this.def };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public getValues(): readonly T[] {
|
|
195
|
-
return this.def.values as readonly T[];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function defineEnum<T extends string>(
|
|
200
|
-
name: string,
|
|
201
|
-
values: readonly T[],
|
|
202
|
-
): EnumBuilder<T> {
|
|
203
|
-
return new EnumBuilder(name, values);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export class JsonArrayBuilder {
|
|
207
|
-
private _element: JsonShape;
|
|
208
|
-
|
|
209
|
-
public constructor(element: JsonShape) {
|
|
210
|
-
this._element = element;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
public get shape(): JsonShape {
|
|
214
|
-
return { kind: "array", element: this._element };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
public toColumnBuilder(): ColumnBuilder {
|
|
218
|
-
return new ColumnBuilder("json", { jsonShape: this.shape });
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export class JsonObjectBuilder<TShape extends Record<string, JsonShape>> {
|
|
223
|
-
private _shape: TShape;
|
|
224
|
-
|
|
225
|
-
public constructor(shape: TShape) {
|
|
226
|
-
this._shape = shape;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
public get shape(): JsonShape {
|
|
230
|
-
return { kind: "object", shape: this._shape };
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
public toColumnBuilder(): ColumnBuilder {
|
|
234
|
-
return new ColumnBuilder("json", { jsonShape: this.shape });
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export class ColumnBuilder {
|
|
239
|
-
private definition: ColumnDefinition;
|
|
240
|
-
|
|
241
|
-
public constructor(
|
|
242
|
-
typeOrDefinition: ColumnType | ColumnDefinition,
|
|
243
|
-
options: ColumnBuilderOptions = {},
|
|
244
|
-
) {
|
|
245
|
-
if (typeof typeOrDefinition === "string") {
|
|
246
|
-
this.definition = {
|
|
247
|
-
kind: "column",
|
|
248
|
-
type: typeOrDefinition,
|
|
249
|
-
sqlName: options.sqlName,
|
|
250
|
-
length: options.length,
|
|
251
|
-
enumValues: options.enumValues,
|
|
252
|
-
enumRef: options.enumRef,
|
|
253
|
-
jsonShape: options.jsonShape,
|
|
254
|
-
};
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
this.definition = {
|
|
259
|
-
...typeOrDefinition,
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
private with(patch: Partial<ColumnDefinition>): ColumnBuilder {
|
|
264
|
-
return new ColumnBuilder({
|
|
265
|
-
...this.definition,
|
|
266
|
-
...patch,
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
public sql(name: string): ColumnBuilder {
|
|
271
|
-
return this.with({ sqlName: name });
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
public notNull(): ColumnBuilder {
|
|
275
|
-
return this.with({ notNull: true, nullable: false });
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
public nullable(): ColumnBuilder {
|
|
279
|
-
return this.with({ nullable: true, notNull: false });
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
public primaryKey(options: PrimaryKeyOptions = {}): ColumnBuilder {
|
|
283
|
-
return this.with({
|
|
284
|
-
primaryKey: true,
|
|
285
|
-
autoIncrement: options.autoIncrement ?? this.definition.autoIncrement,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
public unique(name?: string): ColumnBuilder {
|
|
290
|
-
return this.with({ unique: name ? { name } : true });
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
public index(name?: string): ColumnBuilder {
|
|
294
|
-
return this.with({ index: name ? { name } : true });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
public default(value: unknown): ColumnBuilder {
|
|
298
|
-
return this.with({ sqlDefault: value });
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
public defaultFn(fn: () => unknown): ColumnBuilder {
|
|
302
|
-
return this.with({ runtimeDefaultFn: fn });
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Shorthand for date columns that should default to the current timestamp.
|
|
307
|
-
* Sets SQL `DEFAULT CURRENT_TIMESTAMP` (via `nowDefault`) and
|
|
308
|
-
* a runtime default of `new Date()`.
|
|
309
|
-
*/
|
|
310
|
-
public defaultNow(): ColumnBuilder {
|
|
311
|
-
return this.with({
|
|
312
|
-
nowDefault: true,
|
|
313
|
-
runtimeDefaultFn: () => new Date(),
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
public references(
|
|
318
|
-
table: string,
|
|
319
|
-
column = "id",
|
|
320
|
-
actions: Pick<ColumnReference, "onDelete" | "onUpdate"> = {},
|
|
321
|
-
): ColumnBuilder {
|
|
322
|
-
return this.with({
|
|
323
|
-
references: {
|
|
324
|
-
table,
|
|
325
|
-
column,
|
|
326
|
-
onDelete: actions.onDelete,
|
|
327
|
-
onUpdate: actions.onUpdate,
|
|
328
|
-
},
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** Marks the column as an array (e.g., for enum arrays). */
|
|
333
|
-
public array(): ColumnBuilder {
|
|
334
|
-
return this.with({ isArray: true });
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
public toDefinition(): ColumnDefinition {
|
|
338
|
-
return { ...this.definition };
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export function table(
|
|
343
|
-
shape: TableShape,
|
|
344
|
-
options: TableOptions = {},
|
|
345
|
-
): TableDefinition {
|
|
346
|
-
const columns: Record<string, ColumnDefinition> = {};
|
|
347
|
-
const relations: Record<string, RelationDefinition> = {};
|
|
348
|
-
|
|
349
|
-
for (const [fieldName, value] of Object.entries(shape)) {
|
|
350
|
-
if (value instanceof ColumnBuilder) {
|
|
351
|
-
columns[fieldName] = value.toDefinition();
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (value.kind === "relation") {
|
|
356
|
-
relations[fieldName] = value;
|
|
357
|
-
continue;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
throw new Error(
|
|
361
|
-
`Invalid table field '${fieldName}'. Use column builders or relation helpers.`,
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return {
|
|
366
|
-
kind: "table",
|
|
367
|
-
sqlName: options.sqlName,
|
|
368
|
-
columns,
|
|
369
|
-
relations,
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
export type SchemaOptions = {
|
|
374
|
-
enums?: Record<string, EnumDefinition>;
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
export function schema(
|
|
378
|
-
tables: Record<string, TableDefinition>,
|
|
379
|
-
options: SchemaOptions = {},
|
|
380
|
-
): SchemaDefinition {
|
|
381
|
-
return {
|
|
382
|
-
kind: "schema",
|
|
383
|
-
tables,
|
|
384
|
-
enums: options.enums ?? {},
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
export function isSchemaDefinition(value: unknown): value is SchemaDefinition {
|
|
389
|
-
if (typeof value !== "object" || value === null) {
|
|
390
|
-
return false;
|
|
391
|
-
}
|
|
392
|
-
const candidate = value as {
|
|
393
|
-
kind?: string;
|
|
394
|
-
tables?: unknown;
|
|
395
|
-
enums?: unknown;
|
|
396
|
-
};
|
|
397
|
-
return (
|
|
398
|
-
candidate.kind === "schema" &&
|
|
399
|
-
typeof candidate.tables === "object" &&
|
|
400
|
-
(typeof candidate.enums === "object" || candidate.enums === undefined)
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// ---------------------------------------------------------------------------
|
|
405
|
-
// Relation builder helpers
|
|
406
|
-
// ---------------------------------------------------------------------------
|
|
407
|
-
|
|
408
|
-
function buildOneRelation(
|
|
409
|
-
targetTable: string,
|
|
410
|
-
fieldOrOptions?: string | RelationOneOptions,
|
|
411
|
-
options: RelationOneOptions = {},
|
|
412
|
-
): OneRelationDefinition {
|
|
413
|
-
const field = typeof fieldOrOptions === "string" ? fieldOrOptions : undefined;
|
|
414
|
-
const mergedOptions =
|
|
415
|
-
typeof fieldOrOptions === "string" ? options : (fieldOrOptions ?? options);
|
|
416
|
-
|
|
417
|
-
return {
|
|
418
|
-
kind: "relation",
|
|
419
|
-
relation: "one",
|
|
420
|
-
targetTable,
|
|
421
|
-
field: mergedOptions.field ?? field,
|
|
422
|
-
referenceField: mergedOptions.referenceField,
|
|
423
|
-
fkType: mergedOptions.fkType,
|
|
424
|
-
sqlName: mergedOptions.sqlName,
|
|
425
|
-
notNull: mergedOptions.notNull,
|
|
426
|
-
nullable: mergedOptions.nullable,
|
|
427
|
-
onDelete: mergedOptions.onDelete,
|
|
428
|
-
onUpdate: mergedOptions.onUpdate,
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
function buildManyRelation(
|
|
433
|
-
targetTable: string,
|
|
434
|
-
fieldOrOptions?: string | RelationManyOptions,
|
|
435
|
-
options: RelationManyOptions = {},
|
|
436
|
-
): ManyRelationDefinition {
|
|
437
|
-
const field = typeof fieldOrOptions === "string" ? fieldOrOptions : undefined;
|
|
438
|
-
const mergedOptions =
|
|
439
|
-
typeof fieldOrOptions === "string" ? options : (fieldOrOptions ?? options);
|
|
440
|
-
|
|
441
|
-
return {
|
|
442
|
-
kind: "relation",
|
|
443
|
-
relation: "many",
|
|
444
|
-
targetTable,
|
|
445
|
-
field: mergedOptions.field ?? field,
|
|
446
|
-
referenceField: mergedOptions.referenceField,
|
|
447
|
-
fkType: mergedOptions.fkType,
|
|
448
|
-
sqlName: mergedOptions.sqlName,
|
|
449
|
-
notNull: mergedOptions.notNull,
|
|
450
|
-
nullable: mergedOptions.nullable,
|
|
451
|
-
onDelete: mergedOptions.onDelete,
|
|
452
|
-
onUpdate: mergedOptions.onUpdate,
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
function buildManyToManyRelation(
|
|
457
|
-
targetTable: string,
|
|
458
|
-
options: RelationManyToManyOptions = {},
|
|
459
|
-
): ManyToManyRelationDefinition {
|
|
460
|
-
return {
|
|
461
|
-
kind: "relation",
|
|
462
|
-
relation: "manyToMany",
|
|
463
|
-
targetTable,
|
|
464
|
-
referenceField: options.referenceField,
|
|
465
|
-
targetReferenceField: options.targetReferenceField,
|
|
466
|
-
junctionTable: options.junctionTable,
|
|
467
|
-
sourceField: options.sourceField,
|
|
468
|
-
targetField: options.targetField,
|
|
469
|
-
sourceSqlName: options.sourceSqlName,
|
|
470
|
-
targetSqlName: options.targetSqlName,
|
|
471
|
-
onDelete: options.onDelete,
|
|
472
|
-
onUpdate: options.onUpdate,
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// ---------------------------------------------------------------------------
|
|
477
|
-
// Public API
|
|
478
|
-
// ---------------------------------------------------------------------------
|
|
479
|
-
|
|
480
|
-
function columnBuilderToJsonShape(builder: ColumnBuilder): JsonShape {
|
|
481
|
-
const def = builder.toDefinition();
|
|
482
|
-
if (def.jsonShape) return def.jsonShape;
|
|
483
|
-
if (def.type === "int") return { kind: "number" };
|
|
484
|
-
if (def.type === "string") return { kind: "string" };
|
|
485
|
-
if (def.type === "boolean") return { kind: "boolean" };
|
|
486
|
-
if (def.type === "date") return { kind: "date" };
|
|
487
|
-
if (def.type === "enum" && def.enumValues) {
|
|
488
|
-
return {
|
|
489
|
-
kind: "string",
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
return { kind: "unknown" };
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
export const v = {
|
|
496
|
-
/** Define a reusable enum that can be referenced by multiple columns. */
|
|
497
|
-
defineEnum: <T extends string>(
|
|
498
|
-
name: string,
|
|
499
|
-
values: readonly T[],
|
|
500
|
-
): EnumBuilder<T> => defineEnum(name, values),
|
|
501
|
-
|
|
502
|
-
int: (options: ColumnBuilderOptions = {}) =>
|
|
503
|
-
new ColumnBuilder("int", options),
|
|
504
|
-
number: (options: ColumnBuilderOptions = {}) =>
|
|
505
|
-
new ColumnBuilder("int", options),
|
|
506
|
-
string: (options: ColumnBuilderOptions = {}) =>
|
|
507
|
-
new ColumnBuilder("string", options),
|
|
508
|
-
boolean: (options: ColumnBuilderOptions = {}) =>
|
|
509
|
-
new ColumnBuilder("boolean", options),
|
|
510
|
-
date: (options: ColumnBuilderOptions = {}) =>
|
|
511
|
-
new ColumnBuilder("date", options),
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* UUID primary key column.
|
|
515
|
-
* Stored as a string, marked as primary key and auto-populated with
|
|
516
|
-
* `crypto.randomUUID()` at runtime (no SQL sequence).
|
|
517
|
-
*/
|
|
518
|
-
uuid: (options: ColumnBuilderOptions = {}) =>
|
|
519
|
-
new ColumnBuilder({
|
|
520
|
-
kind: "column",
|
|
521
|
-
type: "string",
|
|
522
|
-
sqlName: options.sqlName,
|
|
523
|
-
length: options.length ?? 36,
|
|
524
|
-
primaryKey: true,
|
|
525
|
-
notNull: true,
|
|
526
|
-
nullable: false,
|
|
527
|
-
uuidPrimaryKey: true,
|
|
528
|
-
runtimeDefaultFn: () => crypto.randomUUID(),
|
|
529
|
-
}),
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Enum column builder.
|
|
533
|
-
* Pass an EnumBuilder (from v.defineEnum) or inline values.
|
|
534
|
-
*/
|
|
535
|
-
enum: <T extends string>(
|
|
536
|
-
valuesOrEnum: readonly T[] | EnumBuilder<T>,
|
|
537
|
-
options: ColumnBuilderOptions = {},
|
|
538
|
-
): ColumnBuilder => {
|
|
539
|
-
if (valuesOrEnum instanceof EnumBuilder) {
|
|
540
|
-
const def = valuesOrEnum.toDefinition();
|
|
541
|
-
return new ColumnBuilder("enum", {
|
|
542
|
-
...options,
|
|
543
|
-
enumValues: def.values,
|
|
544
|
-
enumRef: def.name,
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
return new ColumnBuilder("enum", {
|
|
548
|
-
...options,
|
|
549
|
-
enumValues: valuesOrEnum,
|
|
550
|
-
});
|
|
551
|
-
},
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Enum array column builder.
|
|
555
|
-
* Stores multiple enum values in a single column.
|
|
556
|
-
*/
|
|
557
|
-
enumArray: <T extends string>(
|
|
558
|
-
valuesOrEnum: readonly T[] | EnumBuilder<T>,
|
|
559
|
-
options: ColumnBuilderOptions = {},
|
|
560
|
-
): ColumnBuilder => {
|
|
561
|
-
if (valuesOrEnum instanceof EnumBuilder) {
|
|
562
|
-
const def = valuesOrEnum.toDefinition();
|
|
563
|
-
return new ColumnBuilder("enum", {
|
|
564
|
-
...options,
|
|
565
|
-
enumValues: def.values,
|
|
566
|
-
enumRef: def.name,
|
|
567
|
-
isArray: true,
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
return new ColumnBuilder("enum", {
|
|
571
|
-
...options,
|
|
572
|
-
enumValues: valuesOrEnum,
|
|
573
|
-
isArray: true,
|
|
574
|
-
});
|
|
575
|
-
},
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* JSON array column builder.
|
|
579
|
-
* Stored as JSON text, auto-serialized on insert/query.
|
|
580
|
-
* Use v.string(), v.number(), v.object(), or v.array() as the element type.
|
|
581
|
-
* Example: v.array(v.string()), v.array(v.object({ name: v.string() }))
|
|
582
|
-
*/
|
|
583
|
-
array: (element: JsonArrayBuilder | ColumnBuilder): ColumnBuilder => {
|
|
584
|
-
if (element instanceof JsonArrayBuilder) {
|
|
585
|
-
return element.toColumnBuilder();
|
|
586
|
-
}
|
|
587
|
-
if (element instanceof ColumnBuilder) {
|
|
588
|
-
const def = element.toDefinition();
|
|
589
|
-
if (def.jsonShape) {
|
|
590
|
-
return new ColumnBuilder("json", { jsonShape: def.jsonShape });
|
|
591
|
-
}
|
|
592
|
-
const shape: JsonShape = {
|
|
593
|
-
kind: "array",
|
|
594
|
-
element: columnBuilderToJsonShape(element),
|
|
595
|
-
};
|
|
596
|
-
return new ColumnBuilder("json", { jsonShape: shape });
|
|
597
|
-
}
|
|
598
|
-
return new ColumnBuilder("json", { jsonShape: { kind: "unknown" } });
|
|
599
|
-
},
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* JSON object column builder.
|
|
603
|
-
* Stored as JSON text, auto-serialized on insert/query.
|
|
604
|
-
* Pass a shape record: v.object({ name: v.string(), count: v.number() })
|
|
605
|
-
*/
|
|
606
|
-
object: <TShape extends Record<string, JsonShape | ColumnBuilder>>(
|
|
607
|
-
shape: TShape,
|
|
608
|
-
): ColumnBuilder => {
|
|
609
|
-
const resolved: Record<string, JsonShape> = {};
|
|
610
|
-
for (const [key, value] of Object.entries(shape)) {
|
|
611
|
-
if (value instanceof ColumnBuilder) {
|
|
612
|
-
resolved[key] = columnBuilderToJsonShape(value);
|
|
613
|
-
} else {
|
|
614
|
-
resolved[key] = value;
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return new ColumnBuilder("json", {
|
|
618
|
-
jsonShape: { kind: "object", shape: resolved },
|
|
619
|
-
});
|
|
620
|
-
},
|
|
621
|
-
|
|
622
|
-
one: (
|
|
623
|
-
targetTable: string,
|
|
624
|
-
fieldOrOptions?: string | RelationOneOptions,
|
|
625
|
-
options: RelationOneOptions = {},
|
|
626
|
-
): OneRelationDefinition =>
|
|
627
|
-
buildOneRelation(targetTable, fieldOrOptions, options),
|
|
628
|
-
|
|
629
|
-
many: (
|
|
630
|
-
targetTable: string,
|
|
631
|
-
fieldOrOptions?: string | RelationManyOptions,
|
|
632
|
-
options: RelationManyOptions = {},
|
|
633
|
-
): ManyRelationDefinition =>
|
|
634
|
-
buildManyRelation(targetTable, fieldOrOptions, options),
|
|
635
|
-
|
|
636
|
-
manyToMany: (
|
|
637
|
-
targetTable: string,
|
|
638
|
-
options: RelationManyToManyOptions = {},
|
|
639
|
-
): ManyToManyRelationDefinition =>
|
|
640
|
-
buildManyToManyRelation(targetTable, options),
|
|
641
|
-
};
|
|
1
|
+
export type ColumnType = "int" | "string" | "boolean" | "date" | "enum" | "json";
|
|
2
|
+
|
|
3
|
+
export type JsonShape =
|
|
4
|
+
| { kind: "array"; element: JsonShape }
|
|
5
|
+
| { kind: "object"; shape: Record<string, JsonShape> }
|
|
6
|
+
| { kind: "string" }
|
|
7
|
+
| { kind: "number" }
|
|
8
|
+
| { kind: "boolean" }
|
|
9
|
+
| { kind: "date" }
|
|
10
|
+
| { kind: "unknown" };
|
|
11
|
+
|
|
12
|
+
export type EnumDefinition = {
|
|
13
|
+
kind: "enum";
|
|
14
|
+
name: string;
|
|
15
|
+
sqlName?: string;
|
|
16
|
+
values: readonly string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type ColumnBuilderOptions = {
|
|
20
|
+
sqlName?: string;
|
|
21
|
+
length?: number;
|
|
22
|
+
enumValues?: readonly string[];
|
|
23
|
+
enumRef?: string;
|
|
24
|
+
isArray?: boolean;
|
|
25
|
+
jsonShape?: JsonShape;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type PrimaryKeyOptions = {
|
|
29
|
+
autoIncrement?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type ForeignKeyAction =
|
|
33
|
+
| "cascade"
|
|
34
|
+
| "set null"
|
|
35
|
+
| "set default"
|
|
36
|
+
| "restrict"
|
|
37
|
+
| "no action";
|
|
38
|
+
|
|
39
|
+
export type RelationOneOptions = {
|
|
40
|
+
referenceField?: string;
|
|
41
|
+
field?: string;
|
|
42
|
+
fkType?: ColumnType;
|
|
43
|
+
sqlName?: string;
|
|
44
|
+
notNull?: boolean;
|
|
45
|
+
nullable?: boolean;
|
|
46
|
+
onDelete?: ForeignKeyAction;
|
|
47
|
+
onUpdate?: ForeignKeyAction;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type RelationManyOptions = {
|
|
51
|
+
referenceField?: string;
|
|
52
|
+
field?: string;
|
|
53
|
+
fkType?: ColumnType;
|
|
54
|
+
sqlName?: string;
|
|
55
|
+
notNull?: boolean;
|
|
56
|
+
nullable?: boolean;
|
|
57
|
+
onDelete?: ForeignKeyAction;
|
|
58
|
+
onUpdate?: ForeignKeyAction;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type RelationManyToManyOptions = {
|
|
62
|
+
referenceField?: string;
|
|
63
|
+
targetReferenceField?: string;
|
|
64
|
+
junctionTable?: string;
|
|
65
|
+
sourceField?: string;
|
|
66
|
+
targetField?: string;
|
|
67
|
+
sourceSqlName?: string;
|
|
68
|
+
targetSqlName?: string;
|
|
69
|
+
onDelete?: ForeignKeyAction;
|
|
70
|
+
onUpdate?: ForeignKeyAction;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export type ColumnReference = {
|
|
74
|
+
table: string;
|
|
75
|
+
column: string;
|
|
76
|
+
onDelete?: ForeignKeyAction;
|
|
77
|
+
onUpdate?: ForeignKeyAction;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type ColumnDefinition = {
|
|
81
|
+
kind: "column";
|
|
82
|
+
type: ColumnType;
|
|
83
|
+
sqlName?: string;
|
|
84
|
+
length?: number;
|
|
85
|
+
notNull?: boolean;
|
|
86
|
+
nullable?: boolean;
|
|
87
|
+
primaryKey?: boolean;
|
|
88
|
+
autoIncrement?: boolean;
|
|
89
|
+
/** When true the column stores a UUID generated at runtime (string type). */
|
|
90
|
+
uuidPrimaryKey?: boolean;
|
|
91
|
+
unique?: true | { name?: string };
|
|
92
|
+
index?: true | { name?: string };
|
|
93
|
+
sqlDefault?: unknown;
|
|
94
|
+
/** When true, the SQL column gets DEFAULT CURRENT_TIMESTAMP and runtime inserts use `new Date()`. */
|
|
95
|
+
nowDefault?: boolean;
|
|
96
|
+
runtimeDefaultFn?: () => unknown;
|
|
97
|
+
references?: ColumnReference;
|
|
98
|
+
/** For enum columns: the allowed values. */
|
|
99
|
+
enumValues?: readonly string[];
|
|
100
|
+
/** For enum columns: reference to a named enum definition. */
|
|
101
|
+
enumRef?: string;
|
|
102
|
+
/** When true, the column stores an array of enum values. */
|
|
103
|
+
isArray?: boolean;
|
|
104
|
+
/** For JSON columns: the shape of the stored data. */
|
|
105
|
+
jsonShape?: JsonShape;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type OneRelationDefinition = {
|
|
109
|
+
kind: "relation";
|
|
110
|
+
relation: "one";
|
|
111
|
+
targetTable: string;
|
|
112
|
+
field?: string;
|
|
113
|
+
referenceField?: string;
|
|
114
|
+
fkType?: ColumnType;
|
|
115
|
+
sqlName?: string;
|
|
116
|
+
notNull?: boolean;
|
|
117
|
+
nullable?: boolean;
|
|
118
|
+
onDelete?: ForeignKeyAction;
|
|
119
|
+
onUpdate?: ForeignKeyAction;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export type ManyRelationDefinition = {
|
|
123
|
+
kind: "relation";
|
|
124
|
+
relation: "many";
|
|
125
|
+
targetTable: string;
|
|
126
|
+
field?: string;
|
|
127
|
+
referenceField?: string;
|
|
128
|
+
fkType?: ColumnType;
|
|
129
|
+
sqlName?: string;
|
|
130
|
+
notNull?: boolean;
|
|
131
|
+
nullable?: boolean;
|
|
132
|
+
onDelete?: ForeignKeyAction;
|
|
133
|
+
onUpdate?: ForeignKeyAction;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export type ManyToManyRelationDefinition = {
|
|
137
|
+
kind: "relation";
|
|
138
|
+
relation: "manyToMany";
|
|
139
|
+
targetTable: string;
|
|
140
|
+
referenceField?: string;
|
|
141
|
+
targetReferenceField?: string;
|
|
142
|
+
junctionTable?: string;
|
|
143
|
+
sourceField?: string;
|
|
144
|
+
targetField?: string;
|
|
145
|
+
sourceSqlName?: string;
|
|
146
|
+
targetSqlName?: string;
|
|
147
|
+
onDelete?: ForeignKeyAction;
|
|
148
|
+
onUpdate?: ForeignKeyAction;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export type RelationDefinition =
|
|
152
|
+
| OneRelationDefinition
|
|
153
|
+
| ManyRelationDefinition
|
|
154
|
+
| ManyToManyRelationDefinition;
|
|
155
|
+
|
|
156
|
+
export type TableOptions = {
|
|
157
|
+
sqlName?: string;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export type TableShape = Record<string, ColumnBuilder | RelationDefinition>;
|
|
161
|
+
|
|
162
|
+
export type TableDefinition = {
|
|
163
|
+
kind: "table";
|
|
164
|
+
sqlName?: string;
|
|
165
|
+
columns: Record<string, ColumnDefinition>;
|
|
166
|
+
relations: Record<string, RelationDefinition>;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export type SchemaDefinition = {
|
|
170
|
+
kind: "schema";
|
|
171
|
+
tables: Record<string, TableDefinition>;
|
|
172
|
+
enums: Record<string, EnumDefinition>;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export class EnumBuilder<T extends string> {
|
|
176
|
+
private def: EnumDefinition;
|
|
177
|
+
|
|
178
|
+
public constructor(name: string, values: readonly T[]) {
|
|
179
|
+
this.def = {
|
|
180
|
+
kind: "enum",
|
|
181
|
+
name,
|
|
182
|
+
values: values as readonly string[],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public sql(name: string): EnumBuilder<T> {
|
|
187
|
+
return new EnumBuilder<T>(name, this.def.values as readonly T[]);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public toDefinition(): EnumDefinition {
|
|
191
|
+
return { ...this.def };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public getValues(): readonly T[] {
|
|
195
|
+
return this.def.values as readonly T[];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function defineEnum<T extends string>(
|
|
200
|
+
name: string,
|
|
201
|
+
values: readonly T[],
|
|
202
|
+
): EnumBuilder<T> {
|
|
203
|
+
return new EnumBuilder(name, values);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export class JsonArrayBuilder {
|
|
207
|
+
private _element: JsonShape;
|
|
208
|
+
|
|
209
|
+
public constructor(element: JsonShape) {
|
|
210
|
+
this._element = element;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public get shape(): JsonShape {
|
|
214
|
+
return { kind: "array", element: this._element };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public toColumnBuilder(): ColumnBuilder {
|
|
218
|
+
return new ColumnBuilder("json", { jsonShape: this.shape });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export class JsonObjectBuilder<TShape extends Record<string, JsonShape>> {
|
|
223
|
+
private _shape: TShape;
|
|
224
|
+
|
|
225
|
+
public constructor(shape: TShape) {
|
|
226
|
+
this._shape = shape;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
public get shape(): JsonShape {
|
|
230
|
+
return { kind: "object", shape: this._shape };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public toColumnBuilder(): ColumnBuilder {
|
|
234
|
+
return new ColumnBuilder("json", { jsonShape: this.shape });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export class ColumnBuilder {
|
|
239
|
+
private definition: ColumnDefinition;
|
|
240
|
+
|
|
241
|
+
public constructor(
|
|
242
|
+
typeOrDefinition: ColumnType | ColumnDefinition,
|
|
243
|
+
options: ColumnBuilderOptions = {},
|
|
244
|
+
) {
|
|
245
|
+
if (typeof typeOrDefinition === "string") {
|
|
246
|
+
this.definition = {
|
|
247
|
+
kind: "column",
|
|
248
|
+
type: typeOrDefinition,
|
|
249
|
+
sqlName: options.sqlName,
|
|
250
|
+
length: options.length,
|
|
251
|
+
enumValues: options.enumValues,
|
|
252
|
+
enumRef: options.enumRef,
|
|
253
|
+
jsonShape: options.jsonShape,
|
|
254
|
+
};
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.definition = {
|
|
259
|
+
...typeOrDefinition,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private with(patch: Partial<ColumnDefinition>): ColumnBuilder {
|
|
264
|
+
return new ColumnBuilder({
|
|
265
|
+
...this.definition,
|
|
266
|
+
...patch,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
public sql(name: string): ColumnBuilder {
|
|
271
|
+
return this.with({ sqlName: name });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
public notNull(): ColumnBuilder {
|
|
275
|
+
return this.with({ notNull: true, nullable: false });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public nullable(): ColumnBuilder {
|
|
279
|
+
return this.with({ nullable: true, notNull: false });
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
public primaryKey(options: PrimaryKeyOptions = {}): ColumnBuilder {
|
|
283
|
+
return this.with({
|
|
284
|
+
primaryKey: true,
|
|
285
|
+
autoIncrement: options.autoIncrement ?? this.definition.autoIncrement,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
public unique(name?: string): ColumnBuilder {
|
|
290
|
+
return this.with({ unique: name ? { name } : true });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
public index(name?: string): ColumnBuilder {
|
|
294
|
+
return this.with({ index: name ? { name } : true });
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
public default(value: unknown): ColumnBuilder {
|
|
298
|
+
return this.with({ sqlDefault: value });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
public defaultFn(fn: () => unknown): ColumnBuilder {
|
|
302
|
+
return this.with({ runtimeDefaultFn: fn });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Shorthand for date columns that should default to the current timestamp.
|
|
307
|
+
* Sets SQL `DEFAULT CURRENT_TIMESTAMP` (via `nowDefault`) and
|
|
308
|
+
* a runtime default of `new Date()`.
|
|
309
|
+
*/
|
|
310
|
+
public defaultNow(): ColumnBuilder {
|
|
311
|
+
return this.with({
|
|
312
|
+
nowDefault: true,
|
|
313
|
+
runtimeDefaultFn: () => new Date(),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public references(
|
|
318
|
+
table: string,
|
|
319
|
+
column = "id",
|
|
320
|
+
actions: Pick<ColumnReference, "onDelete" | "onUpdate"> = {},
|
|
321
|
+
): ColumnBuilder {
|
|
322
|
+
return this.with({
|
|
323
|
+
references: {
|
|
324
|
+
table,
|
|
325
|
+
column,
|
|
326
|
+
onDelete: actions.onDelete,
|
|
327
|
+
onUpdate: actions.onUpdate,
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Marks the column as an array (e.g., for enum arrays). */
|
|
333
|
+
public array(): ColumnBuilder {
|
|
334
|
+
return this.with({ isArray: true });
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
public toDefinition(): ColumnDefinition {
|
|
338
|
+
return { ...this.definition };
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function table(
|
|
343
|
+
shape: TableShape,
|
|
344
|
+
options: TableOptions = {},
|
|
345
|
+
): TableDefinition {
|
|
346
|
+
const columns: Record<string, ColumnDefinition> = {};
|
|
347
|
+
const relations: Record<string, RelationDefinition> = {};
|
|
348
|
+
|
|
349
|
+
for (const [fieldName, value] of Object.entries(shape)) {
|
|
350
|
+
if (value instanceof ColumnBuilder) {
|
|
351
|
+
columns[fieldName] = value.toDefinition();
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (value.kind === "relation") {
|
|
356
|
+
relations[fieldName] = value;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
throw new Error(
|
|
361
|
+
`Invalid table field '${fieldName}'. Use column builders or relation helpers.`,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
kind: "table",
|
|
367
|
+
sqlName: options.sqlName,
|
|
368
|
+
columns,
|
|
369
|
+
relations,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export type SchemaOptions = {
|
|
374
|
+
enums?: Record<string, EnumDefinition>;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
export function schema(
|
|
378
|
+
tables: Record<string, TableDefinition>,
|
|
379
|
+
options: SchemaOptions = {},
|
|
380
|
+
): SchemaDefinition {
|
|
381
|
+
return {
|
|
382
|
+
kind: "schema",
|
|
383
|
+
tables,
|
|
384
|
+
enums: options.enums ?? {},
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function isSchemaDefinition(value: unknown): value is SchemaDefinition {
|
|
389
|
+
if (typeof value !== "object" || value === null) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
const candidate = value as {
|
|
393
|
+
kind?: string;
|
|
394
|
+
tables?: unknown;
|
|
395
|
+
enums?: unknown;
|
|
396
|
+
};
|
|
397
|
+
return (
|
|
398
|
+
candidate.kind === "schema" &&
|
|
399
|
+
typeof candidate.tables === "object" &&
|
|
400
|
+
(typeof candidate.enums === "object" || candidate.enums === undefined)
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// ---------------------------------------------------------------------------
|
|
405
|
+
// Relation builder helpers
|
|
406
|
+
// ---------------------------------------------------------------------------
|
|
407
|
+
|
|
408
|
+
function buildOneRelation(
|
|
409
|
+
targetTable: string,
|
|
410
|
+
fieldOrOptions?: string | RelationOneOptions,
|
|
411
|
+
options: RelationOneOptions = {},
|
|
412
|
+
): OneRelationDefinition {
|
|
413
|
+
const field = typeof fieldOrOptions === "string" ? fieldOrOptions : undefined;
|
|
414
|
+
const mergedOptions =
|
|
415
|
+
typeof fieldOrOptions === "string" ? options : (fieldOrOptions ?? options);
|
|
416
|
+
|
|
417
|
+
return {
|
|
418
|
+
kind: "relation",
|
|
419
|
+
relation: "one",
|
|
420
|
+
targetTable,
|
|
421
|
+
field: mergedOptions.field ?? field,
|
|
422
|
+
referenceField: mergedOptions.referenceField,
|
|
423
|
+
fkType: mergedOptions.fkType,
|
|
424
|
+
sqlName: mergedOptions.sqlName,
|
|
425
|
+
notNull: mergedOptions.notNull,
|
|
426
|
+
nullable: mergedOptions.nullable,
|
|
427
|
+
onDelete: mergedOptions.onDelete,
|
|
428
|
+
onUpdate: mergedOptions.onUpdate,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function buildManyRelation(
|
|
433
|
+
targetTable: string,
|
|
434
|
+
fieldOrOptions?: string | RelationManyOptions,
|
|
435
|
+
options: RelationManyOptions = {},
|
|
436
|
+
): ManyRelationDefinition {
|
|
437
|
+
const field = typeof fieldOrOptions === "string" ? fieldOrOptions : undefined;
|
|
438
|
+
const mergedOptions =
|
|
439
|
+
typeof fieldOrOptions === "string" ? options : (fieldOrOptions ?? options);
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
kind: "relation",
|
|
443
|
+
relation: "many",
|
|
444
|
+
targetTable,
|
|
445
|
+
field: mergedOptions.field ?? field,
|
|
446
|
+
referenceField: mergedOptions.referenceField,
|
|
447
|
+
fkType: mergedOptions.fkType,
|
|
448
|
+
sqlName: mergedOptions.sqlName,
|
|
449
|
+
notNull: mergedOptions.notNull,
|
|
450
|
+
nullable: mergedOptions.nullable,
|
|
451
|
+
onDelete: mergedOptions.onDelete,
|
|
452
|
+
onUpdate: mergedOptions.onUpdate,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function buildManyToManyRelation(
|
|
457
|
+
targetTable: string,
|
|
458
|
+
options: RelationManyToManyOptions = {},
|
|
459
|
+
): ManyToManyRelationDefinition {
|
|
460
|
+
return {
|
|
461
|
+
kind: "relation",
|
|
462
|
+
relation: "manyToMany",
|
|
463
|
+
targetTable,
|
|
464
|
+
referenceField: options.referenceField,
|
|
465
|
+
targetReferenceField: options.targetReferenceField,
|
|
466
|
+
junctionTable: options.junctionTable,
|
|
467
|
+
sourceField: options.sourceField,
|
|
468
|
+
targetField: options.targetField,
|
|
469
|
+
sourceSqlName: options.sourceSqlName,
|
|
470
|
+
targetSqlName: options.targetSqlName,
|
|
471
|
+
onDelete: options.onDelete,
|
|
472
|
+
onUpdate: options.onUpdate,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// ---------------------------------------------------------------------------
|
|
477
|
+
// Public API
|
|
478
|
+
// ---------------------------------------------------------------------------
|
|
479
|
+
|
|
480
|
+
function columnBuilderToJsonShape(builder: ColumnBuilder): JsonShape {
|
|
481
|
+
const def = builder.toDefinition();
|
|
482
|
+
if (def.jsonShape) return def.jsonShape;
|
|
483
|
+
if (def.type === "int") return { kind: "number" };
|
|
484
|
+
if (def.type === "string") return { kind: "string" };
|
|
485
|
+
if (def.type === "boolean") return { kind: "boolean" };
|
|
486
|
+
if (def.type === "date") return { kind: "date" };
|
|
487
|
+
if (def.type === "enum" && def.enumValues) {
|
|
488
|
+
return {
|
|
489
|
+
kind: "string",
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return { kind: "unknown" };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export const v = {
|
|
496
|
+
/** Define a reusable enum that can be referenced by multiple columns. */
|
|
497
|
+
defineEnum: <T extends string>(
|
|
498
|
+
name: string,
|
|
499
|
+
values: readonly T[],
|
|
500
|
+
): EnumBuilder<T> => defineEnum(name, values),
|
|
501
|
+
|
|
502
|
+
int: (options: ColumnBuilderOptions = {}) =>
|
|
503
|
+
new ColumnBuilder("int", options),
|
|
504
|
+
number: (options: ColumnBuilderOptions = {}) =>
|
|
505
|
+
new ColumnBuilder("int", options),
|
|
506
|
+
string: (options: ColumnBuilderOptions = {}) =>
|
|
507
|
+
new ColumnBuilder("string", options),
|
|
508
|
+
boolean: (options: ColumnBuilderOptions = {}) =>
|
|
509
|
+
new ColumnBuilder("boolean", options),
|
|
510
|
+
date: (options: ColumnBuilderOptions = {}) =>
|
|
511
|
+
new ColumnBuilder("date", options),
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* UUID primary key column.
|
|
515
|
+
* Stored as a string, marked as primary key and auto-populated with
|
|
516
|
+
* `crypto.randomUUID()` at runtime (no SQL sequence).
|
|
517
|
+
*/
|
|
518
|
+
uuid: (options: ColumnBuilderOptions = {}) =>
|
|
519
|
+
new ColumnBuilder({
|
|
520
|
+
kind: "column",
|
|
521
|
+
type: "string",
|
|
522
|
+
sqlName: options.sqlName,
|
|
523
|
+
length: options.length ?? 36,
|
|
524
|
+
primaryKey: true,
|
|
525
|
+
notNull: true,
|
|
526
|
+
nullable: false,
|
|
527
|
+
uuidPrimaryKey: true,
|
|
528
|
+
runtimeDefaultFn: () => crypto.randomUUID(),
|
|
529
|
+
}),
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Enum column builder.
|
|
533
|
+
* Pass an EnumBuilder (from v.defineEnum) or inline values.
|
|
534
|
+
*/
|
|
535
|
+
enum: <T extends string>(
|
|
536
|
+
valuesOrEnum: readonly T[] | EnumBuilder<T>,
|
|
537
|
+
options: ColumnBuilderOptions = {},
|
|
538
|
+
): ColumnBuilder => {
|
|
539
|
+
if (valuesOrEnum instanceof EnumBuilder) {
|
|
540
|
+
const def = valuesOrEnum.toDefinition();
|
|
541
|
+
return new ColumnBuilder("enum", {
|
|
542
|
+
...options,
|
|
543
|
+
enumValues: def.values,
|
|
544
|
+
enumRef: def.name,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
return new ColumnBuilder("enum", {
|
|
548
|
+
...options,
|
|
549
|
+
enumValues: valuesOrEnum,
|
|
550
|
+
});
|
|
551
|
+
},
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Enum array column builder.
|
|
555
|
+
* Stores multiple enum values in a single column.
|
|
556
|
+
*/
|
|
557
|
+
enumArray: <T extends string>(
|
|
558
|
+
valuesOrEnum: readonly T[] | EnumBuilder<T>,
|
|
559
|
+
options: ColumnBuilderOptions = {},
|
|
560
|
+
): ColumnBuilder => {
|
|
561
|
+
if (valuesOrEnum instanceof EnumBuilder) {
|
|
562
|
+
const def = valuesOrEnum.toDefinition();
|
|
563
|
+
return new ColumnBuilder("enum", {
|
|
564
|
+
...options,
|
|
565
|
+
enumValues: def.values,
|
|
566
|
+
enumRef: def.name,
|
|
567
|
+
isArray: true,
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
return new ColumnBuilder("enum", {
|
|
571
|
+
...options,
|
|
572
|
+
enumValues: valuesOrEnum,
|
|
573
|
+
isArray: true,
|
|
574
|
+
});
|
|
575
|
+
},
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* JSON array column builder.
|
|
579
|
+
* Stored as JSON text, auto-serialized on insert/query.
|
|
580
|
+
* Use v.string(), v.number(), v.object(), or v.array() as the element type.
|
|
581
|
+
* Example: v.array(v.string()), v.array(v.object({ name: v.string() }))
|
|
582
|
+
*/
|
|
583
|
+
array: (element: JsonArrayBuilder | ColumnBuilder): ColumnBuilder => {
|
|
584
|
+
if (element instanceof JsonArrayBuilder) {
|
|
585
|
+
return element.toColumnBuilder();
|
|
586
|
+
}
|
|
587
|
+
if (element instanceof ColumnBuilder) {
|
|
588
|
+
const def = element.toDefinition();
|
|
589
|
+
if (def.jsonShape) {
|
|
590
|
+
return new ColumnBuilder("json", { jsonShape: { kind: "array", element: def.jsonShape } });
|
|
591
|
+
}
|
|
592
|
+
const shape: JsonShape = {
|
|
593
|
+
kind: "array",
|
|
594
|
+
element: columnBuilderToJsonShape(element),
|
|
595
|
+
};
|
|
596
|
+
return new ColumnBuilder("json", { jsonShape: shape });
|
|
597
|
+
}
|
|
598
|
+
return new ColumnBuilder("json", { jsonShape: { kind: "unknown" } });
|
|
599
|
+
},
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* JSON object column builder.
|
|
603
|
+
* Stored as JSON text, auto-serialized on insert/query.
|
|
604
|
+
* Pass a shape record: v.object({ name: v.string(), count: v.number() })
|
|
605
|
+
*/
|
|
606
|
+
object: <TShape extends Record<string, JsonShape | ColumnBuilder>>(
|
|
607
|
+
shape: TShape,
|
|
608
|
+
): ColumnBuilder => {
|
|
609
|
+
const resolved: Record<string, JsonShape> = {};
|
|
610
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
611
|
+
if (value instanceof ColumnBuilder) {
|
|
612
|
+
resolved[key] = columnBuilderToJsonShape(value);
|
|
613
|
+
} else {
|
|
614
|
+
resolved[key] = value;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return new ColumnBuilder("json", {
|
|
618
|
+
jsonShape: { kind: "object", shape: resolved },
|
|
619
|
+
});
|
|
620
|
+
},
|
|
621
|
+
|
|
622
|
+
one: (
|
|
623
|
+
targetTable: string,
|
|
624
|
+
fieldOrOptions?: string | RelationOneOptions,
|
|
625
|
+
options: RelationOneOptions = {},
|
|
626
|
+
): OneRelationDefinition =>
|
|
627
|
+
buildOneRelation(targetTable, fieldOrOptions, options),
|
|
628
|
+
|
|
629
|
+
many: (
|
|
630
|
+
targetTable: string,
|
|
631
|
+
fieldOrOptions?: string | RelationManyOptions,
|
|
632
|
+
options: RelationManyOptions = {},
|
|
633
|
+
): ManyRelationDefinition =>
|
|
634
|
+
buildManyRelation(targetTable, fieldOrOptions, options),
|
|
635
|
+
|
|
636
|
+
manyToMany: (
|
|
637
|
+
targetTable: string,
|
|
638
|
+
options: RelationManyToManyOptions = {},
|
|
639
|
+
): ManyToManyRelationDefinition =>
|
|
640
|
+
buildManyToManyRelation(targetTable, options),
|
|
641
|
+
};
|