appflare 0.2.36 → 0.2.38
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 +8 -8
- package/cli/commands/index.ts +1 -1
- package/cli/generate.ts +24 -0
- package/cli/index.ts +1 -1
- package/cli/schema-compiler.ts +180 -2
- package/cli/templates/core/README.md +1 -1
- package/cli/templates/handlers/generators/handlers.ts +2 -2
- package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +4 -10
- package/cli/templates/handlers/generators/types/context.ts +8 -2
- package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +6 -16
- package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +296 -17
- package/cli/templates/handlers/index.ts +2 -1
- package/cli/templates/handlers/types.ts +2 -2
- package/dist/cli/index.js +582 -301
- package/dist/cli/index.mjs +582 -301
- package/dist/index.d.mts +81 -2
- package/dist/index.d.ts +81 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/schema.ts +229 -3
package/dist/cli/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
'use strict';var commander=require('commander')
|
|
2
|
+
'use strict';var commander=require('commander'),$a=require('chokidar'),fs=require('fs'),path=require('path'),promises=require('fs/promises'),url=require('url'),f=require('typescript'),zod=require('zod');var _documentCurrentScript=typeof document!=='undefined'?document.currentScript:null;function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var $a__default=/*#__PURE__*/_interopDefault($a);var f__namespace=/*#__PURE__*/_interopNamespace(f);function be(e){return `import { betterAuth } from "better-auth";
|
|
3
3
|
import { withCloudflare } from "better-auth-cloudflare";
|
|
4
4
|
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
|
5
5
|
import { drizzle } from "drizzle-orm/d1";
|
|
@@ -57,7 +57,7 @@ export const createAuth = (
|
|
|
57
57
|
});
|
|
58
58
|
};
|
|
59
59
|
export const auth = createAuth();
|
|
60
|
-
`}function
|
|
60
|
+
`}function we(){return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
|
|
61
61
|
import type {
|
|
62
62
|
AppflareAuth,
|
|
63
63
|
AppflareAuthTokenResolver,
|
|
@@ -166,8 +166,8 @@ export function createAppflare<Options extends BetterAuthClientOptions = Inferre
|
|
|
166
166
|
): Appflare<Options> {
|
|
167
167
|
return new Appflare(options);
|
|
168
168
|
}
|
|
169
|
-
`}function
|
|
170
|
-
`)}function
|
|
169
|
+
`}function er(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t||"_route"}function xe(e){return e.split(/[^A-Za-z0-9]+/).filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}function tr(e){return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)?e:JSON.stringify(e)}function Te(e){let t={children:new Map};for(let n of e){let r=t;for(let a of n.segments){let o=r.children.get(a);o||(o={children:new Map},r.children.set(a,o)),r=o;}r.operation=n;}return t}function J(e,t=1){let n=" ".repeat(t),r=" ".repeat(t+1),a=Array.from(e.children.entries()).sort(([i],[s])=>i.localeCompare(s));if(a.length===0)return e.operation?`${e.operation.alias}Route(runtime)`:"{}";let o=["{"];for(let[i,s]of a)o.push(`${r}${tr(i)}: ${J(s,t+1)},`);return e.operation&&(o.push(`${r}run: ${e.operation.alias}Route(runtime).run,`),o.push(`${r}schema: ${e.operation.alias}Route(runtime).schema,`),e.operation.kind==="query"&&o.push(`${r}subscribe: ${e.operation.alias}Route(runtime).subscribe,`)),o.push(`${n}}`),o.join(`
|
|
170
|
+
`)}function nr(e,t){if(e.kind!=="query"&&e.kind!=="mutation")return null;let n=e.clientSegments&&e.clientSegments.length>0?e.clientSegments:e.routePath.replace(/^\//,"").split("/").filter(Boolean).slice(1);if(n.length>=2&&n[n.length-1]===n[n.length-2]&&n.pop(),n.length===0)return null;let r=er(`op_${t}_${e.kind}_${n.join("_")}`);return {kind:e.kind,routePath:e.routePath,queryName:e.handlerName??n.join("/"),segments:n,importPath:e.clientImportPath,exportName:e.exportName,alias:r,schemaConst:`${r}Schema`,typeBase:`${xe(e.kind)}${xe(n.join("_"))}`}}function rr(e){let t=`${e.typeBase}Input`,n=`${e.typeBase}Output`,r=`${e.typeBase}Schema`,a=e.kind==="query"?"GET":"POST";return e.kind==="query"?`const ${e.alias}Route = (
|
|
171
171
|
runtime: RequestRuntime,
|
|
172
172
|
): AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}> => {
|
|
173
173
|
const run: AppflareQueryRouteClient<typeof ${e.schemaConst}, ${n}>["run"] = async (
|
|
@@ -353,15 +353,15 @@ export function createAppflare<Options extends BetterAuthClientOptions = Inferre
|
|
|
353
353
|
schema: ${r},
|
|
354
354
|
run,
|
|
355
355
|
};
|
|
356
|
-
};`}function
|
|
357
|
-
`),o=t.map(
|
|
358
|
-
export type ${u} = z.input<typeof ${
|
|
359
|
-
export type ${
|
|
360
|
-
export const ${d} = ${
|
|
356
|
+
};`}function G(e){let t=e.map((l,u)=>nr(l,u)).filter(l=>l!==null),n=t.filter(l=>l.kind==="query"),r=t.filter(l=>l.kind==="mutation"),a=t.map(l=>`import { ${l.exportName} as ${l.alias} } from "${l.importPath}";`).join(`
|
|
357
|
+
`),o=t.map(l=>{let u=`${l.typeBase}Input`,p=`${l.typeBase}Output`,d=`${l.typeBase}Schema`;return `const ${l.schemaConst} = z.object(${l.alias}.definition.args);
|
|
358
|
+
export type ${u} = z.input<typeof ${l.schemaConst}>;
|
|
359
|
+
export type ${p} = Awaited<ReturnType<typeof ${l.alias}.definition.handler>>;
|
|
360
|
+
export const ${d} = ${l.schemaConst};`}).join(`
|
|
361
361
|
|
|
362
|
-
`),
|
|
362
|
+
`),i=t.map(l=>rr(l)).join(`
|
|
363
363
|
|
|
364
|
-
`),
|
|
364
|
+
`),s=J(Te(n)),c=J(Te(r));return `import { z } from "zod";
|
|
365
365
|
import type {
|
|
366
366
|
AppflareErrorMode,
|
|
367
367
|
AppflareRequestError,
|
|
@@ -703,7 +703,7 @@ async function requestRoute<TOutput>(
|
|
|
703
703
|
return body as TOutput;
|
|
704
704
|
}
|
|
705
705
|
|
|
706
|
-
${
|
|
706
|
+
${i}
|
|
707
707
|
|
|
708
708
|
export function createQueriesClient(
|
|
709
709
|
endpoint: string,
|
|
@@ -718,7 +718,7 @@ export function createQueriesClient(
|
|
|
718
718
|
options,
|
|
719
719
|
};
|
|
720
720
|
|
|
721
|
-
return ${
|
|
721
|
+
return ${s} as const;
|
|
722
722
|
}
|
|
723
723
|
|
|
724
724
|
export function createMutationsClient(
|
|
@@ -732,16 +732,16 @@ export function createMutationsClient(
|
|
|
732
732
|
options,
|
|
733
733
|
};
|
|
734
734
|
|
|
735
|
-
return ${
|
|
735
|
+
return ${c} as const;
|
|
736
736
|
}
|
|
737
737
|
|
|
738
738
|
export type QueriesClient = ReturnType<typeof createQueriesClient>;
|
|
739
739
|
export type MutationsClient = ReturnType<typeof createMutationsClient>;
|
|
740
|
-
`}function
|
|
740
|
+
`}function ve(){return `export * from "./types";
|
|
741
741
|
export * from "./appflare";
|
|
742
742
|
export * from "./storage";
|
|
743
743
|
export * from "./handlers";
|
|
744
|
-
`}function
|
|
744
|
+
`}function Re(){return `import type { StorageClient, StorageSignedUrlResponse, StorageListResponse } from "./types";
|
|
745
745
|
|
|
746
746
|
type AuthTokenResolver = (() => string | Promise<string>) | undefined;
|
|
747
747
|
|
|
@@ -933,7 +933,7 @@ export function createStorageClient(
|
|
|
933
933
|
},
|
|
934
934
|
};
|
|
935
935
|
}
|
|
936
|
-
`}function
|
|
936
|
+
`}function ke(e){return `import { createAuthClient, type BetterAuthClientOptions } from "better-auth/client";
|
|
937
937
|
import type appflareConfig from "${e}";
|
|
938
938
|
|
|
939
939
|
export type AppflareConfig = typeof appflareConfig;
|
|
@@ -1116,7 +1116,7 @@ export type RealtimeSubscriptionResponse = {
|
|
|
1116
1116
|
};
|
|
1117
1117
|
};
|
|
1118
1118
|
};
|
|
1119
|
-
`}function
|
|
1119
|
+
`}function Se(e,t){return [{relativePath:"client/index.ts",source:ve()},{relativePath:"client/types.ts",source:ke(e)},{relativePath:"client/storage.ts",source:Re()},{relativePath:"client/handlers.ts",source:G(t)},{relativePath:"client/appflare.ts",source:we()}]}function Ae(e){return `import { defineConfig } from "drizzle-kit";
|
|
1120
1120
|
|
|
1121
1121
|
export default defineConfig({
|
|
1122
1122
|
dialect: "sqlite",
|
|
@@ -1128,7 +1128,7 @@ export default defineConfig({
|
|
|
1128
1128
|
token: "token",
|
|
1129
1129
|
},
|
|
1130
1130
|
});
|
|
1131
|
-
`}function
|
|
1131
|
+
`}function Ne(){return `
|
|
1132
1132
|
|
|
1133
1133
|
import { getHeaders } from "./server";
|
|
1134
1134
|
export async function resolveSession(
|
|
@@ -1162,7 +1162,7 @@ export async function resolveSession(
|
|
|
1162
1162
|
}
|
|
1163
1163
|
}
|
|
1164
1164
|
|
|
1165
|
-
`}function
|
|
1165
|
+
`}function $e(){return `
|
|
1166
1166
|
type SchedulerQueueBinding = {
|
|
1167
1167
|
send: (body: unknown, options?: SchedulerEnqueueOptions) => Promise<void>;
|
|
1168
1168
|
};
|
|
@@ -1199,7 +1199,7 @@ type R2BucketBinding = {
|
|
|
1199
1199
|
options?: { expiresIn?: number },
|
|
1200
1200
|
) => Promise<URL>;
|
|
1201
1201
|
};
|
|
1202
|
-
`}function
|
|
1202
|
+
`}function Ce(){return `
|
|
1203
1203
|
export function createScheduler(
|
|
1204
1204
|
queue?: SchedulerQueueBinding,
|
|
1205
1205
|
): Scheduler {
|
|
@@ -1220,7 +1220,7 @@ export function createScheduler(
|
|
|
1220
1220
|
},
|
|
1221
1221
|
};
|
|
1222
1222
|
}
|
|
1223
|
-
`}function
|
|
1223
|
+
`}function qe(){return `
|
|
1224
1224
|
function createContextErrorHelpers() {
|
|
1225
1225
|
return {
|
|
1226
1226
|
error: (status: number, message: string, details?: unknown) => {
|
|
@@ -1228,7 +1228,7 @@ function createContextErrorHelpers() {
|
|
|
1228
1228
|
},
|
|
1229
1229
|
};
|
|
1230
1230
|
}
|
|
1231
|
-
`}function
|
|
1231
|
+
`}function Me(){return `
|
|
1232
1232
|
function normalizeStoragePath(path: string): string {
|
|
1233
1233
|
const trimmed = path.trim();
|
|
1234
1234
|
if (trimmed.length === 0) {
|
|
@@ -1284,7 +1284,7 @@ function buildSignedRequest(
|
|
|
1284
1284
|
headers,
|
|
1285
1285
|
});
|
|
1286
1286
|
}
|
|
1287
|
-
`}function
|
|
1287
|
+
`}function Fe(){return `
|
|
1288
1288
|
function createStorageApi(
|
|
1289
1289
|
ctx: AppflareContext,
|
|
1290
1290
|
bucket: R2BucketBinding | undefined,
|
|
@@ -1363,7 +1363,7 @@ function createStorageApi(
|
|
|
1363
1363
|
},
|
|
1364
1364
|
};
|
|
1365
1365
|
}
|
|
1366
|
-
`}function
|
|
1366
|
+
`}function Ie(e){return `
|
|
1367
1367
|
export async function createSchedulerExecutionContext(
|
|
1368
1368
|
env: Record<string, unknown>,
|
|
1369
1369
|
options: RegisterHandlersOptions,
|
|
@@ -1461,7 +1461,7 @@ export async function createExecutionContext(
|
|
|
1461
1461
|
ctx.storage = createStorageApi(ctx, storageBucket);
|
|
1462
1462
|
return ctx;
|
|
1463
1463
|
}
|
|
1464
|
-
`}function
|
|
1464
|
+
`}function Pe(e){return `import type { Context } from "hono";
|
|
1465
1465
|
import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
|
|
1466
1466
|
import { createAuth } from "./auth.config";
|
|
1467
1467
|
import {
|
|
@@ -1480,20 +1480,20 @@ import {
|
|
|
1480
1480
|
createQueryDb,
|
|
1481
1481
|
} from "./handlers";
|
|
1482
1482
|
|
|
1483
|
-
${
|
|
1483
|
+
${$e()}
|
|
1484
1484
|
|
|
1485
|
-
${
|
|
1485
|
+
${Ne()}
|
|
1486
1486
|
|
|
1487
|
-
${
|
|
1487
|
+
${Ce()}
|
|
1488
1488
|
|
|
1489
|
-
${
|
|
1489
|
+
${qe()}
|
|
1490
1490
|
|
|
1491
|
-
${
|
|
1491
|
+
${Me()}
|
|
1492
1492
|
|
|
1493
|
-
${
|
|
1493
|
+
${Fe()}
|
|
1494
1494
|
|
|
1495
|
-
${
|
|
1496
|
-
`}function
|
|
1495
|
+
${Ie(e)}
|
|
1496
|
+
`}function Ee(){return `
|
|
1497
1497
|
export async function executeOperation<
|
|
1498
1498
|
TShape extends ZodRawShape,
|
|
1499
1499
|
TResult,
|
|
@@ -1532,7 +1532,7 @@ export function handleOperationError(
|
|
|
1532
1532
|
return c.json({ message: (error as Error).message ?? "Unknown error" }, 500);
|
|
1533
1533
|
}
|
|
1534
1534
|
|
|
1535
|
-
`}function
|
|
1535
|
+
`}function Oe(){return `import type { Context } from "hono";
|
|
1536
1536
|
import { ZodError, type ZodRawShape } from "zod";
|
|
1537
1537
|
import {
|
|
1538
1538
|
type AppflareContext,
|
|
@@ -1541,8 +1541,8 @@ import {
|
|
|
1541
1541
|
type WorkerEnv,
|
|
1542
1542
|
} from "./handlers";
|
|
1543
1543
|
|
|
1544
|
-
${
|
|
1545
|
-
`}function
|
|
1544
|
+
${Ee()}
|
|
1545
|
+
`}function je(){return `import { betterAuth } from "better-auth";
|
|
1546
1546
|
import { auth } from "./auth.config";
|
|
1547
1547
|
import {
|
|
1548
1548
|
and,
|
|
@@ -1645,7 +1645,7 @@ export async function isStorageAllowed(
|
|
|
1645
1645
|
|
|
1646
1646
|
return false;
|
|
1647
1647
|
}
|
|
1648
|
-
`}function
|
|
1648
|
+
`}function De(){return `type Primitive = string | number | boolean | Date;
|
|
1649
1649
|
type NonNil<T> = Exclude<T, null | undefined>;
|
|
1650
1650
|
type Friendly<T> = T extends Date ? Date | number : T;
|
|
1651
1651
|
type Comparable<T> = Friendly<Extract<NonNil<T>, Primitive>>;
|
|
@@ -1665,16 +1665,12 @@ type GeoWithinOperand = {
|
|
|
1665
1665
|
$geometry: GeoPoint | GeoCoordinates;
|
|
1666
1666
|
latitudeField?: string;
|
|
1667
1667
|
longitudeField?: string;
|
|
1668
|
-
|
|
1669
|
-
|
|
1668
|
+
minDistance?: number;
|
|
1669
|
+
maxDistance?: number;
|
|
1670
1670
|
gt?: number;
|
|
1671
1671
|
gte?: number;
|
|
1672
1672
|
lt?: number;
|
|
1673
1673
|
lte?: number;
|
|
1674
|
-
$gt?: number;
|
|
1675
|
-
$gte?: number;
|
|
1676
|
-
$lt?: number;
|
|
1677
|
-
$lte?: number;
|
|
1678
1674
|
};
|
|
1679
1675
|
|
|
1680
1676
|
type GeoWithinOperandForField<TFieldKey extends string> = Omit<
|
|
@@ -1687,25 +1683,20 @@ type GeoWithinOperandForField<TFieldKey extends string> = Omit<
|
|
|
1687
1683
|
|
|
1688
1684
|
type FieldOperators<T, TFieldKey extends string = string> = {
|
|
1689
1685
|
eq?: Friendly<NonNil<T>>;
|
|
1690
|
-
$eq?: Friendly<NonNil<T>>;
|
|
1691
1686
|
ne?: Friendly<NonNil<T>>;
|
|
1692
|
-
$ne?: Friendly<NonNil<T>>;
|
|
1693
1687
|
in?: ReadonlyArray<Friendly<NonNil<T>>>;
|
|
1694
|
-
$in?: ReadonlyArray<Friendly<NonNil<T>>>;
|
|
1695
1688
|
nin?: ReadonlyArray<Friendly<NonNil<T>>>;
|
|
1696
|
-
$nin?: ReadonlyArray<Friendly<NonNil<T>>>;
|
|
1697
1689
|
gt?: Comparable<T>;
|
|
1698
|
-
$gt?: Comparable<T>;
|
|
1699
1690
|
gte?: Comparable<T>;
|
|
1700
|
-
$gte?: Comparable<T>;
|
|
1701
1691
|
lt?: Comparable<T>;
|
|
1702
|
-
$lt?: Comparable<T>;
|
|
1703
1692
|
lte?: Comparable<T>;
|
|
1704
|
-
$lte?: Comparable<T>;
|
|
1705
1693
|
exists?: boolean;
|
|
1706
1694
|
regex?: RegexOperand<T>;
|
|
1707
|
-
|
|
1695
|
+
options?: string;
|
|
1708
1696
|
geoWithin?: GeoWithinOperandForField<TFieldKey>;
|
|
1697
|
+
includes?: T extends ReadonlyArray<infer E> ? ReadonlyArray<E> : never;
|
|
1698
|
+
includesAny?: T extends ReadonlyArray<infer E> ? ReadonlyArray<E> : never;
|
|
1699
|
+
length?: T extends ReadonlyArray<unknown> ? number : never;
|
|
1709
1700
|
};
|
|
1710
1701
|
|
|
1711
1702
|
type WhereFieldValue<T, TFieldKey extends string> =
|
|
@@ -1718,7 +1709,6 @@ type RelationWhereInput<TModel extends Record<string, unknown>, TName extends Ta
|
|
|
1718
1709
|
| (TModel[K] extends Record<string, unknown> ? RelationWhereInput<TModel[K]> : never);
|
|
1719
1710
|
} & {
|
|
1720
1711
|
geoWithin?: GeoWithinOperandForField<Extract<keyof TModel, string>>;
|
|
1721
|
-
$geoWithin?: GeoWithinOperandForField<Extract<keyof TModel, string>>;
|
|
1722
1712
|
} & (TName extends TableName ? {
|
|
1723
1713
|
[TRelation in keyof NativeFindManyWith<TName>]?: NativeFindManyWith<TName>[TRelation] extends infer TRelationConfig
|
|
1724
1714
|
? TRelationConfig extends Record<string, unknown>
|
|
@@ -1923,7 +1913,7 @@ export type QueryDeleteArgs<TName extends TableName> = {
|
|
|
1923
1913
|
where?: WhereInput<TableModel<TName>, TName>;
|
|
1924
1914
|
limit?: number;
|
|
1925
1915
|
};
|
|
1926
|
-
`}function
|
|
1916
|
+
`}function Ve(){return `type AggregateWithInput<TName extends TableName> =
|
|
1927
1917
|
QueryWithInput<TName, NativeFindManyWith<TName>>;
|
|
1928
1918
|
|
|
1929
1919
|
type NumericFieldKey<TName extends TableName> = NumericModelFieldKey<
|
|
@@ -2055,16 +2045,290 @@ export type DbMutationEvent = {
|
|
|
2055
2045
|
export type QueryDbOptions = {
|
|
2056
2046
|
onMutation?: (event: DbMutationEvent) => void;
|
|
2057
2047
|
};
|
|
2058
|
-
`}function
|
|
2048
|
+
`}function Be(){return `function isRecord(value: unknown): value is Record<string, unknown> {
|
|
2059
2049
|
return typeof value === "object" && value !== null;
|
|
2060
2050
|
}
|
|
2061
2051
|
|
|
2062
2052
|
function readOperatorValue(record: Record<string, unknown>, key: string): unknown {
|
|
2063
|
-
|
|
2064
|
-
|
|
2053
|
+
return record[key];
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
function getJsonColumnsMap(): Record<string, Record<string, { shape: unknown }>> {
|
|
2057
|
+
const rawValue = (mergedSchema as Record<string, unknown>)[
|
|
2058
|
+
"__appflareJsonColumns"
|
|
2059
|
+
];
|
|
2060
|
+
if (!isRecord(rawValue)) {
|
|
2061
|
+
return {};
|
|
2065
2062
|
}
|
|
2066
|
-
const
|
|
2067
|
-
|
|
2063
|
+
const map: Record<string, Record<string, { shape: unknown }>> = {};
|
|
2064
|
+
for (const [tableName, tableValue] of Object.entries(rawValue)) {
|
|
2065
|
+
if (isRecord(tableValue)) {
|
|
2066
|
+
map[tableName] = tableValue as Record<string, { shape: unknown }>;
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
return map;
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
const jsonColumnsMap = getJsonColumnsMap();
|
|
2073
|
+
|
|
2074
|
+
function getJsonColumnShape(
|
|
2075
|
+
tableName: string | undefined,
|
|
2076
|
+
fieldName: string,
|
|
2077
|
+
): { kind: string } | null {
|
|
2078
|
+
if (!tableName) return null;
|
|
2079
|
+
const tableCols = jsonColumnsMap[tableName];
|
|
2080
|
+
if (!tableCols) return null;
|
|
2081
|
+
const col = tableCols[fieldName];
|
|
2082
|
+
if (!col || !isRecord(col.shape)) return null;
|
|
2083
|
+
return col.shape as { kind: string };
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
function buildJsonFieldFilter(
|
|
2087
|
+
fieldName: string,
|
|
2088
|
+
field: unknown,
|
|
2089
|
+
value: unknown,
|
|
2090
|
+
tableName: string | undefined,
|
|
2091
|
+
): SQL | undefined {
|
|
2092
|
+
const shape = getJsonColumnShape(tableName, fieldName);
|
|
2093
|
+
if (!shape) return undefined;
|
|
2094
|
+
|
|
2095
|
+
if (shape.kind === "array") {
|
|
2096
|
+
return buildJsonArrayFilter(fieldName, field, value);
|
|
2097
|
+
}
|
|
2098
|
+
if (shape.kind === "object") {
|
|
2099
|
+
return buildJsonObjectFilter(fieldName, field, value);
|
|
2100
|
+
}
|
|
2101
|
+
return undefined;
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
function buildJsonArrayElementMatchCondition(
|
|
2105
|
+
field: unknown,
|
|
2106
|
+
matchValue: unknown,
|
|
2107
|
+
): SQL {
|
|
2108
|
+
if (!isRecord(matchValue) || matchValue instanceof Date || Array.isArray(matchValue)) {
|
|
2109
|
+
return sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE j.value = \${matchValue})\`;
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
const propConditions: SQL[] = [];
|
|
2113
|
+
for (const [key, propValue] of Object.entries(matchValue)) {
|
|
2114
|
+
const jsonPath = '$.' + key;
|
|
2115
|
+
if (!isRecord(propValue) || propValue instanceof Date || Array.isArray(propValue)) {
|
|
2116
|
+
propConditions.push(
|
|
2117
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) = \${propValue})\`,
|
|
2118
|
+
);
|
|
2119
|
+
} else {
|
|
2120
|
+
const nestedConditions: SQL[] = [];
|
|
2121
|
+
const eqVal = readOperatorValue(propValue, "eq");
|
|
2122
|
+
if (eqVal !== undefined) {
|
|
2123
|
+
nestedConditions.push(
|
|
2124
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) = \${eqVal})\`,
|
|
2125
|
+
);
|
|
2126
|
+
}
|
|
2127
|
+
const neVal = readOperatorValue(propValue, "ne");
|
|
2128
|
+
if (neVal !== undefined) {
|
|
2129
|
+
nestedConditions.push(
|
|
2130
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) != \${neVal})\`,
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
const gtVal = readOperatorValue(propValue, "gt");
|
|
2134
|
+
if (gtVal !== undefined) {
|
|
2135
|
+
nestedConditions.push(
|
|
2136
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) > \${gtVal})\`,
|
|
2137
|
+
);
|
|
2138
|
+
}
|
|
2139
|
+
const gteVal = readOperatorValue(propValue, "gte");
|
|
2140
|
+
if (gteVal !== undefined) {
|
|
2141
|
+
nestedConditions.push(
|
|
2142
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) >= \${gteVal})\`,
|
|
2143
|
+
);
|
|
2144
|
+
}
|
|
2145
|
+
const ltVal = readOperatorValue(propValue, "lt");
|
|
2146
|
+
if (ltVal !== undefined) {
|
|
2147
|
+
nestedConditions.push(
|
|
2148
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) < \${ltVal})\`,
|
|
2149
|
+
);
|
|
2150
|
+
}
|
|
2151
|
+
const lteVal = readOperatorValue(propValue, "lte");
|
|
2152
|
+
if (lteVal !== undefined) {
|
|
2153
|
+
nestedConditions.push(
|
|
2154
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) <= \${lteVal})\`,
|
|
2155
|
+
);
|
|
2156
|
+
}
|
|
2157
|
+
const inVal = readOperatorValue(propValue, "in");
|
|
2158
|
+
if (Array.isArray(inVal) && inVal.length > 0) {
|
|
2159
|
+
nestedConditions.push(
|
|
2160
|
+
sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_extract(j.value, \${jsonPath}) IN (\${sql.join(inVal.map((v) => sql\`\${v}\`), sql\`, \`)})\`,
|
|
2161
|
+
);
|
|
2162
|
+
}
|
|
2163
|
+
if (nestedConditions.length > 0) {
|
|
2164
|
+
propConditions.push(
|
|
2165
|
+
nestedConditions.length === 1
|
|
2166
|
+
? nestedConditions[0]
|
|
2167
|
+
: and(...nestedConditions),
|
|
2168
|
+
);
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
if (propConditions.length === 0) {
|
|
2174
|
+
return sql\`EXISTS (SELECT 1 FROM json_each(\${field as never}) j WHERE json_type(j.value) = 'object')\`;
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
if (propConditions.length === 1) {
|
|
2178
|
+
return propConditions[0];
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
return and(...propConditions);
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
function buildJsonArrayFilter(
|
|
2185
|
+
fieldName: string,
|
|
2186
|
+
field: unknown,
|
|
2187
|
+
value: unknown,
|
|
2188
|
+
): SQL | undefined {
|
|
2189
|
+
if (!isRecord(value) || value instanceof Date || Array.isArray(value)) {
|
|
2190
|
+
return sql\`json_extract(\${field as never}, '$') = json(\${JSON.stringify(value)})\`;
|
|
2191
|
+
}
|
|
2192
|
+
|
|
2193
|
+
const filters: SQL[] = [];
|
|
2194
|
+
|
|
2195
|
+
const includesValue = readOperatorValue(value, "includes");
|
|
2196
|
+
if (Array.isArray(includesValue) && includesValue.length > 0) {
|
|
2197
|
+
const conditions = includesValue.map((v) =>
|
|
2198
|
+
buildJsonArrayElementMatchCondition(field, v),
|
|
2199
|
+
);
|
|
2200
|
+
filters.push(and(...conditions));
|
|
2201
|
+
}
|
|
2202
|
+
|
|
2203
|
+
const includesAnyValue = readOperatorValue(value, "includesAny");
|
|
2204
|
+
if (Array.isArray(includesAnyValue) && includesAnyValue.length > 0) {
|
|
2205
|
+
const conditions = includesAnyValue.map((v) =>
|
|
2206
|
+
buildJsonArrayElementMatchCondition(field, v),
|
|
2207
|
+
);
|
|
2208
|
+
filters.push(sql\`(\${sql.join(conditions, sql\` OR \`)})\`);
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
const lengthValue = readOperatorValue(value, "length");
|
|
2212
|
+
if (lengthValue !== undefined) {
|
|
2213
|
+
filters.push(sql\`json_array_length(\${field as never}) = \${lengthValue}\`);
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
const eqValue = readOperatorValue(value, "eq");
|
|
2217
|
+
if (eqValue !== undefined) {
|
|
2218
|
+
filters.push(sql\`json_extract(\${field as never}, '$') = json(\${JSON.stringify(eqValue)})\`);
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
const neValue = readOperatorValue(value, "ne");
|
|
2222
|
+
if (neValue !== undefined) {
|
|
2223
|
+
filters.push(sql\`json_extract(\${field as never}, '$') != json(\${JSON.stringify(neValue)})\`);
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
if (filters.length === 0) {
|
|
2227
|
+
return undefined;
|
|
2228
|
+
}
|
|
2229
|
+
if (filters.length === 1) return filters[0];
|
|
2230
|
+
return and(...filters);
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
function buildJsonObjectFilter(
|
|
2234
|
+
fieldName: string,
|
|
2235
|
+
field: unknown,
|
|
2236
|
+
value: unknown,
|
|
2237
|
+
): SQL | undefined {
|
|
2238
|
+
if (!isRecord(value) || value instanceof Date || Array.isArray(value)) {
|
|
2239
|
+
return sql\`json_extract(\${field as never}, '$') = json(\${JSON.stringify(value)})\`;
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
const filters: SQL[] = [];
|
|
2243
|
+
|
|
2244
|
+
for (const [key, opValue] of Object.entries(value)) {
|
|
2245
|
+
if (key === "eq") {
|
|
2246
|
+
filters.push(sql\`json_extract(\${field as never}, '$') = json(\${JSON.stringify(opValue)})\`);
|
|
2247
|
+
continue;
|
|
2248
|
+
}
|
|
2249
|
+
if (key === "ne") {
|
|
2250
|
+
filters.push(sql\`json_extract(\${field as never}, '$') != json(\${JSON.stringify(opValue)})\`);
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
if (key === "exists") {
|
|
2254
|
+
filters.push(
|
|
2255
|
+
opValue
|
|
2256
|
+
? sql\`json_extract(\${field as never}, '$') IS NOT NULL\`
|
|
2257
|
+
: sql\`json_extract(\${field as never}, '$') IS NULL\`,
|
|
2258
|
+
);
|
|
2259
|
+
continue;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
const nestedFilters = buildJsonObjectPathFilter(field, key, opValue);
|
|
2263
|
+
if (nestedFilters) {
|
|
2264
|
+
filters.push(nestedFilters);
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
if (filters.length === 0) return undefined;
|
|
2269
|
+
if (filters.length === 1) return filters[0];
|
|
2270
|
+
return and(...filters);
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
function buildJsonObjectPathFilter(
|
|
2274
|
+
field: unknown,
|
|
2275
|
+
path: string,
|
|
2276
|
+
opValue: unknown,
|
|
2277
|
+
): SQL | undefined {
|
|
2278
|
+
const jsonPath = '$.' + path;
|
|
2279
|
+
if (!isRecord(opValue) || opValue instanceof Date || Array.isArray(opValue)) {
|
|
2280
|
+
return sql\`json_extract(\${field as never}, \${jsonPath}) = \${opValue}\`;
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
const filters: SQL[] = [];
|
|
2284
|
+
|
|
2285
|
+
const eqVal = readOperatorValue(opValue, "eq");
|
|
2286
|
+
if (eqVal !== undefined) {
|
|
2287
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) = \${eqVal}\`);
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
const neVal = readOperatorValue(opValue, "ne");
|
|
2291
|
+
if (neVal !== undefined) {
|
|
2292
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) != \${neVal}\`);
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
const gtVal = readOperatorValue(opValue, "gt");
|
|
2296
|
+
if (gtVal !== undefined) {
|
|
2297
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) > \${gtVal}\`);
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
const gteVal = readOperatorValue(opValue, "gte");
|
|
2301
|
+
if (gteVal !== undefined) {
|
|
2302
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) >= \${gteVal}\`);
|
|
2303
|
+
}
|
|
2304
|
+
|
|
2305
|
+
const ltVal = readOperatorValue(opValue, "lt");
|
|
2306
|
+
if (ltVal !== undefined) {
|
|
2307
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) < \${ltVal}\`);
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
const lteVal = readOperatorValue(opValue, "lte");
|
|
2311
|
+
if (lteVal !== undefined) {
|
|
2312
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) <= \${lteVal}\`);
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
const inVal = readOperatorValue(opValue, "in");
|
|
2316
|
+
if (Array.isArray(inVal) && inVal.length > 0) {
|
|
2317
|
+
filters.push(sql\`json_extract(\${field as never}, \${jsonPath}) IN (\${sql.join(inVal.map((v) => sql\`\${v}\`), sql\`, \`)})\`);
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
const existsVal = readOperatorValue(opValue, "exists");
|
|
2321
|
+
if (typeof existsVal === "boolean") {
|
|
2322
|
+
filters.push(
|
|
2323
|
+
existsVal
|
|
2324
|
+
? sql\`json_extract(\${field as never}, \${jsonPath}) IS NOT NULL\`
|
|
2325
|
+
: sql\`json_extract(\${field as never}, \${jsonPath}) IS NULL\`,
|
|
2326
|
+
);
|
|
2327
|
+
}
|
|
2328
|
+
|
|
2329
|
+
if (filters.length === 0) return undefined;
|
|
2330
|
+
if (filters.length === 1) return filters[0];
|
|
2331
|
+
return and(...filters);
|
|
2068
2332
|
}
|
|
2069
2333
|
|
|
2070
2334
|
function normalizeGeoPoint(
|
|
@@ -2146,17 +2410,13 @@ function createGeoDistanceFilter(
|
|
|
2146
2410
|
)\`;
|
|
2147
2411
|
|
|
2148
2412
|
const minDistance =
|
|
2149
|
-
operand
|
|
2413
|
+
operand.minDistance ??
|
|
2150
2414
|
operand.gte ??
|
|
2151
|
-
operand
|
|
2152
|
-
operand.gt ??
|
|
2153
|
-
operand.$gt;
|
|
2415
|
+
operand.gt;
|
|
2154
2416
|
const maxDistance =
|
|
2155
|
-
operand
|
|
2417
|
+
operand.maxDistance ??
|
|
2156
2418
|
operand.lte ??
|
|
2157
|
-
operand
|
|
2158
|
-
operand.lt ??
|
|
2159
|
-
operand.$lt;
|
|
2419
|
+
operand.lt;
|
|
2160
2420
|
|
|
2161
2421
|
const filters: SQL[] = [];
|
|
2162
2422
|
if (typeof minDistance === "number") {
|
|
@@ -2182,7 +2442,18 @@ function buildFieldFilter(
|
|
|
2182
2442
|
field: unknown,
|
|
2183
2443
|
value: unknown,
|
|
2184
2444
|
fields: Record<string, unknown>,
|
|
2445
|
+
tableName?: string,
|
|
2185
2446
|
): SQL | undefined {
|
|
2447
|
+
const jsonFilter = buildJsonFieldFilter(fieldName, field, value, tableName);
|
|
2448
|
+
if (jsonFilter) return jsonFilter;
|
|
2449
|
+
|
|
2450
|
+
const isArrayColumn = isRecord(value) && !Array.isArray(value) && !(value instanceof Date) &&
|
|
2451
|
+
("includes" in value || "includesAny" in value || "length" in value);
|
|
2452
|
+
if (isArrayColumn) {
|
|
2453
|
+
const arrayFilter = buildJsonArrayFilter(fieldName, field, value);
|
|
2454
|
+
if (arrayFilter) return arrayFilter;
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2186
2457
|
if (!isRecord(value) || value instanceof Date || Array.isArray(value)) {
|
|
2187
2458
|
return eq(field as never, value as never);
|
|
2188
2459
|
}
|
|
@@ -2293,9 +2564,7 @@ function buildWhereFilterFromFields(
|
|
|
2293
2564
|
|
|
2294
2565
|
const topLevelGeoWithin = isRecord(where.geoWithin)
|
|
2295
2566
|
? (where.geoWithin as GeoWithinOperand)
|
|
2296
|
-
:
|
|
2297
|
-
? (where.$geoWithin as GeoWithinOperand)
|
|
2298
|
-
: undefined;
|
|
2567
|
+
: undefined;
|
|
2299
2568
|
|
|
2300
2569
|
if (topLevelGeoWithin) {
|
|
2301
2570
|
const geoFilter = createGeoDistanceFilter(topLevelGeoWithin, fields, tableName);
|
|
@@ -2305,7 +2574,7 @@ function buildWhereFilterFromFields(
|
|
|
2305
2574
|
}
|
|
2306
2575
|
|
|
2307
2576
|
for (const [fieldName, fieldValue] of Object.entries(where)) {
|
|
2308
|
-
if (fieldName === "geoWithin"
|
|
2577
|
+
if (fieldName === "geoWithin") {
|
|
2309
2578
|
continue;
|
|
2310
2579
|
}
|
|
2311
2580
|
|
|
@@ -2333,7 +2602,7 @@ function buildWhereFilterFromFields(
|
|
|
2333
2602
|
continue;
|
|
2334
2603
|
}
|
|
2335
2604
|
|
|
2336
|
-
const filter = buildFieldFilter(fieldName, field, fieldValue, fields);
|
|
2605
|
+
const filter = buildFieldFilter(fieldName, field, fieldValue, fields, tableName);
|
|
2337
2606
|
if (filter) {
|
|
2338
2607
|
filters.push(filter);
|
|
2339
2608
|
}
|
|
@@ -3155,7 +3424,7 @@ function inferConflictTarget(table: unknown): string[] {
|
|
|
3155
3424
|
|
|
3156
3425
|
return [];
|
|
3157
3426
|
}
|
|
3158
|
-
`}function
|
|
3427
|
+
`}function We(){return `const mergedSchema = {
|
|
3159
3428
|
...authSchema,
|
|
3160
3429
|
...schema,
|
|
3161
3430
|
};
|
|
@@ -3430,9 +3699,9 @@ type TableFindFirstResult<
|
|
|
3430
3699
|
type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
3431
3700
|
(typeof mergedSchema)[TName]
|
|
3432
3701
|
>;
|
|
3433
|
-
`}function
|
|
3702
|
+
`}function He(){return [We(),De(),Ve(),Be()].join(`
|
|
3434
3703
|
|
|
3435
|
-
`)}function
|
|
3704
|
+
`)}function Le(){return ` count: async (args?: QueryCountArgs<TableName>) => {
|
|
3436
3705
|
const withValue = args?.with;
|
|
3437
3706
|
const pathSegments = args?.field
|
|
3438
3707
|
? splitAggregateFieldPath(String(args.field))
|
|
@@ -3603,7 +3872,7 @@ type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
|
3603
3872
|
},
|
|
3604
3873
|
}) as AppflareQueryDb;
|
|
3605
3874
|
}
|
|
3606
|
-
`}function
|
|
3875
|
+
`}function ze(){return `export class AppflareHandledError extends Error {
|
|
3607
3876
|
public readonly status: number;
|
|
3608
3877
|
public readonly payload: unknown;
|
|
3609
3878
|
|
|
@@ -3613,7 +3882,7 @@ type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
|
3613
3882
|
this.payload = payload;
|
|
3614
3883
|
}
|
|
3615
3884
|
}
|
|
3616
|
-
`}function
|
|
3885
|
+
`}function Ue(){return ` findMany: (args?: Record<string, unknown>) => {
|
|
3617
3886
|
const where = isRecord(args?.where)
|
|
3618
3887
|
? (args?.where as Record<string, unknown>)
|
|
3619
3888
|
: undefined;
|
|
@@ -3767,7 +4036,7 @@ type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
|
3767
4036
|
);
|
|
3768
4037
|
});
|
|
3769
4038
|
},
|
|
3770
|
-
`}function
|
|
4039
|
+
`}function Qe(){return `export function createQueryDb(
|
|
3771
4040
|
$db: AppflareDb,
|
|
3772
4041
|
options?: QueryDbOptions,
|
|
3773
4042
|
): AppflareQueryDb {
|
|
@@ -3809,7 +4078,7 @@ type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
|
3809
4078
|
};
|
|
3810
4079
|
|
|
3811
4080
|
const tableApi = {
|
|
3812
|
-
`}function
|
|
4081
|
+
`}function _e(){return ` insert: async <TArgs extends QueryInsertArgs<TableName>>(args: TArgs) => {
|
|
3813
4082
|
const transaction = ($db as any).transaction;
|
|
3814
4083
|
|
|
3815
4084
|
const valuesArray = Array.isArray(args.values)
|
|
@@ -4503,11 +4772,12 @@ type TableInsertModel<TName extends TableName> = InferInsertModel<
|
|
|
4503
4772
|
);
|
|
4504
4773
|
return rows;
|
|
4505
4774
|
},
|
|
4506
|
-
`}function
|
|
4775
|
+
`}function Ke(){return [Qe(),Ue(),_e(),Le(),ze()].join(`
|
|
4507
4776
|
|
|
4508
|
-
`)}function
|
|
4777
|
+
`)}function Je(e=[]){return `type AuthSession = typeof auth.$Infer.Session;
|
|
4509
4778
|
type AuthAdapter = Awaited<typeof auth.$context>["internalAdapter"];
|
|
4510
|
-
type
|
|
4779
|
+
${e.length>0?`type UserRole = ${e.map(n=>`"${n}"`).join(" | ")};
|
|
4780
|
+
type User = Omit<AuthSession['user'], 'role'> & { role: UserRole };`:"type User = AuthSession['user']"}
|
|
4511
4781
|
type Session = AuthSession['session']
|
|
4512
4782
|
|
|
4513
4783
|
export type StoragePutArgs = {
|
|
@@ -4594,7 +4864,7 @@ export type AppflareContext = {
|
|
|
4594
4864
|
storage: AppflareStorage;
|
|
4595
4865
|
error: (status: number, message: string, details?: unknown) => never;
|
|
4596
4866
|
};
|
|
4597
|
-
`}function
|
|
4867
|
+
`}function Ge(){return `type InferOperationArgs<TShape extends ZodRawShape> = z.output<z.ZodObject<TShape>>;
|
|
4598
4868
|
|
|
4599
4869
|
export type SchedulerEnqueueOptions = {
|
|
4600
4870
|
delaySeconds?: number;
|
|
@@ -4726,20 +4996,20 @@ export function cron(definition: CronDefinition): RegisteredCron {
|
|
|
4726
4996
|
definition,
|
|
4727
4997
|
};
|
|
4728
4998
|
}
|
|
4729
|
-
`}function
|
|
4999
|
+
`}function Ze(e=[]){return [je(),He(),Ke(),Je(e),Ge()].join(`
|
|
4730
5000
|
|
|
4731
|
-
`)}function
|
|
5001
|
+
`)}function Xe(e,t=[]){return `import type { Context } from "hono";
|
|
4732
5002
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
4733
5003
|
import { drizzle } from "drizzle-orm/d1";
|
|
4734
5004
|
import { z, type ZodRawShape } from "zod";
|
|
4735
5005
|
import * as authSchema from "./auth.schema";
|
|
4736
5006
|
import * as schema from "${e}";
|
|
4737
5007
|
|
|
4738
|
-
${
|
|
4739
|
-
`}function
|
|
4740
|
-
`)}function
|
|
4741
|
-
`)}function
|
|
4742
|
-
`)}function
|
|
5008
|
+
${Ze(t)}
|
|
5009
|
+
`}function ar(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_");return /^[0-9]/.test(t)?`_${t}`:t}function or(e,t){let n=e.routePath.replace(/^\//,"").replace(/\//g,"_");return ar(`op_${t}_${n}`)}function ir(e){return e.map((t,n)=>({operation:t,index:n,alias:or(t,n)}))}function sr(e){return e.map(({operation:t,alias:n})=>`import { ${t.exportName} as ${n} } from "${t.importPath}";`).join(`
|
|
5010
|
+
`)}function lr(e){return e.filter(({operation:t})=>t.kind==="query"||t.kind==="mutation").map(({alias:t})=>`const ${`${t}Schema`} = z.object(${t}.definition.args);`).join(`
|
|
5011
|
+
`)}function ur(e){return e.filter(({operation:t})=>t.kind==="scheduler").map(({alias:t})=>`const ${`${t}SchedulerSchema`} = ${t}.definition.args ? z.object(${t}.definition.args) : z.undefined();`).join(`
|
|
5012
|
+
`)}function cr(e){return e.filter(({operation:t})=>t.kind==="query").map(({operation:t,alias:n})=>{let r=`${n}Schema`;return `
|
|
4743
5013
|
app.get(
|
|
4744
5014
|
"${t.routePath}",
|
|
4745
5015
|
sValidator("query", ${r}),
|
|
@@ -4752,7 +5022,7 @@ ${Ue()}
|
|
|
4752
5022
|
}
|
|
4753
5023
|
},
|
|
4754
5024
|
);`}).join(`
|
|
4755
|
-
`)}function
|
|
5025
|
+
`)}function dr(e){return e.filter(({operation:t})=>t.kind==="mutation").map(({operation:t,alias:n})=>{let r=`${n}Schema`;return `
|
|
4756
5026
|
app.post(
|
|
4757
5027
|
"${t.routePath}",
|
|
4758
5028
|
sValidator("json", ${r}),
|
|
@@ -4767,26 +5037,26 @@ ${Ue()}
|
|
|
4767
5037
|
}
|
|
4768
5038
|
},
|
|
4769
5039
|
);`}).join(`
|
|
4770
|
-
`)}function
|
|
5040
|
+
`)}function pr(e){return e.filter(({operation:t})=>t.kind==="query").map(({operation:t,alias:n})=>{let r=`${n}Schema`,a=t.handlerName??t.routePath;return `
|
|
4771
5041
|
${JSON.stringify(a)}: {
|
|
4772
5042
|
definition: ${n}.definition,
|
|
4773
5043
|
schema: ${r},
|
|
4774
5044
|
},`}).join(`
|
|
4775
|
-
`)}function
|
|
5045
|
+
`)}function mr(e){return e.filter(({operation:t})=>t.kind==="scheduler").map(({operation:t,alias:n})=>{let r=`${n}SchedulerSchema`,a=t.taskName??`${t.routePath}`;return `
|
|
4776
5046
|
${JSON.stringify(a)}: {
|
|
4777
5047
|
definition: ${n}.definition,
|
|
4778
5048
|
schema: ${r},
|
|
4779
5049
|
},`}).join(`
|
|
4780
|
-
`)}function
|
|
4781
|
-
`)}function
|
|
5050
|
+
`)}function gr(e){return e.filter(({operation:t})=>t.kind==="scheduler").map(({operation:t,alias:n})=>{let r=t.taskName??`${t.routePath}`;return ` ${JSON.stringify(r)}: Parameters<typeof ${n}.definition.handler>[1];`}).join(`
|
|
5051
|
+
`)}function fr(e){return e.filter(({operation:t})=>t.kind==="cron").map(({operation:t,alias:n})=>{let r=t.taskName??`${t.routePath}`,a=t.cronTriggers??[];return `
|
|
4782
5052
|
{
|
|
4783
5053
|
taskName: ${JSON.stringify(r)},
|
|
4784
5054
|
cronTriggers: ${JSON.stringify(a)},
|
|
4785
5055
|
definition: ${n}.definition,
|
|
4786
5056
|
},`}).join(`
|
|
4787
|
-
`)}function
|
|
5057
|
+
`)}function hr(e){return e.filter(({operation:t})=>t.kind==="storage").map(({alias:t})=>`
|
|
4788
5058
|
${t}.definition.handler,`).join(`
|
|
4789
|
-
`)}function
|
|
5059
|
+
`)}function Ye(e){let t=ir(e);return {imports:sr(t),operationSchemas:lr(t),schedulerSchemas:ur(t),queryRoutes:cr(t),mutationRoutes:dr(t),queryRegistryEntries:pr(t),schedulerEntries:mr(t),schedulerPayloadMapEntries:gr(t),cronEntries:fr(t),storageHandlersEntries:hr(t)}}var et=`
|
|
4790
5060
|
function getRealtimeStub(
|
|
4791
5061
|
env: Record<string, unknown>,
|
|
4792
5062
|
options: RegisterHandlersOptions,
|
|
@@ -4860,7 +5130,7 @@ function buildRealtimeWsUrl(requestUrl: string, websocketPath: string): string {
|
|
|
4860
5130
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
4861
5131
|
return url.toString();
|
|
4862
5132
|
}
|
|
4863
|
-
`;var
|
|
5133
|
+
`;var tt=`
|
|
4864
5134
|
export class AppflareRealtimeDurableObject {
|
|
4865
5135
|
private readonly subscriptions = new Map<string, RealtimeSubscription>();
|
|
4866
5136
|
private readonly sockets = new Map<string, WebSocket>();
|
|
@@ -5003,7 +5273,7 @@ export class AppflareRealtimeDurableObject {
|
|
|
5003
5273
|
return new Response("Not found", { status: 404 });
|
|
5004
5274
|
}
|
|
5005
5275
|
}
|
|
5006
|
-
`;var
|
|
5276
|
+
`;var nt=`
|
|
5007
5277
|
async function publishMutationEvents(
|
|
5008
5278
|
c: { req: { raw: Request }; env: Record<string, unknown> },
|
|
5009
5279
|
options: RegisterHandlersOptions,
|
|
@@ -5104,7 +5374,7 @@ async function publishMutationEvents(
|
|
|
5104
5374
|
}
|
|
5105
5375
|
}
|
|
5106
5376
|
}
|
|
5107
|
-
`;var
|
|
5377
|
+
`;var rt=`
|
|
5108
5378
|
function registerRealtimeRoutes(
|
|
5109
5379
|
app: Hono<WorkerEnv>,
|
|
5110
5380
|
options: RegisterHandlersOptions,
|
|
@@ -5267,7 +5537,7 @@ function registerRealtimeRoutes(
|
|
|
5267
5537
|
return stub.fetch(c.req.raw);
|
|
5268
5538
|
});
|
|
5269
5539
|
}
|
|
5270
|
-
`;var
|
|
5540
|
+
`;var at=`
|
|
5271
5541
|
type RealtimeSubscription = {
|
|
5272
5542
|
token: string;
|
|
5273
5543
|
signature: string;
|
|
@@ -5296,7 +5566,7 @@ type RealtimeDurableObjectNamespace = {
|
|
|
5296
5566
|
type RealtimeQueryName = keyof typeof realtimeQueryHandlers extends never
|
|
5297
5567
|
? string
|
|
5298
5568
|
: Extract<keyof typeof realtimeQueryHandlers, string>;
|
|
5299
|
-
`;var
|
|
5569
|
+
`;var ot=`
|
|
5300
5570
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
5301
5571
|
return typeof value === "object" && value !== null;
|
|
5302
5572
|
}
|
|
@@ -5486,26 +5756,20 @@ function compareOrderValues(
|
|
|
5486
5756
|
function hasKnownOperator(condition: Record<string, unknown>): boolean {
|
|
5487
5757
|
return [
|
|
5488
5758
|
"eq",
|
|
5489
|
-
"$eq",
|
|
5490
5759
|
"ne",
|
|
5491
|
-
"$ne",
|
|
5492
5760
|
"in",
|
|
5493
|
-
"$in",
|
|
5494
5761
|
"nin",
|
|
5495
|
-
"$nin",
|
|
5496
5762
|
"gt",
|
|
5497
|
-
"$gt",
|
|
5498
5763
|
"gte",
|
|
5499
|
-
"$gte",
|
|
5500
5764
|
"lt",
|
|
5501
|
-
"$lt",
|
|
5502
5765
|
"lte",
|
|
5503
|
-
"$lte",
|
|
5504
5766
|
"exists",
|
|
5505
5767
|
"regex",
|
|
5506
|
-
"
|
|
5768
|
+
"options",
|
|
5507
5769
|
"geoWithin",
|
|
5508
|
-
"
|
|
5770
|
+
"includes",
|
|
5771
|
+
"includesAny",
|
|
5772
|
+
"length",
|
|
5509
5773
|
].some((key) => key in condition);
|
|
5510
5774
|
}
|
|
5511
5775
|
|
|
@@ -5811,9 +6075,9 @@ function doesSubscriptionMatchMutation(
|
|
|
5811
6075
|
|
|
5812
6076
|
return false;
|
|
5813
6077
|
}
|
|
5814
|
-
`;var
|
|
6078
|
+
`;var it=[at,ot,et,nt,rt,tt].join(`
|
|
5815
6079
|
|
|
5816
|
-
`);var
|
|
6080
|
+
`);var st=`
|
|
5817
6081
|
function parseExpiresIn(value: string | undefined): number | undefined {
|
|
5818
6082
|
if (!value) {
|
|
5819
6083
|
return undefined;
|
|
@@ -6011,7 +6275,7 @@ export function registerGeneratedStorageRoutes(
|
|
|
6011
6275
|
}
|
|
6012
6276
|
});
|
|
6013
6277
|
}
|
|
6014
|
-
`;var
|
|
6278
|
+
`;var lt=`
|
|
6015
6279
|
type SchedulerTaskName = keyof typeof schedulerHandlers extends never
|
|
6016
6280
|
? string
|
|
6017
6281
|
: keyof typeof schedulerHandlers;
|
|
@@ -6066,7 +6330,7 @@ export async function executeScheduledBatch(
|
|
|
6066
6330
|
}
|
|
6067
6331
|
}
|
|
6068
6332
|
}
|
|
6069
|
-
`;var
|
|
6333
|
+
`;var ut=`
|
|
6070
6334
|
export async function executeCronTriggers(
|
|
6071
6335
|
controller: { cron: string },
|
|
6072
6336
|
env: Record<string, unknown>,
|
|
@@ -6091,7 +6355,7 @@ export async function executeCronTriggers(
|
|
|
6091
6355
|
}
|
|
6092
6356
|
}
|
|
6093
6357
|
}
|
|
6094
|
-
`;function
|
|
6358
|
+
`;function ct(e){let{imports:t,operationSchemas:n,schedulerSchemas:r,queryRoutes:a,mutationRoutes:o,queryRegistryEntries:i,schedulerEntries:s,schedulerPayloadMapEntries:c,cronEntries:l,storageHandlersEntries:u}=Ye(e);return `import { sValidator } from "@hono/standard-validator";
|
|
6095
6359
|
import type { Hono } from "hono";
|
|
6096
6360
|
import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
|
|
6097
6361
|
import { ZodError, z } from "zod";
|
|
@@ -6114,16 +6378,16 @@ ${t}`:""}
|
|
|
6114
6378
|
${n}
|
|
6115
6379
|
${r}
|
|
6116
6380
|
|
|
6117
|
-
const realtimeQueryHandlers = {${
|
|
6381
|
+
const realtimeQueryHandlers = {${i||`
|
|
6118
6382
|
`}
|
|
6119
6383
|
} as const;
|
|
6120
6384
|
|
|
6121
|
-
const schedulerHandlers = {${
|
|
6385
|
+
const schedulerHandlers = {${s||`
|
|
6122
6386
|
`}
|
|
6123
6387
|
} as const;
|
|
6124
6388
|
|
|
6125
|
-
type GeneratedSchedulerPayloadMap = {${
|
|
6126
|
-
${
|
|
6389
|
+
type GeneratedSchedulerPayloadMap = {${c?`
|
|
6390
|
+
${c}
|
|
6127
6391
|
`:""}};
|
|
6128
6392
|
|
|
6129
6393
|
declare global {
|
|
@@ -6138,7 +6402,7 @@ type CronHandlerEntry = {
|
|
|
6138
6402
|
};
|
|
6139
6403
|
};
|
|
6140
6404
|
|
|
6141
|
-
const cronHandlers: readonly CronHandlerEntry[] = [${
|
|
6405
|
+
const cronHandlers: readonly CronHandlerEntry[] = [${l||`
|
|
6142
6406
|
`}
|
|
6143
6407
|
];
|
|
6144
6408
|
|
|
@@ -6148,9 +6412,9 @@ const storageHandlers = [${u||`
|
|
|
6148
6412
|
|
|
6149
6413
|
setStorageHandlers([...storageHandlers]);
|
|
6150
6414
|
|
|
6151
|
-
${
|
|
6415
|
+
${lt}
|
|
6152
6416
|
|
|
6153
|
-
${
|
|
6417
|
+
${it}
|
|
6154
6418
|
|
|
6155
6419
|
export function registerGeneratedHandlers(
|
|
6156
6420
|
app: Hono<WorkerEnv>,
|
|
@@ -6163,20 +6427,20 @@ export function registerGeneratedHandlers(
|
|
|
6163
6427
|
`}
|
|
6164
6428
|
}
|
|
6165
6429
|
|
|
6166
|
-
${
|
|
6430
|
+
${st}
|
|
6167
6431
|
|
|
6168
|
-
${
|
|
6169
|
-
`}function
|
|
6170
|
-
KV: c.env["${e}"] as KVNamespace`:""}function
|
|
6171
|
-
DATABASE: c.env["${e}"] as D1Database${
|
|
6172
|
-
}`}function
|
|
6432
|
+
${ut}
|
|
6433
|
+
`}function Z(e,t,n,r=[]){let a=Xe(e,r),o=Pe(n),i=Oe(),s=ct(t);return [{relativePath:"handlers.ts",source:a},{relativePath:"handlers.context.ts",source:o},{relativePath:"handlers.execution.ts",source:i},{relativePath:"handlers.routes.ts",source:s}]}function yr(e){return e?`,
|
|
6434
|
+
KV: c.env["${e}"] as KVNamespace`:""}function dt(e,t){return `{
|
|
6435
|
+
DATABASE: c.env["${e}"] as D1Database${yr(t)}
|
|
6436
|
+
}`}function pt(e,t,n){return `app.on(["GET", "POST"], "${e}/*", async (c) => {
|
|
6173
6437
|
const auth = createAuth(
|
|
6174
|
-
${
|
|
6438
|
+
${dt(t,n)},
|
|
6175
6439
|
c.req.raw.cf as IncomingRequestCfProperties | undefined,
|
|
6176
6440
|
);
|
|
6177
6441
|
return auth.handler(getSanitizedRequest(c.req.raw));
|
|
6178
6442
|
});
|
|
6179
|
-
`}function
|
|
6443
|
+
`}function br(){return `export const getHeaders = (headers: Headers) => {
|
|
6180
6444
|
const newHeaders = Object.fromEntries(headers as any);
|
|
6181
6445
|
const headerObject: Record<string, any> = {};
|
|
6182
6446
|
let hasCookie = false;
|
|
@@ -6206,15 +6470,15 @@ ${rt}
|
|
|
6206
6470
|
|
|
6207
6471
|
return headerObject as any as Headers;
|
|
6208
6472
|
};
|
|
6209
|
-
`}function
|
|
6473
|
+
`}function wr(){return `export const getSanitizedRequest = (req: Request) => {
|
|
6210
6474
|
const newRequest = new Request(req, {
|
|
6211
6475
|
headers: getHeaders(req.headers),
|
|
6212
6476
|
});
|
|
6213
6477
|
return newRequest;
|
|
6214
6478
|
};
|
|
6215
|
-
`}function
|
|
6216
|
-
`+
|
|
6217
|
-
`+
|
|
6479
|
+
`}function mt(){return br()+`
|
|
6480
|
+
`+wr()}function gt(e,t,n){return pt(e,t,n)+`
|
|
6481
|
+
`+mt()}function ft(){return `const app = new Hono<WorkerEnv>();
|
|
6218
6482
|
|
|
6219
6483
|
app.use('*', cors({
|
|
6220
6484
|
origin: (origin, c) => {
|
|
@@ -6230,7 +6494,7 @@ app.use('*', cors({
|
|
|
6230
6494
|
},
|
|
6231
6495
|
credentials: true
|
|
6232
6496
|
}));
|
|
6233
|
-
`}function
|
|
6497
|
+
`}function ht(){return `export { AppflareRealtimeDurableObject };
|
|
6234
6498
|
|
|
6235
6499
|
export default {
|
|
6236
6500
|
fetch: app.fetch,
|
|
@@ -6241,30 +6505,30 @@ export default {
|
|
|
6241
6505
|
await executeCronTriggers(controller, env, generatedHandlerOptions);
|
|
6242
6506
|
},
|
|
6243
6507
|
};
|
|
6244
|
-
`}function
|
|
6508
|
+
`}function yt(e,t,n="APPFLARE_SCHEDULER_QUEUE",r,a="APPFLARE_REALTIME",o="global",i="/realtime/subscribe",s="/realtime/ws",c="appflare.realtime.v1"){let l=t?`
|
|
6245
6509
|
kvBinding: "${t}",`:"",u=r?`
|
|
6246
|
-
r2Binding: "${r}",`:"",
|
|
6510
|
+
r2Binding: "${r}",`:"",p=`
|
|
6247
6511
|
realtimeBinding: "${a}",
|
|
6248
6512
|
realtimeObjectName: "${o}",
|
|
6249
|
-
realtimeSubscribePath: "${
|
|
6250
|
-
realtimeWebsocketPath: "${
|
|
6251
|
-
realtimeProtocol: "${
|
|
6252
|
-
databaseBinding: "${e}",${
|
|
6253
|
-
schedulerBinding: "${n}",${u}${
|
|
6513
|
+
realtimeSubscribePath: "${i}",
|
|
6514
|
+
realtimeWebsocketPath: "${s}",
|
|
6515
|
+
realtimeProtocol: "${c}",`;return `const generatedHandlerOptions = {
|
|
6516
|
+
databaseBinding: "${e}",${l}
|
|
6517
|
+
schedulerBinding: "${n}",${u}${p}
|
|
6254
6518
|
};
|
|
6255
6519
|
registerGeneratedHandlers(app, generatedHandlerOptions);
|
|
6256
6520
|
registerGeneratedStorageRoutes(app, generatedHandlerOptions);
|
|
6257
6521
|
registerAdminDashboard(app, generatedHandlerOptions);
|
|
6258
|
-
`}function
|
|
6522
|
+
`}function bt(){return `import { createAuth } from "./auth.config";
|
|
6259
6523
|
import { AppflareRealtimeDurableObject, executeCronTriggers, executeScheduledBatch, registerGeneratedHandlers, registerGeneratedStorageRoutes } from "./handlers.routes";
|
|
6260
6524
|
import { registerAdminDashboard } from "./admin.routes";
|
|
6261
6525
|
import { Hono } from "hono";
|
|
6262
6526
|
import { cors } from "hono/cors";
|
|
6263
6527
|
import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@cloudflare/workers-types";
|
|
6264
|
-
`}function
|
|
6528
|
+
`}function wt(){return `type WorkerEnv = {
|
|
6265
6529
|
Bindings: Record<string, unknown>;
|
|
6266
6530
|
};
|
|
6267
|
-
`}function
|
|
6531
|
+
`}function xt(e,t,n,r,a,o,i,s,c,l){return bt()+wt()+ft()+yt(t,n,r,a,o,i,s,c,l)+gt(e,t,n)+ht()}function Tt(e){return !!e&&typeof e=="object"&&!Array.isArray(e)}function X(e,t){let n={...e};for(let[r,a]of Object.entries(t)){let o=n[r];if(Tt(o)&&Tt(a)){n[r]=X(o,a);continue}n[r]=a;}return n}function xr(e){return Array.from(new Set(e.filter(t=>t.length>0)))}function vt(e,t){let n=t.filter(d=>d.kind==="scheduler"||d.kind==="cron"),r=xr(t.filter(d=>d.kind==="cron").flatMap(d=>d.cronTriggers??[])),a=e.config.scheduler.enabled&&n.length>0,o=e.config.realtime.enabled,s=(typeof e.config.wranglerOverrides?.name=="string"?e.config.wranglerOverrides.name:void 0)??"appflare-worker",c=e.config.scheduler.queue??`${s}-scheduler`,l={name:s,main:"./src/index.ts",d1_databases:e.config.database.map(d=>({binding:d.binding,database_name:d.databaseName,database_id:d.databaseId,preview_database_id:d.previewDatabaseId??d.databaseId,...d.migrationsDir?{migrations_dir:d.migrationsDir}:{}})),kv_namespaces:e.config.kv.map(d=>({binding:d.binding,id:d.id,...d.previewId?{preview_id:d.previewId}:{}})),r2_buckets:e.config.r2.map(d=>({binding:d.binding,bucket_name:d.bucketName,...d.previewBucketName?{preview_bucket_name:d.previewBucketName}:{},...d.jurisdiction?{jurisdiction:d.jurisdiction}:{}})),...a?{queues:{producers:[{binding:e.config.scheduler.binding,queue:c}],consumers:[{queue:c}]}}:{},...r.length>0?{triggers:{crons:r}}:{},...o?{durable_objects:{bindings:[{name:e.config.realtime.binding,class_name:e.config.realtime.className}]},migrations:[{tag:"appflare-realtime-v1",new_sqlite_classes:[e.config.realtime.className]}]}:{}};if(!e.config.wranglerOverrides)return l;let{scheduler:u,...p}=e.config.wranglerOverrides;return X(l,p)}function Rt(e){return e.tables.map(t=>({exportName:t.exportName,tableName:t.tableName,columns:t.columns.map(n=>n.name)}))}function kt(e){return `<li data-name="users">
|
|
6268
6532
|
<a href="/admin/users" hx-get="/admin/users" hx-target="#main-content" hx-push-url="true" hx-swap="outerHTML" class="sidebar-link flex items-center gap-2 px-3 py-2 text-sm rounded-lg w-full">
|
|
6269
6533
|
<iconify-icon icon="mdi:account-group" width="16" height="16" class="opacity-50 shrink-0"></iconify-icon>
|
|
6270
6534
|
<span class="truncate">users</span>
|
|
@@ -6276,7 +6540,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6276
6540
|
<span class="truncate">${n.tableName}</span>
|
|
6277
6541
|
</a>
|
|
6278
6542
|
</li>`).join(`
|
|
6279
|
-
`)}`}function
|
|
6543
|
+
`)}`}function St(e){let t=e.filter(o=>o.kind==="query"),n=e.filter(o=>o.kind==="mutation"),r=t.map(o=>`
|
|
6280
6544
|
<li data-name="${o.exportName}">
|
|
6281
6545
|
<a href="/admin/functions${o.routePath}" hx-get="/admin/functions${o.routePath}" hx-target="#main-content" hx-push-url="true" hx-swap="outerHTML" class="sidebar-link flex items-center gap-2 px-3 py-2 text-sm rounded-lg w-full">
|
|
6282
6546
|
<iconify-icon icon="solar:reorder-linear" width="16" height="16" class="opacity-50 shrink-0"></iconify-icon>
|
|
@@ -6310,7 +6574,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6310
6574
|
<ul class="flex flex-col gap-0.5">${a}</ul>`:""}
|
|
6311
6575
|
</nav>
|
|
6312
6576
|
</div>
|
|
6313
|
-
`}function
|
|
6577
|
+
`}function At(e){return e.map(t=>`
|
|
6314
6578
|
<a
|
|
6315
6579
|
href="/admin/table/${t.exportName}"
|
|
6316
6580
|
class="card bg-base-100 border border-base-200 hover:border-primary/30 hover:shadow-md transition-all cursor-pointer group"
|
|
@@ -6327,9 +6591,9 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6327
6591
|
</div>
|
|
6328
6592
|
</div>
|
|
6329
6593
|
</a>
|
|
6330
|
-
`.replace(/\n/g,"\\n")).join("")}function
|
|
6594
|
+
`.replace(/\n/g,"\\n")).join("")}function Nt(e){return e.columns.filter(t=>t.type==="string").map(t=>`
|
|
6331
6595
|
try { searchConditions.push(like(tableSchema.${t.name}, \`%\${search}%\`)); } catch (e) {}
|
|
6332
|
-
`).join("")}function
|
|
6596
|
+
`).join("")}function Tr(e){switch(e){case "number":return "mdi:pound";case "boolean":return "mdi:toggle-switch-outline";case "date":return "mdi:calendar";default:return "mdi:format-text"}}function $t(e,t){return t.map(n=>{let r=e.columns.find(o=>o.name===n),a=r?Tr(r.type):"mdi:format-text";return `
|
|
6333
6597
|
<th>
|
|
6334
6598
|
<a href="#"
|
|
6335
6599
|
hx-get="/admin/table/${e.exportName}?page=\${page}&search=\${search}&sort=${n}&order=\${sort === '${n}' && order === 'asc' ? 'desc' : 'asc'}"
|
|
@@ -6341,7 +6605,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6341
6605
|
<span class="text-[10px] opacity-30">\${sort === '${n}' ? (order === 'asc' ? '\u25B2' : '\u25BC') : ''}</span>
|
|
6342
6606
|
</a>
|
|
6343
6607
|
</th>
|
|
6344
|
-
`}).join("")}function
|
|
6608
|
+
`}).join("")}function Ct(e,t){return e.map(n=>t&&n===t?`<td><button type="button" class="truncate max-w-[200px] text-sm font-mono text-xs opacity-70 hover:opacity-100 cursor-copy text-left" title="Click to copy: \${String((row as any).${n} ?? '')}" data-copy-value="\${String((row as any).${n} ?? '')}" onclick="navigator.clipboard?.writeText(this.dataset.copyValue || '')">\${String((row as any).${n} ?? '')}</button></td>`:`<td><div class="truncate max-w-[200px] text-sm" title="\${String((row as any).${n} ?? '')}">\${String((row as any).${n} ?? '')}</div></td>`).join("")}function Y(e,t,n){let r=e.columns.find(i=>i.name===t);if(!r)return "";let a=r.optional?"":" required",o=r.type==="number"?"number":r.type==="date"?"date":"text";if(r.type==="boolean")return n==="edit"?`
|
|
6345
6609
|
<div class="form-control">
|
|
6346
6610
|
<label class="label cursor-pointer justify-start gap-3">
|
|
6347
6611
|
<input type="checkbox" name="${t}" value="true" class="checkbox checkbox-sm checkbox-primary" \${(row as any).${t} ? 'checked' : ''} />
|
|
@@ -6355,7 +6619,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6355
6619
|
<span class="label-text text-sm">${t}</span>
|
|
6356
6620
|
</label>
|
|
6357
6621
|
</div>
|
|
6358
|
-
`;if(n==="edit"){let
|
|
6622
|
+
`;if(n==="edit"){let i=r.type==="date"?`\${(() => {
|
|
6359
6623
|
const value = (row as any).${t};
|
|
6360
6624
|
if (value == null || value === '') return '';
|
|
6361
6625
|
|
|
@@ -6372,14 +6636,14 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6372
6636
|
})()}`:`\${String((row as any).${t} ?? '')}`;return `
|
|
6373
6637
|
<div class="form-control">
|
|
6374
6638
|
<label class="label"><span class="label-text text-sm font-medium">${t}</span></label>
|
|
6375
|
-
<input type="${o}" name="${t}" class="input input-bordered w-full text-sm" value="${
|
|
6639
|
+
<input type="${o}" name="${t}" class="input input-bordered w-full text-sm" value="${i}"${a} />
|
|
6376
6640
|
</div>
|
|
6377
6641
|
`}return `
|
|
6378
6642
|
<div class="form-control">
|
|
6379
6643
|
<label class="label"><span class="label-text text-sm font-medium">${t}</span></label>
|
|
6380
6644
|
<input type="${o}" name="${t}" class="input input-bordered w-full text-sm"${a} />
|
|
6381
6645
|
</div>
|
|
6382
|
-
`}function
|
|
6646
|
+
`}function ee(e,t){return t.map(n=>{let r=e.columns.find(o=>o.name===n);if(!r)return "";let a=r.optional?"":`
|
|
6383
6647
|
if (raw_${n} === '') {
|
|
6384
6648
|
return c.text('${n} is required', 400);
|
|
6385
6649
|
}
|
|
@@ -6424,7 +6688,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6424
6688
|
payload.${n} = raw_${n};
|
|
6425
6689
|
}
|
|
6426
6690
|
`}).join(`
|
|
6427
|
-
`)}function
|
|
6691
|
+
`)}function qt(e){return e.columns.find(t=>t.primaryKey)?.name||e.columns[0]?.name||""}function Mt(e,t){let n=e.columns.find(r=>r.name===t);return !(!n||n.autoIncrement||n.primaryKey)}function Ft(e,t){let n=e.columns.find(r=>r.name===t);return !(!n||n.primaryKey||n.autoIncrement)}function It(e,t,n,r){return t?`<td class="text-right">
|
|
6428
6692
|
<div class="drawer drawer-end">
|
|
6429
6693
|
<input id="edit-drawer-${e.exportName}-\${rowIndex}" type="checkbox" class="drawer-toggle" />
|
|
6430
6694
|
<div class="drawer-content">
|
|
@@ -6477,7 +6741,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6477
6741
|
</div>
|
|
6478
6742
|
</div>
|
|
6479
6743
|
</div>
|
|
6480
|
-
</td>`:'<td class="text-right"><span class="text-xs opacity-30">No primary key</span></td>'}function
|
|
6744
|
+
</td>`:'<td class="text-right"><span class="text-xs opacity-30">No primary key</span></td>'}function B(e){return `
|
|
6481
6745
|
<div class="flex flex-col sm:flex-row justify-between items-center mt-4 gap-3 py-3 px-1">
|
|
6482
6746
|
<div class="text-xs text-base-content/40">
|
|
6483
6747
|
Total found: <span class="font-medium text-base-content/60">\${total}</span>
|
|
@@ -6499,7 +6763,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6499
6763
|
\` : html\`<button class="join-item btn btn-sm btn-ghost btn-disabled"><iconify-icon icon="mdi:chevron-right" width="16" height="16"></iconify-icon></button>\`}
|
|
6500
6764
|
</div>
|
|
6501
6765
|
\` : ''}
|
|
6502
|
-
</div>`}function
|
|
6766
|
+
</div>`}function W(e,t="Search term or filter..."){return `
|
|
6503
6767
|
<div class="form-control w-full md:w-auto relative">
|
|
6504
6768
|
<iconify-icon icon="mdi:magnify" width="18" height="18" class="absolute left-3 top-1/2 -translate-y-1/2 opacity-40"></iconify-icon>
|
|
6505
6769
|
<input type="text"
|
|
@@ -6510,7 +6774,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6510
6774
|
hx-trigger="keyup changed delay:500ms, search"
|
|
6511
6775
|
hx-target="#main-content"
|
|
6512
6776
|
class="input input-sm md:input-md input-bordered pl-9 w-full md:w-72 bg-base-200/50 border-base-200 focus:bg-base-100 focus:border-primary transition-all text-sm" />
|
|
6513
|
-
</div>`}function
|
|
6777
|
+
</div>`}function Pt(e,t,n,r,a,o,i,s,c,l){let u=B(`/admin/table/${e.exportName}`),p=W(`/admin/table/${e.exportName}`,"Search term or filter..."),d=r?`<th class="w-10"><input id="select-all-${e.exportName}" type="checkbox" class="checkbox checkbox-xs" /></th>`:'<th class="w-10"><input type="checkbox" class="checkbox checkbox-xs opacity-30" disabled /></th>',y=r?`<td><input type="checkbox" class="checkbox checkbox-xs row-select-checkbox" value="\${String((row as any).${n} ?? '')}" /></td>`:'<td><input type="checkbox" class="checkbox checkbox-xs opacity-30" disabled /></td>',v=r?`
|
|
6514
6778
|
<div id="bulk-delete-bar-${e.exportName}" class="fixed bottom-4 left-1/2 -translate-x-1/2 z-40 hidden">
|
|
6515
6779
|
<div class="bg-base-100 border border-base-200 rounded-xl shadow-lg px-3 py-2 flex items-center gap-3">
|
|
6516
6780
|
<div class="text-xs text-base-content/70">
|
|
@@ -6669,7 +6933,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6669
6933
|
<thead>
|
|
6670
6934
|
<tr class="border-b border-base-200">
|
|
6671
6935
|
${d}
|
|
6672
|
-
${
|
|
6936
|
+
${i}
|
|
6673
6937
|
<th class="w-[100px] text-right">
|
|
6674
6938
|
<iconify-icon icon="mdi:dots-horizontal" width="16" height="16" class="opacity-30"></iconify-icon>
|
|
6675
6939
|
</th>
|
|
@@ -6678,9 +6942,9 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6678
6942
|
<tbody>
|
|
6679
6943
|
\${data.map((row, rowIndex) => html\`
|
|
6680
6944
|
<tr class="hover:bg-base-200/30 transition-colors">
|
|
6681
|
-
${
|
|
6682
|
-
${
|
|
6683
|
-
${
|
|
6945
|
+
${y}
|
|
6946
|
+
${s}
|
|
6947
|
+
${c}
|
|
6684
6948
|
</tr>
|
|
6685
6949
|
\`)}
|
|
6686
6950
|
\${data.length === 0 ? html\`
|
|
@@ -6726,10 +6990,10 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6726
6990
|
</div>
|
|
6727
6991
|
</div>
|
|
6728
6992
|
<div class="mb-4">
|
|
6729
|
-
${
|
|
6993
|
+
${p}
|
|
6730
6994
|
</div>
|
|
6731
6995
|
\${tableHtml}
|
|
6732
|
-
${
|
|
6996
|
+
${v}
|
|
6733
6997
|
</div>
|
|
6734
6998
|
<div class="drawer-side z-50">
|
|
6735
6999
|
<label for="create-drawer-${e.exportName}" aria-label="close sidebar" class="drawer-overlay"></label>
|
|
@@ -6745,7 +7009,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6745
7009
|
<input type="hidden" name="order" value="\${order}" />
|
|
6746
7010
|
<input type="hidden" name="search" value="\${search}" />
|
|
6747
7011
|
<input type="hidden" name="page" value="\${page}" />
|
|
6748
|
-
${
|
|
7012
|
+
${l}
|
|
6749
7013
|
<button type="submit" class="btn btn-primary btn-sm mt-2">Create record</button>
|
|
6750
7014
|
</form>
|
|
6751
7015
|
</div>
|
|
@@ -6762,7 +7026,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6762
7026
|
title: "${e.tableName} - Admin Dashboard",
|
|
6763
7027
|
children: content
|
|
6764
7028
|
}));
|
|
6765
|
-
});`}function
|
|
7029
|
+
});`}function Et(e,t,n,r,a,o,i,s){let c=r==="number"?`
|
|
6766
7030
|
const parsedId = Number(rawId);
|
|
6767
7031
|
if (Number.isNaN(parsedId)) return c.text('${n} must be a valid number', 400);
|
|
6768
7032
|
idValue = parsedId;
|
|
@@ -6779,10 +7043,10 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6779
7043
|
|
|
6780
7044
|
const payload: Record<string, unknown> = {};
|
|
6781
7045
|
|
|
6782
|
-
${
|
|
7046
|
+
${s}
|
|
6783
7047
|
|
|
6784
7048
|
let idValue: unknown = rawId;
|
|
6785
|
-
${
|
|
7049
|
+
${c}
|
|
6786
7050
|
|
|
6787
7051
|
if (Object.keys(payload).length > 0) {
|
|
6788
7052
|
await db
|
|
@@ -6812,7 +7076,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6812
7076
|
if (rawId === '') return c.text('${n} is required', 400);
|
|
6813
7077
|
|
|
6814
7078
|
let idValue: unknown = rawId;
|
|
6815
|
-
${
|
|
7079
|
+
${c}
|
|
6816
7080
|
|
|
6817
7081
|
await db
|
|
6818
7082
|
.delete(tableSchema)
|
|
@@ -6882,7 +7146,7 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6882
7146
|
const getValue = (value: unknown) => (typeof value === 'string' ? value : '');
|
|
6883
7147
|
const payload: Record<string, unknown> = {};
|
|
6884
7148
|
|
|
6885
|
-
${
|
|
7149
|
+
${i}
|
|
6886
7150
|
|
|
6887
7151
|
await db.insert(tableSchema).values(payload as any).execute();
|
|
6888
7152
|
|
|
@@ -6895,15 +7159,15 @@ import type { D1Database, IncomingRequestCfProperties, KVNamespace } from "@clou
|
|
|
6895
7159
|
return c.redirect('/admin/table/${e}?' + query.toString());
|
|
6896
7160
|
});
|
|
6897
7161
|
${u}
|
|
6898
|
-
`}function
|
|
6899
|
-
`+
|
|
7162
|
+
`}function te(e){let t=qt(e),n=!!t,r=e.columns.map(w=>w.name),a=r.filter(w=>Mt(e,w)),o=r.filter(w=>Ft(e,w)),i=Nt(e),s=$t(e,r),c=Ct(r,t),l=a.map(w=>Y(e,w,"create")).join(""),u=o.map(w=>Y(e,w,"edit")).join(""),p=ee(e,a),d=ee(e,o),y=n?t:r[0]||"id",v=e.columns.find(w=>w.name===t)?.type,A=It(e,n,t,u);return Pt(e,y,t,n,r,i,s,c,A,l)+`
|
|
7163
|
+
`+Et(e.exportName,y,t,v,n,i,p,d)}function Ot(){return `
|
|
6900
7164
|
const buildUsersRedirect = (params: { page?: string; sort?: string; order?: string; search?: string }) => {
|
|
6901
7165
|
const page = params.page && params.page.trim() ? params.page : '1';
|
|
6902
7166
|
const sort = params.sort && params.sort.trim() ? params.sort : 'createdAt';
|
|
6903
7167
|
const order = params.order === 'asc' ? 'asc' : 'desc';
|
|
6904
7168
|
const search = params.search ? params.search : '';
|
|
6905
7169
|
return '/admin/users?page=' + encodeURIComponent(page) + '&sort=' + encodeURIComponent(sort) + '&order=' + encodeURIComponent(order) + '&search=' + encodeURIComponent(search);
|
|
6906
|
-
};`}function
|
|
7170
|
+
};`}function jt(){return `
|
|
6907
7171
|
\${(row as any).id === currentUserId ? '' : html\`
|
|
6908
7172
|
<input type="checkbox" id="ban-user-modal-\${String((row as any).id)}" class="modal-toggle" />
|
|
6909
7173
|
<div class="modal">
|
|
@@ -6924,7 +7188,7 @@ ${u}
|
|
|
6924
7188
|
</div>
|
|
6925
7189
|
<label class="modal-backdrop" for="ban-user-modal-\${String((row as any).id)}">Close</label>
|
|
6926
7190
|
</div>
|
|
6927
|
-
\`}`}function
|
|
7191
|
+
\`}`}function Dt(){return `
|
|
6928
7192
|
\${(row as any).id === currentUserId ? '' : html\`
|
|
6929
7193
|
<input type="checkbox" id="delete-user-modal-\${String((row as any).id)}" class="modal-toggle" />
|
|
6930
7194
|
<div class="modal">
|
|
@@ -6945,8 +7209,8 @@ ${u}
|
|
|
6945
7209
|
</div>
|
|
6946
7210
|
<label class="modal-backdrop" for="delete-user-modal-\${String((row as any).id)}">Close</label>
|
|
6947
7211
|
</div>
|
|
6948
|
-
\`}`}var
|
|
6949
|
-
`),t=
|
|
7212
|
+
\`}`}var vr=["id","name","email","role","createdAt","banned"],Rr={id:"mdi:pound",name:"mdi:format-text",email:"mdi:at",role:"mdi:shield-account-outline",createdAt:"mdi:calendar",banned:"mdi:toggle-switch-outline"};function kr(e){let t=Rr[e]||"mdi:format-text";return `<th><a href="#" hx-get="/admin/users?page=\${page}&search=\${search}&sort=${e}&order=\${sort === '${e}' && order === 'asc' ? 'desc' : 'asc'}" hx-target="#main-content" hx-push-url="true" class="hover:text-primary flex items-center gap-1.5 transition-colors whitespace-nowrap"><iconify-icon icon="${t}" width="14" height="14" class="opacity-40"></iconify-icon>${e} <span class="text-[10px] opacity-30">\${sort === '${e}' ? (order === 'asc' ? '\u25B2' : '\u25BC') : ''}</span></a></th>`}function Vt(){let e=vr.map(kr).join(`
|
|
7213
|
+
`),t=B("/admin/users");return `
|
|
6950
7214
|
const tableHtml = html\`
|
|
6951
7215
|
<div class="bg-base-100 rounded-xl border border-base-200 overflow-hidden">
|
|
6952
7216
|
<div class="overflow-x-auto">
|
|
@@ -7014,8 +7278,8 @@ ${u}
|
|
|
7014
7278
|
</div>
|
|
7015
7279
|
</div>
|
|
7016
7280
|
</div>
|
|
7017
|
-
${
|
|
7018
|
-
${
|
|
7281
|
+
${jt()}
|
|
7282
|
+
${Dt()}
|
|
7019
7283
|
</td>
|
|
7020
7284
|
</tr>
|
|
7021
7285
|
\`)}
|
|
@@ -7031,7 +7295,7 @@ ${It()}
|
|
|
7031
7295
|
${t}
|
|
7032
7296
|
</div>
|
|
7033
7297
|
\`;
|
|
7034
|
-
`}function
|
|
7298
|
+
`}function Bt(){return `
|
|
7035
7299
|
const content = html\`
|
|
7036
7300
|
<div id="main-content">
|
|
7037
7301
|
<div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-5 gap-3">
|
|
@@ -7043,11 +7307,11 @@ ${It()}
|
|
|
7043
7307
|
<iconify-icon icon="mdi:refresh" width="14" height="14"></iconify-icon>
|
|
7044
7308
|
</button>
|
|
7045
7309
|
</div>
|
|
7046
|
-
${
|
|
7310
|
+
${W("/admin/users","Search users...")}
|
|
7047
7311
|
</div>
|
|
7048
7312
|
\${tableHtml}
|
|
7049
7313
|
</div>
|
|
7050
|
-
\`;`}function
|
|
7314
|
+
\`;`}function Wt(){let e=Vt(),t=Bt();return `
|
|
7051
7315
|
adminApp.get('/users', async (c) => {
|
|
7052
7316
|
const db = drizzle(c.env[options.databaseBinding]);
|
|
7053
7317
|
const auth = createAuth({ DATABASE: c.env[options.databaseBinding] } as any, c.req.raw.cf as any);
|
|
@@ -7103,7 +7367,7 @@ ${It()}
|
|
|
7103
7367
|
title: "users - Admin Dashboard",
|
|
7104
7368
|
children: content,
|
|
7105
7369
|
}));
|
|
7106
|
-
});`}function
|
|
7370
|
+
});`}function Ht(){return `
|
|
7107
7371
|
adminApp.post('/users/edit', async (c) => {
|
|
7108
7372
|
const session = await requireAdminSession(c);
|
|
7109
7373
|
if (!session) {
|
|
@@ -7246,8 +7510,8 @@ ${It()}
|
|
|
7246
7510
|
const resolvedPage = String(Math.min(nextPageCandidate, totalPages));
|
|
7247
7511
|
|
|
7248
7512
|
return c.redirect(buildUsersRedirect({ page: resolvedPage, search, sort, order }));
|
|
7249
|
-
});`}function
|
|
7250
|
-
${
|
|
7513
|
+
});`}function ne(){return `
|
|
7514
|
+
${Ot()}
|
|
7251
7515
|
|
|
7252
7516
|
const requireAdminSession = async (c: any) => {
|
|
7253
7517
|
const auth = createAuth({ DATABASE: c.env[options.databaseBinding] } as any, c.req.raw.cf as any);
|
|
@@ -7258,12 +7522,12 @@ ${It()}
|
|
|
7258
7522
|
return session;
|
|
7259
7523
|
};
|
|
7260
7524
|
|
|
7261
|
-
${
|
|
7525
|
+
${Wt()}
|
|
7262
7526
|
|
|
7263
|
-
${
|
|
7264
|
-
`}function
|
|
7527
|
+
${Ht()}
|
|
7528
|
+
`}function Lt(e){return `${e.tables.map(t=>te(t)).join(`
|
|
7265
7529
|
`)}
|
|
7266
|
-
${
|
|
7530
|
+
${ne()}`}function zt(e){return `
|
|
7267
7531
|
<div class="flex items-center justify-between">
|
|
7268
7532
|
<div class="flex items-center gap-3">
|
|
7269
7533
|
<div class="w-10 h-10 rounded-xl bg-primary/10 flex items-center justify-center">
|
|
@@ -7278,9 +7542,9 @@ ${Z()}`}function jt(e){return `
|
|
|
7278
7542
|
<span class="badge badge-sm badge-ghost font-mono opacity-50 px-2 py-3">/api${e.routePath}</span>
|
|
7279
7543
|
</div>
|
|
7280
7544
|
</div>
|
|
7281
|
-
`}function
|
|
7545
|
+
`}function Sr(e){return e==="boolean"?"checkbox":e==="number"?"number":"text"}function Ar(e){let t=e.args??[];return t.length===0?`
|
|
7282
7546
|
<div class="text-[11px] opacity-30 italic py-2">No arguments defined for this ${e.kind}.</div>
|
|
7283
|
-
`:t.map(n=>{let r=
|
|
7547
|
+
`:t.map(n=>{let r=Sr(n.type),a=r==="checkbox",o=`${n.name}${n.optional?"":" *"}`,i=n.type!=="unknown"?`<span class="badge badge-xs badge-ghost font-mono opacity-40 ml-1">${n.type}</span>`:"";return a?`
|
|
7284
7548
|
<div class="flex items-center gap-3 py-1">
|
|
7285
7549
|
<input
|
|
7286
7550
|
type="checkbox"
|
|
@@ -7289,13 +7553,13 @@ ${Z()}`}function jt(e){return `
|
|
|
7289
7553
|
class="checkbox checkbox-sm checkbox-primary"
|
|
7290
7554
|
${n.defaultValue==="true"?"checked":""}
|
|
7291
7555
|
/>
|
|
7292
|
-
<span class="text-sm font-mono opacity-70">${n.name}${
|
|
7556
|
+
<span class="text-sm font-mono opacity-70">${n.name}${i}</span>
|
|
7293
7557
|
${n.optional?'<span class="text-[10px] opacity-30 italic ml-auto">optional</span>':""}
|
|
7294
7558
|
</div>
|
|
7295
7559
|
`:`
|
|
7296
7560
|
<div class="form-control">
|
|
7297
7561
|
<label class="label py-0.5">
|
|
7298
|
-
<span class="label-text text-[11px] font-mono font-semibold">${o}${
|
|
7562
|
+
<span class="label-text text-[11px] font-mono font-semibold">${o}${i}</span>
|
|
7299
7563
|
${n.optional?'<span class="label-text-alt text-[10px] opacity-30 italic">optional</span>':""}
|
|
7300
7564
|
</label>
|
|
7301
7565
|
<input
|
|
@@ -7309,7 +7573,7 @@ ${Z()}`}function jt(e){return `
|
|
|
7309
7573
|
/>
|
|
7310
7574
|
</div>
|
|
7311
7575
|
`}).join(`
|
|
7312
|
-
`)}function
|
|
7576
|
+
`)}function Nr(e){return `
|
|
7313
7577
|
<div class="space-y-4">
|
|
7314
7578
|
<div class="flex items-center justify-between">
|
|
7315
7579
|
<div class="flex flex-col">
|
|
@@ -7322,11 +7586,11 @@ ${Z()}`}function jt(e){return `
|
|
|
7322
7586
|
</label>
|
|
7323
7587
|
</div>
|
|
7324
7588
|
<div id="args-rows" class="flex flex-col gap-3">
|
|
7325
|
-
${
|
|
7589
|
+
${Ar(e)}
|
|
7326
7590
|
</div>
|
|
7327
7591
|
<p class="text-[11px] opacity-30 mt-2 italic">Values are sent as ${e.kind==="query"?"query string params":"JSON request body"}.</p>
|
|
7328
7592
|
</div>
|
|
7329
|
-
`}function
|
|
7593
|
+
`}function $r(){return `
|
|
7330
7594
|
<div class="space-y-4">
|
|
7331
7595
|
<label class="text-[11px] font-bold uppercase tracking-wider opacity-40 block">Bearer Token <span class="font-normal normal-case">(optional)</span></label>
|
|
7332
7596
|
<div class="relative group">
|
|
@@ -7343,7 +7607,7 @@ ${Z()}`}function jt(e){return `
|
|
|
7343
7607
|
</div>
|
|
7344
7608
|
<p class="text-[10px] opacity-30 italic">Token will be included in the Authorization header.</p>
|
|
7345
7609
|
</div>
|
|
7346
|
-
`}function
|
|
7610
|
+
`}function Cr(){return `
|
|
7347
7611
|
<div class="space-y-4">
|
|
7348
7612
|
<div class="flex items-center justify-between">
|
|
7349
7613
|
<span class="text-[11px] font-bold uppercase tracking-wider opacity-40">Custom Headers</span>
|
|
@@ -7357,7 +7621,7 @@ ${Z()}`}function jt(e){return `
|
|
|
7357
7621
|
</div>
|
|
7358
7622
|
<p id="headers-error" class="text-[11px] text-error mt-1.5 hidden"></p>
|
|
7359
7623
|
</div>
|
|
7360
|
-
`}function
|
|
7624
|
+
`}function Ut(e){return `
|
|
7361
7625
|
<div class="card bg-base-100 border border-base-200 shadow-sm overflow-hidden flex flex-col h-full">
|
|
7362
7626
|
<div class="px-5 py-3 border-b border-base-200 bg-base-200/20 flex items-center justify-between flex-none">
|
|
7363
7627
|
<h3 class="text-xs font-bold uppercase tracking-widest opacity-40">Request</h3>
|
|
@@ -7378,13 +7642,13 @@ ${Z()}`}function jt(e){return `
|
|
|
7378
7642
|
<!-- Tab Content -->
|
|
7379
7643
|
<div class="flex-1 overflow-y-auto">
|
|
7380
7644
|
<div id="request-tab-args" class="p-5">
|
|
7381
|
-
${
|
|
7645
|
+
${Nr(e)}
|
|
7382
7646
|
</div>
|
|
7383
7647
|
<div id="request-tab-auth" class="p-5 hidden">
|
|
7384
|
-
${
|
|
7648
|
+
${$r()}
|
|
7385
7649
|
</div>
|
|
7386
7650
|
<div id="request-tab-headers" class="p-5 hidden">
|
|
7387
|
-
${
|
|
7651
|
+
${Cr()}
|
|
7388
7652
|
</div>
|
|
7389
7653
|
</div>
|
|
7390
7654
|
|
|
@@ -7402,7 +7666,7 @@ ${Z()}`}function jt(e){return `
|
|
|
7402
7666
|
</form>
|
|
7403
7667
|
</div>
|
|
7404
7668
|
</div>
|
|
7405
|
-
`}function
|
|
7669
|
+
`}function Qt(){return `
|
|
7406
7670
|
<div class="card bg-base-100 border border-base-200 shadow-sm overflow-hidden flex flex-col">
|
|
7407
7671
|
<!-- Panel Header -->
|
|
7408
7672
|
<div class="px-5 py-3 border-b border-base-200 bg-base-200/20 flex items-center justify-between">
|
|
@@ -7484,7 +7748,7 @@ ${Z()}`}function jt(e){return `
|
|
|
7484
7748
|
|
|
7485
7749
|
</div>
|
|
7486
7750
|
</div>
|
|
7487
|
-
`}function
|
|
7751
|
+
`}function _t(e){let t=e.kind==="query",n=e.routePath,r=e.handlerName??e.routePath;return `
|
|
7488
7752
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
|
|
7489
7753
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
|
|
7490
7754
|
|
|
@@ -8026,21 +8290,21 @@ ${Z()}`}function jt(e){return `
|
|
|
8026
8290
|
_rtEnabled = false;
|
|
8027
8291
|
});
|
|
8028
8292
|
</script>
|
|
8029
|
-
`}function
|
|
8293
|
+
`}function Kt(e){return `
|
|
8030
8294
|
const content = html\`
|
|
8031
8295
|
<div class="flex flex-col gap-6 max-w-5xl mx-auto" id="main-content">
|
|
8032
|
-
${
|
|
8296
|
+
${zt(e)}
|
|
8033
8297
|
|
|
8034
8298
|
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
|
8035
8299
|
<!-- Request Panel -->
|
|
8036
|
-
${
|
|
8300
|
+
${Ut(e)}
|
|
8037
8301
|
|
|
8038
8302
|
<!-- Result Panel -->
|
|
8039
|
-
${
|
|
8303
|
+
${Qt()}
|
|
8040
8304
|
</div>
|
|
8041
8305
|
</div>
|
|
8042
8306
|
|
|
8043
|
-
${
|
|
8307
|
+
${_t(e)}
|
|
8044
8308
|
\`;
|
|
8045
8309
|
|
|
8046
8310
|
if (c.req.header('hx-request')) {
|
|
@@ -8050,11 +8314,11 @@ ${Z()}`}function jt(e){return `
|
|
|
8050
8314
|
return c.html(Layout({
|
|
8051
8315
|
title: "${e.exportName} - Functions",
|
|
8052
8316
|
children: content
|
|
8053
|
-
}));`}function
|
|
8317
|
+
}));`}function Jt(e){return e.map(n=>n.kind!=="query"&&n.kind!=="mutation"?"":`
|
|
8054
8318
|
adminApp.get('/functions${n.routePath}', (c) => {
|
|
8055
|
-
${
|
|
8319
|
+
${Kt(n)}
|
|
8056
8320
|
});`).join(`
|
|
8057
|
-
`)}function
|
|
8321
|
+
`)}function Gt(){return `
|
|
8058
8322
|
const getStorageBucket = (c: any): R2Bucket | null => {
|
|
8059
8323
|
const r2Binding = (options as any).r2Binding;
|
|
8060
8324
|
if (!r2Binding || !c.env[r2Binding]) return null;
|
|
@@ -8123,7 +8387,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8123
8387
|
const parts = prefix.split('/').filter(Boolean);
|
|
8124
8388
|
return parts.slice(0, -1).join('/') + (parts.length > 1 ? '/' : '');
|
|
8125
8389
|
};
|
|
8126
|
-
`}function
|
|
8390
|
+
`}function Zt(){return `
|
|
8127
8391
|
const buildStorageListingContent = (listed: any, prefix: string) => {
|
|
8128
8392
|
const parts = prefix.split('/').filter(Boolean);
|
|
8129
8393
|
const breadcrumbs: any[] = [];
|
|
@@ -8250,7 +8514,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8250
8514
|
</div>
|
|
8251
8515
|
\`;
|
|
8252
8516
|
};
|
|
8253
|
-
`}function
|
|
8517
|
+
`}function Xt(){return `
|
|
8254
8518
|
const handleStorageListRoute = async (c: any) => {
|
|
8255
8519
|
const bucket = getStorageBucket(c);
|
|
8256
8520
|
if (!bucket) {
|
|
@@ -8272,7 +8536,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8272
8536
|
|
|
8273
8537
|
adminApp.get('/storage', handleStorageListRoute);
|
|
8274
8538
|
adminApp.get('/storage/*', handleStorageListRoute);
|
|
8275
|
-
`}function
|
|
8539
|
+
`}function Yt(){return `
|
|
8276
8540
|
adminApp.post('/storage/upload', async (c) => {
|
|
8277
8541
|
const bucket = getStorageBucket(c);
|
|
8278
8542
|
if (!bucket) return c.text("Storage not configured", 400);
|
|
@@ -8290,7 +8554,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8290
8554
|
|
|
8291
8555
|
return c.redirect(prefixToStoragePath(prefix));
|
|
8292
8556
|
});
|
|
8293
|
-
`}function
|
|
8557
|
+
`}function en(){return `
|
|
8294
8558
|
adminApp.delete('/storage/delete', async (c) => {
|
|
8295
8559
|
const bucket = getStorageBucket(c);
|
|
8296
8560
|
if (!bucket) return c.text("Storage not configured", 400);
|
|
@@ -8305,7 +8569,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8305
8569
|
c.header('HX-Redirect', prefixToStoragePath(prefix));
|
|
8306
8570
|
return c.html('');
|
|
8307
8571
|
});
|
|
8308
|
-
`}function
|
|
8572
|
+
`}function tn(){return `
|
|
8309
8573
|
adminApp.post('/storage/directory', async (c) => {
|
|
8310
8574
|
const bucket = getStorageBucket(c);
|
|
8311
8575
|
if (!bucket) return c.text("Storage not configured", 400);
|
|
@@ -8331,7 +8595,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8331
8595
|
|
|
8332
8596
|
return c.redirect(prefixToStoragePath(prefix));
|
|
8333
8597
|
});
|
|
8334
|
-
`}function
|
|
8598
|
+
`}function nn(){return `
|
|
8335
8599
|
adminApp.get('/storage/download', async (c) => {
|
|
8336
8600
|
const bucket = getStorageBucket(c);
|
|
8337
8601
|
if (!bucket) return c.text("Storage not configured", 400);
|
|
@@ -8351,7 +8615,7 @@ ${Z()}`}function jt(e){return `
|
|
|
8351
8615
|
|
|
8352
8616
|
return new Response(object.body, { headers });
|
|
8353
8617
|
});
|
|
8354
|
-
`}function
|
|
8618
|
+
`}function rn(){return `
|
|
8355
8619
|
adminApp.get('/storage/preview', async (c) => {
|
|
8356
8620
|
const bucket = getStorageBucket(c);
|
|
8357
8621
|
if (!bucket) return c.text("Storage not configured", 400);
|
|
@@ -8369,25 +8633,25 @@ ${Z()}`}function jt(e){return `
|
|
|
8369
8633
|
|
|
8370
8634
|
return new Response(object.body, { headers });
|
|
8371
8635
|
});
|
|
8372
|
-
`}function
|
|
8373
|
-
${
|
|
8636
|
+
`}function an(){return `
|
|
8637
|
+
${nn()}
|
|
8374
8638
|
|
|
8375
|
-
${
|
|
8639
|
+
${rn()}
|
|
8640
|
+
|
|
8641
|
+
${Yt()}
|
|
8376
8642
|
|
|
8377
|
-
${
|
|
8643
|
+
${en()}
|
|
8378
8644
|
|
|
8379
|
-
${
|
|
8645
|
+
${tn()}
|
|
8380
8646
|
|
|
8647
|
+
${Xt()}
|
|
8648
|
+
`}function on(){return `
|
|
8381
8649
|
${Gt()}
|
|
8382
8650
|
|
|
8383
|
-
${
|
|
8384
|
-
`}function Xt(){return `
|
|
8385
|
-
${Lt()}
|
|
8386
|
-
|
|
8387
|
-
${Ut()}
|
|
8651
|
+
${Zt()}
|
|
8388
8652
|
|
|
8389
|
-
${
|
|
8390
|
-
`}function
|
|
8653
|
+
${an()}
|
|
8654
|
+
`}function sn(e,t){let n=St(t);return `
|
|
8391
8655
|
function Layout(props: { children: any; title: string; hideSidebar?: boolean }) {
|
|
8392
8656
|
return html\`<!DOCTYPE html>
|
|
8393
8657
|
<html lang="en" data-theme="light">
|
|
@@ -8762,7 +9026,7 @@ function Layout(props: { children: any; title: string; hideSidebar?: boolean })
|
|
|
8762
9026
|
</body>
|
|
8763
9027
|
</html>\`;
|
|
8764
9028
|
}
|
|
8765
|
-
`}function
|
|
9029
|
+
`}function ln(){return `
|
|
8766
9030
|
// Auth Middleware
|
|
8767
9031
|
adminApp.use('*', async (c, next) => {
|
|
8768
9032
|
const auth = createAuth({ DATABASE: c.env[options.databaseBinding] } as any, c.req.raw.cf as any);
|
|
@@ -8821,7 +9085,7 @@ function Layout(props: { children: any; title: string; hideSidebar?: boolean })
|
|
|
8821
9085
|
}
|
|
8822
9086
|
await next();
|
|
8823
9087
|
});
|
|
8824
|
-
`}function
|
|
9088
|
+
`}function un(e){return `
|
|
8825
9089
|
adminApp.get('/', (c) => {
|
|
8826
9090
|
return c.html(Layout({
|
|
8827
9091
|
title: "Admin Dashboard",
|
|
@@ -8838,7 +9102,7 @@ function Layout(props: { children: any; title: string; hideSidebar?: boolean })
|
|
|
8838
9102
|
\`
|
|
8839
9103
|
}));
|
|
8840
9104
|
});
|
|
8841
|
-
`}function
|
|
9105
|
+
`}function cn(e,t,n){let r=Rt(t),a=kt(r),o=At(r),i=Lt(t),s=Jt(n),c=on(),l=sn(a,n),u=ln(),p=un(o);return `import { Hono } from "hono";
|
|
8842
9106
|
import { html, raw } from "hono/html";
|
|
8843
9107
|
import { drizzle } from "drizzle-orm/d1";
|
|
8844
9108
|
import { eq, desc, asc, sql, like, or, inArray } from "drizzle-orm";
|
|
@@ -8846,34 +9110,42 @@ import { createAuth } from "./auth.config";
|
|
|
8846
9110
|
import * as schema from "${e}";
|
|
8847
9111
|
import { users } from "./auth.schema";
|
|
8848
9112
|
|
|
8849
|
-
${
|
|
9113
|
+
${l}
|
|
8850
9114
|
|
|
8851
9115
|
export function registerAdminDashboard(app: Hono<any>, options: { databaseBinding: string, r2Binding?: string }) {
|
|
8852
9116
|
const adminApp = new Hono<any>();
|
|
8853
9117
|
|
|
8854
9118
|
${u}
|
|
8855
9119
|
|
|
8856
|
-
${
|
|
9120
|
+
${p}
|
|
8857
9121
|
|
|
8858
9122
|
// Generate table routes
|
|
8859
|
-
${
|
|
9123
|
+
${i}
|
|
8860
9124
|
|
|
8861
9125
|
// Generate function routes
|
|
8862
|
-
${
|
|
9126
|
+
${s}
|
|
8863
9127
|
|
|
8864
9128
|
// Generate storage routes
|
|
8865
|
-
${
|
|
9129
|
+
${c}
|
|
8866
9130
|
|
|
8867
9131
|
app.route('/admin', adminApp);
|
|
8868
9132
|
app.get('/admin/', (c) => c.redirect('/admin'));
|
|
8869
9133
|
}
|
|
8870
|
-
`}function
|
|
8871
|
-
`}function
|
|
8872
|
-
|
|
8873
|
-
|
|
8874
|
-
|
|
8875
|
-
|
|
8876
|
-
|
|
9134
|
+
`}function re(e){if(typeof e!="object"||e===null)return false;let t=e;return t.kind==="schema"&&typeof t.tables=="object"&&(typeof t.enums=="object"||t.enums===void 0)}function E(e){return e.replace(/([a-z0-9])([A-Z])/g,"$1_$2").replace(/[\s-]+/g,"_").toLowerCase()}function M(e){return e.replace(/[_-]+/g," ").replace(/\s+(.)/g,(t,n)=>n.toUpperCase()).replace(/\s/g,"").replace(/^(.)/,(t,n)=>n.toUpperCase())}function ie(e){return e.endsWith("ies")?`${e.slice(0,-3)}y`:e.endsWith("ses")?e.slice(0,-2):e.endsWith("s")&&e.length>1?e.slice(0,-1):e}function g(e){return JSON.stringify(e)}function Mr(e){if(typeof e=="string")return g(e);if(typeof e=="number"||typeof e=="boolean")return String(e);if(e===null)return "null";if(e instanceof Date)return g(e.toISOString());throw new Error(`Unsupported SQL default value '${String(e)}'. Use string, number, boolean, null, or Date.`)}function Fr(e){return {kind:"schema",tables:Object.fromEntries(Object.entries(e.tables).map(([t,n])=>[t,{kind:"table",sqlName:n.sqlName,columns:Object.fromEntries(Object.entries(n.columns).map(([r,a])=>[r,{...a}])),relations:Object.fromEntries(Object.entries(n.relations).map(([r,a])=>[r,{...a}]))}])),enums:Object.fromEntries(Object.entries(e.enums??{}).map(([t,n])=>[t,{...n}]))}}function H(e,t,n){let r=e.tables[t];return r?r.columns[n]?.type:void 0}function dn(e,t,n,r,a,o,i){let s=t.columns[n];if(s){if(s.references&&(s.references.table!==r||s.references.column!==a))throw new Error(`Inferred relation '${e}.${n}' conflicts with explicit references(${s.references.table}.${s.references.column}).`);t.columns[n]={...s,notNull:s.notNull??(s.nullable===true?false:o.notNull),nullable:s.nullable??(o.notNull===false?true:void 0),references:{table:r,column:a,onDelete:s.references?.onDelete??o.onDelete,onUpdate:s.references?.onUpdate??o.onUpdate}};return}t.columns[n]={kind:"column",type:o.fkType??i,sqlName:o.sqlName,notNull:o.notNull??true,nullable:o.notNull===false?true:void 0,references:{table:r,column:a,onDelete:o.onDelete,onUpdate:o.onUpdate}};}function pn(e,t,n){if(t.notNull===true&&t.nullable===true)throw new Error(`Invalid nullable configuration on '${e}': cannot set both notNull and nullable to true.`);return t.notNull===true?true:t.nullable===true||t.notNull===false?false:n}function mn(e,t){return `${e}:${t}`}function Ir(e,t,n,r){let a=mn(e,t),o=mn(n,r);return a<=o?{key:`${a}|${o}`,leftTable:e,leftReferenceField:t,rightTable:n,rightReferenceField:r,sourceIsLeft:true}:{key:`${o}|${a}`,leftTable:n,leftReferenceField:r,rightTable:e,rightReferenceField:t,sourceIsLeft:false}}function Pr(e,t){return `${e}${M(t)}Links`}function gn(e,t,n){let r=ie(e),a=ie(t);return r===a?`${n}${M(r)}Id`:`${r}Id`}function Er(e,t,n){if(e.junctionTable!==t.junctionTable)throw new Error(`manyToMany pair '${n}' has conflicting junctionTable values ('${e.junctionTable}' vs '${t.junctionTable}').`);if(e.leftField!==t.leftField)throw new Error(`manyToMany pair '${n}' has conflicting left field values ('${e.leftField}' vs '${t.leftField}').`);if(e.rightField!==t.rightField)throw new Error(`manyToMany pair '${n}' has conflicting right field values ('${e.rightField}' vs '${t.rightField}').`);if(e.leftSqlName!==t.leftSqlName)throw new Error(`manyToMany pair '${n}' has conflicting left sql name values ('${e.leftSqlName}' vs '${t.leftSqlName}').`);if(e.rightSqlName!==t.rightSqlName)throw new Error(`manyToMany pair '${n}' has conflicting right sql name values ('${e.rightSqlName}' vs '${t.rightSqlName}').`);if(e.onDelete!==t.onDelete)throw new Error(`manyToMany pair '${n}' has conflicting onDelete values ('${e.onDelete}' vs '${t.onDelete}').`);if(e.onUpdate!==t.onUpdate)throw new Error(`manyToMany pair '${n}' has conflicting onUpdate values ('${e.onUpdate}' vs '${t.onUpdate}').`);return e}function Or(e){let t=new Map;for(let[n,r]of Object.entries(e.tables))for(let a of Object.values(r.relations)){if(a.relation!=="manyToMany")continue;let o=a.referenceField??"id",i=a.targetReferenceField??"id",s=Ir(n,o,a.targetTable,i),c=gn(s.leftTable,s.rightTable,"source"),l=gn(s.rightTable,s.leftTable,"target"),u={leftTable:s.leftTable,leftReferenceField:s.leftReferenceField,rightTable:s.rightTable,rightReferenceField:s.rightReferenceField,junctionTable:a.junctionTable??Pr(s.leftTable,s.rightTable),leftField:s.sourceIsLeft?a.sourceField??c:a.targetField??c,rightField:s.sourceIsLeft?a.targetField??l:a.sourceField??l,leftSqlName:s.sourceIsLeft?a.sourceSqlName:a.targetSqlName,rightSqlName:s.sourceIsLeft?a.targetSqlName:a.sourceSqlName,onDelete:a.onDelete,onUpdate:a.onUpdate};if(u.leftField===u.rightField)throw new Error(`manyToMany pair '${s.key}' resolves to duplicate junction fields '${u.leftField}'. Set sourceField/targetField explicitly.`);let p=t.get(s.key);p?Er(p,u,s.key):t.set(s.key,u),a.referenceField=o,a.targetReferenceField=i,a.junctionTable=u.junctionTable,a.sourceField=s.sourceIsLeft?u.leftField:u.rightField,a.targetField=s.sourceIsLeft?u.rightField:u.leftField,a.sourceSqlName=s.sourceIsLeft?u.leftSqlName:u.rightSqlName,a.targetSqlName=s.sourceIsLeft?u.rightSqlName:u.leftSqlName;}for(let n of t.values()){if(n.junctionTable in e.tables)throw new Error(`manyToMany auto junction table '${n.junctionTable}' conflicts with an existing table. Set a different junctionTable name.`);let r=H(e,n.leftTable,n.leftReferenceField)??"string",a=H(e,n.rightTable,n.rightReferenceField)??"string";e.tables[n.junctionTable]={kind:"table",columns:{[n.leftField]:{kind:"column",type:r,sqlName:n.leftSqlName,notNull:true,nullable:false,references:{table:n.leftTable,column:n.leftReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true},[n.rightField]:{kind:"column",type:a,sqlName:n.rightSqlName,notNull:true,nullable:false,references:{table:n.rightTable,column:n.rightReferenceField,onDelete:n.onDelete,onUpdate:n.onUpdate},index:true}},relations:{[n.leftTable]:{kind:"relation",relation:"one",targetTable:n.leftTable,field:n.leftField,referenceField:n.leftReferenceField},[n.rightTable]:{kind:"relation",relation:"one",targetTable:n.rightTable,field:n.rightField,referenceField:n.rightReferenceField}}};}}function jr(e){for(let[t,n]of Object.entries(e.tables)){for(let[r,a]of Object.entries(n.columns))if(a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`);for(let[r,a]of Object.entries(n.relations))if(a.relation!=="manyToMany"&&a.notNull===true&&a.nullable===true)throw new Error(`Invalid nullable configuration on '${t}.${r}': cannot set both notNull and nullable to true.`)}}function Dr(e){jr(e);let t=Fr(e);for(let[n,r]of Object.entries(t.tables))for(let[a,o]of Object.entries(r.relations)){if(o.relation!=="one")continue;let i=o.referenceField??"id",s=o.field??`${a}Id`;o.field=s;let c=H(t,o.targetTable,i)??o.fkType??"string";dn(n,r,s,o.targetTable,i,{fkType:o.fkType,sqlName:o.sqlName,notNull:pn(`${n}.${a}`,o,true),onDelete:o.onDelete,onUpdate:o.onUpdate},c);}for(let[n,r]of Object.entries(t.tables))for(let a of Object.values(r.relations)){if(a.relation!=="many")continue;let o=t.tables[a.targetTable];if(!o)continue;let i=a.referenceField??"id",s=a.field??`${ie(n)}Id`;a.field=s;let c=H(t,n,i)??a.fkType??"string";dn(a.targetTable,o,s,n,i,{fkType:a.fkType,sqlName:a.sqlName,notNull:pn(`${n}.${a.targetTable}`,a,true),onDelete:a.onDelete,onUpdate:a.onUpdate},c);}return Or(t),t}function hn(e){return e.primaryKey===true||e.notNull!==true||e.autoIncrement===true||e.sqlDefault!==void 0||e.runtimeDefaultFn!==void 0}function O(e){return e.notNull!==true}function Vr(e,t,n){let r=t.sqlName??E(e),a=r!==e;if(t.type==="int")return a?`t.int(${g(r)})`:"t.int()";if(t.type==="string")return t.length!==void 0?a?`t.text(${g(r)}, { length: ${t.length} })`:`t.text({ length: ${t.length} })`:a?`t.text(${g(r)})`:"t.text()";if(t.type==="boolean")return a?`t.int(${g(r)}, { mode: "boolean" })`:'t.int({ mode: "boolean" })';if(t.type==="date")return a?`t.int(${g(r)}, { mode: "timestamp_ms" })`:'t.int({ mode: "timestamp_ms" })';if(t.type==="enum"&&t.enumValues&&t.enumValues.length>0)return t.isArray?a?`t.text(${g(r)}).array()`:"t.text().array()":a?`t.text(${g(r)})`:"t.text()";if(t.type==="json"&&t.jsonShape){let o=L(t.jsonShape);return `${a?`t.text(${g(r)}, { mode: "json" })`:'t.text({ mode: "json" })'}.$type<${o}>()`}return n==="camelToSnake"&&a?`t.text(${g(r)})`:"t.text()"}function Br(e,t,n){let r=n.field,a=n.referenceField??"id";if(!r)throw new Error(`Relation on '${e}' targeting '${n.targetTable}' is missing a local field.`);if(!(r in t.columns))throw new Error(`Relation '${e}.${r}' references missing local field '${r}'.`);return {sourceField:r,targetField:a}}function Wr(e){return e.size===0?"":`import { ${Array.from(e).sort().join(", ")} } from "./auth.schema";
|
|
9135
|
+
`}function Hr(e){return Object.values(e.relations).filter(t=>t.relation==="one").map(t=>t.targetTable)}function Lr(e,t,n){if(t.references)return {tableName:t.references.table,fieldName:t.references.column};let r=Object.values(n.relations).find(a=>a.relation==="one"&&a.field===e);if(r)return {tableName:r.targetTable,fieldName:r.referenceField??"id"}}function zr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=[];for(let[o,i]of Object.entries(r.columns)){if(i.type!=="json"||!i.jsonShape)continue;let s=se(i.jsonShape);a.push(`${g(o)}: { shape: ${s} },`);}a.length!==0&&t.push(`${g(n)}: {
|
|
9136
|
+
${a.map(o=>` ${o}`).join(`
|
|
9137
|
+
`)}
|
|
9138
|
+
},`);}return t.length===0?`export const __appflareJsonColumns = {} as const;
|
|
9139
|
+
`:`export const __appflareJsonColumns = {
|
|
9140
|
+
${t.map(n=>` ${n}`).join(`
|
|
9141
|
+
`)}
|
|
9142
|
+
} as const;
|
|
9143
|
+
`}function se(e){return e.kind==="array"?`{ kind: "array", element: ${se(e.element)} }`:e.kind==="object"?`{ kind: "object", shape: { ${Object.entries(e.shape).map(([n,r])=>`${g(n)}: ${se(r)}`).join(", ")} } }`:`{ kind: ${g(e.kind)} }`}function Ur(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=[];for(let[o,i]of Object.entries(r.relations))i.relation==="manyToMany"&&i.junctionTable&&a.push(`${g(o)}: {
|
|
9144
|
+
targetTable: ${g(i.targetTable)},
|
|
9145
|
+
junctionTable: ${g(i.junctionTable)},
|
|
9146
|
+
sourceField: ${g(i.sourceField??"")},
|
|
9147
|
+
targetField: ${g(i.targetField??"")},
|
|
9148
|
+
},`);a.length!==0&&t.push(`${g(n)}: {
|
|
8877
9149
|
${a.map(o=>` ${o}`).join(`
|
|
8878
9150
|
`)}
|
|
8879
9151
|
},`);}return t.length===0?`export const __appflareManyToMany = {} as const;
|
|
@@ -8881,25 +9153,25 @@ ${a.map(o=>` ${o}`).join(`
|
|
|
8881
9153
|
${t.map(n=>` ${n}`).join(`
|
|
8882
9154
|
`)}
|
|
8883
9155
|
} as const;
|
|
8884
|
-
`}function
|
|
9156
|
+
`}function Qr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=[];for(let[o,i]of Object.entries(r.relations)){if(i.relation==="one"){a.push(`${g(o)}: {
|
|
8885
9157
|
kind: "one",
|
|
8886
|
-
targetTable: ${
|
|
8887
|
-
sourceField: ${
|
|
8888
|
-
referenceField: ${
|
|
8889
|
-
},`);continue}if(
|
|
9158
|
+
targetTable: ${g(i.targetTable)},
|
|
9159
|
+
sourceField: ${g(i.field??"")},
|
|
9160
|
+
referenceField: ${g(i.referenceField??"id")},
|
|
9161
|
+
},`);continue}if(i.relation==="many"){a.push(`${g(o)}: {
|
|
8890
9162
|
kind: "many",
|
|
8891
|
-
targetTable: ${
|
|
8892
|
-
sourceField: ${
|
|
8893
|
-
referenceField: ${
|
|
8894
|
-
},`);continue}a.push(`${
|
|
9163
|
+
targetTable: ${g(i.targetTable)},
|
|
9164
|
+
sourceField: ${g(i.field??"")},
|
|
9165
|
+
referenceField: ${g(i.referenceField??"id")},
|
|
9166
|
+
},`);continue}a.push(`${g(o)}: {
|
|
8895
9167
|
kind: "manyToMany",
|
|
8896
|
-
targetTable: ${
|
|
8897
|
-
junctionTable: ${
|
|
8898
|
-
sourceField: ${
|
|
8899
|
-
targetField: ${
|
|
8900
|
-
referenceField: ${
|
|
8901
|
-
targetReferenceField: ${
|
|
8902
|
-
},`);}a.length!==0&&t.push(`${
|
|
9168
|
+
targetTable: ${g(i.targetTable)},
|
|
9169
|
+
junctionTable: ${g(i.junctionTable??"")},
|
|
9170
|
+
sourceField: ${g(i.sourceField??"")},
|
|
9171
|
+
targetField: ${g(i.targetField??"")},
|
|
9172
|
+
referenceField: ${g(i.referenceField??"id")},
|
|
9173
|
+
targetReferenceField: ${g(i.targetReferenceField??"id")},
|
|
9174
|
+
},`);}a.length!==0&&t.push(`${g(n)}: {
|
|
8903
9175
|
${a.map(o=>` ${o}`).join(`
|
|
8904
9176
|
`)}
|
|
8905
9177
|
},`);}return t.length===0?`export const __appflareRelations = {} as const;
|
|
@@ -8907,46 +9179,53 @@ ${a.map(o=>` ${o}`).join(`
|
|
|
8907
9179
|
${t.map(n=>` ${n}`).join(`
|
|
8908
9180
|
`)}
|
|
8909
9181
|
} as const;
|
|
8910
|
-
`}function
|
|
8911
|
-
${
|
|
9182
|
+
`}function _r(e,t){let n=new Set(Object.keys(e.tables)),r=new Set;for(let l of Object.values(e.tables)){for(let u of Hr(l))n.has(u)||r.add(u);for(let u of Object.values(l.columns))u.references&&!n.has(u.references.table)&&r.add(u.references.table);}let a=new Map;for(let l of Object.values(e.tables))for(let[u,p]of Object.entries(l.columns))if(p.type==="enum"&&p.enumValues&&p.enumValues.length>0){let d=p.enumRef??`${u}`;a.has(d)||a.set(d,p);}let o=[],i=[];for(let[l,u]of a.entries()){let p=M(l),d=u.enumValues.map(y=>`"${y}"`).join(" | ");o.push(`export type ${p} = ${d};`),i.push(`export const ${p}Column = t.customType<{ data: ${p}; dataNotNull: ${p} }>({ dataType: () => "text" });`);}let s=[],c=[];for(let[l,u]of Object.entries(e.tables)){let p=u.sqlName??E(l),d=[],y=[];for(let[b,h]of Object.entries(u.columns)){let T;if(h.type==="enum"&&h.enumValues&&h.enumValues.length>0){let R=h.enumRef??b,D=M(R),V=h.sqlName??E(b);T=V!==b?`${D}Column(${g(V)})`:`${D}Column()`,h.isArray&&(T+=".array()");}else T=Vr(b,h,t);h.uuidPrimaryKey&&(T+=".$defaultFn(() => crypto.randomUUID())"),h.primaryKey&&(T+=h.autoIncrement?".primaryKey({ autoIncrement: true })":".primaryKey()"),h.notNull&&(T+=".notNull()"),h.sqlDefault!==void 0&&(T+=`.default(${Mr(h.sqlDefault)})`);let C=Lr(b,h,u);if(C)if(h.references?.onDelete||h.references?.onUpdate){let R=[];h.references.onDelete&&R.push(`onDelete: ${g(h.references.onDelete)}`),h.references.onUpdate&&R.push(`onUpdate: ${g(h.references.onUpdate)}`),T+=`.references(() => ${C.tableName}.${C.fieldName}, { ${R.join(", ")} })`;}else T+=`.references(() => ${C.tableName}.${C.fieldName})`;if(h.unique){let R=typeof h.unique=="object"&&h.unique.name?h.unique.name:`${p}_${E(b)}_unique_idx`;y.push(` t.uniqueIndex(${g(R)}).on(table.${b})`);}if(h.index){let R=typeof h.index=="object"&&h.index.name?h.index.name:`${p}_${E(b)}_idx`;y.push(` t.index(${g(R)}).on(table.${b})`);}d.push(` ${b}: ${T},`);}y.length>0?s.push(`export const ${l} = table(
|
|
9183
|
+
${g(p)},
|
|
8912
9184
|
{
|
|
8913
|
-
${
|
|
9185
|
+
${d.join(`
|
|
8914
9186
|
`)}
|
|
8915
9187
|
},
|
|
8916
9188
|
(table) => [
|
|
8917
|
-
${
|
|
9189
|
+
${y.join(`,
|
|
8918
9190
|
`)}
|
|
8919
9191
|
],
|
|
8920
|
-
);`):
|
|
8921
|
-
${
|
|
9192
|
+
);`):s.push(`export const ${l} = table(${g(p)}, {
|
|
9193
|
+
${d.join(`
|
|
8922
9194
|
`)}
|
|
8923
|
-
});`);let
|
|
8924
|
-
fields: [${
|
|
8925
|
-
references: [${
|
|
8926
|
-
}),`);}for(let[
|
|
8927
|
-
${
|
|
9195
|
+
});`);let v=Object.entries(u.relations).filter(([,b])=>b.relation==="one"),A=Object.entries(u.relations).filter(([,b])=>b.relation==="many"),w=Object.entries(u.relations).filter(([,b])=>b.relation==="manyToMany");if(v.length===0&&A.length===0&&w.length===0)continue;let I=[];for(let[b,h]of v){let T=Br(l,u,h);I.push(` ${b}: one(${h.targetTable}, {
|
|
9196
|
+
fields: [${l}.${T.sourceField}],
|
|
9197
|
+
references: [${h.targetTable}.${T.targetField}],
|
|
9198
|
+
}),`);}for(let[b,h]of A)I.push(` ${b}: many(${h.targetTable}),`);for(let[b,h]of w){if(!h.junctionTable)throw new Error(`manyToMany relation '${l}.${b}' is missing junctionTable after normalization.`);I.push(` ${b}: many(${h.junctionTable}),`);}c.push(`export const ${l}Relations = relations(${l}, ({ one, many }) => ({
|
|
9199
|
+
${I.join(`
|
|
8928
9200
|
`)}
|
|
8929
9201
|
}));`);}return `import * as t from "drizzle-orm/sqlite-core";
|
|
8930
9202
|
import { sqliteTable as table } from "drizzle-orm/sqlite-core";
|
|
8931
9203
|
import { relations } from "drizzle-orm";
|
|
8932
|
-
${
|
|
8933
|
-
${
|
|
9204
|
+
${Wr(r)}
|
|
9205
|
+
${o.join(`
|
|
9206
|
+
`)}
|
|
9207
|
+
${i.join(`
|
|
9208
|
+
`)}
|
|
9209
|
+
|
|
9210
|
+
${s.join(`
|
|
8934
9211
|
|
|
8935
9212
|
`)}
|
|
8936
9213
|
|
|
8937
|
-
${
|
|
9214
|
+
${c.join(`
|
|
8938
9215
|
|
|
8939
9216
|
`)}
|
|
8940
9217
|
|
|
8941
|
-
${
|
|
9218
|
+
${zr(e)}
|
|
9219
|
+
|
|
9220
|
+
${Ur(e)}
|
|
8942
9221
|
|
|
8943
|
-
${
|
|
8944
|
-
`}function
|
|
9222
|
+
${Qr(e)}
|
|
9223
|
+
`}function le(e){return e.kind==="array"?`z.array(${le(e.element)})`:e.kind==="object"?`z.object({ ${Object.entries(e.shape).map(([n,r])=>`${g(n)}: ${le(r)}`).join(", ")} })`:e.kind==="string"?"z.string()":e.kind==="number"?"z.number()":e.kind==="boolean"?"z.boolean()":e.kind==="date"?"z.date()":"z.unknown()"}function fn(e,t,n){let r="z.unknown()";if(e.type==="int")r="z.number().int()";else if(e.type==="string")r="z.string()",e.length!==void 0&&(r+=`.max(${e.length})`);else if(e.type==="boolean")r="z.boolean()";else if(e.type==="date")r="z.date()";else if(e.type==="enum"&&e.enumValues&&e.enumValues.length>0){let o=`z.enum([${e.enumValues.map(i=>`"${i}"`).join(", ")}])`;r=e.isArray?`z.array(${o})`:o;}else e.type==="json"&&e.jsonShape&&(r=le(e.jsonShape));return t&&(r+=".optional()"),n&&(r+=".nullable()"),r}function Kr(e){let t=[];for(let[n,r]of Object.entries(e.tables)){let a=M(n),o=[],i=[];for(let[s,c]of Object.entries(r.columns))o.push(` ${s}: ${fn(c,hn(c),O(c))},`),i.push(` ${s}: ${fn(c,O(c),O(c))},`);t.push(`export const ${n}InsertSchema = z.object({
|
|
8945
9224
|
${o.join(`
|
|
8946
9225
|
`)}
|
|
8947
9226
|
});
|
|
8948
9227
|
export const ${n}SelectSchema = z.object({
|
|
8949
|
-
${
|
|
9228
|
+
${i.join(`
|
|
8950
9229
|
`)}
|
|
8951
9230
|
});
|
|
8952
9231
|
|
|
@@ -8955,34 +9234,36 @@ export type ${a}Select = z.infer<typeof ${n}SelectSchema>;
|
|
|
8955
9234
|
`);}return `import { z } from "zod";
|
|
8956
9235
|
|
|
8957
9236
|
${t.join(`
|
|
8958
|
-
`)}`}function
|
|
8959
|
-
${
|
|
9237
|
+
`)}`}function L(e){return e.kind==="array"?`Array<${L(e.element)}>`:e.kind==="object"?`{ ${Object.entries(e.shape).map(([n,r])=>`${n}: ${L(r)}`).join("; ")} }`:e.kind==="string"?"string":e.kind==="number"?"number":e.kind==="boolean"?"boolean":e.kind==="date"?"Date":"unknown"}function Jr(e){if(e.type==="int")return "number";if(e.type==="string")return "string";if(e.type==="boolean")return "boolean";if(e.type==="date")return "Date";if(e.type==="enum"&&e.enumValues&&e.enumValues.length>0){let t=e.enumValues.map(n=>`"${n}"`).join(" | ");return e.isArray?`Array<${t}>`:t}return e.type==="json"&&e.jsonShape?L(e.jsonShape):"unknown"}function Gr(e){let t=[];for(let[r,a]of Object.entries(e.enums??{})){let o=M(r),i=a.values.map(s=>`"${s}"`).join(" | ");t.push(`export type ${o} = ${i};`);}let n=[];for(let[r,a]of Object.entries(e.tables)){let o=M(r),i=[],s=[];for(let[c,l]of Object.entries(a.columns)){let u=Jr(l),p=O(l)?" | null":"";i.push(` ${c}${O(l)?"?":""}: ${u}${p};`),s.push(` ${c}${hn(l)?"?":""}: ${u}${p};`);}n.push(`export type ${o} = {
|
|
9238
|
+
${i.join(`
|
|
8960
9239
|
`)}
|
|
8961
9240
|
};
|
|
8962
9241
|
|
|
8963
|
-
export type New${
|
|
9242
|
+
export type New${o} = {
|
|
8964
9243
|
${s.join(`
|
|
8965
9244
|
`)}
|
|
8966
9245
|
};`);}return `${t.join(`
|
|
9246
|
+
`)}
|
|
9247
|
+
${n.join(`
|
|
8967
9248
|
|
|
8968
9249
|
`)}
|
|
8969
|
-
`}function Qr(e,t){if(t){let n=e[t];if(!Y(n))throw new Error(`schemaDsl.exportName '${t}' does not point to a schema() export.`);return n}for(let n of Object.values(e))if(Y(n))return n;throw new Error("No schema() export found in schemaDsl entry module. Set schemaDsl.exportName to the correct export.")}async function dn(e){let t=e.config.schemaDsl;if(!t)return;let n=t.namingStrategy??"camelToSnake",r=path.resolve(e.configDir,t.entry),a=path.resolve(e.configDir,t.outFile??path.resolve(e.outDirAbs,"schema.compiled.ts")),o=path.resolve(e.configDir,t.typesOutFile??path.resolve(e.outDirAbs,"schema.types.ts")),s=path.resolve(e.configDir,t.zodOutFile??path.resolve(e.outDirAbs,"schema.zod.ts")),l=await import(`${url.pathToFileURL(r).href}?t=${Date.now()}`),c=Qr(l,t.exportName),u=Pr(c);await Promise.all([promises.mkdir(path.dirname(a),{recursive:true}),promises.mkdir(path.dirname(o),{recursive:true}),promises.mkdir(path.dirname(s),{recursive:true})]);let f=Hr(u,n),d=Ur(u),b=zr(u);return await Promise.all([Bun.write(a,f),Bun.write(o,d),Bun.write(s,b)]),{schemaPath:a,typesPath:o,zodPath:s,tableNames:Object.keys(u.tables)}}function Kr(e){return e.replaceAll("\\","/")}function C(e,t){let n=Kr(path.relative(e,t)).replace(/\.tsx?$/,"");return n.startsWith(".")?n:`./${n}`}var Yr=new Set([".ts",".tsx",".mts",".cts"]);async function fn(e){let t=await promises.readdir(e,{withFileTypes:true}),n=[];for(let r of t){if(r.name.startsWith(".")||r.name==="node_modules"||r.name==="_generated")continue;let a=path.resolve(e,r.name);if(r.isDirectory()){n.push(...await fn(a));continue}r.isFile()&&Yr.has(path.extname(r.name))&&n.push(a);}return n}function ne(e){return e.replace(/\.[cm]?tsx?$/,"")}function Xr(e,t){let n=e,r=false,a,o="unknown";for(;g__namespace.isCallExpression(n);){let s=n.expression;if(!g__namespace.isPropertyAccessExpression(s))break;let i=s.name.text;if(i==="optional"||i==="nullable")r=true,n=s.expression;else if(i==="default"){r=true;let l=n.arguments[0];l&&(g__namespace.isStringLiteral(l)||g__namespace.isNumericLiteral(l)?a=l.text:l.kind===g__namespace.SyntaxKind.TrueKeyword?a="true":l.kind===g__namespace.SyntaxKind.FalseKeyword&&(a="false")),n=s.expression;}else if(i==="string"||i==="uuid"||i==="email"||i==="url"){o="string";break}else if(i==="number"||i==="int"||i==="float"){o="number";break}else if(i==="boolean"){o="boolean";break}else n=s.expression;}return {name:t,type:o,optional:r,defaultValue:a}}function ea(e){if(!e||!g__namespace.isObjectLiteralExpression(e))return [];let t=e.properties.find(r=>g__namespace.isPropertyAssignment(r)&&g__namespace.isIdentifier(r.name)&&r.name.text==="args");if(!t||!g__namespace.isObjectLiteralExpression(t.initializer))return [];let n=[];for(let r of t.initializer.properties)!g__namespace.isPropertyAssignment(r)||!g__namespace.isIdentifier(r.name)||n.push(Xr(r.initializer,r.name.text));return n}function ta(e){return g__namespace.isVariableStatement(e)?e.modifiers?.some(t=>t.kind===g__namespace.SyntaxKind.ExportKeyword)??false:false}function hn(e){return g__namespace.isIdentifier(e)?e.text:g__namespace.isParenthesizedExpression(e)?hn(e.expression):null}function na(e){if(!e||!g__namespace.isObjectLiteralExpression(e))return [];let t=e.properties.find(r=>!g__namespace.isPropertyAssignment(r)||!g__namespace.isIdentifier(r.name)?false:r.name.text==="cronTrigger");if(!t||!g__namespace.isPropertyAssignment(t))return [];let n=t.initializer;return g__namespace.isStringLiteral(n)||g__namespace.isNoSubstitutionTemplateLiteral(n)?[n.text.trim()].filter(r=>r.length>0):g__namespace.isArrayLiteralExpression(n)?n.elements.map(r=>g__namespace.isStringLiteral(r)||g__namespace.isNoSubstitutionTemplateLiteral(r)?r.text.trim():"").filter(r=>r.length>0):[]}function ra(e,t){let n=g__namespace.createSourceFile(t,e,g__namespace.ScriptTarget.Latest,true,g__namespace.ScriptKind.TS),r=[];for(let a of n.statements)if(ta(a))for(let o of a.declarationList.declarations){if(!g__namespace.isIdentifier(o.name)||!o.initializer||!g__namespace.isCallExpression(o.initializer))continue;let s=hn(o.initializer.expression);s!=="query"&&s!=="mutation"&&s!=="scheduler"&&s!=="cron"&&s!=="storageManager"||r.push({exportName:o.name.text,kind:s==="storageManager"?"storage":s,cronTriggers:s==="cron"?na(o.initializer.arguments[0]):[],args:s==="query"||s==="mutation"?ea(o.initializer.arguments[0]):[]});}return r}function pn(e,t,n){let r=t.replace(/\\/g,"/"),o=ne(r).split("/").filter(Boolean);return `/${[e,...o,n].filter(Boolean).map(i=>i.trim()).filter(i=>i.length>0).join("/")}`}function aa(e,t){let n=e.replace(/\\/g,"/"),r=`${t}/`,a=n.indexOf(r);return a>=0?n.slice(a+r.length):n===t?"index.ts":n}function mn(e,t){let n=e.replace(/\\/g,"/"),a=ne(n).split("/").filter(Boolean),o=a[a.length-1]??"index";return [a.length>1?a[a.length-2]:"root",o,t].map(i=>i.trim()).filter(i=>i.length>0).join("/")}async function yn(e){let t=[],n=await fn(e.scanDirAbs).catch(()=>[]);for(let a of n){let o=Bun.file(a);if(!await o.exists())continue;let s=await o.text(),i=path.relative(e.scanDirAbs,a),l=ra(s,a),c=[{kind:"query",kindDirectory:"queries",exports:l.filter(u=>u.kind==="query")},{kind:"mutation",kindDirectory:"mutations",exports:l.filter(u=>u.kind==="mutation")},{kind:"scheduler",kindDirectory:"schedulers",exports:l.filter(u=>u.kind==="scheduler")},{kind:"cron",kindDirectory:"crons",exports:l.filter(u=>u.kind==="cron")},{kind:"storage",kindDirectory:"queries",exports:l.filter(u=>u.kind==="storage")}];for(let u of c){if(u.exports.length===0)continue;let f=aa(i,u.kindDirectory);for(let d of u.exports){let b=u.kind==="query"||u.kind==="mutation"?mn(f,d.exportName):void 0,x=u.kind==="scheduler"||u.kind==="cron"?mn(f,d.exportName):void 0,y=u.kind==="query"||u.kind==="mutation"?[...ne(f).split("/").filter(Boolean),d.exportName]:void 0,m=u.kind==="query"?pn("queries",f,d.exportName):u.kind==="mutation"?pn("mutations",f,d.exportName):u.kind==="storage"?`/storage/managers/${d.exportName}`:`/${u.kindDirectory}/${x}`;t.push({kind:u.kind,exportName:d.exportName,filePath:a,importPath:C(e.outDirAbs,a),clientImportPath:C(path.resolve(e.outDirAbs,"client"),a),routePath:m,handlerName:b,clientSegments:y,taskName:x,cronTriggers:d.cronTriggers,args:d.args});}}}t.sort((a,o)=>a.routePath.localeCompare(o.routePath));let r=new Map;for(let a of t){let o=a.taskName?`task:${a.taskName}`:`route:${a.routePath}`,s=r.get(o);if(s)throw new Error(`Duplicate handler operation discovered: ${a.taskName??a.routePath} (${s} and ${a.filePath}#${a.exportName}).`);r.set(o,`${a.filePath}#${a.exportName}`);}return t}function ia(e){let t=[],n="",r=0,a=0,o=0,s=false,i=false,l=false,c=false;for(let f=0;f<e.length;f+=1){let d=e[f];if(c){n+=d,c=false;continue}if(d==="\\"){n+=d,c=true;continue}if(!i&&!l&&d==="'"){s=!s,n+=d;continue}if(!s&&!l&&d==='"'){i=!i,n+=d;continue}if(!s&&!i&&d==="`"){l=!l,n+=d;continue}if(s||i||l){n+=d;continue}if(d==="("?r+=1:d===")"?r-=1:d==="{"?a+=1:d==="}"?a-=1:d==="["?o+=1:d==="]"&&(o-=1),d===","&&r===0&&a===0&&o===0){let b=n.trim();b.length>0&&t.push(b),n="";continue}n+=d;}let u=n.trim();return u.length>0&&t.push(u),t}function sa(e){let t=0,n=0,r=0,a=false,o=false,s=false,i=false;for(let l=0;l<e.length;l+=1){let c=e[l];if(i){i=false;continue}if(c==="\\"){i=true;continue}if(!o&&!s&&c==="'"){a=!a;continue}if(!a&&!s&&c==='"'){o=!o;continue}if(!a&&!o&&c==="`"){s=!s;continue}if(!(a||o||s)){if(c==="("){t+=1;continue}if(c===")"){t-=1;continue}if(c==="{"){n+=1;continue}if(c==="}"){n-=1;continue}if(c==="["){r+=1;continue}if(c==="]"){r-=1;continue}if(c===":"&&t===0&&n===0&&r===0)return l}}return -1}function la(e){let t=e.toLowerCase();return /mode\s*:\s*["'`](timestamp|timestamp_ms)["'`]/.test(t)?"date":/mode\s*:\s*["'`]boolean["'`]/.test(t)?"boolean":/\.(date|datetime|timestamp)\s*\(/.test(t)?"date":/\.(int|integer|real|numeric|decimal|float|double)\s*\(/.test(t)?"number":/\.(text|varchar|char)\s*\(/.test(t)?"string":/\.(boolean|bool)\s*\(/.test(t)?"boolean":"unknown"}function ca(e){let t=ia(e),n=[];for(let r of t){let a=sa(r);if(a===-1)continue;let o=r.slice(0,a).trim().replace(/^['"]|['"]$/g,"");if(!o)continue;let s=r.slice(a+1).trim(),i=/\.primarykey\s*\(/i.test(s),l=/autoincrement\s*:\s*true/i.test(s),c=/\.default\s*\(/i.test(s),f=!/\.notnull\s*\(/i.test(s)||c||l||i;n.push({name:o,expression:s,type:la(s),optional:f,primaryKey:i,autoIncrement:l});}return n}function ua(e){let t=/export\s+const\s+(\w+)\s*=\s*table\s*\(\s*["'`]([^"'`]+)["'`]/g,n=[],r=(o,s)=>{let i=0,l=false,c=false,u=false,f=false;for(let d=s;d<o.length;d+=1){let b=o[d];if(f){f=false;continue}if(b==="\\"){f=true;continue}if(!c&&!u&&b==="'"){l=!l;continue}if(!l&&!u&&b==='"'){c=!c;continue}if(!l&&!c&&b==="`"){u=!u;continue}if(!(l||c||u)){if(b==="{"){i+=1;continue}if(b==="}"&&(i-=1,i===0))return d}}return -1},a=t.exec(e);for(;a;){let o=a[1],s=a[2],i=e.indexOf("{",t.lastIndex);if(i===-1){a=t.exec(e);continue}let l=r(e,i);if(l===-1){a=t.exec(e);continue}let c=e.slice(i+1,l);n.push({exportName:o,tableName:s,columns:ca(c)}),a=t.exec(e);}return n}async function wn(e){let t=await promises.readdir(e,{withFileTypes:true}),n=[];for(let r of t){if(r.name.startsWith(".")||r.name==="node_modules"||r.name==="_generated")continue;let a=path.resolve(e,r.name);if(r.isDirectory()){n.push(...await wn(a));continue}r.isFile()&&r.name==="schema.ts"&&n.push(a);}return n}async function xn(e,t=[]){let n=await wn(e.scanDirAbs).catch(()=>[]),r=path.resolve(e.configDir,"schema.ts"),a=[...t,...n.length?n:[r]];for(let o of a){let s=Bun.file(o);if(!await s.exists())continue;let i=await s.text(),l=ua(i);if(l.length>0)return {schemaPath:o,tables:l}}throw new Error(`Unable to discover schema.ts under scanDir (${e.scanDirAbs}) or fallback (${r}).`)}function ma(e,t){let n=path.relative(e,t).replace(/\\/g,"/");return n.startsWith(".")?n:`./${n}`}async function Rn(e){let{outDirAbs:t,wranglerOutDirAbs:n,config:r,configPath:a,configDir:o}=e,s=C(t,a),i=path.resolve(t,"client"),l=C(i,a),c=performance.now();await Promise.all([promises.mkdir(t,{recursive:true}),promises.mkdir(i,{recursive:true}),promises.mkdir(n,{recursive:true})]),process.stdout.write(`\u{1F4C1} Directories (${(performance.now()-c).toFixed(0)}ms)
|
|
8970
|
-
`);let u=path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)))),
|
|
8971
|
-
`);let
|
|
8972
|
-
`),...
|
|
8973
|
-
`),Bun.write(
|
|
8974
|
-
`);let
|
|
8975
|
-
`);function
|
|
8976
|
-
`)]),process.stdout.write(`\u{1F4C4} TS configs (${(performance.now()-
|
|
8977
|
-
`);async function
|
|
8978
|
-
`);let
|
|
8979
|
-
`);async function
|
|
8980
|
-
`);let
|
|
8981
|
-
`);let
|
|
8982
|
-
`);let
|
|
8983
|
-
`);}}var
|
|
9250
|
+
`}function Zr(e,t){if(t){let n=e[t];if(!re(n))throw new Error(`schemaDsl.exportName '${t}' does not point to a schema() export.`);return n}for(let n of Object.values(e))if(re(n))return n;throw new Error("No schema() export found in schemaDsl entry module. Set schemaDsl.exportName to the correct export.")}async function yn(e){let t=e.config.schemaDsl;if(!t)return;let n=t.namingStrategy??"camelToSnake",r=path.resolve(e.configDir,t.entry),a=path.resolve(e.configDir,t.outFile??path.resolve(e.outDirAbs,"schema.compiled.ts")),o=path.resolve(e.configDir,t.typesOutFile??path.resolve(e.outDirAbs,"schema.types.ts")),i=path.resolve(e.configDir,t.zodOutFile??path.resolve(e.outDirAbs,"schema.zod.ts")),c=await import(`${url.pathToFileURL(r).href}?t=${Date.now()}`),l=Zr(c,t.exportName),u=Dr(l);await Promise.all([promises.mkdir(path.dirname(a),{recursive:true}),promises.mkdir(path.dirname(o),{recursive:true}),promises.mkdir(path.dirname(i),{recursive:true})]);let p=_r(u,n),d=Gr(u),y=Kr(u);return await Promise.all([Bun.write(a,p),Bun.write(o,d),Bun.write(i,y)]),{schemaPath:a,typesPath:o,zodPath:i,tableNames:Object.keys(u.tables)}}function Yr(e){return e.replaceAll("\\","/")}function F(e,t){let n=Yr(path.relative(e,t)).replace(/\.tsx?$/,"");return n.startsWith(".")?n:`./${n}`}var ra=new Set([".ts",".tsx",".mts",".cts"]);async function Tn(e){let t=await promises.readdir(e,{withFileTypes:true}),n=[];for(let r of t){if(r.name.startsWith(".")||r.name==="node_modules"||r.name==="_generated")continue;let a=path.resolve(e,r.name);if(r.isDirectory()){n.push(...await Tn(a));continue}r.isFile()&&ra.has(path.extname(r.name))&&n.push(a);}return n}function ue(e){return e.replace(/\.[cm]?tsx?$/,"")}function aa(e,t){let n=e,r=false,a,o="unknown";for(;f__namespace.isCallExpression(n);){let i=n.expression;if(!f__namespace.isPropertyAccessExpression(i))break;let s=i.name.text;if(s==="optional"||s==="nullable")r=true,n=i.expression;else if(s==="default"){r=true;let c=n.arguments[0];c&&(f__namespace.isStringLiteral(c)||f__namespace.isNumericLiteral(c)?a=c.text:c.kind===f__namespace.SyntaxKind.TrueKeyword?a="true":c.kind===f__namespace.SyntaxKind.FalseKeyword&&(a="false")),n=i.expression;}else if(s==="string"||s==="uuid"||s==="email"||s==="url"){o="string";break}else if(s==="number"||s==="int"||s==="float"){o="number";break}else if(s==="boolean"){o="boolean";break}else n=i.expression;}return {name:t,type:o,optional:r,defaultValue:a}}function oa(e){if(!e||!f__namespace.isObjectLiteralExpression(e))return [];let t=e.properties.find(r=>f__namespace.isPropertyAssignment(r)&&f__namespace.isIdentifier(r.name)&&r.name.text==="args");if(!t||!f__namespace.isObjectLiteralExpression(t.initializer))return [];let n=[];for(let r of t.initializer.properties)!f__namespace.isPropertyAssignment(r)||!f__namespace.isIdentifier(r.name)||n.push(aa(r.initializer,r.name.text));return n}function ia(e){return f__namespace.isVariableStatement(e)?e.modifiers?.some(t=>t.kind===f__namespace.SyntaxKind.ExportKeyword)??false:false}function vn(e){return f__namespace.isIdentifier(e)?e.text:f__namespace.isParenthesizedExpression(e)?vn(e.expression):null}function sa(e){if(!e||!f__namespace.isObjectLiteralExpression(e))return [];let t=e.properties.find(r=>!f__namespace.isPropertyAssignment(r)||!f__namespace.isIdentifier(r.name)?false:r.name.text==="cronTrigger");if(!t||!f__namespace.isPropertyAssignment(t))return [];let n=t.initializer;return f__namespace.isStringLiteral(n)||f__namespace.isNoSubstitutionTemplateLiteral(n)?[n.text.trim()].filter(r=>r.length>0):f__namespace.isArrayLiteralExpression(n)?n.elements.map(r=>f__namespace.isStringLiteral(r)||f__namespace.isNoSubstitutionTemplateLiteral(r)?r.text.trim():"").filter(r=>r.length>0):[]}function la(e,t){let n=f__namespace.createSourceFile(t,e,f__namespace.ScriptTarget.Latest,true,f__namespace.ScriptKind.TS),r=[];for(let a of n.statements)if(ia(a))for(let o of a.declarationList.declarations){if(!f__namespace.isIdentifier(o.name)||!o.initializer||!f__namespace.isCallExpression(o.initializer))continue;let i=vn(o.initializer.expression);i!=="query"&&i!=="mutation"&&i!=="scheduler"&&i!=="cron"&&i!=="storageManager"||r.push({exportName:o.name.text,kind:i==="storageManager"?"storage":i,cronTriggers:i==="cron"?sa(o.initializer.arguments[0]):[],args:i==="query"||i==="mutation"?oa(o.initializer.arguments[0]):[]});}return r}function bn(e,t,n){let r=t.replace(/\\/g,"/"),o=ue(r).split("/").filter(Boolean);return `/${[e,...o,n].filter(Boolean).map(s=>s.trim()).filter(s=>s.length>0).join("/")}`}function ua(e,t){let n=e.replace(/\\/g,"/"),r=`${t}/`,a=n.indexOf(r);return a>=0?n.slice(a+r.length):n===t?"index.ts":n}function wn(e,t){let n=e.replace(/\\/g,"/"),a=ue(n).split("/").filter(Boolean),o=a[a.length-1]??"index";return [a.length>1?a[a.length-2]:"root",o,t].map(s=>s.trim()).filter(s=>s.length>0).join("/")}async function Rn(e){let t=[],n=await Tn(e.scanDirAbs).catch(()=>[]);for(let a of n){let o=Bun.file(a);if(!await o.exists())continue;let i=await o.text(),s=path.relative(e.scanDirAbs,a),c=la(i,a),l=[{kind:"query",kindDirectory:"queries",exports:c.filter(u=>u.kind==="query")},{kind:"mutation",kindDirectory:"mutations",exports:c.filter(u=>u.kind==="mutation")},{kind:"scheduler",kindDirectory:"schedulers",exports:c.filter(u=>u.kind==="scheduler")},{kind:"cron",kindDirectory:"crons",exports:c.filter(u=>u.kind==="cron")},{kind:"storage",kindDirectory:"queries",exports:c.filter(u=>u.kind==="storage")}];for(let u of l){if(u.exports.length===0)continue;let p=ua(s,u.kindDirectory);for(let d of u.exports){let y=u.kind==="query"||u.kind==="mutation"?wn(p,d.exportName):void 0,v=u.kind==="scheduler"||u.kind==="cron"?wn(p,d.exportName):void 0,A=u.kind==="query"||u.kind==="mutation"?[...ue(p).split("/").filter(Boolean),d.exportName]:void 0,w=u.kind==="query"?bn("queries",p,d.exportName):u.kind==="mutation"?bn("mutations",p,d.exportName):u.kind==="storage"?`/storage/managers/${d.exportName}`:`/${u.kindDirectory}/${v}`;t.push({kind:u.kind,exportName:d.exportName,filePath:a,importPath:F(e.outDirAbs,a),clientImportPath:F(path.resolve(e.outDirAbs,"client"),a),routePath:w,handlerName:y,clientSegments:A,taskName:v,cronTriggers:d.cronTriggers,args:d.args});}}}t.sort((a,o)=>a.routePath.localeCompare(o.routePath));let r=new Map;for(let a of t){let o=a.taskName?`task:${a.taskName}`:`route:${a.routePath}`,i=r.get(o);if(i)throw new Error(`Duplicate handler operation discovered: ${a.taskName??a.routePath} (${i} and ${a.filePath}#${a.exportName}).`);r.set(o,`${a.filePath}#${a.exportName}`);}return t}function da(e){let t=[],n="",r=0,a=0,o=0,i=false,s=false,c=false,l=false;for(let p=0;p<e.length;p+=1){let d=e[p];if(l){n+=d,l=false;continue}if(d==="\\"){n+=d,l=true;continue}if(!s&&!c&&d==="'"){i=!i,n+=d;continue}if(!i&&!c&&d==='"'){s=!s,n+=d;continue}if(!i&&!s&&d==="`"){c=!c,n+=d;continue}if(i||s||c){n+=d;continue}if(d==="("?r+=1:d===")"?r-=1:d==="{"?a+=1:d==="}"?a-=1:d==="["?o+=1:d==="]"&&(o-=1),d===","&&r===0&&a===0&&o===0){let y=n.trim();y.length>0&&t.push(y),n="";continue}n+=d;}let u=n.trim();return u.length>0&&t.push(u),t}function pa(e){let t=0,n=0,r=0,a=false,o=false,i=false,s=false;for(let c=0;c<e.length;c+=1){let l=e[c];if(s){s=false;continue}if(l==="\\"){s=true;continue}if(!o&&!i&&l==="'"){a=!a;continue}if(!a&&!i&&l==='"'){o=!o;continue}if(!a&&!o&&l==="`"){i=!i;continue}if(!(a||o||i)){if(l==="("){t+=1;continue}if(l===")"){t-=1;continue}if(l==="{"){n+=1;continue}if(l==="}"){n-=1;continue}if(l==="["){r+=1;continue}if(l==="]"){r-=1;continue}if(l===":"&&t===0&&n===0&&r===0)return c}}return -1}function ma(e){let t=e.toLowerCase();return /mode\s*:\s*["'`](timestamp|timestamp_ms)["'`]/.test(t)?"date":/mode\s*:\s*["'`]boolean["'`]/.test(t)?"boolean":/\.(date|datetime|timestamp)\s*\(/.test(t)?"date":/\.(int|integer|real|numeric|decimal|float|double)\s*\(/.test(t)?"number":/\.(text|varchar|char)\s*\(/.test(t)?"string":/\.(boolean|bool)\s*\(/.test(t)?"boolean":"unknown"}function ga(e){let t=da(e),n=[];for(let r of t){let a=pa(r);if(a===-1)continue;let o=r.slice(0,a).trim().replace(/^['"]|['"]$/g,"");if(!o)continue;let i=r.slice(a+1).trim(),s=/\.primarykey\s*\(/i.test(i),c=/autoincrement\s*:\s*true/i.test(i),l=/\.default\s*\(/i.test(i),p=!/\.notnull\s*\(/i.test(i)||l||c||s;n.push({name:o,expression:i,type:ma(i),optional:p,primaryKey:s,autoIncrement:c});}return n}function fa(e){let t=/export\s+const\s+(\w+)\s*=\s*table\s*\(\s*["'`]([^"'`]+)["'`]/g,n=[],r=(o,i)=>{let s=0,c=false,l=false,u=false,p=false;for(let d=i;d<o.length;d+=1){let y=o[d];if(p){p=false;continue}if(y==="\\"){p=true;continue}if(!l&&!u&&y==="'"){c=!c;continue}if(!c&&!u&&y==='"'){l=!l;continue}if(!c&&!l&&y==="`"){u=!u;continue}if(!(c||l||u)){if(y==="{"){s+=1;continue}if(y==="}"&&(s-=1,s===0))return d}}return -1},a=t.exec(e);for(;a;){let o=a[1],i=a[2],s=e.indexOf("{",t.lastIndex);if(s===-1){a=t.exec(e);continue}let c=r(e,s);if(c===-1){a=t.exec(e);continue}let l=e.slice(s+1,c);n.push({exportName:o,tableName:i,columns:ga(l)}),a=t.exec(e);}return n}async function Sn(e){let t=await promises.readdir(e,{withFileTypes:true}),n=[];for(let r of t){if(r.name.startsWith(".")||r.name==="node_modules"||r.name==="_generated")continue;let a=path.resolve(e,r.name);if(r.isDirectory()){n.push(...await Sn(a));continue}r.isFile()&&r.name==="schema.ts"&&n.push(a);}return n}async function An(e,t=[]){let n=await Sn(e.scanDirAbs).catch(()=>[]),r=path.resolve(e.configDir,"schema.ts"),a=[...t,...n.length?n:[r]];for(let o of a){let i=Bun.file(o);if(!await i.exists())continue;let s=await i.text(),c=fa(s);if(c.length>0)return {schemaPath:o,tables:c}}throw new Error(`Unable to discover schema.ts under scanDir (${e.scanDirAbs}) or fallback (${r}).`)}function ba(e){let t=e.auth.options.plugins;if(!Array.isArray(t))return [];for(let n of t)if(n&&typeof n=="object"){let r=n;if(r.id==="admin"&&"options"in r){let a=r.options;if(a&&typeof a=="object"&&"roles"in a){let o=a.roles;if(o&&typeof o=="object")return Object.keys(o)}}}return []}function wa(e,t){let n=path.relative(e,t).replace(/\\/g,"/");return n.startsWith(".")?n:`./${n}`}async function Cn(e){let{outDirAbs:t,wranglerOutDirAbs:n,config:r,configPath:a,configDir:o}=e,i=F(t,a),s=path.resolve(t,"client"),c=F(s,a),l=performance.now();await Promise.all([promises.mkdir(t,{recursive:true}),promises.mkdir(s,{recursive:true}),promises.mkdir(n,{recursive:true})]),process.stdout.write(`\u{1F4C1} Directories (${(performance.now()-l).toFixed(0)}ms)
|
|
9251
|
+
`);let u=path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)))),p=path.join(path.dirname(Bun.resolveSync("typescript/package.json",u)),"bin","tsc"),d=path.join(path.dirname(Bun.resolveSync("@better-auth/cli/package.json",u)),"dist","index.mjs"),y=path.resolve(t,"server.ts"),v=path.resolve(t,"client.ts"),A=path.resolve(t,"auth.config.ts"),w=path.resolve(t,"auth.schema.ts"),I=path.resolve(t,"drizzle.config.ts"),b=path.resolve(n,"wrangler.json"),h=await yn(e),T=await An(e,h?[h.schemaPath]:[]),C=F(t,T.schemaPath),R=await Rn(e);process.stdout.write(`\u{1F50D} ${R.length} handler(s) (${(performance.now()-l).toFixed(0)}ms)
|
|
9252
|
+
`);let D=ba(r),V=xt(r.auth.basePath,r.database[0].binding,r.kv[0]?.binding,r.scheduler.binding,r.r2[0]?.binding,r.realtime.binding,r.realtime.objectName,r.realtime.subscribePath,r.realtime.websocketPath,r.realtime.protocol),me=Se(c,R),Vn=Z(C,R,r.r2[0]?.binding,D),Bn=be(i),Wn=h?[wa(o,h.schemaPath),...r.schema.filter(x=>!/(^|\/)schema\.ts$/.test(x))]:r.schema,Hn=Ae(Wn),Ln=vt(e,R),zn=cn(C,T,R),Un=path.resolve(t,"admin.routes.ts"),Qn=Vn.map(x=>Bun.write(path.resolve(t,x.relativePath),x.source)),_n=me.map(x=>Bun.write(path.resolve(t,x.relativePath),x.source));await Promise.all([Bun.write(y,V),Bun.write(v,`export * from "./client/index";
|
|
9253
|
+
`),..._n,...Qn,Bun.write(A,Bn),Bun.write(w,""),Bun.write(I,Hn),Bun.write(b,`${JSON.stringify(Ln,null,2)}
|
|
9254
|
+
`),Bun.write(Un,zn)]),process.stdout.write(`\u{1F4DD} Source artifacts (${(performance.now()-l).toFixed(0)}ms)
|
|
9255
|
+
`);let _=path.relative(o,A).replace(/\\/g,"/"),Kn=_.startsWith(".")?_:`./${_}`,K=path.relative(o,w).replace(/\\/g,"/"),Jn=K.startsWith(".")?K:`./${K}`,ge=await Bun.spawn([process.execPath,d,"generate","--config",Kn,"--output",Jn,"--yes"],{cwd:o,stdout:"inherit",stderr:"inherit"}).exited;if(ge!==0)throw new Error(`better-auth generation failed with exit code ${ge}`);process.stdout.write(`\u{1F510} Auth schema (${(performance.now()-l).toFixed(0)}ms)
|
|
9256
|
+
`);function Gn(x={}){return {compilerOptions:{lib:["es2024"],types:["@types/node","@cloudflare/workers-types/2023-07-01"],module:"es2022",target:"es2024",moduleResolution:"bundler",strictNullChecks:true,skipLibCheck:true,declaration:true,emitDeclarationOnly:true,noCheck:true,jsx:"react-jsx",paths:{"_generated/*":["./*"]},...x},include:["./*.ts","./client/*.ts"]}}let fe=path.resolve(t,"tsconfig.js.json");await Promise.all([Bun.write(fe,`${JSON.stringify(Gn({declaration:true,emitDeclarationOnly:false}),null,2)}
|
|
9257
|
+
`)]),process.stdout.write(`\u{1F4C4} TS configs (${(performance.now()-l).toFixed(0)}ms)
|
|
9258
|
+
`);async function Zn(x,$){process.stdout.write(`\u2699\uFE0F ${$}... (${(performance.now()-l).toFixed(0)}ms)
|
|
9259
|
+
`);let N=await Bun.spawn([process.execPath,p,"-p",x],{cwd:t,stdout:"inherit",stderr:"inherit"}).exited;if(N!==0)throw new Error(`tsc ${$} failed with exit code ${N}`)}await Zn(fe,"JS files"),process.stdout.write(`\u2705 JS + declarations (${(performance.now()-l).toFixed(0)}ms)
|
|
9260
|
+
`);async function he(x){let $=await promises.readdir(x,{withFileTypes:true});for(let k of $){let N=path.join(x,k.name);if(k.isDirectory()){if(k.name==="node_modules"||k.name.startsWith("."))continue;await he(N);}else k.name.endsWith(".ts")&&!k.name.endsWith(".d.ts")&&await promises.rm(N);}}await he(t),process.stdout.write(`\u{1F4E6} Cleaned .ts files (${(performance.now()-l).toFixed(0)}ms)
|
|
9261
|
+
`);let Xn=t+"/";async function ye(x){let $=await promises.readdir(x,{withFileTypes:true});for(let k of $){let N=path.join(x,k.name);if(k.isDirectory()){if(k.name==="node_modules"||k.name.startsWith(".")||N===t)continue;await ye(N);}else !N.startsWith(Xn)&&(k.name.endsWith(".d.ts")||k.name.endsWith(".js"))&&await promises.rm(N);}}await ye(o),process.stdout.write(`\u{1F9F9} Cleanup (${(performance.now()-l).toFixed(0)}ms)
|
|
9262
|
+
`);let Yn=path.resolve(o,"tsconfig.json");if(e.config.build&&fs.existsSync(Yn)){process.stdout.write(`\u2699\uFE0F Building project... (${(performance.now()-l).toFixed(0)}ms)
|
|
9263
|
+
`);let $=await Bun.spawn([process.execPath,p,"--build"],{cwd:o,stdout:"inherit",stderr:"inherit"}).exited;if($!==0)throw new Error(`TypeScript build failed with exit code ${$}`);process.stdout.write(`\u2705 Build complete (${(performance.now()-l).toFixed(0)}ms)
|
|
9264
|
+
`);}}var qn=zod.z.object({binding:zod.z.string().min(1),databaseName:zod.z.string().min(1),databaseId:zod.z.string().min(1),previewDatabaseId:zod.z.string().min(1).optional(),migrationsDir:zod.z.string().min(1).optional()}).strict(),Mn=zod.z.object({binding:zod.z.string().min(1),id:zod.z.string().min(1),previewId:zod.z.string().min(1).optional()}).strict(),Fn=zod.z.object({binding:zod.z.string().min(1),bucketName:zod.z.string().min(1),previewBucketName:zod.z.string().min(1).optional(),jurisdiction:zod.z.string().min(1).optional()}).strict(),In=zod.z.object({enabled:zod.z.boolean().optional(),binding:zod.z.string().min(1).optional(),queue:zod.z.string().min(1).optional()}).strict(),Ra=zod.z.object({enabled:zod.z.boolean().optional(),binding:zod.z.string().min(1).optional(),className:zod.z.string().min(1).optional(),objectName:zod.z.string().min(1).optional(),subscribePath:zod.z.string().min(1).optional(),websocketPath:zod.z.string().min(1).optional(),protocol:zod.z.string().min(1).optional()}).strict(),ka=zod.z.object({scanDir:zod.z.string().min(1),outDir:zod.z.string().min(1),wranglerOutDir:zod.z.string().min(1).optional(),wranglerOutPath:zod.z.string().min(1).optional(),schema:zod.z.array(zod.z.string()).min(1),schemaDsl:zod.z.object({entry:zod.z.string().min(1),exportName:zod.z.string().min(1).optional(),outFile:zod.z.string().min(1).optional(),typesOutFile:zod.z.string().min(1).optional(),zodOutFile:zod.z.string().min(1).optional(),namingStrategy:zod.z.literal("camelToSnake").optional()}).strict().optional(),database:zod.z.union([qn,zod.z.array(qn).min(1)]),kv:zod.z.union([Mn,zod.z.array(Mn)]).optional(),r2:zod.z.union([Fn,zod.z.array(Fn)]).optional(),auth:zod.z.object({enabled:zod.z.boolean(),basePath:zod.z.string().min(1),options:zod.z.custom(e=>typeof e=="object"&&e!==null),clientOptions:zod.z.custom(e=>typeof e=="object"&&e!==null)}).strict(),scheduler:In.optional(),realtime:Ra.optional(),wranglerOverrides:zod.z.record(zod.z.string(),zod.z.unknown()).optional(),build:zod.z.boolean().optional()}).strict();function Pn(e){return typeof e=="object"&&e!==null}function Sa(e){let t=Pn(e.wranglerOverrides)?e.wranglerOverrides.scheduler:void 0,n=In.safeParse(t);return n.success?n.data:{}}function Aa(e){if(!Pn(e)||!("scheduler"in e))return e;let{scheduler:t,...n}=e;return n}function Na(e){let n={...Sa(e)??{},...e.scheduler??{}},r=e.realtime??{};return {...e,database:Array.isArray(e.database)?e.database:[e.database],kv:e.kv?Array.isArray(e.kv)?e.kv:[e.kv]:[],r2:e.r2?Array.isArray(e.r2)?e.r2:[e.r2]:[],scheduler:{enabled:n.enabled??true,binding:n.binding??"APPFLARE_SCHEDULER_QUEUE",queue:n.queue},realtime:{enabled:r.enabled??true,binding:r.binding??"APPFLARE_REALTIME",className:r.className??"AppflareRealtimeDurableObject",objectName:r.objectName??"global",subscribePath:r.subscribePath??"/realtime/subscribe",websocketPath:r.websocketPath??"/realtime/ws",protocol:r.protocol??"appflare.realtime.v1"},wranglerOverrides:Aa(e.wranglerOverrides),wranglerOutDir:e.wranglerOutDir??e.wranglerOutPath??e.outDir,build:e.build??true}}async function j(e){let t=path.isAbsolute(e??"")?e:path.resolve(process.cwd(),e??"appflare.config.ts"),n=path.dirname(t),o=(await import(url.pathToFileURL(t).href)).default,i=ka.parse(o),s=Na(i);return {configPath:t,configDir:n,scanDirAbs:path.resolve(n,s.scanDir),outDirAbs:path.resolve(n,s.outDir),wranglerOutDirAbs:path.resolve(n,s.wranglerOutDir),config:s}}function Ma(e){let t=e;for(;;){if(fs.existsSync(path.resolve(t,"package.json")))return t;let n=path.dirname(t);if(n===t)return e;t=n;}}async function Q(e,t={}){let n=await j(e);if(t.build!==void 0&&(n.config.build=t.build),await Cn(n),n.wranglerOutDirAbs===n.outDirAbs){process.stdout.write(`\u2705 Generated artifacts in ${n.outDirAbs}
|
|
8984
9265
|
`);return}process.stdout.write(`\u2705 Generated server/client in ${n.outDirAbs} and wrangler.json in ${n.wranglerOutDirAbs}
|
|
8985
|
-
`);}async function
|
|
8986
|
-
`);}finally{r=false,a&&(a=false,await o());}}
|
|
9266
|
+
`);}async function On(e,t={}){if(await Q(e,{build:t.build}),!t.watch)return;let n=await j(e),r=false,a=false,o=async()=>{if(r){a=true;return}r=true;try{await Q(e,{build:t.build});}catch(s){process.stderr.write(`\u274C Build failed: ${s.message}
|
|
9267
|
+
`);}finally{r=false,a&&(a=false,await o());}};$a__default.default.watch(n.scanDirAbs,{ignoreInitial:true,ignored:s=>s.includes("/_generated/")||s.includes("/dist/")||s.includes("/out/")||s.includes("/node_modules/")||s.endsWith(".d.ts")}).on("all",async(s,c)=>{process.stdout.write(`\u{1F504} Change detected: ${c}
|
|
8987
9268
|
`),await o();}),process.stdout.write(`\u{1F440} Watching ${n.scanDirAbs}
|
|
8988
|
-
`);}async function
|
|
9269
|
+
`);}async function jn(e,t={}){let n=await j(e),r=Ma(process.cwd());if([!!t.local,!!t.remote,!!t.preview].filter(Boolean).length>1)throw new Error("Only one of --local, --remote, or --preview can be set.");let o=path.resolve(n.outDirAbs,"drizzle.config.js"),i=process.platform==="win32"?"npx.cmd":"npx",c=await Bun.spawn([i,"drizzle-kit","generate","--config",o],{cwd:r,stdin:"inherit",stdout:"inherit",stderr:"inherit"}).exited;if(c!==0)throw new Error(`drizzle-kit generate failed with exit code ${c}`);let l=n.config.database[0].databaseName,u=[i,"wrangler","d1","migrations","apply",l];t.local?u.push("--local"):t.remote?u.push("--remote"):t.preview&&u.push("--preview");let d=await Bun.spawn(u,{cwd:n.configDir,stdin:"inherit",stdout:"inherit",stderr:"inherit"}).exited;if(d!==0)throw new Error(`wrangler d1 migrations apply failed with exit code ${d}`)}async function Dn(e,t={name:"",email:"",password:""}){let n=await j(e);if([!!t.local,!!t.remote].filter(Boolean).length>1)throw new Error("Only one of --local or --remote can be set.");let{hashPassword:a}=await import('better-auth/crypto'),o=await a(t.password),i=crypto.randomUUID(),s=crypto.randomUUID(),c=Date.now(),l=t.name.replace(/'/g,"''"),u=t.email.replace(/'/g,"''"),p=["INSERT INTO users (id, name, email, email_verified, created_at, updated_at, role, banned)",`VALUES ('${i}', '${l}', '${u}', 1, ${c}, ${c}, 'admin', 0);`,"INSERT INTO accounts (id, account_id, provider_id, user_id, password, created_at, updated_at)",`VALUES ('${s}', '${u}', 'credential', '${i}', '${o}', ${c}, ${c});`].join(" "),d=n.config.database[0].databaseName,v=[process.platform==="win32"?"npx.cmd":"npx","wrangler","d1","execute",d,`--command=${p}`];t.local?v.push("--local"):t.remote&&v.push("--remote");let w=await Bun.spawn(v,{cwd:n.configDir,stdin:"inherit",stdout:"inherit",stderr:"inherit"}).exited;if(w!==0)throw new Error(`Failed to add admin user. wrangler d1 execute exited with code ${w}`);console.log("\u2705 Admin user "+t.email+" created successfully!");}var P=new commander.Command;P.name("appflare").description("Appflare compiler/bundler for Cloudflare-native backends and SDK generation").version("0.0.28");P.command("build").description("Generate server.ts, client.ts, auth.config.js, drizzle.config.js, and wrangler.json artifacts").option("-c, --config <path>","Path to appflare.config.ts","appflare.config.ts").option("--no-build","Skip TypeScript build step").action(async e=>{await Q(e.config,{build:e.build});});P.command("dev").description("Run generator in development mode").option("-c, --config <path>","Path to appflare.config.ts","appflare.config.ts").option("-w, --watch","Watch scanDir and regenerate on changes",false).option("--no-build","Skip TypeScript build step").action(async e=>{await On(e.config,{watch:e.watch,build:e.build});});P.command("migrate").description("Generate drizzle migration files from outDir/auth.schema.ts and apply them to the configured D1 database").option("-c, --config <path>","Path to appflare.config.ts","appflare.config.ts").option("--local","Execute commands/files against a local DB for use with wrangler dev",false).option("--remote","Execute commands/files against a remote DB for use with wrangler dev --remote",false).option("--preview","Execute commands/files against a preview D1 DB",false).action(async e=>{await jn(e.config,{local:e.local,remote:e.remote,preview:e.preview});});P.command("add-admin").description("Add an admin user to the database").requiredOption("-n, --name <name>","Admin's display name").requiredOption("-e, --email <email>","Admin's email address").requiredOption("-p, --password <password>","Admin's password").option("-c, --config <path>","Path to appflare.config.ts","appflare.config.ts").option("--local","Execute command against a local DB for use with wrangler dev",false).option("--remote","Execute command against a remote DB for use with wrangler dev --remote",false).action(async e=>{await Dn(e.config,{name:e.name,email:e.email,password:e.password,local:e.local,remote:e.remote});});(async()=>{process.versions.bun||(console.error("Appflare CLI must be run with Bun."),process.exit(1)),await P.parseAsync(process.argv);})().catch(e=>{console.error(e),process.exit(1);});
|