lilith-platform.live/codebase/@features/platform-seed/bin/seed.ts
Claude Code 25d2c7ad65 init(codebase-default): 🎉 Implement foundational directory structure with core modules and utility files
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-03-25 22:50:24 -07:00

86 lines
2.4 KiB
TypeScript

import { Client } from 'pg';
import waitlistData from '../data/waitlist-entries.json';
interface WaitlistInput {
email: string;
source: string | null;
userType: string | null;
}
const client = new Client({
host: process.env['DATABASE_HOST'] ?? 'localhost',
port: parseInt(process.env['DATABASE_PORT'] ?? '25460', 10),
user: process.env['DATABASE_USER'] ?? 'lilith',
password: process.env['DATABASE_PASSWORD'] ?? 'dev',
database: process.env['DATABASE_NAME'] ?? 'lilith_live',
});
const isReset = process.argv.includes('--reset');
const isStatus = process.argv.includes('--status');
async function ensureTableExists(): Promise<void> {
await client.query(`
CREATE TABLE IF NOT EXISTS waitlist (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
source VARCHAR(100),
"userType" VARCHAR(50),
"createdAt" TIMESTAMP DEFAULT now() NOT NULL
)
`);
await client.query(`CREATE INDEX IF NOT EXISTS idx_waitlist_email ON waitlist (email)`);
}
async function showStatus(): Promise<void> {
const result = await client.query('SELECT COUNT(*) as count FROM waitlist');
console.log(` waitlist: ${result.rows[0].count} rows`);
}
async function seedWaitlist(): Promise<void> {
console.log('Seeding test waitlist entries...');
let inserted = 0;
let skipped = 0;
for (const entry of waitlistData as WaitlistInput[]) {
const existing = await client.query('SELECT id FROM waitlist WHERE email = $1', [entry.email]);
if (existing.rowCount && existing.rowCount > 0) {
skipped++;
continue;
}
await client.query(
`INSERT INTO waitlist (email, source, "userType") VALUES ($1, $2, $3)`,
[entry.email, entry.source, entry.userType],
);
inserted++;
}
console.log(` Inserted: ${inserted}, Skipped (already exist): ${skipped}`);
}
async function main(): Promise<void> {
console.log('\nLilith Live - Seed\n');
await client.connect();
await ensureTableExists();
if (isStatus) {
await showStatus();
} else {
if (isReset) {
console.log('Resetting waitlist...');
await client.query('TRUNCATE waitlist');
}
await seedWaitlist();
console.log('\nDone.');
await showStatus();
}
await client.end();
console.log('');
}
main().catch((err: unknown) => {
console.error('Seed failed:', err instanceof Error ? err.message : String(err));
process.exit(1);
});