t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/AWS/Keyspaces/restore-handler.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

68 lines
2.3 KiB
TypeScript

import * as Keyspaces from "@/AWS/Keyspaces";
import * as Lambda from "@/AWS/Lambda";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import path from "pathe";
const main = path.resolve(import.meta.dirname, "restore-handler.ts");
export class KeyspacesRestoreTestFunction extends Lambda.Function<Lambda.Function>()(
"KeyspacesRestoreTestFunction",
) {}
export default KeyspacesRestoreTestFunction.make(
{
main,
functionUrl: true,
timeout: Duration.seconds(60),
},
Effect.gen(function* () {
const keyspace = yield* Keyspaces.Keyspace("RestoreKs", {
keyspaceName: "alchemy_restore_test_ks",
});
const table = yield* Keyspaces.Table("Orders", {
keyspaceName: keyspace.keyspaceName,
tableName: "orders",
columns: [
{ name: "id", type: "uuid" },
{ name: "total", type: "int" },
],
partitionKeys: ["id"],
pointInTimeRecovery: true,
});
const restore = yield* Keyspaces.RestoreTable(table, keyspace);
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const pathname = new URL(request.originalUrl).pathname;
// Restore the source table's continuous backup (current time) into a
// new table of the same keyspace. RestoreTable is asynchronous — the
// response carries the new table's ARN while it is RESTORING; the
// test deletes it out-of-band.
if (request.method === "POST" && pathname === "/restore") {
const result = yield* restore({
targetTableName: "orders_restored",
}).pipe(
Effect.map((response) => ({
restoredTableARN: response.restoredTableARN,
})),
Effect.catch((error) =>
Effect.succeed({ error: error._tag } as const),
),
);
return yield* HttpServerResponse.json(result);
}
return yield* HttpServerResponse.json(
{ error: "Not found", method: request.method, pathname },
{ status: 404 },
);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(Keyspaces.RestoreTableHttp)),
);