caelum0x 261ea5dea7 feat: scribase-migrate — one-command migration from Supabase/Neon/Clerk/Resend onto Scribase
Composes Scribase's own shipped APIs (the `scribase` SDK's imports/migrations/
database/environments resources, plus the GoTrue admin API every self-hosted
Supabase migration tool already uses) rather than reinventing a dump format:

- supabase: wraps the real POST /v1/imports one-call migration (Postgres,
  auth.users with hashes, storage).
- neon: pg_dump schema+data as plain SQL, submitted through POST .../migrations
  (environments.connection() is internal-network-only — verified live — so
  this is the only path that actually reaches a Scribase env from outside);
  auto-generates owner-based RLS so the real migration lint's BLOCK on
  RLS-less public tables is satisfied without ever opening a table to every
  user; branches land in branch_<name> schemas.
- clerk: exports users via the real Backend API, creates them via the
  destination's GoTrue admin API with a one-time password + reset flag
  (Clerk never exposes a portable hash on any plan).
- resend: exports sending domains + broadcasts (as templates) to a portable
  JSON bundle; honest about Scribase having no write endpoint for either yet.

Verified live end-to-end against a real local Supabase-shaped Postgres
(Docker, GoTrue-shaped auth.users with a real bcrypt hash) and a real
Scribase project (ereboslab/scribase-migrate-verify-internal): schema+RLS
applied with 0 lint blocks, data row counts matched via a real HTTP query
(2/2 profiles, 3/3 notes), and a real password-grant login returned a real
access_token for the migrated user. Full artifacts in
test-fixtures/verification-run/.

docs/migrate-page.md is a ready-to-paste scribase.com/migrate page.
2026-11-17 14:54:36 +03:00

64 lines
2.7 KiB
JavaScript

/**
* Resend -> Scribase, best-effort. Resend's public API gives read access to
* sending domains (with their DNS records) and broadcasts; it has no
* "templates" resource distinct from a broadcast's HTML body, so a
* broadcast's content is exported as the portable "template." The
* `scribase` SDK (checked against `edge-repos/scribase-cli` 0.3.1 at the
* time this was written) exposes no write endpoint yet for sending-domain
* or template import, so this module exports a complete, re-importable
* JSON bundle and reports that gap honestly instead of silently no-op'ing.
*/
const RESEND_API = 'https://api.resend.com';
export async function exportResend({ apiKey, fetchImpl = fetch, onLog }) {
const domains = await listAll(`${RESEND_API}/domains`, apiKey, fetchImpl);
onLog?.(`exported ${domains.length} domains`);
const domainsWithRecords = [];
for (const domain of domains) {
const response = await fetchImpl(`${RESEND_API}/domains/${domain.id}`, {
headers: { authorization: `Bearer ${apiKey}` },
});
domainsWithRecords.push(response.ok ? await response.json() : domain);
}
const broadcasts = await listAll(`${RESEND_API}/broadcasts`, apiKey, fetchImpl);
onLog?.(`exported ${broadcasts.length} broadcasts as templates`);
const broadcastsWithBody = [];
for (const broadcast of broadcasts) {
const response = await fetchImpl(`${RESEND_API}/broadcasts/${broadcast.id}`, {
headers: { authorization: `Bearer ${apiKey}` },
});
broadcastsWithBody.push(response.ok ? await response.json() : broadcast);
}
return {
exportedAt: new Date().toISOString(),
domains: domainsWithRecords,
templates: broadcastsWithBody,
notes: [
'Domains: DNS records are listed for re-creation on Scribase Mail '
+ '(scribase.com/mail) — Scribase does not yet expose a sending-domain '
+ 'import API, so the operator adds each domain manually once in the '
+ 'console and re-publishes the same TXT/MX/DKIM records.',
'Templates: Resend has no first-class "template" object distinct from a '
+ 'broadcast; each broadcast\'s subject/html/text is exported as one '
+ 'template record. Scribase Mail has no template-import endpoint yet '
+ '(checked scribase SDK 0.3.1, 2026-11-17) — this file is the portable '
+ 'source of truth to paste in once that lands.',
],
};
}
async function listAll(url, apiKey, fetchImpl) {
const response = await fetchImpl(url, { headers: { authorization: `Bearer ${apiKey}` } });
if (!response.ok) {
const detail = await response.text().catch(() => '');
throw new Error(`Resend request to ${url} failed (${response.status}): ${detail.slice(0, 500)}`);
}
const body = await response.json();
return body.data ?? [];
}