Last active
November 21, 2019 14:17
-
-
Save hmmhmmhm/98a4083e091cf82e10527bae65ac70d7 to your computer and use it in GitHub Desktop.
model.js
This file contains 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
// require('./product') | |
module.exports = (sequelize, DataTypes) => ( | |
sequelize.define('product', { | |
codeNumber: { | |
type: DataTypes.STRING(50), | |
allowNull: false, | |
}, | |
kind: { | |
type: DataTypes.STRING(20), | |
allowNull: false, | |
}, | |
scatteringDay: { | |
type: DataTypes.DATE, | |
allowNull: false, | |
}, | |
breeding: { | |
type: DataTypes.STRING(20), | |
allowNull: false, | |
}, | |
area: { | |
type: DataTypes.STRING(20), | |
allowNull: false, | |
}, | |
memo: { | |
type: DataTypes.STRING(255), | |
allowNull: true, | |
}, | |
remain: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
defaultValue: 0 | |
}, | |
active: { | |
type: DataTypes.BOOLEAN, | |
allowNull: false, | |
defaultValue: false | |
}, | |
}, { | |
timestamps: true, | |
paranoid: true, | |
}) | |
); | |
// require('./productInformation') | |
module.exports = (sequelize, DataTypes) => ( | |
sequelize.define('productInformation', { | |
size: { | |
type: DataTypes.STRING(10), | |
allowNull: false, | |
}, | |
age: { | |
type: DataTypes.STRING(10), | |
allowNull: false, | |
}, | |
price: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
}, | |
bidStartPrice: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
}, | |
quantity: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
}, | |
}, { | |
timestamps: false, | |
paranoid: false, | |
}) | |
); | |
// index.js | |
const Sequelize = require('sequelize'); | |
const env = process.env.NODE_ENV || 'development'; | |
const config = require('../config/config')[env]; | |
const db = {}; | |
const sequelize = new Sequelize( | |
config.database, config.username, config.password, config.options, config.pool, | |
); | |
db.Product = require('./product')(sequelize, Sequelize); | |
db.ProductInformation = require('./productInformation')(sequelize, Sequelize); | |
// Product 1:N ProductInformation | |
db.Product.hasMany(db.ProductInformation, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' }); | |
db.ProductInformation.belongsTo(db.Product, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment