appflare 0.2.25 → 0.2.27
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 +758 -758
- package/cli/commands/index.ts +238 -238
- package/cli/generate.ts +178 -178
- package/cli/index.ts +120 -120
- package/cli/load-config.ts +184 -184
- package/cli/schema-compiler.ts +1183 -1183
- 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 +748 -748
- 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 +205 -180
- package/cli/templates/core/client/types.ts +186 -184
- 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/navigation.ts +122 -122
- 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 +388 -388
- 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 -112
- package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
- package/cli/templates/handlers/generators/context/types.ts +18 -18
- 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 +13 -13
- 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 +516 -516
- package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -56
- package/cli/templates/handlers/generators/registration/modules/storage.ts +192 -194
- package/cli/templates/handlers/generators/registration/sections.ts +210 -210
- package/cli/templates/handlers/generators/types/context.ts +67 -66
- 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 +259 -259
- 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 +1031 -1031
- package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +246 -246
- 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 +121 -121
- package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
- package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +676 -676
- package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
- package/cli/templates/handlers/index.ts +43 -43
- package/cli/templates/handlers/operations.ts +116 -116
- package/cli/templates/handlers/registration.ts +91 -91
- package/cli/templates/handlers/types.ts +15 -15
- 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 +95 -99
- package/dist/cli/index.mjs +95 -99
- package/index.ts +18 -18
- package/package.json +58 -58
- package/react/index.ts +5 -5
- package/react/use-infinite-query.ts +252 -252
- package/react/use-mutation.ts +89 -89
- package/react/use-query.ts +207 -207
- package/schema.ts +415 -415
- package/test-better-auth-hash.ts +2 -2
- package/tsconfig.json +6 -6
- package/tsup.config.ts +82 -82
- package/dist/cli/index.d.mts +0 -2
- package/dist/cli/index.d.ts +0 -2
package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
1
|
-
export function generateQueryRuntimeAggregateAndFooterSection(): string {
|
|
2
|
-
return ` count: async (args?: QueryCountArgs<TableName>) => {
|
|
3
|
-
const withValue = args?.with;
|
|
4
|
-
const pathSegments = args?.field
|
|
5
|
-
? splitAggregateFieldPath(String(args.field))
|
|
6
|
-
: [];
|
|
7
|
-
if (args?.field && pathSegments.length === 0) {
|
|
8
|
-
throw new Error(
|
|
9
|
-
"Invalid count field for table " + tableName + ": " + String(args.field),
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (withValue !== undefined || pathSegments.length > 1) {
|
|
14
|
-
const whereFilter = buildWhereFilter(
|
|
15
|
-
table,
|
|
16
|
-
args?.where as Record<string, unknown> | undefined,
|
|
17
|
-
tableName,
|
|
18
|
-
);
|
|
19
|
-
const transformedWith =
|
|
20
|
-
withValue === undefined
|
|
21
|
-
? undefined
|
|
22
|
-
: transformWithRelations(withValue, tableName);
|
|
23
|
-
const rawRows = await queryTable.findMany({
|
|
24
|
-
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
25
|
-
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
26
|
-
});
|
|
27
|
-
const flattenedRows = flattenManyToManyResult(
|
|
28
|
-
tableName,
|
|
29
|
-
rawRows,
|
|
30
|
-
withValue,
|
|
31
|
-
);
|
|
32
|
-
const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
|
|
33
|
-
const constrainedRows = hasAggregateWithConstraints(withValue)
|
|
34
|
-
? rows.filter((row) =>
|
|
35
|
-
rowMatchesAggregateWithConstraints(row, withValue),
|
|
36
|
-
)
|
|
37
|
-
: rows;
|
|
38
|
-
|
|
39
|
-
if (!args?.field) {
|
|
40
|
-
return constrainedRows.length;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const values = constrainedRows.flatMap((row) =>
|
|
44
|
-
collectValuesFromPath(row, pathSegments),
|
|
45
|
-
);
|
|
46
|
-
const useDistinct = args?.distinct ?? withValue !== undefined;
|
|
47
|
-
return countAggregateValues(values, useDistinct);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const whereFilter = buildWhereFilter(
|
|
51
|
-
table,
|
|
52
|
-
args?.where as Record<string, unknown> | undefined,
|
|
53
|
-
tableName,
|
|
54
|
-
);
|
|
55
|
-
const tableColumns = getTableColumns(table as never) as Record<string, unknown>;
|
|
56
|
-
const selectedField = args?.field
|
|
57
|
-
? tableColumns[String(args.field)]
|
|
58
|
-
: undefined;
|
|
59
|
-
|
|
60
|
-
if (args?.field && !selectedField) {
|
|
61
|
-
throw new Error(
|
|
62
|
-
"Invalid count field for table " + tableName + ": " + args.field,
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let expression: SQL;
|
|
67
|
-
if (selectedField) {
|
|
68
|
-
expression = args?.distinct
|
|
69
|
-
? sql\`count(distinct \${selectedField as never})\`
|
|
70
|
-
: sql\`count(\${selectedField as never})\`;
|
|
71
|
-
} else {
|
|
72
|
-
expression = sql\`count(*)\`;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
let query = ($db as any)
|
|
76
|
-
.select({ value: expression })
|
|
77
|
-
.from(table as any);
|
|
78
|
-
|
|
79
|
-
if (whereFilter) {
|
|
80
|
-
query = query.where(whereFilter);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const rows = (await query) as Array<{ value: unknown }>;
|
|
84
|
-
const value = Number(rows[0]?.value ?? 0);
|
|
85
|
-
return Number.isFinite(value) ? value : 0;
|
|
86
|
-
},
|
|
87
|
-
avg: async (args: QueryAvgArgs<TableName>) => {
|
|
88
|
-
const withValue = args.with;
|
|
89
|
-
const pathSegments = splitAggregateFieldPath(String(args.field));
|
|
90
|
-
if (pathSegments.length === 0) {
|
|
91
|
-
throw new Error(
|
|
92
|
-
"Invalid avg field for table " + tableName + ": " + String(args.field),
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (withValue !== undefined || pathSegments.length > 1) {
|
|
97
|
-
const whereFilter = buildWhereFilter(
|
|
98
|
-
table,
|
|
99
|
-
args.where as Record<string, unknown> | undefined,
|
|
100
|
-
tableName,
|
|
101
|
-
);
|
|
102
|
-
const transformedWith =
|
|
103
|
-
withValue === undefined
|
|
104
|
-
? undefined
|
|
105
|
-
: transformWithRelations(withValue, tableName);
|
|
106
|
-
const rawRows = await queryTable.findMany({
|
|
107
|
-
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
108
|
-
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
109
|
-
});
|
|
110
|
-
const flattenedRows = flattenManyToManyResult(
|
|
111
|
-
tableName,
|
|
112
|
-
rawRows,
|
|
113
|
-
withValue,
|
|
114
|
-
);
|
|
115
|
-
const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
|
|
116
|
-
const constrainedRows = hasAggregateWithConstraints(withValue)
|
|
117
|
-
? rows.filter((row) =>
|
|
118
|
-
rowMatchesAggregateWithConstraints(row, withValue),
|
|
119
|
-
)
|
|
120
|
-
: rows;
|
|
121
|
-
const numericValues = constrainedRows
|
|
122
|
-
.flatMap((row) => collectValuesFromPath(row, pathSegments))
|
|
123
|
-
.map((value) => toFiniteNumber(value))
|
|
124
|
-
.filter((value): value is number => value !== null);
|
|
125
|
-
const values = args.distinct
|
|
126
|
-
? Array.from(new Set<number>(numericValues))
|
|
127
|
-
: numericValues;
|
|
128
|
-
return averageAggregateValues(values);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const whereFilter = buildWhereFilter(
|
|
132
|
-
table,
|
|
133
|
-
args.where as Record<string, unknown> | undefined,
|
|
134
|
-
tableName,
|
|
135
|
-
);
|
|
136
|
-
const tableColumns = getTableColumns(table as never) as Record<string, unknown>;
|
|
137
|
-
const selectedField = tableColumns[String(args.field)];
|
|
138
|
-
|
|
139
|
-
if (!selectedField) {
|
|
140
|
-
throw new Error(
|
|
141
|
-
"Invalid avg field for table " + tableName + ": " + String(args.field),
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const expression = args.distinct
|
|
146
|
-
? sql\`avg(distinct \${selectedField as never})\`
|
|
147
|
-
: sql\`avg(\${selectedField as never})\`;
|
|
148
|
-
|
|
149
|
-
let query = ($db as any)
|
|
150
|
-
.select({ value: expression })
|
|
151
|
-
.from(table as any);
|
|
152
|
-
|
|
153
|
-
if (whereFilter) {
|
|
154
|
-
query = query.where(whereFilter);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const rows = (await query) as Array<{ value: unknown }>;
|
|
158
|
-
const rawValue = rows[0]?.value;
|
|
159
|
-
if (rawValue === null || rawValue === undefined) {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const value = Number(rawValue);
|
|
164
|
-
return Number.isFinite(value) ? value : null;
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
cache.set(tableName, tableApi);
|
|
169
|
-
return tableApi;
|
|
170
|
-
},
|
|
171
|
-
}) as AppflareQueryDb;
|
|
172
|
-
}
|
|
173
|
-
`;
|
|
174
|
-
}
|
|
1
|
+
export function generateQueryRuntimeAggregateAndFooterSection(): string {
|
|
2
|
+
return ` count: async (args?: QueryCountArgs<TableName>) => {
|
|
3
|
+
const withValue = args?.with;
|
|
4
|
+
const pathSegments = args?.field
|
|
5
|
+
? splitAggregateFieldPath(String(args.field))
|
|
6
|
+
: [];
|
|
7
|
+
if (args?.field && pathSegments.length === 0) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
"Invalid count field for table " + tableName + ": " + String(args.field),
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (withValue !== undefined || pathSegments.length > 1) {
|
|
14
|
+
const whereFilter = buildWhereFilter(
|
|
15
|
+
table,
|
|
16
|
+
args?.where as Record<string, unknown> | undefined,
|
|
17
|
+
tableName,
|
|
18
|
+
);
|
|
19
|
+
const transformedWith =
|
|
20
|
+
withValue === undefined
|
|
21
|
+
? undefined
|
|
22
|
+
: transformWithRelations(withValue, tableName);
|
|
23
|
+
const rawRows = await queryTable.findMany({
|
|
24
|
+
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
25
|
+
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
26
|
+
});
|
|
27
|
+
const flattenedRows = flattenManyToManyResult(
|
|
28
|
+
tableName,
|
|
29
|
+
rawRows,
|
|
30
|
+
withValue,
|
|
31
|
+
);
|
|
32
|
+
const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
|
|
33
|
+
const constrainedRows = hasAggregateWithConstraints(withValue)
|
|
34
|
+
? rows.filter((row) =>
|
|
35
|
+
rowMatchesAggregateWithConstraints(row, withValue),
|
|
36
|
+
)
|
|
37
|
+
: rows;
|
|
38
|
+
|
|
39
|
+
if (!args?.field) {
|
|
40
|
+
return constrainedRows.length;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const values = constrainedRows.flatMap((row) =>
|
|
44
|
+
collectValuesFromPath(row, pathSegments),
|
|
45
|
+
);
|
|
46
|
+
const useDistinct = args?.distinct ?? withValue !== undefined;
|
|
47
|
+
return countAggregateValues(values, useDistinct);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const whereFilter = buildWhereFilter(
|
|
51
|
+
table,
|
|
52
|
+
args?.where as Record<string, unknown> | undefined,
|
|
53
|
+
tableName,
|
|
54
|
+
);
|
|
55
|
+
const tableColumns = getTableColumns(table as never) as Record<string, unknown>;
|
|
56
|
+
const selectedField = args?.field
|
|
57
|
+
? tableColumns[String(args.field)]
|
|
58
|
+
: undefined;
|
|
59
|
+
|
|
60
|
+
if (args?.field && !selectedField) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
"Invalid count field for table " + tableName + ": " + args.field,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let expression: SQL;
|
|
67
|
+
if (selectedField) {
|
|
68
|
+
expression = args?.distinct
|
|
69
|
+
? sql\`count(distinct \${selectedField as never})\`
|
|
70
|
+
: sql\`count(\${selectedField as never})\`;
|
|
71
|
+
} else {
|
|
72
|
+
expression = sql\`count(*)\`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let query = ($db as any)
|
|
76
|
+
.select({ value: expression })
|
|
77
|
+
.from(table as any);
|
|
78
|
+
|
|
79
|
+
if (whereFilter) {
|
|
80
|
+
query = query.where(whereFilter);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const rows = (await query) as Array<{ value: unknown }>;
|
|
84
|
+
const value = Number(rows[0]?.value ?? 0);
|
|
85
|
+
return Number.isFinite(value) ? value : 0;
|
|
86
|
+
},
|
|
87
|
+
avg: async (args: QueryAvgArgs<TableName>) => {
|
|
88
|
+
const withValue = args.with;
|
|
89
|
+
const pathSegments = splitAggregateFieldPath(String(args.field));
|
|
90
|
+
if (pathSegments.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"Invalid avg field for table " + tableName + ": " + String(args.field),
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (withValue !== undefined || pathSegments.length > 1) {
|
|
97
|
+
const whereFilter = buildWhereFilter(
|
|
98
|
+
table,
|
|
99
|
+
args.where as Record<string, unknown> | undefined,
|
|
100
|
+
tableName,
|
|
101
|
+
);
|
|
102
|
+
const transformedWith =
|
|
103
|
+
withValue === undefined
|
|
104
|
+
? undefined
|
|
105
|
+
: transformWithRelations(withValue, tableName);
|
|
106
|
+
const rawRows = await queryTable.findMany({
|
|
107
|
+
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
108
|
+
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
109
|
+
});
|
|
110
|
+
const flattenedRows = flattenManyToManyResult(
|
|
111
|
+
tableName,
|
|
112
|
+
rawRows,
|
|
113
|
+
withValue,
|
|
114
|
+
);
|
|
115
|
+
const rows = Array.isArray(flattenedRows) ? flattenedRows : [];
|
|
116
|
+
const constrainedRows = hasAggregateWithConstraints(withValue)
|
|
117
|
+
? rows.filter((row) =>
|
|
118
|
+
rowMatchesAggregateWithConstraints(row, withValue),
|
|
119
|
+
)
|
|
120
|
+
: rows;
|
|
121
|
+
const numericValues = constrainedRows
|
|
122
|
+
.flatMap((row) => collectValuesFromPath(row, pathSegments))
|
|
123
|
+
.map((value) => toFiniteNumber(value))
|
|
124
|
+
.filter((value): value is number => value !== null);
|
|
125
|
+
const values = args.distinct
|
|
126
|
+
? Array.from(new Set<number>(numericValues))
|
|
127
|
+
: numericValues;
|
|
128
|
+
return averageAggregateValues(values);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const whereFilter = buildWhereFilter(
|
|
132
|
+
table,
|
|
133
|
+
args.where as Record<string, unknown> | undefined,
|
|
134
|
+
tableName,
|
|
135
|
+
);
|
|
136
|
+
const tableColumns = getTableColumns(table as never) as Record<string, unknown>;
|
|
137
|
+
const selectedField = tableColumns[String(args.field)];
|
|
138
|
+
|
|
139
|
+
if (!selectedField) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
"Invalid avg field for table " + tableName + ": " + String(args.field),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const expression = args.distinct
|
|
146
|
+
? sql\`avg(distinct \${selectedField as never})\`
|
|
147
|
+
: sql\`avg(\${selectedField as never})\`;
|
|
148
|
+
|
|
149
|
+
let query = ($db as any)
|
|
150
|
+
.select({ value: expression })
|
|
151
|
+
.from(table as any);
|
|
152
|
+
|
|
153
|
+
if (whereFilter) {
|
|
154
|
+
query = query.where(whereFilter);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const rows = (await query) as Array<{ value: unknown }>;
|
|
158
|
+
const rawValue = rows[0]?.value;
|
|
159
|
+
if (rawValue === null || rawValue === undefined) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const value = Number(rawValue);
|
|
164
|
+
return Number.isFinite(value) ? value : null;
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
cache.set(tableName, tableApi);
|
|
169
|
+
return tableApi;
|
|
170
|
+
},
|
|
171
|
+
}) as AppflareQueryDb;
|
|
172
|
+
}
|
|
173
|
+
`;
|
|
174
|
+
}
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
export function generateQueryRuntimeReadSection(): string {
|
|
2
|
-
return ` findMany: (args?: Record<string, unknown>) => {
|
|
3
|
-
const where = isRecord(args?.where)
|
|
4
|
-
? (args?.where as Record<string, unknown>)
|
|
5
|
-
: undefined;
|
|
6
|
-
const whereFilter = buildWhereFilter(table, where, tableName);
|
|
7
|
-
const passthroughArgs =
|
|
8
|
-
where !== undefined && !whereFilter
|
|
9
|
-
? (() => {
|
|
10
|
-
const nextArgs = { ...(args ?? {}) };
|
|
11
|
-
delete nextArgs.where;
|
|
12
|
-
return nextArgs;
|
|
13
|
-
})()
|
|
14
|
-
: args;
|
|
15
|
-
const withValue = args?.with;
|
|
16
|
-
const transformedWithResult =
|
|
17
|
-
withValue === undefined
|
|
18
|
-
? {
|
|
19
|
-
with: undefined,
|
|
20
|
-
aggregatePlan: undefined,
|
|
21
|
-
}
|
|
22
|
-
: transformWithRelationsAndExtractAggregates(withValue, tableName);
|
|
23
|
-
const transformedWith = transformedWithResult.with;
|
|
24
|
-
const aggregatePlan = transformedWithResult.aggregatePlan;
|
|
25
|
-
const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
|
|
26
|
-
tableName,
|
|
27
|
-
withValue,
|
|
28
|
-
);
|
|
29
|
-
if (
|
|
30
|
-
!whereFilter &&
|
|
31
|
-
transformedWith === withValue &&
|
|
32
|
-
!aggregatePlan &&
|
|
33
|
-
!requiresManyToManyFlatten
|
|
34
|
-
) {
|
|
35
|
-
return queryTable.findMany(passthroughArgs);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const queryPromise = queryTable.findMany({
|
|
39
|
-
...(passthroughArgs ?? {}),
|
|
40
|
-
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
41
|
-
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return queryPromise.then((result) => {
|
|
45
|
-
const flattenedResult = flattenManyToManyResult(
|
|
46
|
-
tableName,
|
|
47
|
-
result,
|
|
48
|
-
withValue,
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
if (!aggregatePlan) {
|
|
52
|
-
return flattenedResult;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return applyRelationAggregatePlanToResult(
|
|
56
|
-
flattenedResult,
|
|
57
|
-
aggregatePlan,
|
|
58
|
-
);
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
findFirst: (args?: Record<string, unknown>) => {
|
|
62
|
-
const where = isRecord(args?.where)
|
|
63
|
-
? (args?.where as Record<string, unknown>)
|
|
64
|
-
: undefined;
|
|
65
|
-
const whereFilter = buildWhereFilter(table, where, tableName);
|
|
66
|
-
const passthroughArgs =
|
|
67
|
-
where !== undefined && !whereFilter
|
|
68
|
-
? (() => {
|
|
69
|
-
const nextArgs = { ...(args ?? {}) };
|
|
70
|
-
delete nextArgs.where;
|
|
71
|
-
return nextArgs;
|
|
72
|
-
})()
|
|
73
|
-
: args;
|
|
74
|
-
const withValue = args?.with;
|
|
75
|
-
const transformedWithResult =
|
|
76
|
-
withValue === undefined
|
|
77
|
-
? {
|
|
78
|
-
with: undefined,
|
|
79
|
-
aggregatePlan: undefined,
|
|
80
|
-
}
|
|
81
|
-
: transformWithRelationsAndExtractAggregates(withValue, tableName);
|
|
82
|
-
const transformedWith = transformedWithResult.with;
|
|
83
|
-
const aggregatePlan = transformedWithResult.aggregatePlan;
|
|
84
|
-
const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
|
|
85
|
-
tableName,
|
|
86
|
-
withValue,
|
|
87
|
-
);
|
|
88
|
-
if (
|
|
89
|
-
!whereFilter &&
|
|
90
|
-
transformedWith === withValue &&
|
|
91
|
-
!aggregatePlan &&
|
|
92
|
-
!requiresManyToManyFlatten
|
|
93
|
-
) {
|
|
94
|
-
return queryTable.findFirst(passthroughArgs);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const queryPromise = queryTable.findFirst({
|
|
98
|
-
...(passthroughArgs ?? {}),
|
|
99
|
-
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
100
|
-
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return queryPromise.then((result) => {
|
|
104
|
-
const flattenedResult = flattenManyToManyResult(
|
|
105
|
-
tableName,
|
|
106
|
-
result,
|
|
107
|
-
withValue,
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
if (!aggregatePlan) {
|
|
111
|
-
return flattenedResult;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return applyRelationAggregatePlanToResult(
|
|
115
|
-
flattenedResult,
|
|
116
|
-
aggregatePlan,
|
|
117
|
-
);
|
|
118
|
-
});
|
|
119
|
-
},
|
|
120
|
-
`;
|
|
121
|
-
}
|
|
1
|
+
export function generateQueryRuntimeReadSection(): string {
|
|
2
|
+
return ` findMany: (args?: Record<string, unknown>) => {
|
|
3
|
+
const where = isRecord(args?.where)
|
|
4
|
+
? (args?.where as Record<string, unknown>)
|
|
5
|
+
: undefined;
|
|
6
|
+
const whereFilter = buildWhereFilter(table, where, tableName);
|
|
7
|
+
const passthroughArgs =
|
|
8
|
+
where !== undefined && !whereFilter
|
|
9
|
+
? (() => {
|
|
10
|
+
const nextArgs = { ...(args ?? {}) };
|
|
11
|
+
delete nextArgs.where;
|
|
12
|
+
return nextArgs;
|
|
13
|
+
})()
|
|
14
|
+
: args;
|
|
15
|
+
const withValue = args?.with;
|
|
16
|
+
const transformedWithResult =
|
|
17
|
+
withValue === undefined
|
|
18
|
+
? {
|
|
19
|
+
with: undefined,
|
|
20
|
+
aggregatePlan: undefined,
|
|
21
|
+
}
|
|
22
|
+
: transformWithRelationsAndExtractAggregates(withValue, tableName);
|
|
23
|
+
const transformedWith = transformedWithResult.with;
|
|
24
|
+
const aggregatePlan = transformedWithResult.aggregatePlan;
|
|
25
|
+
const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
|
|
26
|
+
tableName,
|
|
27
|
+
withValue,
|
|
28
|
+
);
|
|
29
|
+
if (
|
|
30
|
+
!whereFilter &&
|
|
31
|
+
transformedWith === withValue &&
|
|
32
|
+
!aggregatePlan &&
|
|
33
|
+
!requiresManyToManyFlatten
|
|
34
|
+
) {
|
|
35
|
+
return queryTable.findMany(passthroughArgs);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const queryPromise = queryTable.findMany({
|
|
39
|
+
...(passthroughArgs ?? {}),
|
|
40
|
+
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
41
|
+
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return queryPromise.then((result) => {
|
|
45
|
+
const flattenedResult = flattenManyToManyResult(
|
|
46
|
+
tableName,
|
|
47
|
+
result,
|
|
48
|
+
withValue,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (!aggregatePlan) {
|
|
52
|
+
return flattenedResult;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return applyRelationAggregatePlanToResult(
|
|
56
|
+
flattenedResult,
|
|
57
|
+
aggregatePlan,
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
findFirst: (args?: Record<string, unknown>) => {
|
|
62
|
+
const where = isRecord(args?.where)
|
|
63
|
+
? (args?.where as Record<string, unknown>)
|
|
64
|
+
: undefined;
|
|
65
|
+
const whereFilter = buildWhereFilter(table, where, tableName);
|
|
66
|
+
const passthroughArgs =
|
|
67
|
+
where !== undefined && !whereFilter
|
|
68
|
+
? (() => {
|
|
69
|
+
const nextArgs = { ...(args ?? {}) };
|
|
70
|
+
delete nextArgs.where;
|
|
71
|
+
return nextArgs;
|
|
72
|
+
})()
|
|
73
|
+
: args;
|
|
74
|
+
const withValue = args?.with;
|
|
75
|
+
const transformedWithResult =
|
|
76
|
+
withValue === undefined
|
|
77
|
+
? {
|
|
78
|
+
with: undefined,
|
|
79
|
+
aggregatePlan: undefined,
|
|
80
|
+
}
|
|
81
|
+
: transformWithRelationsAndExtractAggregates(withValue, tableName);
|
|
82
|
+
const transformedWith = transformedWithResult.with;
|
|
83
|
+
const aggregatePlan = transformedWithResult.aggregatePlan;
|
|
84
|
+
const requiresManyToManyFlatten = hasManyToManyRelationsInWith(
|
|
85
|
+
tableName,
|
|
86
|
+
withValue,
|
|
87
|
+
);
|
|
88
|
+
if (
|
|
89
|
+
!whereFilter &&
|
|
90
|
+
transformedWith === withValue &&
|
|
91
|
+
!aggregatePlan &&
|
|
92
|
+
!requiresManyToManyFlatten
|
|
93
|
+
) {
|
|
94
|
+
return queryTable.findFirst(passthroughArgs);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const queryPromise = queryTable.findFirst({
|
|
98
|
+
...(passthroughArgs ?? {}),
|
|
99
|
+
...(whereFilter ? { where: () => whereFilter } : {}),
|
|
100
|
+
...(transformedWith !== undefined ? { with: transformedWith } : {}),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return queryPromise.then((result) => {
|
|
104
|
+
const flattenedResult = flattenManyToManyResult(
|
|
105
|
+
tableName,
|
|
106
|
+
result,
|
|
107
|
+
withValue,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
if (!aggregatePlan) {
|
|
111
|
+
return flattenedResult;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return applyRelationAggregatePlanToResult(
|
|
115
|
+
flattenedResult,
|
|
116
|
+
aggregatePlan,
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
`;
|
|
121
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
export function generateQueryRuntimeSetupSection(): string {
|
|
2
|
-
return `export function createQueryDb(
|
|
3
|
-
$db: AppflareDb,
|
|
4
|
-
options?: QueryDbOptions,
|
|
5
|
-
): AppflareQueryDb {
|
|
6
|
-
const cache = new Map<string, unknown>();
|
|
7
|
-
const onMutation = options?.onMutation;
|
|
8
|
-
const queryDb = $db.query as Record<
|
|
9
|
-
string,
|
|
10
|
-
{
|
|
11
|
-
findMany: (args?: unknown) => Promise<unknown>;
|
|
12
|
-
findFirst: (args?: unknown) => Promise<unknown>;
|
|
13
|
-
}
|
|
14
|
-
>;
|
|
15
|
-
|
|
16
|
-
return new Proxy({} as AppflareQueryDb, {
|
|
17
|
-
get: (_target, property) => {
|
|
18
|
-
const tableName = String(property);
|
|
19
|
-
if (cache.has(tableName)) {
|
|
20
|
-
return cache.get(tableName);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const queryTable = queryDb[tableName];
|
|
24
|
-
if (!queryTable) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const table = (mergedSchema as Record<string, unknown>)[tableName];
|
|
29
|
-
|
|
30
|
-
const emitMutation = (
|
|
31
|
-
kind: DbMutationKind,
|
|
32
|
-
args: Record<string, unknown>,
|
|
33
|
-
rows: unknown[],
|
|
34
|
-
): void => {
|
|
35
|
-
onMutation?.({
|
|
36
|
-
kind,
|
|
37
|
-
table: tableName,
|
|
38
|
-
args,
|
|
39
|
-
rows,
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const tableApi = {
|
|
44
|
-
`;
|
|
45
|
-
}
|
|
1
|
+
export function generateQueryRuntimeSetupSection(): string {
|
|
2
|
+
return `export function createQueryDb(
|
|
3
|
+
$db: AppflareDb,
|
|
4
|
+
options?: QueryDbOptions,
|
|
5
|
+
): AppflareQueryDb {
|
|
6
|
+
const cache = new Map<string, unknown>();
|
|
7
|
+
const onMutation = options?.onMutation;
|
|
8
|
+
const queryDb = $db.query as Record<
|
|
9
|
+
string,
|
|
10
|
+
{
|
|
11
|
+
findMany: (args?: unknown) => Promise<unknown>;
|
|
12
|
+
findFirst: (args?: unknown) => Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
return new Proxy({} as AppflareQueryDb, {
|
|
17
|
+
get: (_target, property) => {
|
|
18
|
+
const tableName = String(property);
|
|
19
|
+
if (cache.has(tableName)) {
|
|
20
|
+
return cache.get(tableName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const queryTable = queryDb[tableName];
|
|
24
|
+
if (!queryTable) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const table = (mergedSchema as Record<string, unknown>)[tableName];
|
|
29
|
+
|
|
30
|
+
const emitMutation = (
|
|
31
|
+
kind: DbMutationKind,
|
|
32
|
+
args: Record<string, unknown>,
|
|
33
|
+
rows: unknown[],
|
|
34
|
+
): void => {
|
|
35
|
+
onMutation?.({
|
|
36
|
+
kind,
|
|
37
|
+
table: tableName,
|
|
38
|
+
args,
|
|
39
|
+
rows,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const tableApi = {
|
|
44
|
+
`;
|
|
45
|
+
}
|