backend/src/loaders/database.ts

16 lines
615 B
TypeScript
Raw Normal View History

2020-11-27 17:28:42 +00:00
import { createConnection } from "typeorm";
import { runSeeder } from 'typeorm-seeding';
import { User } from '../models/entities/User';
import SeedUsers from '../seeds/SeedUsers';
2020-12-05 18:09:08 +00:00
/**
* Loader for the database that creates the database connection and initializes the database tabels.
* It also triggers the seeding process if no users got detected in the database.
2020-12-05 18:09:08 +00:00
*/
2020-11-27 17:28:42 +00:00
export default async () => {
2020-11-25 17:51:48 +00:00
const connection = await createConnection();
await connection.synchronize();
if (await connection.getRepository(User).count() === 0) {
await runSeeder(SeedUsers);
}
2020-11-25 17:51:48 +00:00
return connection;
2020-11-25 17:31:06 +00:00
};