appflare 0.2.14 → 0.2.16

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,
@@ -38,19 +44,159 @@ type ResolveTableFindManySelection<
38
44
  (ResolveNativeFindManyWith<TName, TArgs> extends never
39
45
  ? {}
40
46
  : { with: ResolveNativeFindManyWith<TName, TArgs> });
47
+ type TableModel<TName extends TableName> = InferSelectModel<
48
+ (typeof mergedSchema)[TName]
49
+ >;
50
+ type ManyToManySchemaMap = typeof schema extends {
51
+ __appflareManyToMany: infer TMap;
52
+ }
53
+ ? TMap extends Record<string, Record<string, { targetTable: string }>>
54
+ ? TMap
55
+ : {}
56
+ : {};
57
+ type ManyToManyTargetTableName<
58
+ TSourceTable extends TableName,
59
+ TRelationName extends string,
60
+ > = TSourceTable extends keyof ManyToManySchemaMap
61
+ ? TRelationName extends keyof ManyToManySchemaMap[TSourceTable]
62
+ ? ManyToManySchemaMap[TSourceTable][TRelationName] extends {
63
+ targetTable: infer TTarget extends string;
64
+ }
65
+ ? Extract<TTarget, TableName>
66
+ : never
67
+ : never
68
+ : never;
69
+ type RuntimeRelationsSchemaMap = typeof schema extends {
70
+ __appflareRelations: infer TMap;
71
+ }
72
+ ? TMap extends Record<
73
+ string,
74
+ Record<string, { kind: string; targetTable: string }>
75
+ >
76
+ ? TMap
77
+ : {}
78
+ : {};
79
+ type RuntimeRelationName<TSourceTable extends TableName> =
80
+ TSourceTable extends keyof RuntimeRelationsSchemaMap
81
+ ? Extract<keyof RuntimeRelationsSchemaMap[TSourceTable], string>
82
+ : never;
83
+ type RuntimeRelationConfig<
84
+ TSourceTable extends TableName,
85
+ TRelationName extends string,
86
+ > = TSourceTable extends keyof RuntimeRelationsSchemaMap
87
+ ? TRelationName extends keyof RuntimeRelationsSchemaMap[TSourceTable]
88
+ ? RuntimeRelationsSchemaMap[TSourceTable][TRelationName]
89
+ : never
90
+ : never;
91
+ type RuntimeRelationKind<
92
+ TSourceTable extends TableName,
93
+ TRelationName extends string,
94
+ > = RuntimeRelationConfig<TSourceTable, TRelationName> extends {
95
+ kind: infer TKind extends string;
96
+ }
97
+ ? TKind
98
+ : never;
99
+ type RuntimeRelationTargetTable<
100
+ TSourceTable extends TableName,
101
+ TRelationName extends string,
102
+ > = RuntimeRelationConfig<TSourceTable, TRelationName> extends {
103
+ targetTable: infer TTarget extends string;
104
+ }
105
+ ? Extract<TTarget, TableName>
106
+ : never;
107
+ type TableInsertScalarModel<TName extends TableName> = Omit<
108
+ TableInsertModel<TName>,
109
+ RuntimeRelationName<TName>
110
+ >;
111
+ type FindManyWithFromArgs<TArgs> = TArgs extends { with?: infer TWith }
112
+ ? TWith
113
+ : undefined;
114
+ type ManyToManyRelationRows<
115
+ TTargetTable extends TableName,
116
+ TRelationArgs,
117
+ > = TRelationArgs extends true
118
+ ? Array<TableModel<TTargetTable>>
119
+ : TRelationArgs extends Record<string, unknown>
120
+ ? Awaited<
121
+ TableFindManyResult<
122
+ TTargetTable,
123
+ Extract<
124
+ Omit<TRelationArgs, "_count" | "_avg">,
125
+ QueryFindManyArgs<TTargetTable>
126
+ >
127
+ >
128
+ >
129
+ : Array<TableModel<TTargetTable>>;
130
+ type ReplaceManyToManyRelationsInRow<
131
+ TSourceTable extends TableName,
132
+ TRow,
133
+ TWith,
134
+ > = TRow extends Record<string, unknown>
135
+ ? {
136
+ [K in keyof TRow]: K extends string
137
+ ? TWith extends Record<string, unknown>
138
+ ? K extends keyof TWith
139
+ ? ManyToManyTargetTableName<TSourceTable, K> extends infer TTarget
140
+ ? TTarget extends TableName
141
+ ? ManyToManyRelationRows<TTarget, TWith[K]>
142
+ : TRow[K]
143
+ : TRow[K]
144
+ : TRow[K]
145
+ : TRow[K]
146
+ : TRow[K];
147
+ }
148
+ : TRow;
149
+ type ApplyManyToManyFindManyResult<
150
+ TSourceTable extends TableName,
151
+ TArgs extends QueryFindManyArgs<TSourceTable> | undefined,
152
+ TResult,
153
+ > = TResult extends Promise<infer TRows>
154
+ ? TRows extends Array<infer TRow>
155
+ ? Promise<
156
+ Array<
157
+ ReplaceManyToManyRelationsInRow<
158
+ TSourceTable,
159
+ TRow,
160
+ FindManyWithFromArgs<TArgs>
161
+ >
162
+ >
163
+ >
164
+ : TResult
165
+ : TResult;
166
+ type ApplyManyToManyFindFirstResult<
167
+ TSourceTable extends TableName,
168
+ TArgs extends QueryFindFirstArgs<TSourceTable> | undefined,
169
+ TResult,
170
+ > = TResult extends Promise<infer TRow>
171
+ ? Promise<
172
+ TRow extends null
173
+ ? null
174
+ : ReplaceManyToManyRelationsInRow<
175
+ TSourceTable,
176
+ TRow,
177
+ FindManyWithFromArgs<TArgs>
178
+ >
179
+ >
180
+ : TResult;
41
181
  type TableFindManyResult<
