appflare 0.2.48 → 0.2.49

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.
Files changed (139) hide show
  1. package/Documentation.md +898 -898
  2. package/cli/commands/index.ts +247 -247
  3. package/cli/generate.ts +360 -360
  4. package/cli/index.ts +120 -120
  5. package/cli/load-config.ts +184 -184
  6. package/cli/schema-compiler.ts +1366 -1366
  7. package/cli/templates/auth/README.md +156 -156
  8. package/cli/templates/auth/config.ts +61 -61
  9. package/cli/templates/auth/route-config.ts +1 -1
  10. package/cli/templates/auth/route-handler.ts +1 -1
  11. package/cli/templates/auth/route-request-utils.ts +5 -5
  12. package/cli/templates/auth/route.config.ts +18 -18
  13. package/cli/templates/auth/route.handler.ts +18 -18
  14. package/cli/templates/auth/route.request-utils.ts +55 -55
  15. package/cli/templates/auth/route.ts +14 -14
  16. package/cli/templates/core/README.md +266 -266
  17. package/cli/templates/core/app-creation.ts +19 -19
  18. package/cli/templates/core/client/appflare.ts +112 -112
  19. package/cli/templates/core/client/handlers/index.ts +763 -763
  20. package/cli/templates/core/client/handlers.ts +1 -1
  21. package/cli/templates/core/client/index.ts +7 -7
  22. package/cli/templates/core/client/storage.ts +195 -195
  23. package/cli/templates/core/client/types.ts +187 -187
  24. package/cli/templates/core/client-modules/appflare.ts +1 -1
  25. package/cli/templates/core/client-modules/handlers.ts +1 -1
  26. package/cli/templates/core/client-modules/index.ts +1 -1
  27. package/cli/templates/core/client-modules/storage.ts +1 -1
  28. package/cli/templates/core/client-modules/types.ts +1 -1
  29. package/cli/templates/core/client.artifacts.ts +39 -39
  30. package/cli/templates/core/client.ts +4 -4
  31. package/cli/templates/core/drizzle.ts +15 -15
  32. package/cli/templates/core/export.ts +14 -14
  33. package/cli/templates/core/handlers.route.ts +24 -24
  34. package/cli/templates/core/handlers.ts +1 -1
  35. package/cli/templates/core/imports.ts +9 -9
  36. package/cli/templates/core/server.ts +38 -38
  37. package/cli/templates/core/types.ts +6 -6
  38. package/cli/templates/core/wrangler.ts +109 -109
  39. package/cli/templates/dashboard/builders/functions/index.ts +17 -17
  40. package/cli/templates/dashboard/builders/functions/render-page/header.ts +20 -20
  41. package/cli/templates/dashboard/builders/functions/render-page/index.ts +33 -33
  42. package/cli/templates/dashboard/builders/functions/render-page/request-panel.ts +271 -271
  43. package/cli/templates/dashboard/builders/functions/render-page/result-panel.ts +85 -85
  44. package/cli/templates/dashboard/builders/functions/render-page/scripts.ts +703 -703
  45. package/cli/templates/dashboard/builders/functions/tree-builder.ts +47 -47
  46. package/cli/templates/dashboard/builders/navigation.ts +155 -155
  47. package/cli/templates/dashboard/builders/storage/index.ts +13 -13
  48. package/cli/templates/dashboard/builders/storage/routes/create-directory-route.ts +29 -29
  49. package/cli/templates/dashboard/builders/storage/routes/delete-route.ts +18 -18
  50. package/cli/templates/dashboard/builders/storage/routes/download-route.ts +23 -23
  51. package/cli/templates/dashboard/builders/storage/routes/index.ts +22 -22
  52. package/cli/templates/dashboard/builders/storage/routes/list-route.ts +25 -25
  53. package/cli/templates/dashboard/builders/storage/routes/preview-route.ts +21 -21
  54. package/cli/templates/dashboard/builders/storage/routes/upload-route.ts +21 -21
  55. package/cli/templates/dashboard/builders/storage/runtime/helpers.ts +72 -72
  56. package/cli/templates/dashboard/builders/storage/runtime/storage-page.ts +130 -130
  57. package/cli/templates/dashboard/builders/table-routes/common/drawer-panel.ts +27 -27
  58. package/cli/templates/dashboard/builders/table-routes/common/pagination.ts +30 -30
  59. package/cli/templates/dashboard/builders/table-routes/common/search-bar.ts +23 -23
  60. package/cli/templates/dashboard/builders/table-routes/fragments.ts +257 -217
  61. package/cli/templates/dashboard/builders/table-routes/helpers.ts +45 -45
  62. package/cli/templates/dashboard/builders/table-routes/index.ts +8 -8
  63. package/cli/templates/dashboard/builders/table-routes/table/actions-cell.ts +71 -71
  64. package/cli/templates/dashboard/builders/table-routes/table/get-route.ts +291 -291
  65. package/cli/templates/dashboard/builders/table-routes/table/index.ts +80 -80
  66. package/cli/templates/dashboard/builders/table-routes/table/post-routes.ts +163 -163
  67. package/cli/templates/dashboard/builders/table-routes/table-route.ts +7 -7
  68. package/cli/templates/dashboard/builders/table-routes/users/get-route.ts +69 -69
  69. package/cli/templates/dashboard/builders/table-routes/users/html/modals.ts +57 -57
  70. package/cli/templates/dashboard/builders/table-routes/users/html/page.ts +27 -27
  71. package/cli/templates/dashboard/builders/table-routes/users/html/table.ts +128 -128
  72. package/cli/templates/dashboard/builders/table-routes/users/index.ts +32 -32
  73. package/cli/templates/dashboard/builders/table-routes/users/post-routes.ts +150 -150
  74. package/cli/templates/dashboard/builders/table-routes/users/redirect.ts +14 -14
  75. package/cli/templates/dashboard/builders/table-routes/users-route.ts +10 -10
  76. package/cli/templates/dashboard/components/dashboard-home.ts +23 -23
  77. package/cli/templates/dashboard/components/layout.ts +420 -420
  78. package/cli/templates/dashboard/components/login-page.ts +65 -65
  79. package/cli/templates/dashboard/index.ts +61 -61
  80. package/cli/templates/dashboard/types.ts +9 -9
  81. package/cli/templates/handlers/README.md +353 -353
  82. package/cli/templates/handlers/auth.ts +37 -37
  83. package/cli/templates/handlers/execution.ts +44 -42
  84. package/cli/templates/handlers/generators/context/context-creation.ts +101 -101
  85. package/cli/templates/handlers/generators/context/error-helpers.ts +11 -11
  86. package/cli/templates/handlers/generators/context/scheduler.ts +24 -24
  87. package/cli/templates/handlers/generators/context/storage-api.ts +82 -82
  88. package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
  89. package/cli/templates/handlers/generators/context/types.ts +40 -40
  90. package/cli/templates/handlers/generators/context.ts +43 -43
  91. package/cli/templates/handlers/generators/execution.ts +15 -15
  92. package/cli/templates/handlers/generators/handlers.ts +14 -14
  93. package/cli/templates/handlers/generators/registration/modules/cron.ts +35 -35
  94. package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -75
  95. package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -144
  96. package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +14 -14
  97. package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +102 -102
  98. package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +164 -164
  99. package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -30
  100. package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +510 -510
  101. package/cli/templates/handlers/generators/registration/modules/scheduler.ts +65 -65
  102. package/cli/templates/handlers/generators/registration/modules/storage.ts +199 -199
  103. package/cli/templates/handlers/generators/registration/sections.ts +210 -210
  104. package/cli/templates/handlers/generators/types/context.ts +121 -121
  105. package/cli/templates/handlers/generators/types/core.ts +108 -108
  106. package/cli/templates/handlers/generators/types/operations.ts +135 -135
  107. package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +291 -291
  108. package/cli/templates/handlers/generators/types/query-definitions/query-api-types.ts +135 -135
  109. package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +1382 -1382
  110. package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -278
  111. package/cli/templates/handlers/generators/types/query-definitions.ts +13 -13
  112. package/cli/templates/handlers/generators/types/query-runtime/handled-error.ts +13 -13
  113. package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts +174 -174
  114. package/cli/templates/handlers/generators/types/query-runtime/runtime-read.ts +156 -156
  115. package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
  116. package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +958 -958
  117. package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
  118. package/cli/templates/handlers/index.ts +47 -47
  119. package/cli/templates/handlers/operations.ts +116 -116
  120. package/cli/templates/handlers/registration.ts +91 -91
  121. package/cli/templates/handlers/types.ts +17 -17
  122. package/cli/templates/handlers/utils.ts +48 -48
  123. package/cli/types.ts +110 -110
  124. package/cli/utils/handler-discovery.ts +501 -501
  125. package/cli/utils/json-utils.ts +24 -24
  126. package/cli/utils/path-utils.ts +19 -19
  127. package/cli/utils/schema-discovery.ts +402 -399
  128. package/dist/cli/index.js +77 -55
  129. package/dist/cli/index.mjs +77 -55
  130. package/index.ts +18 -18
  131. package/package.json +58 -58
  132. package/react/index.ts +5 -5
  133. package/react/use-infinite-query.ts +255 -255
  134. package/react/use-mutation.ts +89 -89
  135. package/react/use-query.ts +210 -210
  136. package/schema.ts +641 -641
  137. package/test-better-auth-hash.ts +2 -2
  138. package/tsconfig.json +6 -6
  139. package/tsup.config.ts +82 -82
