aaa
This commit is contained in:
48
db/seed-user.mjs
Normal file
48
db/seed-user.mjs
Normal file
@@ -0,0 +1,48 @@
|
||||
// Seed a single user (no places/collections). Use after `npm run db:push`.
|
||||
// Defaults to vodanh.2901@gmail.com / renolation. Pass args to override.
|
||||
//
|
||||
// node --env-file=.env.local db/seed-user.mjs
|
||||
// node --env-file=.env.local db/seed-user.mjs other@email.com pw123456 "Other Name"
|
||||
|
||||
import bcrypt from "bcryptjs";
|
||||
import pg from "pg";
|
||||
|
||||
const EMAIL = process.argv[2] || "vodanh.2901@gmail.com";
|
||||
const PASSWORD = process.argv[3] || "renolation";
|
||||
const NAME = process.argv[4] || "Vô Danh";
|
||||
|
||||
function deriveInitials(name) {
|
||||
const parts = name.trim().split(/\s+/);
|
||||
if (parts.length >= 2) {
|
||||
return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
|
||||
}
|
||||
return parts[0].slice(0, 2).toUpperCase();
|
||||
}
|
||||
|
||||
const conn = process.env.DATABASE_URL
|
||||
? { connectionString: process.env.DATABASE_URL }
|
||||
: {
|
||||
host: process.env.PGHOST,
|
||||
user: process.env.PGUSER,
|
||||
password: process.env.PGPASSWORD,
|
||||
database: process.env.PGDATABASE,
|
||||
};
|
||||
|
||||
const client = new pg.Client(conn);
|
||||
await client.connect();
|
||||
|
||||
const hash = await bcrypt.hash(PASSWORD, 10);
|
||||
const initials = deriveInitials(NAME);
|
||||
|
||||
const res = await client.query(
|
||||
`INSERT INTO users (email, password_hash, name, initials)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (email) DO UPDATE SET password_hash = EXCLUDED.password_hash
|
||||
RETURNING id`,
|
||||
[EMAIL, hash, NAME, initials],
|
||||
);
|
||||
|
||||
console.log(`[user] ${EMAIL} → id=${res.rows[0].id}`);
|
||||
console.log(`[login] ${EMAIL} / ${PASSWORD}`);
|
||||
|
||||
await client.end();
|
||||
Reference in New Issue
Block a user