/
aaronair
/
Test_task_rt_solution
Обзор
Документация
Войти
/
aaronair
/
Test_task_rt_solution
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/scripts/seed.ts
158 строк
5 KB
Ilikedrinkpivo
first_commit
28 май 2026, 14:51
28 май 2026, 14:51
b4498fc
Код
Авторство
О чём код?
import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { eq } from "drizzle-orm"; import { loadConfig } from "../src/config.js"; import { getDb, closeDb } from "../src/db/client.js"; import { runMigrations } from "../src/db/migrate.js"; import { contractors, initiativeDocuments, initiatives, initiativeTechnologies, technologies, users, } from "../src/db/schema.js"; import { DEFAULT_CONTRACTORS, DEFAULT_TECHNOLOGIES } from "../src/constants/meta.js"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); interface SeedInitiative { id: string; title: string; segmentTag: string; segment: string; owner: { name: string; email: string; employeeId?: string }; contractor: string; description: string; status: string; technologies: string[]; businessEffect: string; budgetMethodology: string; budget: string; serviceUrl?: string; startDate?: string; endDate?: string; documents: { name: string; type: string; sizeKb: number; isPilotProtocol?: boolean }[]; isCkAiInProgress: boolean; likesCount: number; createdAt: string; } function slugifyName(name: string): string { const map: Record<string, string> = { а: "a", б: "b", в: "v", г: "g", д: "d", е: "e", ё: "e", ж: "zh", з: "z", и: "i", й: "y", к: "k", л: "l", м: "m", н: "n", о: "o", п: "p", р: "r", с: "s", т: "t", у: "u", ф: "f", х: "h", ц: "ts", ч: "ch", ш: "sh", щ: "sch", ъ: "", ы: "y", ь: "", э: "e", ю: "yu", я: "ya", }; return name .toLowerCase() .split("") .map((c) => map[c] ?? c) .join("") .replace(/[^a-z0-9]+/g, "-") .replace(/^-|-$/g, ""); } async function main() { const env = loadConfig(); await runMigrations(env.DATABASE_URL); const db = getDb(env.DATABASE_URL); const seedPath = path.join(__dirname, "initiatives.seed.json"); const INITIATIVES = JSON.parse(readFileSync(seedPath, "utf-8")) as SeedInitiative[]; for (const name of DEFAULT_CONTRACTORS) { const existing = await db.select().from(contractors).where(eq(contractors.name, name)).limit(1); if (!existing[0]) await db.insert(contractors).values({ name }); } for (const name of DEFAULT_TECHNOLOGIES) { const existing = await db.select().from(technologies).where(eq(technologies.name, name)).limit(1); if (!existing[0]) await db.insert(technologies).values({ name }); } for (const item of INITIATIVES) { let [owner] = await db .select() .from(users) .where(eq(users.email, item.owner.email)) .limit(1); if (!owner) { [owner] = await db .insert(users) .values({ email: item.owner.email, name: item.owner.name, slug: item.owner.employeeId ?? slugifyName(item.owner.name), }) .returning(); } const existing = await db .select() .from(initiatives) .where(eq(initiatives.slug, item.id)) .limit(1); if (existing[0]) continue; const [initiative] = await db .insert(initiatives) .values({ slug: item.id, title: item.title, description: item.description, status: item.status, segmentTag: item.segmentTag, segmentDetail: item.segment, ownerId: owner.id, contractor: item.contractor, serviceUrl: item.serviceUrl, budget: item.budget, budgetMethodology: item.budgetMethodology, businessEffect: item.businessEffect, startDate: item.startDate?.slice(0, 10), endDate: item.endDate?.slice(0, 10), isCkAiInProgress: item.isCkAiInProgress, likesCount: item.likesCount, createdAt: new Date(item.createdAt), }) .returning(); for (const techName of item.technologies) { let [tech] = await db .select() .from(technologies) .where(eq(technologies.name, techName)) .limit(1); if (!tech) { [tech] = await db.insert(technologies).values({ name: techName }).returning(); } await db.insert(initiativeTechnologies).values({ initiativeId: initiative.id, technologyId: tech.id, }); } for (const doc of item.documents) { await db.insert(initiativeDocuments).values({ initiativeId: initiative.id, name: doc.name, type: doc.type, sizeKb: doc.sizeKb, isPilotProtocol: doc.isPilotProtocol ?? false, }); } } console.log(`Seeded ${INITIATIVES.length} initiatives`); await closeDb(); } main().catch((err) => { console.error(err); process.exit(1); });