model处理业务逻辑,router里面不要写业务逻辑
code first,优先建模,sequelize会把模型转为数据库表
数据库的设计,有粗到细
- model目录下的每个 *.js就是一个模型
 

this.ctx.model.${Modelname}调用的Modelname 必须与 ../model/{fileName}.js中fileName一致,
且Modelname必须大写开头,而fileName则无规定大小写。
例如this.ctx.model.Userinfo其对应的model路径为../model/userinfo.js,
const router = new Router({prefix: '/v1/user',});// 中间件形式,是错误的用法,因为中间件只在 koa初始化的时候,执行一次不要以 class类的形式组织中间件,必须是函数的方式router.get('/register', new RegisterValidator(), async() => {});router.get('/register', async() => {// 这种每次都会执行const validate = await new RegisterValidate()})module.exports = app => {const { STRING, INTEGER } = app.Sequelize;const User = app.model.define('user', {id: {type: INTEGER,autoIncrement: true,primaryKey: true},name: {type: STRING,allowNull: false},password: {type: STRING(32),allowNull: false}});// 表关联的字段User.associate = function() {// 一对多app.model.User.hasMany(app.model.Diary, { foreignKey: 'user_id', targetKey: 'id'})}return User;}
