Setup sequelize and create models
This commit is contained in:
parent
a62d7a1921
commit
519e020831
8 changed files with 928 additions and 4 deletions
3
bin/www
3
bin/www
|
@ -7,6 +7,7 @@
|
|||
let app = require("../app");
|
||||
let debug = require("debug")("sod:server");
|
||||
let http = require("http");
|
||||
let models = require("../models");
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
|
@ -25,9 +26,11 @@ let server = http.createServer(app);
|
|||
* Listen on provided port, on all network interfaces.
|
||||
*/
|
||||
|
||||
models.sequelize.sync().then(() => {
|
||||
server.listen(port);
|
||||
server.on("error", onError);
|
||||
server.on("listening", onListening);
|
||||
});
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
|
|
7
config/config_exemple.json
Normal file
7
config/config_exemple.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"username": "root",
|
||||
"password": null,
|
||||
"database": "database_development",
|
||||
"host": "127.0.0.1",
|
||||
"dialect": "mysql"
|
||||
}
|
31
models/command.js
Normal file
31
models/command.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
"use strict";
|
||||
const {
|
||||
Model
|
||||
} = require("sequelize");
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class Command extends Model {
|
||||
static associate(models) {
|
||||
Command.belongsToMany(models.Sandwich, {through: "SandwichCommand"});
|
||||
Command.belongsTo(models.Department);
|
||||
}
|
||||
}
|
||||
Command.init({
|
||||
firstName: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
lastName: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
date: {
|
||||
type: DataTypes.DATEONLY,
|
||||
defaultValue: DataTypes.NOW,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "Command",
|
||||
});
|
||||
return Command;
|
||||
};
|
22
models/department.js
Normal file
22
models/department.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
"use strict";
|
||||
const {
|
||||
Model
|
||||
} = require("sequelize");
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class Department extends Model {
|
||||
static associate(models) {
|
||||
Department.hasMany(models.Command);
|
||||
}
|
||||
}
|
||||
Department.init({
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "Department",
|
||||
});
|
||||
return Department;
|
||||
};
|
36
models/index.js
Normal file
36
models/index.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const Sequelize = require("sequelize");
|
||||
const basename = path.basename(__filename);
|
||||
const config = require(__dirname + "/../config/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);
|
||||
}
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
.filter(file => {
|
||||
return (file.indexOf(".") !== 0) && (file !== basename) && (file.slice(-3) === ".js");
|
||||
})
|
||||
.forEach(file => {
|
||||
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
|
||||
db[model.name] = model;
|
||||
});
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
|
||||
module.exports = db;
|
26
models/sandwich.js
Normal file
26
models/sandwich.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
"use strict";
|
||||
const {
|
||||
Model
|
||||
} = require("sequelize");
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class Sandwich extends Model {
|
||||
static associate(models) {
|
||||
Sandwich.belongsToMany(models.Command, {through: "SandwichCommand"});
|
||||
}
|
||||
}
|
||||
Sandwich.init({
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
price: {
|
||||
type: DataTypes.FLOAT,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: "Sandwich",
|
||||
});
|
||||
return Sandwich;
|
||||
};
|
795
package-lock.json
generated
795
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -11,6 +11,10 @@
|
|||
"express": "~4.16.1",
|
||||
"http-errors": "~1.6.3",
|
||||
"morgan": "~1.9.1",
|
||||
"pug": "2.0.0-beta11"
|
||||
"pg": "^8.3.0",
|
||||
"pg-hstore": "^2.3.3",
|
||||
"pug": "2.0.0-beta11",
|
||||
"sequelize": "^6.3.4",
|
||||
"sequelize-cli": "^6.2.0"
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue