appflare 0.2.49 → 0.2.51
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/cli/schema-compiler.ts
CHANGED
|
@@ -965,11 +965,13 @@ function emitDrizzleSchema(
|
|
|
965
965
|
|
|
966
966
|
const tableBlocks: string[] = [];
|
|
967
967
|
const relationBlocks: string[] = [];
|
|
968
|
+
const dbSchemaEntries: string[] = [];
|
|
968
969
|
|
|
969
970
|
for (const [tableName, table] of Object.entries(definition.tables)) {
|
|
970
971
|
const sqlTableName = table.sqlName ?? toSnakeCase(tableName);
|
|
971
972
|
const columnLines: string[] = [];
|
|
972
973
|
const indexes: string[] = [];
|
|
974
|
+
dbSchemaEntries.push(`\t${tableName},`);
|
|
973
975
|
|
|
974
976
|
for (const [fieldName, column] of Object.entries(table.columns)) {
|
|
975
977
|
let expr: string;
|
|
@@ -1018,9 +1020,9 @@ function emitDrizzleSchema(
|
|
|
1018
1020
|
if (column.references.onUpdate) {
|
|
1019
1021
|
actions.push(`onUpdate: ${quote(column.references.onUpdate)}`);
|
|
1020
1022
|
}
|
|
1021
|
-
expr += `.references(() => ${reference.tableName}.${reference.fieldName}, { ${actions.join(", ")} })`;
|
|
1023
|
+
expr += `.references((): AnySQLiteColumn => ${reference.tableName}.${reference.fieldName}, { ${actions.join(", ")} })`;
|
|
1022
1024
|
} else {
|
|
1023
|
-
expr += `.references(() => ${reference.tableName}.${reference.fieldName})`;
|
|
1025
|
+
expr += `.references((): AnySQLiteColumn => ${reference.tableName}.${reference.fieldName})`;
|
|
1024
1026
|
}
|
|
1025
1027
|
}
|
|
1026
1028
|
|
|
@@ -1101,10 +1103,11 @@ function emitDrizzleSchema(
|
|
|
1101
1103
|
relationBlocks.push(
|
|
1102
1104
|
`export const ${tableName}Relations = relations(${tableName}, ({ one, many }) => ({\n${relationLines.join("\n")}\n}));`,
|
|
1103
1105
|
);
|
|
1106
|
+
dbSchemaEntries.push(`\t${tableName}Relations,`);
|
|
1104
1107
|
}
|
|
1105
1108
|
|
|
1106
1109
|
return `import * as t from "drizzle-orm/sqlite-core";
|
|
1107
|
-
import { sqliteTable as table } from "drizzle-orm/sqlite-core";
|
|
1110
|
+
import { sqliteTable as table, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
|
|
1108
1111
|
import { relations } from "drizzle-orm";
|
|
1109
1112
|
${buildExternalTableImportLines(externalTables)}
|
|
1110
1113
|
${enumTypeLines.join("\n")}
|
|
@@ -1114,6 +1117,10 @@ ${tableBlocks.join("\n\n")}
|
|
|
1114
1117
|
|
|
1115
1118
|
${relationBlocks.join("\n\n")}
|
|
1116
1119
|
|
|
1120
|
+
export const __appflareDbSchema = {
|
|
1121
|
+
${dbSchemaEntries.join("\n")}
|
|
1122
|
+
} as const;
|
|
1123
|
+
|
|
1117
1124
|
${emitJsonColumnsMetadata(definition)}
|
|
1118
1125
|
|
|
1119
1126
|
${emitManyToManyRuntimeMetadata(definition)}
|
|
@@ -4,12 +4,17 @@ export function generateSchemaAndTableTypesSection(): string {
|
|
|
4
4
|
...schema,
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
+
const mergedDbSchema = {
|
|
8
|
+
...authSchema,
|
|
9
|
+
...schema.__appflareDbSchema,
|
|
10
|
+
};
|
|
11
|
+
|
|
7
12
|
export const createDb = (database: D1Database) =>
|
|
8
|
-
drizzle(database, { schema:
|
|
13
|
+
drizzle(database, { schema: mergedDbSchema });
|
|
9
14
|
|
|
10
15
|
export type AppflareDb = ReturnType<typeof createDb>;
|
|
11
16
|
|
|
12
|
-
type SchemaRelations = ExtractTablesWithRelations<typeof
|
|
17
|
+
type SchemaRelations = ExtractTablesWithRelations<typeof mergedDbSchema>;
|
|
13
18
|
type TableName = Extract<keyof SchemaRelations, string>;
|
|
14
19
|
type TableRelationConfig<TName extends TableName> =
|
|
15
20
|
TName extends keyof SchemaRelations ? SchemaRelations[TName] : never;
|
|
@@ -23,29 +28,11 @@ type NativeFindManyWith<TName extends TableName> =
|
|
|
23
28
|
NonNullable<TableFindManyArgs<TName>> extends { with?: infer TWith }
|
|
24
29
|
? TWith
|
|
25
30
|
: never;
|
|
26
|
-
type NormalizeWhereInWith<TWith, TNativeWith> = TWith extends Record<string, unknown>
|
|
27
|
-
? {
|
|
28
|
-
[K in keyof TWith]: K extends keyof TNativeWith
|
|
29
|
-
? TWith[K] extends Record<string, unknown>
|
|
30
|
-
? Omit<TWith[K], 'where'> & { where?: NonNullable<Extract<TNativeWith[K], Record<string, unknown>>['where']> } & (TWith[K] extends { with: infer TNestedWith }
|
|
31
|
-
? { with: NormalizeWhereInWith<TNestedWith, TNativeWith[K]> }
|
|
32
|
-
: {})
|
|
33
|
-
: TWith[K]
|
|
34
|
-
: TWith[K];
|
|
35
|
-
}
|
|
36
|
-
: TWith;
|
|
37
|
-
|
|
38
31
|
type ResolveNativeFindManyWith<
|
|
39
32
|
TName extends TableName,
|
|
40
33
|
TArgs extends QueryFindManyArgs<TName> | undefined,
|
|
41
34
|
> = TArgs extends { with?: infer TWith }
|
|
42
|
-
?
|
|
43
|
-
? [TResolved] extends [never]
|
|
44
|
-
? TWith extends Record<string, unknown>
|
|
45
|
-
? NormalizeWhereInWith<TWith, NativeFindManyWith<TName>>
|
|
46
|
-
: never
|
|
47
|
-
: TResolved
|
|
48
|
-
: never
|
|
35
|
+
? NormalizeNativeWithForTable<TName, TWith>
|
|
49
36
|
: never;
|
|
50
37
|
type ResolveTableFindManySelection<
|
|
51
38
|
TName extends TableName,
|
|
@@ -57,7 +44,7 @@ type ResolveTableFindManySelection<
|
|
|
57
44
|
? {}
|
|
58
45
|
: { with: ResolveNativeFindManyWith<TName, TArgs> });
|
|
59
46
|
type TableModel<TName extends TableName> = InferSelectModel<
|
|
60
|
-
(typeof
|
|
47
|
+
(typeof mergedDbSchema)[TName]
|
|
61
48
|
>;
|
|
62
49
|
type ManyToManySchemaMap = typeof schema extends {
|
|
63
50
|
__appflareManyToMany: infer TMap extends Record<string, unknown>;
|
|
@@ -115,6 +102,148 @@ type RuntimeRelationTargetTable<
|
|
|
115
102
|
}
|
|
116
103
|
? Extract<TTarget, TableName>
|
|
117
104
|
: never;
|
|
105
|
+
type NativeRelationEntry<
|
|
106
|
+
TSourceTable extends TableName,
|
|
107
|
+
TRelationName extends string,
|
|
108
|
+
> = TRelationName extends keyof NativeFindManyWith<TSourceTable>
|
|
109
|
+
? NativeFindManyWith<TSourceTable>[TRelationName]
|
|
110
|
+
: never;
|
|
111
|
+
type NativeRelationWhere<TEntry> = Extract<
|
|
112
|
+
TEntry,
|
|
113
|
+
Record<string, unknown>
|
|
114
|
+
> extends {
|
|
115
|
+
where?: infer TWhere;
|
|
116
|
+
}
|
|
117
|
+
? TWhere
|
|
118
|
+
: never;
|
|
119
|
+
type ManyToManyJunctionTableName<
|
|
120
|
+
TSourceTable extends TableName,
|
|
121
|
+
TRelationName extends string,
|
|
122
|
+
> = TRelationName extends keyof ManyToManySourceTableMap<TSourceTable>
|
|
123
|
+
? ManyToManySourceTableMap<TSourceTable>[TRelationName] extends {
|
|
124
|
+
junctionTable: infer TJunctionTable extends string;
|
|
125
|
+
}
|
|
126
|
+
? Extract<TJunctionTable, TableName>
|
|
127
|
+
: never
|
|
128
|
+
: never;
|
|
129
|
+
type CleanRelationConfig<TEntry> = Omit<
|
|
130
|
+
Extract<TEntry, Record<string, unknown>>,
|
|
131
|
+
"where" | "with" | "_count" | "_avg"
|
|
132
|
+
>;
|
|
133
|
+
type NormalizeNativeRelationConfig<
|
|
134
|
+
TSourceTable extends TableName,
|
|
135
|
+
TRelationName extends string,
|
|
136
|
+
TEntry,
|
|
137
|
+
> = CleanRelationConfig<TEntry> &
|
|
138
|
+
(Extract<TEntry, Record<string, unknown>> extends { where?: unknown }
|
|
139
|
+
? {
|
|
140
|
+
where?: NativeRelationWhere<
|
|
141
|
+
NativeRelationEntry<TSourceTable, TRelationName>
|
|
142
|
+
>;
|
|
143
|
+
}
|
|
144
|
+
: {}) &
|
|
145
|
+
(Extract<TEntry, Record<string, unknown>> extends { with?: infer TNestedWith }
|
|
146
|
+
? RuntimeRelationTargetTable<TSourceTable, TRelationName> extends infer TTargetTable
|
|
147
|
+
? TTargetTable extends TableName
|
|
148
|
+
? {
|
|
149
|
+
with?: NormalizeNativeWithForTable<TTargetTable, TNestedWith>;
|
|
150
|
+
}
|
|
151
|
+
: {}
|
|
152
|
+
: {}
|
|
153
|
+
: {});
|
|
154
|
+
type NormalizeManyToManyTargetConfig<
|
|
155
|
+
TSourceTable extends TableName,
|
|
156
|
+
TRelationName extends string,
|
|
157
|
+
TEntry,
|
|
158
|
+
> = ManyToManyTargetTableName<
|
|
159
|
+
TSourceTable,
|
|
160
|
+
TRelationName
|
|
161
|
+
> extends infer TTargetTable
|
|
162
|
+
? TTargetTable extends TableName
|
|
163
|
+
? CleanRelationConfig<TEntry> &
|
|
164
|
+
(Extract<TEntry, Record<string, unknown>> extends { where?: unknown }
|
|
165
|
+
? ManyToManyJunctionTableName<
|
|
166
|
+
TSourceTable,
|
|
167
|
+
TRelationName
|
|
168
|
+
> extends infer TJunctionTable
|
|
169
|
+
? TJunctionTable extends TableName
|
|
170
|
+
? {
|
|
171
|
+
where?: NativeRelationWhere<
|
|
172
|
+
NativeRelationEntry<TJunctionTable, TTargetTable>
|
|
173
|
+
>;
|
|
174
|
+
}
|
|
175
|
+
: {}
|
|
176
|
+
: {}
|
|
177
|
+
: {}) &
|
|
178
|
+
(Extract<TEntry, Record<string, unknown>> extends {
|
|
179
|
+
with?: infer TNestedWith;
|
|
180
|
+
}
|
|
181
|
+
? {
|
|
182
|
+
with?: NormalizeNativeWithForTable<TTargetTable, TNestedWith>;
|
|
183
|
+
}
|
|
184
|
+
: {})
|
|
185
|
+
: never
|
|
186
|
+
: never;
|
|
187
|
+
type NormalizeRelationInputForTable<
|
|
188
|
+
TSourceTable extends TableName,
|
|
189
|
+
TRelationName extends string,
|
|
190
|
+
TEntry,
|
|
191
|
+
> = [ManyToManyTargetTableName<TSourceTable, TRelationName>] extends [never]
|
|
192
|
+
? (Extract<TEntry, true> extends never ? never : true) |
|
|
193
|
+
(Extract<TEntry, Record<string, unknown>> extends infer TConfig
|
|
194
|
+
? TConfig extends Record<string, unknown>
|
|
195
|
+
? NormalizeNativeRelationConfig<
|
|
196
|
+
TSourceTable,
|
|
197
|
+
TRelationName,
|
|
198
|
+
TConfig
|
|
199
|
+
>
|
|
200
|
+
: never
|
|
201
|
+
: never)
|
|
202
|
+
: ManyToManyTargetTableName<
|
|
203
|
+
TSourceTable,
|
|
204
|
+
TRelationName
|
|
205
|
+
> extends infer TTargetTable
|
|
206
|
+
? TTargetTable extends TableName
|
|
207
|
+
? (Extract<TEntry, true> extends never
|
|
208
|
+
? never
|
|
209
|
+
: {
|
|
210
|
+
with: {
|
|
211
|
+
[K in TTargetTable]: true;
|
|
212
|
+
};
|
|
213
|
+
}) |
|
|
214
|
+
(Extract<TEntry, Record<string, unknown>> extends infer TConfig
|
|
215
|
+
? TConfig extends Record<string, unknown>
|
|
216
|
+
? CleanRelationConfig<TConfig> & {
|
|
217
|
+
with: {
|
|
218
|
+
[K in TTargetTable]: keyof NormalizeManyToManyTargetConfig<
|
|
219
|
+
TSourceTable,
|
|
220
|
+
TRelationName,
|
|
221
|
+
TConfig
|
|
222
|
+
> extends never
|
|
223
|
+
? true
|
|
224
|
+
: NormalizeManyToManyTargetConfig<
|
|
225
|
+
TSourceTable,
|
|
226
|
+
TRelationName,
|
|
227
|
+
TConfig
|
|
228
|
+
>;
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
: never
|
|
232
|
+
: never)
|
|
233
|
+
: never
|
|
234
|
+
: never;
|
|
235
|
+
type NormalizeNativeWithForTable<
|
|
236
|
+
TSourceTable extends TableName,
|
|
237
|
+
TWith,
|
|
238
|
+
> = TWith extends Record<string, unknown>
|
|
239
|
+
? {
|
|
240
|
+
[K in keyof TWith]: NormalizeRelationInputForTable<
|
|
241
|
+
TSourceTable,
|
|
242
|
+
Extract<K, string>,
|
|
243
|
+
TWith[K]
|
|
244
|
+
>;
|
|
245
|
+
}
|
|
246
|
+
: TWith;
|
|
118
247
|
|
|
119
248
|
type TargetTableForRelation<
|
|
120
249
|
TSource extends TableName,
|
|
@@ -145,6 +274,48 @@ type ManyToManyRelationRows<
|
|
|
145
274
|
>
|
|
146
275
|
>
|
|
147
276
|
: Array<TableModel<TTargetTable>>;
|
|
277
|
+
type NativeManyRelationRows<
|
|
278
|
+
TSourceTable extends TableName,
|
|
279
|
+
TRelationName extends string,
|
|
280
|
+
TRelationArgs,
|
|
281
|
+
> = RuntimeRelationTargetTable<TSourceTable, TRelationName> extends infer TTarget
|
|
282
|
+
? TTarget extends TableName
|
|
283
|
+
? TRelationArgs extends true
|
|
284
|
+
? Array<TableModel<TTarget>>
|
|
285
|
+
: TRelationArgs extends Record<string, unknown>
|
|
286
|
+
? Awaited<
|
|
287
|
+
TableFindManyResult<
|
|
288
|
+
TTarget,
|
|
289
|
+
Extract<
|
|
290
|
+
Omit<TRelationArgs, "_count" | "_avg">,
|
|
291
|
+
QueryFindManyArgs<TTarget>
|
|
292
|
+
>
|
|
293
|
+
>
|
|
294
|
+
>
|
|
295
|
+
: Array<TableModel<TTarget>>
|
|
296
|
+
: never
|
|
297
|
+
: never;
|
|
298
|
+
type NativeOneRelationValue<
|
|
299
|
+
TSourceTable extends TableName,
|
|
300
|
+
TRelationName extends string,
|
|
301
|
+
TRelationArgs,
|
|
302
|
+
> = RuntimeRelationTargetTable<TSourceTable, TRelationName> extends infer TTarget
|
|
303
|
+
? TTarget extends TableName
|
|
304
|
+
? TRelationArgs extends true
|
|
305
|
+
? TableModel<TTarget> | null
|
|
306
|
+
: TRelationArgs extends Record<string, unknown>
|
|
307
|
+
? Awaited<
|
|
308
|
+
TableFindFirstResult<
|
|
309
|
+
TTarget,
|
|
310
|
+
Extract<
|
|
311
|
+
Omit<TRelationArgs, "_count" | "_avg">,
|
|
312
|
+
QueryFindFirstArgs<TTarget>
|
|
313
|
+
>
|
|
314
|
+
>
|
|
315
|
+
>
|
|
316
|
+
: TableModel<TTarget> | null
|
|
317
|
+
: never
|
|
318
|
+
: never;
|
|
148
319
|
type ReplaceManyToManyRelationsInRow<
|
|
149
320
|
TSourceTable extends string,
|
|
150
321
|
TRow,
|
|
@@ -155,15 +326,33 @@ type ReplaceManyToManyRelationsInRow<
|
|
|
155
326
|
? TWith extends Record<string, unknown>
|
|
156
327
|
? K extends keyof TWith
|
|
157
328
|
? [ManyToManyTargetTableName<Extract<TSourceTable, TableName>, K>] extends [never]
|
|
158
|
-
?
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
329
|
+
? RuntimeRelationKind<Extract<TSourceTable, TableName>, K> extends "many"
|
|
330
|
+
? NativeManyRelationRows<
|
|
331
|
+
Extract<TSourceTable, TableName>,
|
|
332
|
+
K,
|
|
333
|
+
TWith[K]
|
|
334
|
+
>
|
|
335
|
+
: RuntimeRelationKind<Extract<TSourceTable, TableName>, K> extends "one"
|
|
336
|
+
? NativeOneRelationValue<
|
|
337
|
+
Extract<TSourceTable, TableName>,
|
|
338
|
+
K,
|
|
339
|
+
TWith[K]
|
|
340
|
+
>
|
|
341
|
+
: ReplaceManyToManyRelationsInRow<
|
|
342
|
+
[RuntimeRelationTargetTable<
|
|
343
|
+
Extract<TSourceTable, TableName>,
|
|
344
|
+
K
|
|
345
|
+
>] extends [never]
|
|
346
|
+
? K
|
|
347
|
+
: RuntimeRelationTargetTable<
|
|
348
|
+
Extract<TSourceTable, TableName>,
|
|
349
|
+
K
|
|
350
|
+
>,
|
|
351
|
+
TRow[K],
|
|
352
|
+
TWith[K] extends { with: infer TNestedWith }
|
|
353
|
+
? TNestedWith
|
|
354
|
+
: undefined
|
|
355
|
+
>
|
|
167
356
|
: ManyToManyRelationRows<
|
|
168
357
|
Extract<ManyToManyTargetTableName<Extract<TSourceTable, TableName>, K>, TableName>,
|
|
169
358
|
TWith[K]
|
|
@@ -236,13 +425,7 @@ type ResolveNativeFindFirstWith<
|
|
|
236
425
|
TName extends TableName,
|
|
237
426
|
TArgs extends QueryFindFirstArgs<TName> | undefined,
|
|
238
427
|
> = TArgs extends { with?: infer TWith }
|
|
239
|
-
?
|
|
240
|
-
? [TResolved] extends [never]
|
|
241
|
-
? TWith extends Record<string, unknown>
|
|
242
|
-
? NormalizeWhereInWith<TWith, NativeFindFirstWith<TName>>
|
|
243
|
-
: never
|
|
244
|
-
: TResolved
|
|
245
|
-
: never
|
|
428
|
+
? NormalizeNativeWithForTable<TName, TWith>
|
|
246
429
|
: never;
|
|
247
430
|
type ResolveTableFindFirstSelection<
|
|
248
431
|
TName extends TableName,
|
|
@@ -272,7 +455,7 @@ type TableFindFirstResult<
|
|
|
272
455
|
>
|
|
273
456
|
>;
|
|
274
457
|
type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
275
|
-
(typeof
|
|
458
|
+
(typeof mergedDbSchema)[TName]
|
|
276
459
|
>;
|
|
277
460
|
`;
|
|
278
461
|
}
|