61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import fs from "node:fs";
|
||
import postgres from "postgres";
|
||
import { drizzle } from "drizzle-orm/postgres-js";
|
||
import { user } from "./lib/db/schema";
|
||
import { generateHashedPassword } from "./lib/db/utils";
|
||
|
||
async function main() {
|
||
let connectionString = process.env.POSTGRES_URL;
|
||
|
||
// 如果环境变量中没有 POSTGRES_URL,则尝试从 .env.local 中读取
|
||
if (!connectionString && fs.existsSync(".env.local")) {
|
||
const envContent = fs.readFileSync(".env.local", "utf8");
|
||
for (const line of envContent.split("\n")) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||
const [key, ...rest] = trimmed.split("=");
|
||
if (key === "POSTGRES_URL") {
|
||
connectionString = rest.join("=").trim();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!connectionString) {
|
||
throw new Error("POSTGRES_URL is not set");
|
||
}
|
||
|
||
const client = postgres(connectionString);
|
||
const db = drizzle(client);
|
||
|
||
const email = "admin@example.com";
|
||
const password = "Admin@123456";
|
||
|
||
const hashedPassword = generateHashedPassword(password);
|
||
try {
|
||
await db.insert(user).values({
|
||
email,
|
||
password: hashedPassword,
|
||
role: "admin",
|
||
isActive: true,
|
||
});
|
||
|
||
// biome-ignore lint/style/noConsole: 仅用于一次性脚本
|
||
console.log("Admin user created:", email, "password:", password);
|
||
} catch (error) {
|
||
// 如果已存在则忽略错误
|
||
// biome-ignore lint/style/noConsole: 仅用于一次性脚本
|
||
console.log("Admin user may already exist:", email, error);
|
||
}
|
||
|
||
await client.end();
|
||
}
|
||
|
||
main().catch((error) => {
|
||
// biome-ignore lint/style/noConsole: 仅用于一次性脚本
|
||
console.error(error);
|
||
process.exit(1);
|
||
});
|
||
|
||
|