const Koa = require('koa')
const Router = require('koa-router')
const {Sequelize, Model} = require('sequelize');
const app = new Koa()
const router = new Router()
const config = {
dbName: 'wxapp',
host: 'localhost',
port: 3306,
user: 'root',
password: '123456'
};
const sequelize = new Sequelize(config.dbName, config.user, config.password, {
dialect: 'mysql',
host: config.host,
port: config.port,
logging: true,
// 指定东8时区,很重要
timezone: '+08:00',
define: {
timestamps: false
}
})
class User extends Model {}
User.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
nickname: Sequelize.STRING,
}, {
sequelize,
tableName: 'user'
})
// 上面的if有我们的 router.get 来判断
// 我们可以把所有的路由写在一个文件里面
router.get('/classic/latest', async (ctx, next) => {
const user = {
nickname: 'xxx'
}
const r = await User.create(user)
})
app.use(router.routes())
app.listen(3000)
创建数据库表