appflare 0.2.15 → 0.2.17

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.
@@ -27,7 +27,13 @@ type ResolveNativeFindManyWith<
27
27
  TName extends TableName,
28
28
  TArgs extends QueryFindManyArgs<TName> | undefined,
29
29
  > = TArgs extends { with?: infer TWith }
30
- ? Extract<TWith, NativeFindManyWith<TName>>
30
+ ? Extract<TWith, NativeFindManyWith<TName>> extends infer TResolved
31
+ ? [TResolved] extends [never]
32
+ ? TWith extends Record<string, unknown>
33
+ ? NativeFindManyWith<TName>
34
+ : never
35
+ : TResolved
36
+ : never
31
37
  : never;
32
38
  type ResolveTableFindManySelection<
33
39
  TName extends TableName,
@@ -41,35 +47,170 @@ type ResolveTableFindManySelection<
41
47
  type TableModel<TName extends TableName> = InferSelectModel<
42
48
  (typeof mergedSchema)[TName]
43
49
  >;
50
+ type ManyToManySchemaMap = typeof schema extends {
51
+ __appflareManyToMany: infer TMap extends Record<string, unknown>;
52
+ }
53
+ ? TMap
54
+ : {};
55
+ type ManyToManySourceTableMap<TSourceTable extends TableName> =
56
+ TSourceTable extends keyof ManyToManySchemaMap
57
+ ? ManyToManySchemaMap[TSourceTable] extends Record<string, unknown>
58
+ ? ManyToManySchemaMap[TSourceTable]
59
+ : {}
60
+ : {};
61
+ type ManyToManyTargetTableName<
62
+ TSourceTable extends TableName,
63
+ TRelationName extends string,
64
+ > = TRelationName extends keyof ManyToManySourceTableMap<TSourceTable>
65
+ ? ManyToManySourceTableMap<TSourceTable>[TRelationName] extends {
66
+ targetTable: infer TTarget extends string;
67
+ }
68
+ ? Extract<TTarget, TableName>
69
+ : never
70
+ : never;
71
+ type RuntimeRelationsSchemaMap = typeof schema extends {
72
+ __appflareRelations: infer TMap extends Record<string, unknown>;
73
+ }
74
+ ? TMap
75
+ : {};
76
+ type RuntimeRelationsSourceTableMap<TSourceTable extends TableName> =
77
+ TSourceTable extends keyof RuntimeRelationsSchemaMap
78
+ ? RuntimeRelationsSchemaMap[TSourceTable] extends Record<string, unknown>
79
+ ? RuntimeRelationsSchemaMap[TSourceTable]
80
+ : {}
81
+ : {};
82
+ type RuntimeRelationName<TSourceTable extends TableName> =
83
+ Extract<keyof RuntimeRelationsSourceTableMap<TSourceTable>, string>;
84
+ type RuntimeRelationConfig<
85
+ TSourceTable extends TableName,
86
+ TRelationName extends string,
87
+ > = TRelationName extends keyof RuntimeRelationsSourceTableMap<TSourceTable>
88
+ ? RuntimeRelationsSourceTableMap<TSourceTable>[TRelationName]
89
+ : never;
90
+ type RuntimeRelationKind<
91
+ TSourceTable extends TableName,
92
+ TRelationName extends string,
93
+ > = RuntimeRelationConfig<TSourceTable, TRelationName> extends {
94
+ kind: infer TKind extends string;
95
+ }
96
+ ? TKind
97
+ : never;
98
+ type RuntimeRelationTargetTable<
99
+ TSourceTable extends TableName,
100
+ TRelationName extends string,
101
+ > = RuntimeRelationConfig<TSourceTable, TRelationName> extends {
102
+ targetTable: infer TTarget extends string;
103
+ }
104
+ ? Extract<TTarget, TableName>
105
+ : never;
106
+ type TableInsertScalarModel<TName extends TableName> = TableInsertModel<TName>;
107
+ type FindManyWithFromArgs<TArgs> = TArgs extends { with?: infer TWith }
108
+ ? TWith
109
+ : undefined;
110
+ type ManyToManyRelationRows<
111
+ TTargetTable extends TableName,
112
+ TRelationArgs,
113
+ > = TRelationArgs extends true
114
+ ? Array<TableModel<TTargetTable>>
115
+ : TRelationArgs extends Record<string, unknown>
116
+ ? Awaited<
117
+ TableFindManyResult<
118
+ TTargetTable,
119
+ Extract<
120
+ Omit<TRelationArgs, "_count" | "_avg">,
121
+ QueryFindManyArgs<TTargetTable>
122
+ >
123
+ >
124
+ >
125
+ : Array<TableModel<TTargetTable>>;
126
+ type ReplaceManyToManyRelationsInRow<
127
+ TSourceTable extends TableName,
128
+ TRow,
129
+ TWith,
130
+ > = TRow extends Record<string, unknown>
131
+ ? {
132
+ [K in keyof TRow]: K extends string
133
+ ? TWith extends Record<string, unknown>
134
+ ? K extends keyof TWith
135
+ ? [ManyToManyTargetTableName<TSourceTable, K>] extends [never]
136
+ ? TRow[K]
137
+ : ManyToManyRelationRows<
138
+ Extract<ManyToManyTargetTableName<TSourceTable, K>, TableName>,
139
+ TWith[K]
140
+ >
141
+ : TRow[K]
142
+ : TRow[K]
143
+ : TRow[K];
144
+ }
145
+ : TRow;
146
+ type ApplyManyToManyFindManyResult<
147
+ TSourceTable extends TableName,
148
+ TArgs extends QueryFindManyArgs<TSourceTable> | undefined,
149
+ TResult,
150
+ > = TResult extends Promise<infer TRows>
151
+ ? TRows extends Array<infer TRow>
152
+ ? Promise<
153
+ Array<
154
+ ReplaceManyToManyRelationsInRow<
155
+ TSourceTable,
156
+ TRow,
157
+ FindManyWithFromArgs<TArgs>
158
+ >
159
+ >
160
+ >
161
+ : TResult
162
+ : TResult;
163
+ type ApplyManyToManyFindFirstResult<
164
+ TSourceTable extends TableName,
165
+ TArgs extends QueryFindFirstArgs<TSourceTable> | undefined,
166
+ TResult,
167
+ > = TResult extends Promise<infer TRow>
168
+ ? Promise<
169
+ TRow extends null
170
+ ? null
171
+ : ReplaceManyToManyRelationsInRow<
172
+ TSourceTable,
173
+ TRow,
174
+ FindManyWithFromArgs<TArgs>
175
+ >
176
+ >
177
+ : TResult;
44
178
  type TableFindManyResult<
