chore: add initial migration and seed script
Made-with: Cursor
This commit is contained in:
47
src/db/seed.ts
Normal file
47
src/db/seed.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'dotenv/config';
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import pg from 'pg';
|
||||
import argon2 from 'argon2';
|
||||
import { env } from '../config/env.js';
|
||||
import { users } from './schema/index.js';
|
||||
|
||||
const { Pool } = pg;
|
||||
|
||||
const TEST_USER = {
|
||||
email: 'test@example.com',
|
||||
password: 'TestPassword123!',
|
||||
nickname: 'TestUser',
|
||||
};
|
||||
|
||||
async function runSeed() {
|
||||
const pool = new Pool({ connectionString: env.DATABASE_URL });
|
||||
const db = drizzle(pool);
|
||||
|
||||
if (env.NODE_ENV !== 'development') {
|
||||
await pool.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = await db.select().from(users).where(eq(users.email, TEST_USER.email)).limit(1);
|
||||
if (existing.length > 0) {
|
||||
await pool.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordHash = await argon2.hash(TEST_USER.password);
|
||||
await db.insert(users).values({
|
||||
email: TEST_USER.email,
|
||||
passwordHash,
|
||||
nickname: TEST_USER.nickname,
|
||||
role: 'free',
|
||||
emailVerifiedAt: new Date(),
|
||||
});
|
||||
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
runSeed().catch((err) => {
|
||||
console.error('Seed failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user