ec-nest/prisma/seed.ts
liuyun 13510ee216 feat(tenant): 租户 onboarding 模板 + 级联删除
- 抽取 seed 资源/字典到 tenant-template.ts,initTenant 幂等初始化租户
- 创建租户自动初始化管理员/根组织/角色/资源/字典
- 删除租户级联清理全部业务数据
- 修复 BaseCrudService 对 Tenant 模型的 tenantCode 大小写注入 bug

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 16:32:46 +08:00

43 lines
1.2 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { PLATFORM_ROLE_CODE, PLATFORM_TENANT_CODE } from '../src/common/constants.js';
import { initTenant } from '../src/common/tenant-template.js';
const prisma = new PrismaClient();
const TENANT = PLATFORM_TENANT_CODE;
const SUPER_ROLE = PLATFORM_ROLE_CODE;
const ADMIN_USERNAME = 'touyun';
const ADMIN_PASSWORD = 'Touyun@123';
async function main(): Promise<void> {
// 1. 平台租户
await prisma.tenant.upsert({
where: { code: TENANT },
update: { name: '投云', status: 'enabled' },
create: { code: TENANT, name: '投云', status: 'enabled' },
});
// 2. 平台租户初始化(根组织 + 超管角色 + 管理员 + 全量资源 + 内置字典 + 授权)
await initTenant({
prisma,
tenantCode: TENANT,
tenantName: '投云',
adminRoleCode: SUPER_ROLE,
adminRoleName: '超级管理员',
adminUsername: ADMIN_USERNAME,
adminPassword: ADMIN_PASSWORD,
includePlatformPerms: true,
});
console.log(`seed 完成:平台租户 ${TENANT}、用户 ${ADMIN_USERNAME}、角色 ${SUPER_ROLE}。`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});