Browse Source

feat: 移除公共 slug 生成逻辑,简化用户插入操作

acas
npmrun 2 weeks ago
parent
commit
afe3e1503b
  1. 14
      build-files/seed/sqlite3.js

14
build-files/seed/sqlite3.js

@ -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')

Loading…
Cancel
Save