implemented data initializer

This commit is contained in:
X4Lo committed 2024-04-21 16:03:16 +01:00
1 parent a750ad16a3
commit 883d899380
2 files changed
+67 -1

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
const Genre = require("../models/Genre");
async function initializeData() {
initializeGenres();
}
async function initializeGenres() {
try {
// * data to insert:
const initialGenres = [
{ id: 10759, name: 'Action & Adventure' },
{ id: 16, name: 'Animation' },
{ id: 35, name: 'Comedy' },
{ id: 80, name: 'Crime' },
{ id: 99, name: 'Documentary' },
{ id: 18, name: 'Drama' },
{ id: 10751, name: 'Family' },
{ id: 10762, name: 'Kids' },
{ id: 9648, name: 'Mystery' },
{ id: 10763, name: 'News' },
{ id: 10764, name: 'Reality' },
{ id: 10765, name: 'Sci-Fi & Fantasy' },
{ id: 10766, name: 'Soap' },
{ id: 10767, name: 'Talk' },
{ id: 10768, name: 'War & Politics' },
{ id: 37, name: 'Western' },
{ id: 28, name: 'Action' },
{ id: 12, name: 'Adventure' },
{ id: 14, name: 'Fantasy' },
{ id: 36, name: 'History' },
{ id: 27, name: 'Horror' },
{ id: 10402, name: 'Music' },
{ id: 10749, name: 'Romance' },
{ id: 878, name: 'Science Fiction' },
{ id: 10770, name: 'TV Movie' },
{ id: 53, name: 'Thriller' },
{ id: 10752, name: 'War' }
];
// * Check if the data already exists
const existingGenres = await Genre.findAll();
// ? If data already exists, return
if (existingGenres.length == initialGenres.length) {
console.log('Genres already initialized');
return;
}
// ? If data doesn't exist, insert initial data
await Genre.bulkCreate(initialGenres);
console.log(`${initialGenres.length} Genres initialized successfully`);
} catch (error) {
console.error('Error initializing data:', error);
}
}
module.exports = {
initializeData,
initializeGenres,
}
+4 -1
View File
@@ -29,13 +29,16 @@ app.use((req, res, next) => {
app.use('/api/v1', routes);
// Start the server
const relations = require('./db/relations.js')
const relations = require('./db/relations.js');
const { initializeGenres, initializeData } = require('./db/dataInitializer.js');
sequelize
// .sync({force: true}) // will drop all tables and recreate them.
// .sync({ alter: true }) // will attempt to automatically alter tables to fit the model definitions.
.sync()
.then((result) => {
initializeData();
app.listen(PORT, () => {
//const Entry = require("./models/Entry.js")
console.log(`Backend server running on http://localhost:${PORT}`);