42
182
  TName extends TableName,
43
183
  TArgs extends QueryFindManyArgs<TName> | undefined =
44
184
  | QueryFindManyArgs<TName>
45
185
  | undefined,
46
- > = Promise<
47
- Array<
48
- BuildQueryResult<
49
- SchemaRelations,
50
- TableRelationConfig<TName>,
51
- ResolveTableFindManySelection<TName, TArgs>
52
- >
53
- >
186
+ > = ApplyManyToManyFindManyResult<
187
+ TName,
188
+ TArgs,
189
+ TArgs extends undefined
190
+ ? Promise<Array<TableModel<TName>>>
191
+ : Promise<
192
+ Array<
193
+ BuildQueryResult<
194
+ SchemaRelations,
195
+ TableRelationConfig<TName>,
196
+ ResolveTableFindManySelection<TName, TArgs>
197
+ >
198
+ >
199
+ >
54
200
  >;
55
201
  type TableFindFirstArgs<TName extends TableName> = Omit<
56
202
  TableFindManyArgs<TName>,
@@ -64,7 +210,13 @@ type ResolveNativeFindFirstWith<
64
210
  TName extends TableName,
65
211
  TArgs extends QueryFindFirstArgs<TName> | undefined,
66
212
  > = TArgs extends { with?: infer TWith }
67
- ? Extract<TWith, NativeFindFirstWith<TName>>
213
+ ? Extract<TWith, NativeFindFirstWith<TName>> extends infer TResolved
214
+ ? [TResolved] extends [never]
215
+ ? TWith extends Record<string, unknown>
216
+ ? NativeFindFirstWith<TName>
217
+ : never
218
+ : TResolved
219
+ : never
68
220
  : never;
69
221
  type ResolveTableFindFirstSelection<
70
222
  TName extends TableName,
@@ -80,15 +232,18 @@ type TableFindFirstResult<
80
232
  TArgs extends QueryFindFirstArgs<TName> | undefined =
81
233
  | QueryFindFirstArgs<TName>
82
234
  | undefined,
83
- > = Promise<
84
- BuildQueryResult<
85
- SchemaRelations,
86
- TableRelationConfig<TName>,
87
- ResolveTableFindFirstSelection<TName, TArgs>
88
- > | null
89
- >;
90
- type TableModel<TName extends TableName> = InferSelectModel<
91
- (typeof mergedSchema)[TName]
235
+ > = ApplyManyToManyFindFirstResult<
236
+ TName,
237
+ TArgs,
238
+ TArgs extends undefined
239
+ ? Promise<TableModel<TName> | null>
240
+ : Promise<
241
+ BuildQueryResult<
242
+ SchemaRelations,
243
+ TableRelationConfig<TName>,
244
+ ResolveTableFindFirstSelection<TName, TArgs>
245
+ > | null
246
+ >
92
247
  >;
93
248
  type TableInsertModel<TName extends TableName> = InferInsertModel<
94
249
  (typeof mergedSchema)[TName]
@@ -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
  }