sequlize로 테이블과 모델네임을 다르게 만들 수 있다.
{
sequelize,
modelName: 'Customer',
}
현재 모델만 이름이 있고 테이블의 이름은 없다. 그냥 사용하게 되면 Customer라는 이름으로 테이블이 생기게 된다.
{
sequelize,
tableName: 'customer',
modelName: 'Customer',
}
원하는 테이블이름을 설정할 수 있다.
Joi로 유효성 검사를할 수 있다. Joi에 어떤 기능이 있는지는 좀더 살펴봐야 하지만 밑에 쓴 코드에는 문자인지,알파뱃과 숫자인지, 공백이 없는지 검사한다.
Joi에서 통과를 하지 못하면 에러를 뱉는데 컨트롤러단에서 try/catch로 잡아줘야 한다.
const Joi = require('joi')
const validation = Joi.object({
nickname: Joi.string().alphanum().not('').required()
})
module.exports = {
validation,
}
//서비스 단에서 catch로 에러 잡을때
if(err.isJoi){
return res.json({message: err.details[0].message})
sequlize로 외래키를 만들어 놓고 models에서 설정을 하자. as뒤에 붙는 이름은 join시 사용할 이름이다.
//post에서
static associate(models) {
this.belongsTo(models.User, {foreignKey: 'userId', as 'user' })
}
//user에서
static associate(models) {
this.hasMany(models.Post, {foreignKey: 'userId', as 'posts' })
}
Join하는 방법
findAll({
include: [{model: User, as : 'user', attribute: nickname}],
attribute: {exclude:['userId']}
})