Files
devos/prisma/seed.ts
T
phptits 0d6cd37579 DevOS v1 + Phase 2: unified resource model, infra ops, website deployment, AI chat ops
- Monorepo: NestJS API (Fastify) + React web (antd/ProComponents) + shared packages
- PostgreSQL/Prisma (unified resources, relations graph, audit, events, jobs)
- Auth/RBAC/workspace isolation, credential vault (AES-256-GCM)
- Servers/SSH, Docker discovery & lifecycle, Job Center
- Websites & deployments (git provider credential clone, env, health verification)
- Domains/DNS (Cloudflare adapter), certificates (TLS probe), monitoring
- Automation (cron/event/webhook HMAC + notifications), AI Gateway, MCP (29 tools)
- AI chat window (LLM function-calling + rules fallback, SSE streaming, approvals)
- Notes: Vditor WYSIWYG markdown editor, non-blocking autosave
- Tests: 35 unit/security tests; UI audit clean (desktop+mobile, deep links)
2026-09-10 06:35:55 +00:00

58 lines
1.8 KiB
TypeScript

/**
* # 种子脚本
* 策略:幂等。按 .env 中 SEED_ADMIN_EMAIL/SEED_ADMIN_PASSWORD 创建管理员用户、
* 默认工作区与 OWNER 成员关系;重复执行不会产生重复数据。
*/
import { PrismaClient } from "@prisma/client";
import { hashPassword } from "../packages/auth/src/index";
/** # 全局 Prisma 客户端(种子专用) **/
const prisma = new PrismaClient();
/** # 入口:创建管理员与默认工作区 */
async function main() {
const email = process.env.SEED_ADMIN_EMAIL ?? "admin@devos.local";
const password = process.env.SEED_ADMIN_PASSWORD ?? "Devos12345!";
let user = await prisma.user.findUnique({ where: { email } });
if (!user) {
user = await prisma.user.create({
data: {
email,
name: "Admin",
passwordHash: await hashPassword(password),
},
});
console.log(`[seed] created admin user ${email}`);
} else {
console.log(`[seed] admin user ${email} already exists`);
}
const slug = "main";
let workspace = await prisma.workspace.findUnique({ where: { slug } });
if (!workspace) {
workspace = await prisma.workspace.create({
data: { name: "Main Workspace", slug },
});
console.log(`[seed] created workspace ${workspace.name}`);
}
const membership = await prisma.workspaceMember.findUnique({
where: { workspaceId_userId: { workspaceId: workspace.id, userId: user.id } },
});
if (!membership) {
await prisma.workspaceMember.create({
data: { workspaceId: workspace.id, userId: user.id, role: "OWNER" },
});
console.log(`[seed] added admin as OWNER of workspace`);
}
console.log(`[seed] done. workspaceId=${workspace.id}`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => prisma.$disconnect());