Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Last active April 16, 2024 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzn0x/c3724a23cab4389c4f0aa175b665427b to your computer and use it in GitHub Desktop.
Save fzn0x/c3724a23cab4389c4f0aa175b665427b to your computer and use it in GitHub Desktop.
Sync model with database (creating tables)
"use strict";
const { Sequelize } = require("sequelize");
const db = {};
const fs = require("fs");
const path = require("path");
const sequelize = new Sequelize("xxxx", "xxxx", "xxxx", {
host: "xxxx",
port: "xxxx",
dialect: "xxxx",
logging: console.log,
benchmark: true,
timezone: "+00:00",
});
//Read all models in models folder
const modelDir = require("./");
log(
"\u001b[33m",
"CRAWLING DATABASE MODEL DEFINITION in ",
modelDir,
"\u001b[39m"
);
fs.readdirSync(modelDir)
.filter((file) => {
return file.indexOf(".") !== 0 && file.slice(-8) === "model.js";
})
.forEach((file) => {
let modelPath = path.join(modelDir, file);
let model = require(modelPath)(sequelize, Sequelize.DataTypes);
log(
"\u001b[36m",
"[MODEL FOUND]",
model,
" (",
modelPath,
")",
"\u001b[39m"
);
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
console.log(db[modelName].associate);
// call the custom function
if (db[modelName].associate) db[modelName].associate(db);
});
// exports sequelize connection and Object
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment