Skip to content

Commit

Permalink
fixExampleBugs&readyToRun
Browse files Browse the repository at this point in the history
  • Loading branch information
mmRoshani committed Jul 25, 2022
1 parent 5306414 commit 3d0640e
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 580 deletions.
3 changes: 2 additions & 1 deletion example/.env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DATABASE_USER=postgres
DATABASE_PASS=root
DATABASE_PASS=6939
DATABASE_PORT=5432
DATABASE_NAME=STM
DATABASE_DIALECT=postgres
DATABASE_TIMEZONE=+03:30
2 changes: 1 addition & 1 deletion example/.sequelizerc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require("path");

module.exports = {
config: path.resolve("db", "config.js"),
config: path.resolve("db", "config.json"),
"migrations-path": path.resolve("db", "migrations"),
"models-path": path.resolve("db", "models"),
"seeders-path": path.resolve("db", "seeders"),
Expand Down
17 changes: 0 additions & 17 deletions example/db/config.js

This file was deleted.

23 changes: 23 additions & 0 deletions example/db/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "6939",
"database": "STM",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
148 changes: 0 additions & 148 deletions example/db/migrations/00000001-noname.js

This file was deleted.

46 changes: 27 additions & 19 deletions example/db/models/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
'use strict';
"use strict";

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const env = process.env.NODE_ENV || "development";
const config = require("../config.json");
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const sequelize = new Sequelize({
username: config.development.username,
password: config.development.password,
database: config.development.database,
host: config.development.host,
dialect: config.development.dialect,
port: process.env.DATABASE_PORT,
timezone: process.env.DATABASE_TIMEZONE,
protocol: "tcp",
});

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
fs.readdirSync(__dirname)
.filter((file) => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
.forEach((file) => {
const model = require(path.join(__dirname, file))(
sequelize,
Sequelize.DataTypes
);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
Expand Down
Loading

0 comments on commit 3d0640e

Please sign in to comment.