From fb14f4326449e41080d453a89638cd6ee38295f9 Mon Sep 17 00:00:00 2001 From: Matt Aitken Date: Tue, 19 Nov 2024 12:09:27 +0000 Subject: [PATCH] Initial experiments getting pglite working --- apps/webapp/app/pglite/index.ts | 10 +++++ apps/webapp/app/pglite/worker.ts | 76 ++++++++++++++++++++++++++++++++ apps/webapp/package.json | 1 + pnpm-lock.yaml | 7 +++ 4 files changed, 94 insertions(+) create mode 100644 apps/webapp/app/pglite/index.ts create mode 100644 apps/webapp/app/pglite/worker.ts diff --git a/apps/webapp/app/pglite/index.ts b/apps/webapp/app/pglite/index.ts new file mode 100644 index 000000000..0306d24ee --- /dev/null +++ b/apps/webapp/app/pglite/index.ts @@ -0,0 +1,10 @@ +import { PGliteWorker } from "@electric-sql/pglite/worker"; + +export async function pglite() { + return await PGliteWorker.create( + new Worker(new URL("./worker.js", import.meta.url), { + type: "module", + }), + {} + ); +} diff --git a/apps/webapp/app/pglite/worker.ts b/apps/webapp/app/pglite/worker.ts new file mode 100644 index 000000000..3607c00c1 --- /dev/null +++ b/apps/webapp/app/pglite/worker.ts @@ -0,0 +1,76 @@ +import { PGlite } from "@electric-sql/pglite"; +import { worker } from "@electric-sql/pglite/worker"; + +worker({ + async init(options) { + const db = new PGlite("idb://triggerdotdev", { + relaxedDurability: true, + ...options, + }); + + await db.exec(` + DROP TYPE IF EXISTS "public"."TaskRunStatus"; + CREATE TYPE "public"."TaskRunStatus" AS ENUM ('PENDING', 'EXECUTING', 'WAITING_TO_RESUME', 'RETRYING_AFTER_FAILURE', 'PAUSED', 'CANCELED', 'COMPLETED_SUCCESSFULLY', 'COMPLETED_WITH_ERRORS', 'INTERRUPTED', 'SYSTEM_FAILURE', 'CRASHED', 'WAITING_FOR_DEPLOY', 'DELAYED', 'EXPIRED', 'TIMED_OUT'); + + CREATE TABLE "public"."TaskRun" ( + "id" text NOT NULL, + "idempotencyKey" text, + "payload" text NOT NULL, + "payloadType" text NOT NULL DEFAULT 'application/json'::text, + "context" jsonb, + "runtimeEnvironmentId" text NOT NULL, + "projectId" text NOT NULL, + "createdAt" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" timestamp NOT NULL, + "taskIdentifier" text NOT NULL, + "lockedAt" timestamp, + "lockedById" text, + "friendlyId" text NOT NULL, + "lockedToVersionId" text, + "traceContext" jsonb, + "spanId" text NOT NULL, + "traceId" text NOT NULL, + "concurrencyKey" text, + "queue" text NOT NULL, + "number" int4 NOT NULL DEFAULT 0, + "isTest" bool NOT NULL DEFAULT false, + "status" "public"."TaskRunStatus" NOT NULL DEFAULT 'PENDING'::"TaskRunStatus", + "scheduleId" text, + "scheduleInstanceId" text, + "startedAt" timestamp, + "usageDurationMs" int4 NOT NULL DEFAULT 0, + "costInCents" float8 NOT NULL DEFAULT 0, + "baseCostInCents" float8 NOT NULL DEFAULT 0, + "machinePreset" text, + "delayUntil" timestamp, + "queuedAt" timestamp, + "expiredAt" timestamp, + "ttl" text, + "maxAttempts" int4, + "completedAt" timestamp, + "logsDeletedAt" timestamp, + "batchId" text, + "depth" int4 NOT NULL DEFAULT 0, + "parentTaskRunAttemptId" text, + "parentTaskRunId" text, + "resumeParentOnCompletion" bool NOT NULL DEFAULT false, + "rootTaskRunId" text, + "parentSpanId" text, + "metadata" text, + "metadataType" text NOT NULL DEFAULT 'application/json'::text, + "output" text, + "outputType" text NOT NULL DEFAULT 'application/json'::text, + "error" jsonb, + "seedMetadata" text, + "seedMetadataType" text NOT NULL DEFAULT 'application/json'::text, + "runTags" _text, + "maxDurationInSeconds" int4, + CONSTRAINT "TaskRun_parentTaskRunId_fkey" FOREIGN KEY ("parentTaskRunId") REFERENCES "public"."TaskRun"("id") ON DELETE SET NULL, + CONSTRAINT "TaskRun_rootTaskRunId_fkey" FOREIGN KEY ("rootTaskRunId") REFERENCES "public"."TaskRun"("id") ON DELETE SET NULL, + PRIMARY KEY ("id") + ); + `); + + return db; + }, +}); diff --git a/apps/webapp/package.json b/apps/webapp/package.json index b976bc09f..9a95070df 100644 --- a/apps/webapp/package.json +++ b/apps/webapp/package.json @@ -46,6 +46,7 @@ "@conform-to/react": "^0.6.1", "@conform-to/zod": "^0.6.1", "@depot/sdk-node": "^1.0.0", + "@electric-sql/pglite": "^0.2.13", "@electric-sql/react": "^0.3.5", "@headlessui/react": "^1.7.8", "@heroicons/react": "^2.0.12", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 337ffd1c4..8284e3b93 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -231,6 +231,9 @@ importers: '@depot/sdk-node': specifier: ^1.0.0 version: 1.0.0 + '@electric-sql/pglite': + specifier: ^0.2.13 + version: 0.2.13 '@electric-sql/react': specifier: ^0.3.5 version: 0.3.5(react@18.2.0) @@ -4809,6 +4812,10 @@ packages: '@rollup/rollup-darwin-arm64': 4.21.3 dev: false + /@electric-sql/pglite@0.2.13: + resolution: {integrity: sha512-YRY806NnScVqa21/1L1vaysSQ+0/cAva50z7vlwzaGiBOTS9JhdzIRHN0KfgMhobFAphbznZJ7urMso4RtMBIQ==} + dev: false + /@electric-sql/react@0.3.5(react@18.2.0): resolution: {integrity: sha512-qPrlF3BsRg5L8zAn1sLGzc3pkswfEHyQI3lNOu7Xllv1DBx85RvHR1zgGGPAUfC8iwyWupQu9pFPE63GdbeuhA==} peerDependencies: