appflare 0.2.49 → 0.2.50

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.
@@ -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: mergedSchema });
13
+ drizzle(database, { schema: mergedDbSchema });
9
14
 
10
15
  export type AppflareDb = ReturnType<typeof createDb>;
11
16
 
12
- type SchemaRelations = ExtractTablesWithRelations<typeof mergedSchema>;
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
- ? Extract<TWith, NativeFindManyWith<TName>> extends infer TResolved
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 mergedSchema)[TName]
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,
@@ -236,13 +365,7 @@ type ResolveNativeFindFirstWith<
236
365
  TName extends TableName,
237
366
  TArgs extends QueryFindFirstArgs<TName> | undefined,
238
367
  > = TArgs extends { with?: infer TWith }
239
- ? Extract<TWith, NativeFindFirstWith<TName>> extends infer TResolved
240
- ? [TResolved] extends [never]
241
- ? TWith extends Record<string, unknown>
242
- ? NormalizeWhereInWith<TWith, NativeFindFirstWith<TName>>
243
- : never
244
- : TResolved
245
- : never
368
+ ? NormalizeNativeWithForTable<TName, TWith>
246
369
  : never;
247
370
  type ResolveTableFindFirstSelection<
248
371
  TName extends TableName,
@@ -272,7 +395,7 @@ type TableFindFirstResult<
272
395
  >
273
396
  >;
274
397
  type TableInsertModel<TName extends TableName> = InferInsertModel<
275
- (typeof mergedSchema)[TName]
398
+ (typeof mergedDbSchema)[TName]
276
399
  >;
277
400
  `;
278
401
  }