Last active
March 21, 2021 18:55
-
-
Save intech/60584642efd980eac51ec80c7b5b0ae7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: models/factory.js | |
"use strict"; | |
module.exports = (sequelize, DataTypes) => { | |
const Factory = sequelize.define("factory", { | |
uuid: { | |
type: DataTypes.UUID, | |
defaultValue: DataTypes.UUIDV4, | |
allowNull: false, | |
unique: true, | |
primaryKey: true | |
}, | |
google_id: { | |
type: DataTypes.STRING | |
}, | |
price_id: { | |
type: DataTypes.INTEGER, | |
}, | |
name: { | |
type: DataTypes.STRING, | |
unique: true | |
} | |
}); | |
Factory.associate = function (models) { | |
Factory.hasOne(models.prices, {foreignKey: "id", sourceKey: "price_id"}); | |
Factory.hasMany(models.bills, {foreignKey: "factory_id", sourceKey: "uuid"}); | |
Factory.hasMany(models.acts, {foreignKey: "factory_id", sourceKey: "uuid"}); | |
Factory.hasMany(models.user, {foreignKey: "factory_id", sourceKey: "uuid"}); | |
Factory.hasMany(models.ribbon, {foreignKey: "factory_id", sourceKey: "uuid"}); | |
Factory.hasMany(models.cable, {foreignKey: "factory_id", sourceKey: "uuid"}); | |
}; | |
return Factory; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: middlewares/sequelize.js | |
"use strict"; | |
const Sequelize = require("sequelize"); | |
const fs = require("fs"); | |
const path = require("path"); | |
module.exports = function SequelizeDbMiddleware(options = {}) { | |
options = { | |
dialect: "postgres", | |
define: { | |
timestamps: false | |
}, | |
logging: console.log, | |
pool: { | |
max: 5, | |
min: 1, | |
acquire: 6000, | |
idle: 3000, | |
log: true | |
}, | |
...options | |
}; | |
const connection = new Sequelize(process.env.DB, options); | |
const models = {}; | |
return { | |
name: "Sequelize", | |
async starting() { | |
await connection.connectionManager.getConnection(); | |
this.logger.info("Sequelize middleware ready"); | |
const dir = __dirname + "/../models"; | |
fs | |
.readdirSync(dir) | |
.filter(file => file.endsWith(".js")) | |
.map(file => { | |
const model = require(path.join(dir, file))(connection, Sequelize.DataTypes); | |
models[model.name] = model; | |
return model; | |
}).forEach(model => { | |
if ("associate" in model) model.associate(models); | |
}); | |
this.logger.info("Sequelize all models loaded!"); | |
this.connection = connection; | |
}, | |
async stopping() { | |
if (connection) await connection.close(); | |
this.logger.info("CockroachDB middleware close"); | |
}, | |
serviceCreating(service, schema) { | |
if (!schema.name.startsWith("$")) { | |
service.crdb = connection; | |
service.crdb.Op = Sequelize.Op; | |
service.models = models; | |
service.Sequelize = Sequelize; | |
this.logger.debug("Sequelize models for `" + schema.name + "` ready"); | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment