Files
2026-08-07 11:37:46 +01:00

69 lines
1.9 KiB
TypeScript

import {
SCHEMA_PLAN,
type CollectionDef,
} from "@shared/pb/schema.ts";
import { PB_ENDPOINT, PB_EMAIL, PB_PASSWORD } from "../src/env.ts";
async function getSuperadminToken(): Promise<string> {
const res = await fetch(
`${PB_ENDPOINT}/api/collections/_superusers/auth-with-password`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ identity: PB_EMAIL, password: PB_PASSWORD }),
},
);
const data = await res.json();
if (!res.ok) throw new Error(`Auth failed: ${JSON.stringify(data)}`);
return data.token;
}
async function createCollection(
token: string,
col: CollectionDef,
): Promise<string | null> {
const existing = await fetch(
`${PB_ENDPOINT}/api/collections?filter=name='${col.name}'`,
{ headers: { Authorization: `Bearer ${token}` } },
);
const existingData = await existing.json();
if (existingData?.items?.length > 0) {
console.log(` ↳ Already exists: ${col.name}`);
return existingData.items[0].id;
}
const res = await fetch(`${PB_ENDPOINT}/api/collections`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(col),
});
const data = await res.json();
if (!res.ok)
throw new Error(`Create ${col.name} failed: ${JSON.stringify(data)}`);
console.log(` ✓ Created: ${col.name}`);
return data.id;
}
async function main() {
console.log("Connecting to PB at", PB_ENDPOINT);
const token = await getSuperadminToken();
console.log("Authenticated as superadmin\n");
const ids: Record<string, string> = {};
for (const entry of SCHEMA_PLAN) {
const id = await createCollection(token, entry.build(ids));
if (id) ids[entry.name] = id;
}
console.log("\n✅ All collections created successfully");
console.log("Collection IDs:", ids);
}
main().catch((err) => {
console.error("Seed failed:", err);
process.exit(1);
});