@@ -1,399 +1,402 @@
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" | "json" | "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 (/mode\s*:\s*["'`]json["'`]/.test(lowered) || /\.json\s*\(/.test(lowered)) {
207
+ return "json";
208
+ }
209
+ if (/\.(text|varchar|char)\s*\(/.test(lowered)) {
210
+ return "string";
211
+ }
212
+ if (/\.(boolean|bool)\s*\(/.test(lowered)) {
213
+ return "boolean";
214
+ }
215
+ return "unknown";
216
+ }
217
+
218
+ function discoverColumns(rawColumns: string): DiscoveredColumn[] {
219
+ const entries = splitTopLevel(rawColumns);
220
+ const columns: DiscoveredColumn[] = [];
221
+
222
+ for (const entry of entries) {
223
+ const colonIndex = getTopLevelColonIndex(entry);
224
+ if (colonIndex === -1) {
225
+ continue;
226
+ }
227
+
228
+ const key = entry
229
+ .slice(0, colonIndex)
230
+ .trim()
231
+ .replace(/^['"]|['"]$/g, "");
232
+ if (!key) {
233
+ continue;
234
+ }
235
+
236
+ const expression = entry.slice(colonIndex + 1).trim();
237
+ const primaryKey = /\.primarykey\s*\(/i.test(expression);
238
+ const autoIncrement = /autoincrement\s*:\s*true/i.test(expression);
239
+ const hasDefault = /\.default\s*\(/i.test(expression);
240
+ const notNull = /\.notnull\s*\(/i.test(expression);
241
+ const optional = !notNull || hasDefault || autoIncrement || primaryKey;
242
+
243
+ columns.push({
244
+ name: key,
245
+ expression,
246
+ type: inferColumnType(expression),
247
+ optional,
248
+ primaryKey,
249
+ autoIncrement,
250
+ });
251
+ }
252
+
253
+ return columns;
254
+ }
255
+
256
+ function discoverTables(schemaSource: string): DiscoveredTable[] {
257
+ const tablePattern =
258
+ /export\s+const\s+(\w+)\s*=\s*table\s*\(\s*["'`]([^"'`]+)["'`]/g;
259
+ const tables: DiscoveredTable[] = [];
260
+
261
+ const findMatchingBrace = (source: string, startIndex: number): number => {
262
+ let depth = 0;
263
+ let inSingleQuote = false;
264
+ let inDoubleQuote = false;
265
+ let inTemplateString = false;
266
+ let escaped = false;
267
+
268
+ for (let i = startIndex; i < source.length; i += 1) {
269
+ const char = source[i];
270
+
271
+ if (escaped) {
272
+ escaped = false;
273
+ continue;
274
+ }
275
+
276
+ if (char === "\\") {
277
+ escaped = true;
278
+ continue;
279
+ }
280
+
281
+ if (!inDoubleQuote && !inTemplateString && char === "'") {
282
+ inSingleQuote = !inSingleQuote;
283
+ continue;
284
+ }
285
+
286
+ if (!inSingleQuote && !inTemplateString && char === '"') {
287
+ inDoubleQuote = !inDoubleQuote;
288
+ continue;
289
+ }
290
+
291
+ if (!inSingleQuote && !inDoubleQuote && char === "`") {
292
+ inTemplateString = !inTemplateString;
293
+ continue;
294
+ }
295
+
296
+ if (inSingleQuote || inDoubleQuote || inTemplateString) {
297
+ continue;
298
+ }
299
+
300
+ if (char === "{") {
301
+ depth += 1;
302
+ continue;
303
+ }
304
+
305
+ if (char === "}") {
306
+ depth -= 1;
307
+ if (depth === 0) {
308
+ return i;
309
+ }
310
+ }
311
+ }
312
+
313
+ return -1;
314
+ };
315
+
316
+ let match = tablePattern.exec(schemaSource);
317
+ while (match) {
318
+ const exportName = match[1];
319
+ const tableName = match[2];
320
+ const objectStart = schemaSource.indexOf("{", tablePattern.lastIndex);
321
+ if (objectStart === -1) {
322
+ match = tablePattern.exec(schemaSource);
323
+ continue;
324
+ }
325
+
326
+ const objectEnd = findMatchingBrace(schemaSource, objectStart);
327
+ if (objectEnd === -1) {
328
+ match = tablePattern.exec(schemaSource);
329
+ continue;
330
+ }
331
+
332
+ const rawColumns = schemaSource.slice(objectStart + 1, objectEnd);
333
+ tables.push({
334
+ exportName,
335
+ tableName,
336
+ columns: discoverColumns(rawColumns),
337
+ });
338
+ match = tablePattern.exec(schemaSource);
339
+ }
340
+
341
+ return tables;
342
+ }
343
+
344
+ async function findSchemaCandidates(dirPath: string): Promise<string[]> {
345
+ const entries = await readdir(dirPath, { withFileTypes: true });
346
+ const discovered: string[] = [];
347
+
348
+ for (const entry of entries) {
349
+ if (entry.name.startsWith(".")) {
350
+ continue;
351
+ }
352
+ if (entry.name === "node_modules" || entry.name === "_generated") {
353
+ continue;
354
+ }
355
+
356
+ const entryPath = resolve(dirPath, entry.name);
357
+ if (entry.isDirectory()) {
358
+ discovered.push(...(await findSchemaCandidates(entryPath)));
359
+ continue;
360
+ }
361
+
362
+ if (entry.isFile() && entry.name === "schema.ts") {
363
+ discovered.push(entryPath);
364
+ }
365
+ }
366
+
367
+ return discovered;
368
+ }
369
+
370
+ export async function discoverSchema(
371
+ loadedConfig: LoadedAppflareConfig,
372
+ prioritySchemaPaths: string[] = [],
373
+ ): Promise<DiscoveredSchema> {
374
+ const scanCandidates = await findSchemaCandidates(
375
+ loadedConfig.scanDirAbs,
376
+ ).catch(() => []);
377
+
378
+ const fallbackCandidate = resolve(loadedConfig.configDir, "schema.ts");
379
+ const candidates = [
380
+ ...prioritySchemaPaths,
381
+ ...(scanCandidates.length ? scanCandidates : [fallbackCandidate]),
382
+ ];
383
+
384
+ for (const schemaPath of candidates) {
385
+ const file = Bun.file(schemaPath);
386
+ if (!(await file.exists())) {
387
+ continue;
388
+ }
389
+ const source = await file.text();
390
+ const tables = discoverTables(source);
391
+ if (tables.length > 0) {
392
+ return {
393
+ schemaPath,
394
+ tables,
395
+ };
396
+ }
397
+ }
398
+
399
+ throw new Error(
400
+ `Unable to discover schema.ts under scanDir (${loadedConfig.scanDirAbs}) or fallback (${fallbackCandidate}).`,
401
+ );
402
+ }