Commit 1d7c4bf3 authored by likely's avatar likely

第三课数据库操作内容

parent 1895ad7a
const Sequelize = require('sequelize')
const db = {
database: 'library',
user: 'root',
password: 'root',
host: 'localhost'
}
const sequelize = new Sequelize(db.database, db.user, db.password, {
host: db.host,
dialect: 'mysql',
operatorsAliases: false,
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci'
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
})
sequelize
.authenticate()
.then(res => {
console.log('数据库连接成功')
})
.catch(err => {
console.log('数据库连接失败')
})
module.exports = sequelize
\ No newline at end of file
const koa = require('koa')
const koaBody = require('koa-body')
const app = new koa()
const router = require('./routes')
require('./connector')
app.use(koaBody())
app.use(router.routes()).use(router.allowedMethods())
app.use(async ctx => {
ctx.body = 'hello world'
})
app.listen(3000)
\ No newline at end of file
const Sequelize = require('sequelize')
const instance = require('../connector')
const model = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
defaultValue: '',
allowNull: false
}
}
const option = {
}
const modelDefine = instance.define('books', model, option)
modelDefine.sync()
module.exports = modelDefine
\ No newline at end of file
const koaRouter = require('koa-router')
const router = new koaRouter({
prefix: '/api'
})
const book = require('./model/book')
router.get('/books', async ctx => {
const data = await book.findAll()
ctx.body = {
code: 0,
data,
msg: ''
}
})
router.post('/books', async ctx => {
const body = ctx.request.body
const data = await book.create(body)
ctx.body = {
code: 0,
data,
msg: ''
}
})
router.del('/books/:id', async ctx => {
const data = await book.destroy({
where: {
id: ctx.params.id
}
})
ctx.body = {
code: 0,
data,
msg: ''
}
})
router.put('/books/:id', async ctx => {
const instance = await book.findOne({
where: {
id: ctx.params.id
}
})
const body = ctx.request.body
const data = await instance.update({
name: body.name
})
ctx.body = {
code: 0,
data,
msg: ''
}
})
module.exports = router
\ No newline at end of file
const koa = require('koa')
const app = new koa()
const koaRouter = require('koa-router')
const Sequelize = require('sequelize')
const router = new koaRouter({})
const db = {
database: 'hunter',
user: 'root',
password: 'root',
host: 'localhost'
}
const sequelize = new Sequelize(db.database, db.user, db.password, {
host: db.host,
dialect: 'mysql',
operatorsAliases: false,
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci'
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
})
router.get('/test', async ctx => {
await sequelize
.authenticate()
.then(res => {
ctx.body = '数据库连接成功'
})
.catch(err => {
ctx.body = '数据库连接失败'
})
})
app.use(router.routes()).use(router.allowedMethods())
app.use(async ctx => {
ctx.body = 'hello world'
})
app.listen(3000)
\ No newline at end of file
{
"dependencies": {
"koa": "^2.7.0",
"koa-body": "^4.1.0",
"koa-router": "^7.4.0",
"mysql2": "^1.6.5",
"sequelize": "^5.7.1"
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment