I'm trying to initialize database table according model, but table can't be created, and there is no any errors. Furthermore, after this action, sequelize.models object has AccountsModel.
import { DataTypes, Model, Sequelize } from 'sequelize'
import { DatabaseConfig } from '../../database/config'
export class AccountsModel extends Model { }
export function initializeAccountsModel (sequelize: Sequelize): void {
AccountsModel.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
sequelize,
tableName: DatabaseConfig.TABLE_NAMES.Accounts,
})
}
Model.sync() method is working, but I'm gonna follow OOP.
The .sync() method is the action that actually creates the tables when you start your application. It goes into your models directory and builds the tables based on the attributes you've specified in the individual models. If you don't run .sync(), this action will not occur.
If you wish to not use .sync(), the alternative is to either manually create the tables (Not great if you ever need to re-deploy for some reason), or utilize migrations.
Another important note regarding .sync() is that it will use the SQL query CREATE TABLE IF NOT EXISTS when building from the models, so if you have added attributes to the model in between syncs, these new columns will not be added to the table when you re-sync. You can bypass this by passing {force: true} into .sync(), but that will first DROP the table and then CREATE it again, causing data loss.
Migrations are strongly recommended for production applications.