From 1ff1e2453009eaba83bbc8e90430f52fad466442 Mon Sep 17 00:00:00 2001 From: wajeht <58354193+wajeht@users.noreply.github.com> Date: Thu, 15 Aug 2024 23:02:56 -0500 Subject: [PATCH] feat(utils): Add database seeding function and update cleanDatabase function --- src/handler.ts | 10 ++++++++-- src/router.ts | 3 +++ src/utils.ts | 27 +++++++++++++++++++++++++++ src/views/pages/settings.html | 4 ++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/handler.ts b/src/handler.ts index e25885b..8c84775 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -1,6 +1,6 @@ -import { Request, Response } from 'express'; import { db } from './db/db'; -import { cleanDatabase } from './utils'; +import { Request, Response } from 'express'; +import { cleanDatabase, seedDatabase } from './utils'; // GET /healthz export function getHealthzHandler(req: Request, res: Response) { @@ -226,6 +226,12 @@ export async function postCleanDatabaseHandler(req: Request, res: Response) { return res.redirect('back'); } +// POST /settings/seed-database +export async function postSeedDatabaseHandler(req: Request, res: Response) { + await seedDatabase(); + return res.redirect('back'); +} + // POST /apps export async function postCreateAppHandler(req: Request, res: Response) { const { name, is_active, description, url } = req.body; diff --git a/src/router.ts b/src/router.ts index b6214a6..b4496b5 100644 --- a/src/router.ts +++ b/src/router.ts @@ -6,6 +6,7 @@ import { getJobsPageHandler, getAppEditPageHandler, postAppUpdateHandler, + postSeedDatabaseHandler, getNotificationsPageHandler, postCreateAppHandler, getNewAppChannelPageHandler, @@ -55,6 +56,8 @@ router.get('/settings', catchAsyncErrorMiddleware(getSettingsPageHandler)); router.post('/settings/clean-database', catchAsyncErrorMiddleware(postCleanDatabaseHandler)); +router.post('/settings/seed-database', catchAsyncErrorMiddleware(postSeedDatabaseHandler)); + router.get('/jobs', catchAsyncErrorMiddleware(getJobsPageHandler)); router.get('/notifications', catchAsyncErrorMiddleware(getNotificationsPageHandler)); diff --git a/src/utils.ts b/src/utils.ts index b7307c1..d025af1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,6 +2,33 @@ import path from 'node:path'; import { db } from './db/db'; import { appConfig } from './config'; +export async function seedDatabase() { + try { + if (appConfig.env === 'production') { + console.log('Cannot run database seeding in production environment'); + return; + } + + console.log('Starting database seeding...'); + + const config = { + directory: path.resolve(path.join(process.cwd(), 'dist', 'src', 'db', 'seeds')), + }; + + const [seedFiles] = await db.seed.run(config); + + if (seedFiles.length === 0) { + console.log('No seed files found or all seeds have already been run'); + } else { + const seedList = seedFiles.map((seedFile: string) => path.basename(seedFile)).join(', '); + console.log(`Database seeding completed for: ${seedList}`); + } + } catch (error) { + console.error('Error seeding database:', error); + throw error; + } +} + export async function cleanDatabase() { try { // Get all table names in the current schema diff --git a/src/views/pages/settings.html b/src/views/pages/settings.html index 4545384..2c0312c 100644 --- a/src/views/pages/settings.html +++ b/src/views/pages/settings.html @@ -41,6 +41,10 @@

Danger Zone

+
+ +
+