Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of associations #106 #113

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions express-main-example/sequelize/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"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;