56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
import bcrypt from "bcryptjs";
|
|
import path from "path";
|
|
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
|
|
const url =
|
|
process.env.DATABASE_URL ||
|
|
"file:" + path.join(process.cwd(), "data/database.sqlite");
|
|
|
|
const adapter = new PrismaBetterSqlite3({
|
|
url,
|
|
});
|
|
|
|
const prisma = new PrismaClient({
|
|
log: ["query", "error", "warn"],
|
|
adapter,
|
|
});
|
|
|
|
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();
|