appflare 0.2.24 → 0.2.26
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 -749
- 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 +180 -180
- package/cli/templates/core/client/types.ts +184 -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 +134 -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 +196 -194
- package/cli/templates/handlers/generators/registration/sections.ts +210 -210
- package/cli/templates/handlers/generators/types/context.ts +68 -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 -83
- 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 +61 -28
- package/dist/cli/index.mjs +61 -28
- 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
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { generateQueryRuntimeAggregateAndFooterSection } from "./query-runtime/runtime-aggregate-and-footer";
|
|
2
|
-
import { generateQueryRuntimeHandledErrorSection } from "./query-runtime/handled-error";
|
|
3
|
-
import { generateQueryRuntimeReadSection } from "./query-runtime/runtime-read";
|
|
4
|
-
import { generateQueryRuntimeSetupSection } from "./query-runtime/runtime-setup";
|
|
5
|
-
import { generateQueryRuntimeWriteSection } from "./query-runtime/runtime-write";
|
|
6
|
-
|
|
7
|
-
export function generateTypesQueryRuntimeSection(): string {
|
|
8
|
-
return [
|
|
9
|
-
generateQueryRuntimeSetupSection(),
|
|
10
|
-
generateQueryRuntimeReadSection(),
|
|
11
|
-
generateQueryRuntimeWriteSection(),
|
|
12
|
-
generateQueryRuntimeAggregateAndFooterSection(),
|
|
13
|
-
generateQueryRuntimeHandledErrorSection(),
|
|
14
|
-
].join("\n\n");
|
|
15
|
-
}
|
|
1
|
+
import { generateQueryRuntimeAggregateAndFooterSection } from "./query-runtime/runtime-aggregate-and-footer";
|
|
2
|
+
import { generateQueryRuntimeHandledErrorSection } from "./query-runtime/handled-error";
|
|
3
|
+
import { generateQueryRuntimeReadSection } from "./query-runtime/runtime-read";
|
|
4
|
+
import { generateQueryRuntimeSetupSection } from "./query-runtime/runtime-setup";
|
|
5
|
+
import { generateQueryRuntimeWriteSection } from "./query-runtime/runtime-write";
|
|
6
|
+
|
|
7
|
+
export function generateTypesQueryRuntimeSection(): string {
|
|
8
|
+
return [
|
|
9
|
+
generateQueryRuntimeSetupSection(),
|
|
10
|
+
generateQueryRuntimeReadSection(),
|
|
11
|
+
generateQueryRuntimeWriteSection(),
|
|
12
|
+
generateQueryRuntimeAggregateAndFooterSection(),
|
|
13
|
+
generateQueryRuntimeHandledErrorSection(),
|
|
14
|
+
].join("\n\n");
|
|
15
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
|
|
2
|
-
import { generateContextSource } from "./generators/context";
|
|
3
|
-
import { generateExecutionSource } from "./generators/execution";
|
|
4
|
-
import { generateHandlersSource } from "./generators/handlers";
|
|
5
|
-
import { generateRegistration } from "./registration";
|
|
6
|
-
|
|
7
|
-
export type GeneratedHandlerArtifact = {
|
|
8
|
-
relativePath: string;
|
|
9
|
-
source: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function generateHandlersArtifacts(
|
|
13
|
-
schemaImportPath: string,
|
|
14
|
-
operations: DiscoveredHandlerOperation[],
|
|
15
|
-
defaultR2Binding?: string,
|
|
16
|
-
): GeneratedHandlerArtifact[] {
|
|
17
|
-
const handlersSource = generateHandlersSource(schemaImportPath);
|
|
18
|
-
|
|
19
|
-
const contextSource = generateContextSource(defaultR2Binding);
|
|
20
|
-
|
|
21
|
-
const executionSource = generateExecutionSource();
|
|
22
|
-
|
|
23
|
-
const routesSource = generateRegistration(operations);
|
|
24
|
-
|
|
25
|
-
return [
|
|
26
|
-
{
|
|
27
|
-
relativePath: "handlers.ts",
|
|
28
|
-
source: handlersSource,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
relativePath: "handlers.context.ts",
|
|
32
|
-
source: contextSource,
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
relativePath: "handlers.execution.ts",
|
|
36
|
-
source: executionSource,
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
relativePath: "handlers.routes.ts",
|
|
40
|
-
source: routesSource,
|
|
41
|
-
},
|
|
42
|
-
];
|
|
43
|
-
}
|
|
1
|
+
import { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
|
|
2
|
+
import { generateContextSource } from "./generators/context";
|
|
3
|
+
import { generateExecutionSource } from "./generators/execution";
|
|
4
|
+
import { generateHandlersSource } from "./generators/handlers";
|
|
5
|
+
import { generateRegistration } from "./registration";
|
|
6
|
+
|
|
7
|
+
export type GeneratedHandlerArtifact = {
|
|
8
|
+
relativePath: string;
|
|
9
|
+
source: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function generateHandlersArtifacts(
|
|
13
|
+
schemaImportPath: string,
|
|
14
|
+
operations: DiscoveredHandlerOperation[],
|
|
15
|
+
defaultR2Binding?: string,
|
|
16
|
+
): GeneratedHandlerArtifact[] {
|
|
17
|
+
const handlersSource = generateHandlersSource(schemaImportPath);
|
|
18
|
+
|
|
19
|
+
const contextSource = generateContextSource(defaultR2Binding);
|
|
20
|
+
|
|
21
|
+
const executionSource = generateExecutionSource();
|
|
22
|
+
|
|
23
|
+
const routesSource = generateRegistration(operations);
|
|
24
|
+
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
relativePath: "handlers.ts",
|
|
28
|
+
source: handlersSource,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
relativePath: "handlers.context.ts",
|
|
32
|
+
source: contextSource,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
relativePath: "handlers.execution.ts",
|
|
36
|
+
source: executionSource,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
relativePath: "handlers.routes.ts",
|
|
40
|
+
source: routesSource,
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
}
|
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import {
|
|
2
|
-
toPascalCase,
|
|
3
|
-
singularize,
|
|
4
|
-
zodForColumn,
|
|
5
|
-
zodForQueryColumn,
|
|
6
|
-
} from "./utils";
|
|
7
|
-
import type {
|
|
8
|
-
DiscoveredColumn,
|
|
9
|
-
DiscoveredTable,
|
|
10
|
-
} from "../../utils/schema-discovery";
|
|
11
|
-
|
|
12
|
-
function createArgsShape(columns: DiscoveredColumn[]): string {
|
|
13
|
-
const creatableColumns = columns.filter(
|
|
14
|
-
(column) => !(column.primaryKey && column.autoIncrement),
|
|
15
|
-
);
|
|
16
|
-
if (creatableColumns.length === 0) {
|
|
17
|
-
return "{}";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return `{
|
|
21
|
-
${creatableColumns.map((column) => `\t\t${column.name}: ${zodForColumn(column)},`).join("\n")}
|
|
22
|
-
\t}`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function createInsertValues(columns: DiscoveredColumn[]): string {
|
|
26
|
-
const creatableColumns = columns.filter(
|
|
27
|
-
(column) => !(column.primaryKey && column.autoIncrement),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
if (creatableColumns.length === 0) {
|
|
31
|
-
return "{}";
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return `{
|
|
35
|
-
${creatableColumns.map((column) => `\t\t\t${column.name}: args.${column.name},`).join("\n")}
|
|
36
|
-
\t\t}`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function createQueryBlock(table: DiscoveredTable): string {
|
|
40
|
-
const idColumn = table.columns.find((column) => column.name === "id");
|
|
41
|
-
const queryName = `get${toPascalCase(table.exportName)}`;
|
|
42
|
-
const args = idColumn
|
|
43
|
-
? `{
|
|
44
|
-
id: ${zodForQueryColumn(idColumn)},
|
|
45
|
-
\t}`
|
|
46
|
-
: "{}";
|
|
47
|
-
const queryBody = idColumn
|
|
48
|
-
? `if (args.id !== undefined) {
|
|
49
|
-
return ctx.$db.select().from(schema.${table.exportName}).where(eq(schema.${table.exportName}.id, args.id));
|
|
50
|
-
\t\t}
|
|
51
|
-
\n\t\treturn ctx.$db.select().from(schema.${table.exportName});`
|
|
52
|
-
: `return ctx.$db.select().from(schema.${table.exportName});`;
|
|
53
|
-
|
|
54
|
-
return `export const ${queryName} = query({
|
|
55
|
-
\targs: ${args},
|
|
56
|
-
\thandler: async (ctx, args) => {
|
|
57
|
-
\t\t${queryBody}
|
|
58
|
-
\t},
|
|
59
|
-
});`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function createMutationBlock(table: DiscoveredTable): string {
|
|
63
|
-
const mutationName = `create${toPascalCase(singularize(table.exportName))}`;
|
|
64
|
-
const argsShape = createArgsShape(table.columns);
|
|
65
|
-
const values = createInsertValues(table.columns);
|
|
66
|
-
|
|
67
|
-
return `export const ${mutationName} = mutation({
|
|
68
|
-
\targs: ${argsShape},
|
|
69
|
-
\thandler: async (ctx, args) => {
|
|
70
|
-
const [created] = await ctx.$db
|
|
71
|
-
\t\t\t.insert(schema.${table.exportName})
|
|
72
|
-
\t\t\t.values(${values})
|
|
73
|
-
\t\t\t.returning();
|
|
74
|
-
|
|
75
|
-
\n\t\treturn created ?? null;
|
|
76
|
-
\t},
|
|
77
|
-
});`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function createRegistry(tableNames: string[]): string {
|
|
81
|
-
const queryEntries = tableNames.map((name) => {
|
|
82
|
-
return `\t${`get${toPascalCase(name)}`},`;
|
|
83
|
-
});
|
|
84
|
-
const mutationEntries = tableNames.map((name) => {
|
|
85
|
-
return `\t${`create${toPascalCase(singularize(name))}`},`;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
return `const generatedQueries = {
|
|
89
|
-
${queryEntries.join("\n")}
|
|
90
|
-
} as const;
|
|
91
|
-
|
|
92
|
-
const generatedMutations = {
|
|
93
|
-
${mutationEntries.join("\n")}
|
|
94
|
-
} as const;`;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function generateOperations(tables: DiscoveredTable[]): string {
|
|
98
|
-
if (tables.length === 0) {
|
|
99
|
-
return `const generatedQueries = {} as const;
|
|
100
|
-
const generatedMutations = {} as const;`;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const queryBlocks = tables
|
|
104
|
-
.map((table) => createQueryBlock(table))
|
|
105
|
-
.join("\n\n");
|
|
106
|
-
const mutationBlocks = tables
|
|
107
|
-
.map((table) => createMutationBlock(table))
|
|
108
|
-
.join("\n\n");
|
|
109
|
-
const registry = createRegistry(tables.map((table) => table.exportName));
|
|
110
|
-
|
|
111
|
-
return `${queryBlocks}
|
|
112
|
-
|
|
113
|
-
${mutationBlocks}
|
|
114
|
-
|
|
115
|
-
${registry}`;
|
|
116
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
toPascalCase,
|
|
3
|
+
singularize,
|
|
4
|
+
zodForColumn,
|
|
5
|
+
zodForQueryColumn,
|
|
6
|
+
} from "./utils";
|
|
7
|
+
import type {
|
|
8
|
+
DiscoveredColumn,
|
|
9
|
+
DiscoveredTable,
|
|
10
|
+
} from "../../utils/schema-discovery";
|
|
11
|
+
|
|
12
|
+
function createArgsShape(columns: DiscoveredColumn[]): string {
|
|
13
|
+
const creatableColumns = columns.filter(
|
|
14
|
+
(column) => !(column.primaryKey && column.autoIncrement),
|
|
15
|
+
);
|
|
16
|
+
if (creatableColumns.length === 0) {
|
|
17
|
+
return "{}";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return `{
|
|
21
|
+
${creatableColumns.map((column) => `\t\t${column.name}: ${zodForColumn(column)},`).join("\n")}
|
|
22
|
+
\t}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function createInsertValues(columns: DiscoveredColumn[]): string {
|
|
26
|
+
const creatableColumns = columns.filter(
|
|
27
|
+
(column) => !(column.primaryKey && column.autoIncrement),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
if (creatableColumns.length === 0) {
|
|
31
|
+
return "{}";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return `{
|
|
35
|
+
${creatableColumns.map((column) => `\t\t\t${column.name}: args.${column.name},`).join("\n")}
|
|
36
|
+
\t\t}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createQueryBlock(table: DiscoveredTable): string {
|
|
40
|
+
const idColumn = table.columns.find((column) => column.name === "id");
|
|
41
|
+
const queryName = `get${toPascalCase(table.exportName)}`;
|
|
42
|
+
const args = idColumn
|
|
43
|
+
? `{
|
|
44
|
+
id: ${zodForQueryColumn(idColumn)},
|
|
45
|
+
\t}`
|
|
46
|
+
: "{}";
|
|
47
|
+
const queryBody = idColumn
|
|
48
|
+
? `if (args.id !== undefined) {
|
|
49
|
+
return ctx.$db.select().from(schema.${table.exportName}).where(eq(schema.${table.exportName}.id, args.id));
|
|
50
|
+
\t\t}
|
|
51
|
+
\n\t\treturn ctx.$db.select().from(schema.${table.exportName});`
|
|
52
|
+
: `return ctx.$db.select().from(schema.${table.exportName});`;
|
|
53
|
+
|
|
54
|
+
return `export const ${queryName} = query({
|
|
55
|
+
\targs: ${args},
|
|
56
|
+
\thandler: async (ctx, args) => {
|
|
57
|
+
\t\t${queryBody}
|
|
58
|
+
\t},
|
|
59
|
+
});`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createMutationBlock(table: DiscoveredTable): string {
|
|
63
|
+
const mutationName = `create${toPascalCase(singularize(table.exportName))}`;
|
|
64
|
+
const argsShape = createArgsShape(table.columns);
|
|
65
|
+
const values = createInsertValues(table.columns);
|
|
66
|
+
|
|
67
|
+
return `export const ${mutationName} = mutation({
|
|
68
|
+
\targs: ${argsShape},
|
|
69
|
+
\thandler: async (ctx, args) => {
|
|
70
|
+
const [created] = await ctx.$db
|
|
71
|
+
\t\t\t.insert(schema.${table.exportName})
|
|
72
|
+
\t\t\t.values(${values})
|
|
73
|
+
\t\t\t.returning();
|
|
74
|
+
|
|
75
|
+
\n\t\treturn created ?? null;
|
|
76
|
+
\t},
|
|
77
|
+
});`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function createRegistry(tableNames: string[]): string {
|
|
81
|
+
const queryEntries = tableNames.map((name) => {
|
|
82
|
+
return `\t${`get${toPascalCase(name)}`},`;
|
|
83
|
+
});
|
|
84
|
+
const mutationEntries = tableNames.map((name) => {
|
|
85
|
+
return `\t${`create${toPascalCase(singularize(name))}`},`;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return `const generatedQueries = {
|
|
89
|
+
${queryEntries.join("\n")}
|
|
90
|
+
} as const;
|
|
91
|
+
|
|
92
|
+
const generatedMutations = {
|
|
93
|
+
${mutationEntries.join("\n")}
|
|
94
|
+
} as const;`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function generateOperations(tables: DiscoveredTable[]): string {
|
|
98
|
+
if (tables.length === 0) {
|
|
99
|
+
return `const generatedQueries = {} as const;
|
|
100
|
+
const generatedMutations = {} as const;`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const queryBlocks = tables
|
|
104
|
+
.map((table) => createQueryBlock(table))
|
|
105
|
+
.join("\n\n");
|
|
106
|
+
const mutationBlocks = tables
|
|
107
|
+
.map((table) => createMutationBlock(table))
|
|
108
|
+
.join("\n\n");
|
|
109
|
+
const registry = createRegistry(tables.map((table) => table.exportName));
|
|
110
|
+
|
|
111
|
+
return `${queryBlocks}
|
|
112
|
+
|
|
113
|
+
${mutationBlocks}
|
|
114
|
+
|
|
115
|
+
${registry}`;
|
|
116
|
+
}
|
|
@@ -1,83 +1,91 @@
|
|
|
1
|
-
import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
|
|
2
|
-
import { buildRegistrationTemplateSections } from "./generators/registration/sections";
|
|
3
|
-
import { realtimeModule } from "./generators/registration/modules/realtime";
|
|
4
|
-
import { storageModule } from "./generators/registration/modules/storage";
|
|
5
|
-
import { schedulerModule } from "./generators/registration/modules/scheduler";
|
|
6
|
-
import { cronModule } from "./generators/registration/modules/cron";
|
|
7
|
-
|
|
8
|
-
export function generateRegistration(
|
|
9
|
-
operations: DiscoveredHandlerOperation[],
|
|
10
|
-
): string {
|
|
11
|
-
const {
|
|
12
|
-
imports,
|
|
13
|
-
operationSchemas,
|
|
14
|
-
schedulerSchemas,
|
|
15
|
-
queryRoutes,
|
|
16
|
-
mutationRoutes,
|
|
17
|
-
queryRegistryEntries,
|
|
18
|
-
schedulerEntries,
|
|
19
|
-
schedulerPayloadMapEntries,
|
|
20
|
-
cronEntries,
|
|
21
|
-
storageHandlersEntries,
|
|
22
|
-
} = buildRegistrationTemplateSections(operations);
|
|
23
|
-
|
|
24
|
-
return `import { sValidator } from "@hono/standard-validator";
|
|
25
|
-
import type { Hono } from "hono";
|
|
26
|
-
import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
|
|
27
|
-
import { ZodError, z } from "zod";
|
|
28
|
-
import {
|
|
29
|
-
AppflareHandledError,
|
|
30
|
-
type AppflareContext,
|
|
31
|
-
type DbMutationEvent,
|
|
32
|
-
createDb,
|
|
33
|
-
createQueryDb,
|
|
34
|
-
setStorageHandlers,
|
|
35
|
-
type RegisterHandlersOptions,
|
|
36
|
-
type StorageMethod,
|
|
37
|
-
type WorkerEnv,
|
|
38
|
-
} from "./handlers";
|
|
39
|
-
import { createExecutionContext, createSchedulerExecutionContext, resolveSession } from "./handlers.context";
|
|
40
|
-
import { executeOperation, handleOperationError } from "./handlers.execution";
|
|
41
|
-
${imports ? `\n${imports}` : ""}
|
|
42
|
-
|
|
43
|
-
${operationSchemas}
|
|
44
|
-
${schedulerSchemas}
|
|
45
|
-
|
|
46
|
-
const realtimeQueryHandlers = {${queryRegistryEntries || "\n"}
|
|
47
|
-
} as const;
|
|
48
|
-
|
|
49
|
-
const schedulerHandlers = {${schedulerEntries || "\n"}
|
|
50
|
-
} as const;
|
|
51
|
-
|
|
52
|
-
type GeneratedSchedulerPayloadMap = {${
|
|
53
|
-
schedulerPayloadMapEntries ? `\n${schedulerPayloadMapEntries}\n` : ""
|
|
54
|
-
}};
|
|
55
|
-
|
|
56
|
-
declare global {
|
|
57
|
-
interface AppflareSchedulerHandlerMap extends GeneratedSchedulerPayloadMap {}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
${
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
1
|
+
import type { DiscoveredHandlerOperation } from "../../utils/handler-discovery";
|
|
2
|
+
import { buildRegistrationTemplateSections } from "./generators/registration/sections";
|
|
3
|
+
import { realtimeModule } from "./generators/registration/modules/realtime";
|
|
4
|
+
import { storageModule } from "./generators/registration/modules/storage";
|
|
5
|
+
import { schedulerModule } from "./generators/registration/modules/scheduler";
|
|
6
|
+
import { cronModule } from "./generators/registration/modules/cron";
|
|
7
|
+
|
|
8
|
+
export function generateRegistration(
|
|
9
|
+
operations: DiscoveredHandlerOperation[],
|
|
10
|
+
): string {
|
|
11
|
+
const {
|
|
12
|
+
imports,
|
|
13
|
+
operationSchemas,
|
|
14
|
+
schedulerSchemas,
|
|
15
|
+
queryRoutes,
|
|
16
|
+
mutationRoutes,
|
|
17
|
+
queryRegistryEntries,
|
|
18
|
+
schedulerEntries,
|
|
19
|
+
schedulerPayloadMapEntries,
|
|
20
|
+
cronEntries,
|
|
21
|
+
storageHandlersEntries,
|
|
22
|
+
} = buildRegistrationTemplateSections(operations);
|
|
23
|
+
|
|
24
|
+
return `import { sValidator } from "@hono/standard-validator";
|
|
25
|
+
import type { Hono } from "hono";
|
|
26
|
+
import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
|
|
27
|
+
import { ZodError, z } from "zod";
|
|
28
|
+
import {
|
|
29
|
+
AppflareHandledError,
|
|
30
|
+
type AppflareContext,
|
|
31
|
+
type DbMutationEvent,
|
|
32
|
+
createDb,
|
|
33
|
+
createQueryDb,
|
|
34
|
+
setStorageHandlers,
|
|
35
|
+
type RegisterHandlersOptions,
|
|
36
|
+
type StorageMethod,
|
|
37
|
+
type WorkerEnv,
|
|
38
|
+
} from "./handlers";
|
|
39
|
+
import { createExecutionContext, createSchedulerExecutionContext, resolveSession } from "./handlers.context";
|
|
40
|
+
import { executeOperation, handleOperationError } from "./handlers.execution";
|
|
41
|
+
${imports ? `\n${imports}` : ""}
|
|
42
|
+
|
|
43
|
+
${operationSchemas}
|
|
44
|
+
${schedulerSchemas}
|
|
45
|
+
|
|
46
|
+
const realtimeQueryHandlers = {${queryRegistryEntries || "\n"}
|
|
47
|
+
} as const;
|
|
48
|
+
|
|
49
|
+
const schedulerHandlers = {${schedulerEntries || "\n"}
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
type GeneratedSchedulerPayloadMap = {${
|
|
53
|
+
schedulerPayloadMapEntries ? `\n${schedulerPayloadMapEntries}\n` : ""
|
|
54
|
+
}};
|
|
55
|
+
|
|
56
|
+
declare global {
|
|
57
|
+
interface AppflareSchedulerHandlerMap extends GeneratedSchedulerPayloadMap {}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type CronHandlerEntry = {
|
|
61
|
+
taskName: string;
|
|
62
|
+
cronTriggers: readonly string[];
|
|
63
|
+
definition: {
|
|
64
|
+
handler: (ctx: AppflareContext) => void | Promise<void>;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const cronHandlers: readonly CronHandlerEntry[] = [${cronEntries || "\n"}
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const storageHandlers = [${storageHandlersEntries || "\n"}
|
|
72
|
+
] as const;
|
|
73
|
+
|
|
74
|
+
setStorageHandlers([...storageHandlers]);
|
|
75
|
+
|
|
76
|
+
${schedulerModule}
|
|
77
|
+
|
|
78
|
+
${realtimeModule}
|
|
79
|
+
|
|
80
|
+
export function registerGeneratedHandlers(
|
|
81
|
+
app: Hono<WorkerEnv>,
|
|
82
|
+
options: RegisterHandlersOptions,
|
|
83
|
+
): void {
|
|
84
|
+
registerRealtimeRoutes(app, options);${queryRoutes || "\n\t// No query handlers discovered under scanDir/queries.\n"}${mutationRoutes || "\n\t// No mutation handlers discovered under scanDir/mutations.\n"}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
${storageModule}
|
|
88
|
+
|
|
89
|
+
${cronModule}
|
|
90
|
+
`;
|
|
91
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { generateTypesCoreSection } from "./generators/types/core";
|
|
2
|
-
import { generateTypesQueryDefinitionsSection } from "./generators/types/query-definitions";
|
|
3
|
-
import { generateTypesQueryRuntimeSection } from "./generators/types/query-runtime";
|
|
4
|
-
import { generateTypesContextSection } from "./generators/types/context";
|
|
5
|
-
import { generateTypesOperationsSection } from "./generators/types/operations";
|
|
6
|
-
|
|
7
|
-
export function generateTypes(): string {
|
|
8
|
-
return [
|
|
9
|
-
generateTypesCoreSection(),
|
|
10
|
-
generateTypesQueryDefinitionsSection(),
|
|
11
|
-
generateTypesQueryRuntimeSection(),
|
|
12
|
-
generateTypesContextSection(),
|
|
13
|
-
generateTypesOperationsSection(),
|
|
14
|
-
].join("\n\n");
|
|
15
|
-
}
|
|
1
|
+
import { generateTypesCoreSection } from "./generators/types/core";
|
|
2
|
+
import { generateTypesQueryDefinitionsSection } from "./generators/types/query-definitions";
|
|
3
|
+
import { generateTypesQueryRuntimeSection } from "./generators/types/query-runtime";
|
|
4
|
+
import { generateTypesContextSection } from "./generators/types/context";
|
|
5
|
+
import { generateTypesOperationsSection } from "./generators/types/operations";
|
|
6
|
+
|
|
7
|
+
export function generateTypes(): string {
|
|
8
|
+
return [
|
|
9
|
+
generateTypesCoreSection(),
|
|
10
|
+
generateTypesQueryDefinitionsSection(),
|
|
11
|
+
generateTypesQueryRuntimeSection(),
|
|
12
|
+
generateTypesContextSection(),
|
|
13
|
+
generateTypesOperationsSection(),
|
|
14
|
+
].join("\n\n");
|
|
15
|
+
}
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import type { DiscoveredColumn } from "../../utils/schema-discovery";
|
|
2
|
-
|
|
3
|
-
export function toPascalCase(value: string): string {
|
|
4
|
-
return value
|
|
5
|
-
.replace(/[_-]+/g, " ")
|
|
6
|
-
.replace(/\s+(.)/g, (_match, char: string) => char.toUpperCase())
|
|
7
|
-
.replace(/\s/g, "")
|
|
8
|
-
.replace(/^(.)/, (_match, char: string) => char.toUpperCase());
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function singularize(value: string): string {
|
|
12
|
-
if (value.endsWith("ies")) {
|
|
13
|
-
return `${value.slice(0, -3)}y`;
|
|
14
|
-
}
|
|
15
|
-
if (value.endsWith("ses")) {
|
|
16
|
-
return value.slice(0, -2);
|
|
17
|
-
}
|
|
18
|
-
if (value.endsWith("s") && value.length > 1) {
|
|
19
|
-
return value.slice(0, -1);
|
|
20
|
-
}
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function zodForColumn(column: DiscoveredColumn): string {
|
|
25
|
-
if (column.type === "number") {
|
|
26
|
-
return `z.number()${column.optional ? ".optional()" : ""}`;
|
|
27
|
-
}
|
|
28
|
-
if (column.type === "boolean") {
|
|
29
|
-
return `z.boolean()${column.optional ? ".optional()" : ""}`;
|
|
30
|
-
}
|
|
31
|
-
if (column.type === "string") {
|
|
32
|
-
return `z.string()${column.optional ? ".optional()" : ""}`;
|
|
33
|
-
}
|
|
34
|
-
return `z.unknown()${column.optional ? ".optional()" : ""}`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function zodForQueryColumn(column: DiscoveredColumn): string {
|
|
38
|
-
if (column.type === "number") {
|
|
39
|
-
return "z.coerce.number().optional()";
|
|
40
|
-
}
|
|
41
|
-
if (column.type === "boolean") {
|
|
42
|
-
return "z.coerce.boolean().optional()";
|
|
43
|
-
}
|
|
44
|
-
if (column.type === "string") {
|
|
45
|
-
return "z.string().optional()";
|
|
46
|
-
}
|
|
47
|
-
return "z.unknown().optional()";
|
|
48
|
-
}
|
|
1
|
+
import type { DiscoveredColumn } from "../../utils/schema-discovery";
|
|
2
|
+
|
|
3
|
+
export function toPascalCase(value: string): string {
|
|
4
|
+
return value
|
|
5
|
+
.replace(/[_-]+/g, " ")
|
|
6
|
+
.replace(/\s+(.)/g, (_match, char: string) => char.toUpperCase())
|
|
7
|
+
.replace(/\s/g, "")
|
|
8
|
+
.replace(/^(.)/, (_match, char: string) => char.toUpperCase());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function singularize(value: string): string {
|
|
12
|
+
if (value.endsWith("ies")) {
|
|
13
|
+
return `${value.slice(0, -3)}y`;
|
|
14
|
+
}
|
|
15
|
+
if (value.endsWith("ses")) {
|
|
16
|
+
return value.slice(0, -2);
|
|
17
|
+
}
|
|
18
|
+
if (value.endsWith("s") && value.length > 1) {
|
|
19
|
+
return value.slice(0, -1);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function zodForColumn(column: DiscoveredColumn): string {
|
|
25
|
+
if (column.type === "number") {
|
|
26
|
+
return `z.number()${column.optional ? ".optional()" : ""}`;
|
|
27
|
+
}
|
|
28
|
+
if (column.type === "boolean") {
|
|
29
|
+
return `z.boolean()${column.optional ? ".optional()" : ""}`;
|
|
30
|
+
}
|
|
31
|
+
if (column.type === "string") {
|
|
32
|
+
return `z.string()${column.optional ? ".optional()" : ""}`;
|
|
33
|
+
}
|
|
34
|
+
return `z.unknown()${column.optional ? ".optional()" : ""}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function zodForQueryColumn(column: DiscoveredColumn): string {
|
|
38
|
+
if (column.type === "number") {
|
|
39
|
+
return "z.coerce.number().optional()";
|
|
40
|
+
}
|
|
41
|
+
if (column.type === "boolean") {
|
|
42
|
+
return "z.coerce.boolean().optional()";
|
|
43
|
+
}
|
|
44
|
+
if (column.type === "string") {
|
|
45
|
+
return "z.string().optional()";
|
|
46
|
+
}
|
|
47
|
+
return "z.unknown().optional()";
|
|
48
|
+
}
|