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

main
ts 40 lines 1.35 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as HttpClient from "effect/http/HttpClient";
3 import * as HttpClientRequest from "effect/http/HttpClientRequest";
4 import { exportDatabase } from "./ExportDatabase.ts";
5 import { importD1Database } from "./ImportDatabase.ts";
6
7 export interface CloneDatabaseOptions {
8 accountId: string;
9 sourceDatabaseId: string;
10 targetDatabaseId: string;
11 }
12
13 /**
14 * Clone a D1 database by exporting from the source and importing into the
15 * target. Fetches the SQL dump from the export's signed URL and streams the
16 * payload through the import flow.
17 */
18 export const cloneDatabase = (options: CloneDatabaseOptions) =>
19 Effect.gen(function* () {
20 const exportResult = yield* exportDatabase({
21 accountId: options.accountId,
22 databaseId: options.sourceDatabaseId,
23 });
24
25 const client = yield* HttpClient.HttpClient;
26 const dumpRes = yield* client
27 .execute(HttpClientRequest.get(exportResult.signedUrl))
28 .pipe(Effect.orDie);
29 if (dumpRes.status < 200 || dumpRes.status >= 300) {
30 return yield* Effect.die(`Failed to fetch D1 export dump (${dumpRes.status})`);
31 }
32 const sqlData = yield* dumpRes.text.pipe(Effect.orDie);
33
34 return yield* importD1Database({
35 accountId: options.accountId,
36 databaseId: options.targetDatabaseId,
37 sqlData,
38 filename: exportResult.filename,
39 });
40 });