The best way to control Claude Code, Codex and more from any computer or phone

main
ts 64 lines 1.87 KB
Raw
1 import type { Credentials } from "@distilled.cloud/cloudflare/Credentials";
2 import * as d1 from "@distilled.cloud/cloudflare/d1";
3 import * as Effect from "effect/Effect";
4 import type * as HttpClient from "effect/http/HttpClient";
5
6 export interface ExportD1DatabaseOptions {
7 accountId: string;
8 databaseId: string;
9 dumpOptions?: {
10 tables?: string[];
11 noSchema?: boolean;
12 noData?: boolean;
13 };
14 }
15
16 export interface ExportD1DatabaseResult {
17 filename: string;
18 signedUrl: string;
19 }
20
21 /**
22 * Initiates an export of a Cloudflare D1 database and returns a signed
23 * download URL. Recursively polls with the bookmark until the export
24 * completes or fails.
25 */
26 export const exportDatabase = (
27 options: ExportD1DatabaseOptions,
28 ): Effect.Effect<
29 ExportD1DatabaseResult,
30 d1.ExportDatabaseError,
31 Credentials | HttpClient.HttpClient
32 > =>
33 Effect.gen(function* () {
34 const exportDb = yield* d1.exportDatabase;
35
36 const poll = (
37 currentBookmark?: string,
38 ): Effect.Effect<ExportD1DatabaseResult, d1.ExportDatabaseError, never> =>
39 Effect.gen(function* () {
40 const data = yield* exportDb({
41 accountId: options.accountId,
42 databaseId: options.databaseId,
43 outputFormat: "polling",
44 currentBookmark,
45 dumpOptions: options.dumpOptions,
46 });
47
48 if (data.status === "complete" && data.result) {
49 if (!data.result.filename || !data.result.signedUrl) {
50 return yield* Effect.die("D1 export completed but missing filename/signedUrl");
51 }
52 return {
53 filename: data.result.filename,
54 signedUrl: data.result.signedUrl,
55 };
56 }
57 if (data.status === "error") {
58 return yield* Effect.die(data.error ?? "Error during D1 export");
59 }
60 return yield* poll(data.atBookmark ?? undefined);
61 });
62
63 return yield* poll();
64 });