45
179
  TName extends TableName,
46
180
  TArgs extends QueryFindManyArgs<TName> | undefined =
47
181
  | QueryFindManyArgs<TName>
48
182
  | undefined,
49
- > = TArgs extends undefined
50
- ? Promise<Array<TableModel<TName>>>
51
- : Promise<
52
- Array<
53
- BuildQueryResult<
54
- SchemaRelations,
55
- TableRelationConfig<TName>,
56
- ResolveTableFindManySelection<TName, TArgs>
183
+ > = ApplyManyToManyFindManyResult<
184
+ TName,
185
+ TArgs,
186
+ TArgs extends undefined
187
+ ? Promise<Array<TableModel<TName>>>
188
+ : Promise<
189
+ Array<
190
+ BuildQueryResult<
191
+ SchemaRelations,
192
+ TableRelationConfig<TName>,
193
+ ResolveTableFindManySelection<TName, TArgs>
194
+ >
57
195
  >
58
- >
59
- >;
196
+ >
197
+ >;
60
198
  type TableFindFirstArgs<TName extends TableName> = Omit<
61
199
  TableFindManyArgs<TName>,
62
200
  "limit"
63
201
  >;
64
- type NativeFindFirstWith<TName extends TableName> =
65
- NonNullable<TableFindFirstArgs<TName>> extends { with?: infer TWith }
66
- ? TWith
67
- : never;
202
+ type NativeFindFirstWith<TName extends TableName> = NativeFindManyWith<TName>;
68
203
  type ResolveNativeFindFirstWith<
