45 lines
1023 B
TypeScript
45 lines
1023 B
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
const prisma = new PrismaClient({
|
|
log: ["query", "error", "warn"],
|
|
});
|
|
|
|
const createDefaultUser = async (): Promise<void> => {
|
|
const username = "admin";
|
|
const password = "Beifan@2026";
|
|
const name = "系统管理员";
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { username },
|
|
});
|
|
|
|
if (!user) {
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
await prisma.user.create({
|
|
data: {
|
|
username,
|
|
password: hashedPassword,
|
|
name,
|
|
email: "admin@example.com",
|
|
role: "admin",
|
|
},
|
|
});
|
|
|
|
console.log("默认管理员用户创建完成:");
|
|
console.log("用户名:", username);
|
|
console.log("密码:", password);
|
|
} else {
|
|
console.log("默认管理员用户已存在");
|
|
}
|
|
};
|
|
|
|
const initDatabase = async (): Promise<void> => {
|
|
await createDefaultUser();
|
|
await prisma.$disconnect();
|
|
console.log("数据库连接已关闭");
|
|
};
|
|
|
|
initDatabase();
|