feat(04-01): create seed-forms script and npm run seed:forms command
- Create scripts/seed-forms.ts: reads seeds/forms/, upserts PDFs into form_templates via onConflictDoUpdate on filename - Add seed:forms script to package.json with DOTENV_CONFIG_PATH=.env.local prefix - Empty seeds/forms/ prints guidance message and exits 0 (monthly-sync workflow ready)
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
"db:generate": "drizzle-kit generate",
|
"db:generate": "drizzle-kit generate",
|
||||||
"db:migrate": "drizzle-kit migrate",
|
"db:migrate": "drizzle-kit migrate",
|
||||||
"db:seed": "tsx scripts/seed.ts",
|
"db:seed": "tsx scripts/seed.ts",
|
||||||
|
"seed:forms": "DOTENV_CONFIG_PATH=.env.local npx tsx scripts/seed-forms.ts",
|
||||||
"db:studio": "drizzle-kit studio"
|
"db:studio": "drizzle-kit studio"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
37
teressa-copeland-homes/scripts/seed-forms.ts
Normal file
37
teressa-copeland-homes/scripts/seed-forms.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
|
import { readdir } from 'node:fs/promises';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { formTemplates } from '@/lib/db/schema';
|
||||||
|
|
||||||
|
const SEEDS_DIR = path.join(process.cwd(), 'seeds', 'forms');
|
||||||
|
|
||||||
|
async function seedForms() {
|
||||||
|
const files = await readdir(SEEDS_DIR);
|
||||||
|
const pdfs = files.filter(f => f.endsWith('.pdf'));
|
||||||
|
|
||||||
|
if (pdfs.length === 0) {
|
||||||
|
console.log('No PDF files found in seeds/forms/. Add PDFs downloaded from the SkySlope portal and re-run.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let seeded = 0;
|
||||||
|
for (const filename of pdfs) {
|
||||||
|
const name = filename
|
||||||
|
.replace(/\.pdf$/i, '')
|
||||||
|
.replace(/[-_]/g, ' ')
|
||||||
|
.replace(/\b\w/g, c => c.toUpperCase());
|
||||||
|
|
||||||
|
await db.insert(formTemplates)
|
||||||
|
.values({ name, filename })
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: formTemplates.filename,
|
||||||
|
set: { name, updatedAt: new Date() },
|
||||||
|
});
|
||||||
|
seeded++;
|
||||||
|
}
|
||||||
|
console.log(`Seeded ${seeded} forms into form_templates.`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
seedForms().catch(err => { console.error(err); process.exit(1); });
|
||||||
Reference in New Issue
Block a user