Files
wajs-server/prisma/seed.ts
2026-02-05 16:33:43 +08:00

93 lines
2.6 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { prisma } from "@/server/lib/prisma";
export async function seed() {
console.log("🌱 Starting seeding...");
// 1. Seed User from environment variables or defaults
const adminEmail = process.env.ADMIN_EMAIL || "admin@example.com";
const adminPassword = process.env.ADMIN_PASSWORD || "admin123";
const user = await prisma.user.upsert({
where: { email: adminEmail },
update: {},
create: {
name: "Administrator",
email: adminEmail,
password: adminPassword,
},
});
console.log(`✅ User seeded: ${user.email}`);
// 2. Seed Default API Key
const defaultKey = "wajs_sk_live_default_key_2026";
await prisma.apiKey.upsert({
where: { key: defaultKey },
update: {},
create: {
name: "Default API Key",
key: defaultKey,
userId: user.id,
description: "Auto-generated default API key",
},
});
console.log("✅ Default API Key seeded");
// 3. Seed Sample Webhook
const webhookUrl = "https://webhook.site/wajs-test";
const existingWebhook = await prisma.webHook.findFirst({
where: { url: webhookUrl },
});
if (!existingWebhook) {
await prisma.webHook.create({
data: {
name: "Sample Webhook",
url: webhookUrl,
description: "Test webhook for capturing events",
enabled: true,
method: "POST",
},
});
console.log("✅ Sample Webhook seeded");
} else {
console.log(" Sample Webhook already exists, skipping");
}
// 4. Seed Initial ChatFlow
const flowUrl = "initial-flow";
await prisma.chatFlows.upsert({
where: { flowUrl: flowUrl },
update: {},
create: {
flowUrl: flowUrl,
flows: {
nodes: [
{
id: "1",
type: "input",
data: { label: "Start" },
position: { x: 250, y: 5 },
},
],
edges: [],
},
active: true,
defaultFlow: "Main Flow",
},
});
console.log("✅ Initial ChatFlow seeded");
console.log("✨ Seeding completed successfully!");
return { user, defaultKey, webhookUrl, flowUrl };
}
if (import.meta.main) {
seed()
.catch((e) => {
console.error("❌ Seeding failed:", e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
}