52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('开始初始化数据库...');
|
|
|
|
// 创建管理员用户
|
|
const adminPassword = await bcrypt.hash('admin123', 12);
|
|
const admin = await prisma.user.upsert({
|
|
where: { username: 'admin' },
|
|
update: {},
|
|
create: {
|
|
username: 'admin',
|
|
password: adminPassword,
|
|
isAdmin: true,
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
// 为管理员分配所有账号
|
|
// const allAccounts = await prisma.websiteAccount.findMany();
|
|
// for (const account of allAccounts) {
|
|
// await prisma.accountAssignment.upsert({
|
|
// where: {
|
|
// userId_accountId: {
|
|
// userId: admin.id,
|
|
// accountId: account.id,
|
|
// }
|
|
// },
|
|
// update: {},
|
|
// create: {
|
|
// userId: admin.id,
|
|
// accountId: account.id,
|
|
// isActive: true,
|
|
// },
|
|
// });
|
|
// }
|
|
|
|
console.log('数据库初始化完成!');
|
|
console.log('管理员账户:', admin.username, '密码: admin123');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('数据库初始化失败:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|