|
|
|
@ -16,14 +16,6 @@ import { fileURLToPath } from 'node:url' |
|
|
|
const USERNAME_REGEX = /^[a-zA-Z0-9_]{3,20}$/ |
|
|
|
const MIN_PASSWORD_LENGTH = 6 |
|
|
|
|
|
|
|
/** 与 seed.ts 一致:仅当小写用户名本身符合 slug 规则时写入 public_slug */ |
|
|
|
const PUBLIC_SLUG_REGEX = /^[a-z0-9]{3,20}$/ |
|
|
|
|
|
|
|
function derivePublicSlug(username) { |
|
|
|
const lower = username.toLowerCase() |
|
|
|
return PUBLIC_SLUG_REGEX.test(lower) ? lower : null |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 与 `migrate-sqlite.js` 一致:相对路径相对 `process.cwd()`(例如 `.output/run.sh` 下与迁移写入同一文件)。 |
|
|
|
* Nitro 内 `resolveSqliteDatabaseUrl` 在无法锚定 drizzle-pkg 时也会回退到 cwd,避免「迁移/seed 成功、服务 CANTOPEN」。 |
|
|
|
@ -79,13 +71,12 @@ async function main() { |
|
|
|
const maxRs = await db.execute(`SELECT COALESCE(MAX(id), 0) AS maxId FROM users`) |
|
|
|
const { maxId } = maxRs.rows[0] |
|
|
|
const userId = (maxId ?? 0) + 1 |
|
|
|
const publicSlug = derivePublicSlug(username) |
|
|
|
|
|
|
|
try { |
|
|
|
await db.execute({ |
|
|
|
sql: ` |
|
|
|
INSERT INTO users (id, username, password, role, status, public_slug) |
|
|
|
VALUES (@id, @username, @password, @role, @status, @public_slug) |
|
|
|
INSERT INTO users (id, username, password, role, status) |
|
|
|
VALUES (@id, @username, @password, @role, @status) |
|
|
|
`,
|
|
|
|
args: { |
|
|
|
id: userId, |
|
|
|
@ -93,7 +84,6 @@ async function main() { |
|
|
|
password: passwordHash, |
|
|
|
role: 'admin', |
|
|
|
status: 'active', |
|
|
|
public_slug: publicSlug, |
|
|
|
}, |
|
|
|
}) |
|
|
|
console.log('Bootstrap complete: admin user created') |
|
|
|
|