appflare 0.2.30 → 0.2.32
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 +243 -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 +195 -195
- package/cli/templates/core/client/types.ts +186 -186
- 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 -82
- package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
- package/cli/templates/handlers/generators/context/types.ts +40 -40
- 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 +199 -199
- package/cli/templates/handlers/generators/registration/sections.ts +210 -210
- package/cli/templates/handlers/generators/types/context.ts +92 -92
- 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 +281 -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 +1103 -1031
- package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -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 +157 -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 +697 -697
- 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.d.mts +2 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +366 -203
- package/dist/cli/index.mjs +366 -203
- 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,399 +1,399 @@
|
|
|
1
|
-
import { readdir } from "node:fs/promises";
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
import type { LoadedAppflareConfig } from "../types";
|
|
4
|
-
|
|
5
|
-
export type DiscoveredColumn = {
|
|
6
|
-
name: string;
|
|
7
|
-
expression: string;
|
|
8
|
-
type: "string" | "number" | "boolean" | "date" | "unknown";
|
|
9
|
-
optional: boolean;
|
|
10
|
-
primaryKey: boolean;
|
|
11
|
-
autoIncrement: boolean;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type DiscoveredTable = {
|
|
15
|
-
exportName: string;
|
|
16
|
-
tableName: string;
|
|
17
|
-
columns: DiscoveredColumn[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type DiscoveredSchema = {
|
|
21
|
-
schemaPath: string;
|
|
22
|
-
tables: DiscoveredTable[];
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
function splitTopLevel(value: string): string[] {
|
|
26
|
-
const result: string[] = [];
|
|
27
|
-
let current = "";
|
|
28
|
-
let depthParen = 0;
|
|
29
|
-
let depthBrace = 0;
|
|
30
|
-
let depthBracket = 0;
|
|
31
|
-
let inSingleQuote = false;
|
|
32
|
-
let inDoubleQuote = false;
|
|
33
|
-
let inTemplateString = false;
|
|
34
|
-
let escaped = false;
|
|
35
|
-
|
|
36
|
-
for (let i = 0; i < value.length; i += 1) {
|
|
37
|
-
const char = value[i];
|
|
38
|
-
|
|
39
|
-
if (escaped) {
|
|
40
|
-
current += char;
|
|
41
|
-
escaped = false;
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (char === "\\") {
|
|
46
|
-
current += char;
|
|
47
|
-
escaped = true;
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
52
|
-
inSingleQuote = !inSingleQuote;
|
|
53
|
-
current += char;
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
58
|
-
inDoubleQuote = !inDoubleQuote;
|
|
59
|
-
current += char;
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
64
|
-
inTemplateString = !inTemplateString;
|
|
65
|
-
current += char;
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
70
|
-
current += char;
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (char === "(") {
|
|
75
|
-
depthParen += 1;
|
|
76
|
-
} else if (char === ")") {
|
|
77
|
-
depthParen -= 1;
|
|
78
|
-
} else if (char === "{") {
|
|
79
|
-
depthBrace += 1;
|
|
80
|
-
} else if (char === "}") {
|
|
81
|
-
depthBrace -= 1;
|
|
82
|
-
} else if (char === "[") {
|
|
83
|
-
depthBracket += 1;
|
|
84
|
-
} else if (char === "]") {
|
|
85
|
-
depthBracket -= 1;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
char === "," &&
|
|
90
|
-
depthParen === 0 &&
|
|
91
|
-
depthBrace === 0 &&
|
|
92
|
-
depthBracket === 0
|
|
93
|
-
) {
|
|
94
|
-
const segment = current.trim();
|
|
95
|
-
if (segment.length > 0) {
|
|
96
|
-
result.push(segment);
|
|
97
|
-
}
|
|
98
|
-
current = "";
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
current += char;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const trailing = current.trim();
|
|
106
|
-
if (trailing.length > 0) {
|
|
107
|
-
result.push(trailing);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function getTopLevelColonIndex(entry: string): number {
|
|
114
|
-
let depthParen = 0;
|
|
115
|
-
let depthBrace = 0;
|
|
116
|
-
let depthBracket = 0;
|
|
117
|
-
let inSingleQuote = false;
|
|
118
|
-
let inDoubleQuote = false;
|
|
119
|
-
let inTemplateString = false;
|
|
120
|
-
let escaped = false;
|
|
121
|
-
|
|
122
|
-
for (let i = 0; i < entry.length; i += 1) {
|
|
123
|
-
const char = entry[i];
|
|
124
|
-
|
|
125
|
-
if (escaped) {
|
|
126
|
-
escaped = false;
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (char === "\\") {
|
|
131
|
-
escaped = true;
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
136
|
-
inSingleQuote = !inSingleQuote;
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
141
|
-
inDoubleQuote = !inDoubleQuote;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
146
|
-
inTemplateString = !inTemplateString;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (char === "(") {
|
|
155
|
-
depthParen += 1;
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
if (char === ")") {
|
|
159
|
-
depthParen -= 1;
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
if (char === "{") {
|
|
163
|
-
depthBrace += 1;
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
if (char === "}") {
|
|
167
|
-
depthBrace -= 1;
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (char === "[") {
|
|
171
|
-
depthBracket += 1;
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (char === "]") {
|
|
175
|
-
depthBracket -= 1;
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
char === ":" &&
|
|
181
|
-
depthParen === 0 &&
|
|
182
|
-
depthBrace === 0 &&
|
|
183
|
-
depthBracket === 0
|
|
184
|
-
) {
|
|
185
|
-
return i;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return -1;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function inferColumnType(expression: string): DiscoveredColumn["type"] {
|
|
193
|
-
const lowered = expression.toLowerCase();
|
|
194
|
-
if (/mode\s*:\s*["'`](timestamp|timestamp_ms)["'`]/.test(lowered)) {
|
|
195
|
-
return "date";
|
|
196
|
-
}
|
|
197
|
-
if (/mode\s*:\s*["'`]boolean["'`]/.test(lowered)) {
|
|
198
|
-
return "boolean";
|
|
199
|
-
}
|
|
200
|
-
if (/\.(date|datetime|timestamp)\s*\(/.test(lowered)) {
|
|
201
|
-
return "date";
|
|
202
|
-
}
|
|
203
|
-
if (/\.(int|integer|real|numeric|decimal|float|double)\s*\(/.test(lowered)) {
|
|
204
|
-
return "number";
|
|
205
|
-
}
|
|
206
|
-
if (/\.(text|varchar|char)\s*\(/.test(lowered)) {
|
|
207
|
-
return "string";
|
|
208
|
-
}
|
|
209
|
-
if (/\.(boolean|bool)\s*\(/.test(lowered)) {
|
|
210
|
-
return "boolean";
|
|
211
|
-
}
|
|
212
|
-
return "unknown";
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function discoverColumns(rawColumns: string): DiscoveredColumn[] {
|
|
216
|
-
const entries = splitTopLevel(rawColumns);
|
|
217
|
-
const columns: DiscoveredColumn[] = [];
|
|
218
|
-
|
|
219
|
-
for (const entry of entries) {
|
|
220
|
-
const colonIndex = getTopLevelColonIndex(entry);
|
|
221
|
-
if (colonIndex === -1) {
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const key = entry
|
|
226
|
-
.slice(0, colonIndex)
|
|
227
|
-
.trim()
|
|
228
|
-
.replace(/^['"]|['"]$/g, "");
|
|
229
|
-
if (!key) {
|
|
230
|
-
continue;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const expression = entry.slice(colonIndex + 1).trim();
|
|
234
|
-
const primaryKey = /\.primarykey\s*\(/i.test(expression);
|
|
235
|
-
const autoIncrement = /autoincrement\s*:\s*true/i.test(expression);
|
|
236
|
-
const hasDefault = /\.default\s*\(/i.test(expression);
|
|
237
|
-
const notNull = /\.notnull\s*\(/i.test(expression);
|
|
238
|
-
const optional = !notNull || hasDefault || autoIncrement || primaryKey;
|
|
239
|
-
|
|
240
|
-
columns.push({
|
|
241
|
-
name: key,
|
|
242
|
-
expression,
|
|
243
|
-
type: inferColumnType(expression),
|
|
244
|
-
optional,
|
|
245
|
-
primaryKey,
|
|
246
|
-
autoIncrement,
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return columns;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function discoverTables(schemaSource: string): DiscoveredTable[] {
|
|
254
|
-
const tablePattern =
|
|
255
|
-
/export\s+const\s+(\w+)\s*=\s*table\s*\(\s*["'`]([^"'`]+)["'`]/g;
|
|
256
|
-
const tables: DiscoveredTable[] = [];
|
|
257
|
-
|
|
258
|
-
const findMatchingBrace = (source: string, startIndex: number): number => {
|
|
259
|
-
let depth = 0;
|
|
260
|
-
let inSingleQuote = false;
|
|
261
|
-
let inDoubleQuote = false;
|
|
262
|
-
let inTemplateString = false;
|
|
263
|
-
let escaped = false;
|
|
264
|
-
|
|
265
|
-
for (let i = startIndex; i < source.length; i += 1) {
|
|
266
|
-
const char = source[i];
|
|
267
|
-
|
|
268
|
-
if (escaped) {
|
|
269
|
-
escaped = false;
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (char === "\\") {
|
|
274
|
-
escaped = true;
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
279
|
-
inSingleQuote = !inSingleQuote;
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
284
|
-
inDoubleQuote = !inDoubleQuote;
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
289
|
-
inTemplateString = !inTemplateString;
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (char === "{") {
|
|
298
|
-
depth += 1;
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if (char === "}") {
|
|
303
|
-
depth -= 1;
|
|
304
|
-
if (depth === 0) {
|
|
305
|
-
return i;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return -1;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
let match = tablePattern.exec(schemaSource);
|
|
314
|
-
while (match) {
|
|
315
|
-
const exportName = match[1];
|
|
316
|
-
const tableName = match[2];
|
|
317
|
-
const objectStart = schemaSource.indexOf("{", tablePattern.lastIndex);
|
|
318
|
-
if (objectStart === -1) {
|
|
319
|
-
match = tablePattern.exec(schemaSource);
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const objectEnd = findMatchingBrace(schemaSource, objectStart);
|
|
324
|
-
if (objectEnd === -1) {
|
|
325
|
-
match = tablePattern.exec(schemaSource);
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const rawColumns = schemaSource.slice(objectStart + 1, objectEnd);
|
|
330
|
-
tables.push({
|
|
331
|
-
exportName,
|
|
332
|
-
tableName,
|
|
333
|
-
columns: discoverColumns(rawColumns),
|
|
334
|
-
});
|
|
335
|
-
match = tablePattern.exec(schemaSource);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return tables;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
async function findSchemaCandidates(dirPath: string): Promise<string[]> {
|
|
342
|
-
const entries = await readdir(dirPath, { withFileTypes: true });
|
|
343
|
-
const discovered: string[] = [];
|
|
344
|
-
|
|
345
|
-
for (const entry of entries) {
|
|
346
|
-
if (entry.name.startsWith(".")) {
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
if (entry.name === "node_modules" || entry.name === "_generated") {
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const entryPath = resolve(dirPath, entry.name);
|
|
354
|
-
if (entry.isDirectory()) {
|
|
355
|
-
discovered.push(...(await findSchemaCandidates(entryPath)));
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
if (entry.isFile() && entry.name === "schema.ts") {
|
|
360
|
-
discovered.push(entryPath);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
return discovered;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export async function discoverSchema(
|
|
368
|
-
loadedConfig: LoadedAppflareConfig,
|
|
369
|
-
prioritySchemaPaths: string[] = [],
|
|
370
|
-
): Promise<DiscoveredSchema> {
|
|
371
|
-
const scanCandidates = await findSchemaCandidates(
|
|
372
|
-
loadedConfig.scanDirAbs,
|
|
373
|
-
).catch(() => []);
|
|
374
|
-
|
|
375
|
-
const fallbackCandidate = resolve(loadedConfig.configDir, "schema.ts");
|
|
376
|
-
const candidates = [
|
|
377
|
-
...prioritySchemaPaths,
|
|
378
|
-
...(scanCandidates.length ? scanCandidates : [fallbackCandidate]),
|
|
379
|
-
];
|
|
380
|
-
|
|
381
|
-
for (const schemaPath of candidates) {
|
|
382
|
-
const file = Bun.file(schemaPath);
|
|
383
|
-
if (!(await file.exists())) {
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
const source = await file.text();
|
|
387
|
-
const tables = discoverTables(source);
|
|
388
|
-
if (tables.length > 0) {
|
|
389
|
-
return {
|
|
390
|
-
schemaPath,
|
|
391
|
-
tables,
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
throw new Error(
|
|
397
|
-
`Unable to discover schema.ts under scanDir (${loadedConfig.scanDirAbs}) or fallback (${fallbackCandidate}).`,
|
|
398
|
-
);
|
|
399
|
-
}
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import type { LoadedAppflareConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export type DiscoveredColumn = {
|
|
6
|
+
name: string;
|
|
7
|
+
expression: string;
|
|
8
|
+
type: "string" | "number" | "boolean" | "date" | "unknown";
|
|
9
|
+
optional: boolean;
|
|
10
|
+
primaryKey: boolean;
|
|
11
|
+
autoIncrement: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type DiscoveredTable = {
|
|
15
|
+
exportName: string;
|
|
16
|
+
tableName: string;
|
|
17
|
+
columns: DiscoveredColumn[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type DiscoveredSchema = {
|
|
21
|
+
schemaPath: string;
|
|
22
|
+
tables: DiscoveredTable[];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function splitTopLevel(value: string): string[] {
|
|
26
|
+
const result: string[] = [];
|
|
27
|
+
let current = "";
|
|
28
|
+
let depthParen = 0;
|
|
29
|
+
let depthBrace = 0;
|
|
30
|
+
let depthBracket = 0;
|
|
31
|
+
let inSingleQuote = false;
|
|
32
|
+
let inDoubleQuote = false;
|
|
33
|
+
let inTemplateString = false;
|
|
34
|
+
let escaped = false;
|
|
35
|
+
|
|
36
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
37
|
+
const char = value[i];
|
|
38
|
+
|
|
39
|
+
if (escaped) {
|
|
40
|
+
current += char;
|
|
41
|
+
escaped = false;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (char === "\\") {
|
|
46
|
+
current += char;
|
|
47
|
+
escaped = true;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
52
|
+
inSingleQuote = !inSingleQuote;
|
|
53
|
+
current += char;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
58
|
+
inDoubleQuote = !inDoubleQuote;
|
|
59
|
+
current += char;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
64
|
+
inTemplateString = !inTemplateString;
|
|
65
|
+
current += char;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
70
|
+
current += char;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (char === "(") {
|
|
75
|
+
depthParen += 1;
|
|
76
|
+
} else if (char === ")") {
|
|
77
|
+
depthParen -= 1;
|
|
78
|
+
} else if (char === "{") {
|
|
79
|
+
depthBrace += 1;
|
|
80
|
+
} else if (char === "}") {
|
|
81
|
+
depthBrace -= 1;
|
|
82
|
+
} else if (char === "[") {
|
|
83
|
+
depthBracket += 1;
|
|
84
|
+
} else if (char === "]") {
|
|
85
|
+
depthBracket -= 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
char === "," &&
|
|
90
|
+
depthParen === 0 &&
|
|
91
|
+
depthBrace === 0 &&
|
|
92
|
+
depthBracket === 0
|
|
93
|
+
) {
|
|
94
|
+
const segment = current.trim();
|
|
95
|
+
if (segment.length > 0) {
|
|
96
|
+
result.push(segment);
|
|
97
|
+
}
|
|
98
|
+
current = "";
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
current += char;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const trailing = current.trim();
|
|
106
|
+
if (trailing.length > 0) {
|
|
107
|
+
result.push(trailing);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function getTopLevelColonIndex(entry: string): number {
|
|
114
|
+
let depthParen = 0;
|
|
115
|
+
let depthBrace = 0;
|
|
116
|
+
let depthBracket = 0;
|
|
117
|
+
let inSingleQuote = false;
|
|
118
|
+
let inDoubleQuote = false;
|
|
119
|
+
let inTemplateString = false;
|
|
120
|
+
let escaped = false;
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < entry.length; i += 1) {
|
|
123
|
+
const char = entry[i];
|
|
124
|
+
|
|
125
|
+
if (escaped) {
|
|
126
|
+
escaped = false;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (char === "\\") {
|
|
131
|
+
escaped = true;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
136
|
+
inSingleQuote = !inSingleQuote;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
141
|
+
inDoubleQuote = !inDoubleQuote;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
146
|
+
inTemplateString = !inTemplateString;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (char === "(") {
|
|
155
|
+
depthParen += 1;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (char === ")") {
|
|
159
|
+
depthParen -= 1;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (char === "{") {
|
|
163
|
+
depthBrace += 1;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (char === "}") {
|
|
167
|
+
depthBrace -= 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (char === "[") {
|
|
171
|
+
depthBracket += 1;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (char === "]") {
|
|
175
|
+
depthBracket -= 1;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (
|
|
180
|
+
char === ":" &&
|
|
181
|
+
depthParen === 0 &&
|
|
182
|
+
depthBrace === 0 &&
|
|
183
|
+
depthBracket === 0
|
|
184
|
+
) {
|
|
185
|
+
return i;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return -1;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function inferColumnType(expression: string): DiscoveredColumn["type"] {
|
|
193
|
+
const lowered = expression.toLowerCase();
|
|
194
|
+
if (/mode\s*:\s*["'`](timestamp|timestamp_ms)["'`]/.test(lowered)) {
|
|
195
|
+
return "date";
|
|
196
|
+
}
|
|
197
|
+
if (/mode\s*:\s*["'`]boolean["'`]/.test(lowered)) {
|
|
198
|
+
return "boolean";
|
|
199
|
+
}
|
|
200
|
+
if (/\.(date|datetime|timestamp)\s*\(/.test(lowered)) {
|
|
201
|
+
return "date";
|
|
202
|
+
}
|
|
203
|
+
if (/\.(int|integer|real|numeric|decimal|float|double)\s*\(/.test(lowered)) {
|
|
204
|
+
return "number";
|
|
205
|
+
}
|
|
206
|
+
if (/\.(text|varchar|char)\s*\(/.test(lowered)) {
|
|
207
|
+
return "string";
|
|
208
|
+
}
|
|
209
|
+
if (/\.(boolean|bool)\s*\(/.test(lowered)) {
|
|
210
|
+
return "boolean";
|
|
211
|
+
}
|
|
212
|
+
return "unknown";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function discoverColumns(rawColumns: string): DiscoveredColumn[] {
|
|
216
|
+
const entries = splitTopLevel(rawColumns);
|
|
217
|
+
const columns: DiscoveredColumn[] = [];
|
|
218
|
+
|
|
219
|
+
for (const entry of entries) {
|
|
220
|
+
const colonIndex = getTopLevelColonIndex(entry);
|
|
221
|
+
if (colonIndex === -1) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const key = entry
|
|
226
|
+
.slice(0, colonIndex)
|
|
227
|
+
.trim()
|
|
228
|
+
.replace(/^['"]|['"]$/g, "");
|
|
229
|
+
if (!key) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const expression = entry.slice(colonIndex + 1).trim();
|
|
234
|
+
const primaryKey = /\.primarykey\s*\(/i.test(expression);
|
|
235
|
+
const autoIncrement = /autoincrement\s*:\s*true/i.test(expression);
|
|
236
|
+
const hasDefault = /\.default\s*\(/i.test(expression);
|
|
237
|
+
const notNull = /\.notnull\s*\(/i.test(expression);
|
|
238
|
+
const optional = !notNull || hasDefault || autoIncrement || primaryKey;
|
|
239
|
+
|
|
240
|
+
columns.push({
|
|
241
|
+
name: key,
|
|
242
|
+
expression,
|
|
243
|
+
type: inferColumnType(expression),
|
|
244
|
+
optional,
|
|
245
|
+
primaryKey,
|
|
246
|
+
autoIncrement,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return columns;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function discoverTables(schemaSource: string): DiscoveredTable[] {
|
|
254
|
+
const tablePattern =
|
|
255
|
+
/export\s+const\s+(\w+)\s*=\s*table\s*\(\s*["'`]([^"'`]+)["'`]/g;
|
|
256
|
+
const tables: DiscoveredTable[] = [];
|
|
257
|
+
|
|
258
|
+
const findMatchingBrace = (source: string, startIndex: number): number => {
|
|
259
|
+
let depth = 0;
|
|
260
|
+
let inSingleQuote = false;
|
|
261
|
+
let inDoubleQuote = false;
|
|
262
|
+
let inTemplateString = false;
|
|
263
|
+
let escaped = false;
|
|
264
|
+
|
|
265
|
+
for (let i = startIndex; i < source.length; i += 1) {
|
|
266
|
+
const char = source[i];
|
|
267
|
+
|
|
268
|
+
if (escaped) {
|
|
269
|
+
escaped = false;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (char === "\\") {
|
|
274
|
+
escaped = true;
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (!inDoubleQuote && !inTemplateString && char === "'") {
|
|
279
|
+
inSingleQuote = !inSingleQuote;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!inSingleQuote && !inTemplateString && char === '"') {
|
|
284
|
+
inDoubleQuote = !inDoubleQuote;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (!inSingleQuote && !inDoubleQuote && char === "`") {
|
|
289
|
+
inTemplateString = !inTemplateString;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (inSingleQuote || inDoubleQuote || inTemplateString) {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (char === "{") {
|
|
298
|
+
depth += 1;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (char === "}") {
|
|
303
|
+
depth -= 1;
|
|
304
|
+
if (depth === 0) {
|
|
305
|
+
return i;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return -1;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
let match = tablePattern.exec(schemaSource);
|
|
314
|
+
while (match) {
|
|
315
|
+
const exportName = match[1];
|
|
316
|
+
const tableName = match[2];
|
|
317
|
+
const objectStart = schemaSource.indexOf("{", tablePattern.lastIndex);
|
|
318
|
+
if (objectStart === -1) {
|
|
319
|
+
match = tablePattern.exec(schemaSource);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const objectEnd = findMatchingBrace(schemaSource, objectStart);
|
|
324
|
+
if (objectEnd === -1) {
|
|
325
|
+
match = tablePattern.exec(schemaSource);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const rawColumns = schemaSource.slice(objectStart + 1, objectEnd);
|
|
330
|
+
tables.push({
|
|
331
|
+
exportName,
|
|
332
|
+
tableName,
|
|
333
|
+
columns: discoverColumns(rawColumns),
|
|
334
|
+
});
|
|
335
|
+
match = tablePattern.exec(schemaSource);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return tables;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async function findSchemaCandidates(dirPath: string): Promise<string[]> {
|
|
342
|
+
const entries = await readdir(dirPath, { withFileTypes: true });
|
|
343
|
+
const discovered: string[] = [];
|
|
344
|
+
|
|
345
|
+
for (const entry of entries) {
|
|
346
|
+
if (entry.name.startsWith(".")) {
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (entry.name === "node_modules" || entry.name === "_generated") {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const entryPath = resolve(dirPath, entry.name);
|
|
354
|
+
if (entry.isDirectory()) {
|
|
355
|
+
discovered.push(...(await findSchemaCandidates(entryPath)));
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (entry.isFile() && entry.name === "schema.ts") {
|
|
360
|
+
discovered.push(entryPath);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return discovered;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export async function discoverSchema(
|
|
368
|
+
loadedConfig: LoadedAppflareConfig,
|
|
369
|
+
prioritySchemaPaths: string[] = [],
|
|
370
|
+
): Promise<DiscoveredSchema> {
|
|
371
|
+
const scanCandidates = await findSchemaCandidates(
|
|
372
|
+
loadedConfig.scanDirAbs,
|
|
373
|
+
).catch(() => []);
|
|
374
|
+
|
|
375
|
+
const fallbackCandidate = resolve(loadedConfig.configDir, "schema.ts");
|
|
376
|
+
const candidates = [
|
|
377
|
+
...prioritySchemaPaths,
|
|
378
|
+
...(scanCandidates.length ? scanCandidates : [fallbackCandidate]),
|
|
379
|
+
];
|
|
380
|
+
|
|
381
|
+
for (const schemaPath of candidates) {
|
|
382
|
+
const file = Bun.file(schemaPath);
|
|
383
|
+
if (!(await file.exists())) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const source = await file.text();
|
|
387
|
+
const tables = discoverTables(source);
|
|
388
|
+
if (tables.length > 0) {
|
|
389
|
+
return {
|
|
390
|
+
schemaPath,
|
|
391
|
+
tables,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
throw new Error(
|
|
397
|
+
`Unable to discover schema.ts under scanDir (${loadedConfig.scanDirAbs}) or fallback (${fallbackCandidate}).`,
|
|
398
|
+
);
|
|
399
|
+
}
|