69
204
  TName extends TableName,
70
205
  TArgs extends QueryFindFirstArgs<TName> | undefined,
71
206
  > = TArgs extends { with?: infer TWith }
72
- ? Extract<TWith, NativeFindFirstWith<TName>>
207
+ ? Extract<TWith, NativeFindFirstWith<TName>> extends infer TResolved
208
+ ? [TResolved] extends [never]
209
+ ? TWith extends Record<string, unknown>
210
+ ? NativeFindFirstWith<TName>
211
+ : never
212
+ : TResolved
213
+ : never
73
214
  : never;
74
215
  type ResolveTableFindFirstSelection<
75
216
  TName extends TableName,
@@ -85,15 +226,19 @@ type TableFindFirstResult<
85
226
  TArgs extends QueryFindFirstArgs<TName> | undefined =
86
227
  | QueryFindFirstArgs<TName>
87
228
  | undefined,
88
- > = TArgs extends undefined
89
- ? Promise<TableModel<TName> | null>
90
- : Promise<
91
- BuildQueryResult<
92
- SchemaRelations,
93
- TableRelationConfig<TName>,
94
- ResolveTableFindFirstSelection<TName, TArgs>
95
- > | null
96
- >;
229
+ > = ApplyManyToManyFindFirstResult<
230
+ TName,
231
+ TArgs,
232
+ TArgs extends undefined
233
+ ? Promise<TableModel<TName> | null>
234
+ : Promise<
235
+ BuildQueryResult<
236
+ SchemaRelations,
237
+ TableRelationConfig<TName>,
238
+ ResolveTableFindFirstSelection<TName, TArgs>
239
+ > | null
240
+ >
241
+ >;
97
242
  type TableInsertModel<TName extends TableName> = InferInsertModel<
98
243
  (typeof mergedSchema)[TName]
99
244
  >;
