init source
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
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 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 = sequelize['import'](path.join(__dirname, file));
|
||||
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;
|
||||
@@ -0,0 +1,64 @@
|
||||
'use strict';
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const member = sequelize.define('member', {
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: {
|
||||
msg: 'This email is already taken.'
|
||||
},
|
||||
validate: {
|
||||
isEmail: {
|
||||
msg: 'Email address must be valid.'
|
||||
}
|
||||
}
|
||||
},
|
||||
username: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: {
|
||||
msg: 'This username is already taken.'
|
||||
},
|
||||
validate: {
|
||||
len: {
|
||||
args: [5, 50],
|
||||
msg: 'Your username may be 5 to 50 characters only.'
|
||||
}
|
||||
}
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
len: {
|
||||
args: [5, 72],
|
||||
msg: 'Your password may be 5 to 72 characters only.'
|
||||
}
|
||||
}
|
||||
},
|
||||
firstname: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
len: {
|
||||
args: [5, 50],
|
||||
msg: 'Your first name may be 5 to 50 characters only.'
|
||||
}
|
||||
}
|
||||
},
|
||||
lastname: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
len: {
|
||||
args: [5, 50],
|
||||
msg: 'Your last name may be 5 to 50 characters only.'
|
||||
}
|
||||
}
|
||||
},
|
||||
}, {});
|
||||
member.associate = function (models) {
|
||||
// associations
|
||||
};
|
||||
return member;
|
||||
};
|
||||
Reference in New Issue
Block a user