2023-09-14 15:00:14 +10:00
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
|
|
|
|
const seedDatabase = async () => {
|
2023-12-06 05:41:51 +05:30
|
|
|
const files = fs.readdirSync(path.join(__dirname, './seed'));
|
2023-09-14 15:00:14 +10:00
|
|
|
|
2023-12-06 05:41:51 +05:30
|
|
|
for (const file of files) {
|
|
|
|
|
const stat = fs.statSync(path.join(__dirname, './seed', file));
|
2023-09-14 15:00:14 +10:00
|
|
|
|
2023-12-06 05:41:51 +05:30
|
|
|
if (stat.isFile()) {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
|
const mod = require(path.join(__dirname, './seed', file));
|
2023-09-14 15:00:14 +10:00
|
|
|
|
2023-12-06 05:41:51 +05:30
|
|
|
if ('seedDatabase' in mod && typeof mod.seedDatabase === 'function') {
|
|
|
|
|
console.log(`[SEEDING]: ${file}`);
|
2024-03-07 18:17:28 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await mod.seedDatabase();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(`[SEEDING]: Seed failed for ${file}`);
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2023-12-06 05:41:51 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-14 15:00:14 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
seedDatabase()
|
|
|
|
|
.then(() => {
|
|
|
|
|
console.log('Database seeded');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|