Skip to content

Commit

Permalink
feat(utils): Add database seeding function and update cleanDatabase f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
wajeht committed Aug 16, 2024
1 parent 838ee10 commit 1ff1e24
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getJobsPageHandler,
getAppEditPageHandler,
postAppUpdateHandler,
postSeedDatabaseHandler,
getNotificationsPageHandler,
postCreateAppHandler,
getNewAppChannelPageHandler,
Expand Down Expand Up @@ -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));
Expand Down
27 changes: 27 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/views/pages/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ <h3>Danger Zone</h3>
<button type="submit" style="width: 100%;">Clean Database</button>
</form>

<form style="display: flex; flex-direction: column; gap: 10px;" action="/settings/seed-database" method="POST">
<button type="submit" style="width: 100%;">Seed Database</button>
</form>

<form action="#" method="POST">
<button type="submit" style="width: 100%;">Delete my account</button>
</form>
Expand Down

0 comments on commit 1ff1e24

Please sign in to comment.