When it comes to using Express with React on the backend, I'm accustomed to working in a server.js file to synchronize the database. However, I've recently started working with Next.js and noticed that there's no server.js file to sync the models with sequelize. Currently, my workaround involves syncing the models upon user login/sign up (as seen in the code below), but I anticipate that this approach will lead to numerous issues in the future. What is the best way to organize my files and sync the models when starting up a Next.js server?
This is how I've structured my backend so far:
./src/pages/api/user/login.js
const { User } = require('../../../db/model');
const { signToken } = require('../../../auth/auth');
const sequelize = require('../../../db/config/connections');
sequelize.sync({ force: false });
export default async function handler(req, res) {
// Code for handling POST requests goes here
}
./src/db/model/user.js
const { Model, DataTypes } = require("sequelize");
const sequelize = require("../config/connections");
const bcrypt = require("bcrypt");
// Code for defining the User model goes here
./src/db/config/connections.js
require("dotenv").config();
const Sequelize = require("sequelize");
// Code for establishing the database connection goes here
While the tables are currently able to sync, my goal is to have them synced automatically when the server starts up.