@@ -19,12 +19,17 @@ export function generateQueryRuntimeAggregateAndFooterSection(): string {
19
19
  const transformedWith =
20
20
  withValue === undefined
21
21
  ? undefined
22
- : transformWithRelations(withValue);
22
+ : transformWithRelations(withValue, tableName);
23
23
  const rawRows = await queryTable.findMany({
24
24
  ...(whereFilter ? { where: () => whereFilter } : {}),
25
25
  ...(transformedWith !== undefined ? { with: transformedWith } : {}),
26
26
  });
27
- const rows = Array.isArray(rawRows) ? rawRows : [];
27
+ const flattenedRows = flattenManyToManyResult(
28
+ tableName,
29
+ rawRows,
30
+ withValue,
31
+ );
32
+ const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
28
33
  const constrainedRows = hasAggregateWithConstraints(withValue)
29
34
  ? rows.filter((row) =>
30
35
  rowMatchesAggregateWithConstraints(row, withValue),
@@ -97,12 +102,17 @@ export function generateQueryRuntimeAggregateAndFooterSection(): string {
97
102
  const transformedWith =
98
103
  withValue === undefined
99
104
  ? undefined
100
- : transformWithRelations(withValue);
105
+ : transformWithRelations(withValue, tableName);
101
106
  const rawRows = await queryTable.findMany({
102
107
  ...(whereFilter ? { where: () => whereFilter } : {}),
103
108
  ...(transformedWith !== undefined ? { with: transformedWith } : {}),
104
109
  });
105
- const rows = Array.isArray(rawRows) ? rawRows : [];
110
+ const flattenedRows = flattenManyToManyResult(
111
+ tableName,
112
+ rawRows,
113
+ withValue,
114
+ );
115
+ const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
106
116
  const constrainedRows = hasAggregateWithConstraints(withValue)
107
117
  ? rows.filter((row) =>
108
118
  rowMatchesAggregateWithConstraints(row, withValue),
@@ -19,10 +19,19 @@ export function generateQueryRuntimeReadSection(): string {
19
19
  with: undefined,
20
20
  aggregatePlan: undefined,
21
21
  }
22
- : transformWithRelationsAndExtractAggregates(withValue);
22
+ : transformWithRelationsAndExtractAggregates(withValue, tableName);
23
23
  const transformedWith = transformedWithResult.with;
24
24
  const aggregatePlan = transformedWithResult.aggregatePlan;
25
- if (!whereFilter && transformedWith === withValue && !aggregatePlan) {
25
+ const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
26
+ tableName,
27
+ withValue,
28
+ );
29
+ if (
30
+ !whereFilter &&
31
+ transformedWith === withValue &&
32
+ !aggregatePlan &&
33
+ !requiresManyToManyFlatten
34
+ ) {
26
35
  return queryTable.findMany(passthroughArgs);
27
36
  }
28
37
 
@@ -32,13 +41,22 @@ export function generateQueryRuntimeReadSection(): string {
32
41
  ...(transformedWith !== undefined ? { with: transformedWith } : {}),
33
42
  });
34
43
 
35
- if (!aggregatePlan) {
36
- return queryPromise;
37
- }
44
+ return queryPromise.then((result) => {
45
+ const flattenedResult = flattenManyToManyResult(
46
+ tableName,
47
+ result,
48
+ withValue,
49
+ );
38
50
 
39
- return queryPromise.then((result) =>
40
- applyRelationAggregatePlanToResult(result, aggregatePlan),
41
- );
51
+ if (!aggregatePlan) {
52
+ return flattenedResult;
53
+ }
54
+
55
+ return applyRelationAggregatePlanToResult(
56
+ flattenedResult,
57
+ aggregatePlan,
58
+ );
59
+ });
42
60
  },
43
61
  findFirst: (args?: Record<string, unknown>) => {
44
62
  const where = isRecord(args?.where)
@@ -60,10 +78,19 @@ export function generateQueryRuntimeReadSection(): string {
60
78
  with: undefined,
61
79
  aggregatePlan: undefined,
62
80
  }
63
- : transformWithRelationsAndExtractAggregates(withValue);
81
+ : transformWithRelationsAndExtractAggregates(withValue, tableName);
64
82
  const transformedWith = transformedWithResult.with;
65
83
  const aggregatePlan = transformedWithResult.aggregatePlan;
66
- if (!whereFilter && transformedWith === withValue && !aggregatePlan) {
84
+ const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
85
+ tableName,
86
+ withValue,
87
+ );
88
+ if (
89
+ !whereFilter &&
90
+ transformedWith === withValue &&
91
+ !aggregatePlan &&
92
+ !requiresManyToManyFlatten
93
+ ) {
67
94
  return queryTable.findFirst(passthroughArgs);
68
95
  }
69
96
 
@@ -73,13 +100,22 @@ export function generateQueryRuntimeReadSection(): string {
73
100
  ...(transformedWith !== undefined ? { with: transformedWith } : {}),
74
101
  });
75
102
 
76
- if (!aggregatePlan) {
77
- return queryPromise;
78
- }
103
+ return queryPromise.then((result) => {
104
+ const flattenedResult = flattenManyToManyResult(
105
+ tableName,
106
+ result,
107
+ withValue,
108
+ );
79
109
 
80
- return queryPromise.then((result) =>
81
- applyRelationAggregatePlanToResult(result, aggregatePlan),
82
- );
110
+ if (!aggregatePlan) {
111
+ return flattenedResult;
112
+ }
113
+
114
+ return applyRelationAggregatePlanToResult(
115
+ flattenedResult,
116
+ aggregatePlan,
117
+ );
118
+ });
83
119
  },
84
120
  `;
85
121
  }