const Sequelize = require('sequelize');
const sequelize = new Sequelize('databaseName', 'userName', 'password', {
host:'localhost', // 数据库服务地址
dialect: 'mysql' // SQL语言类型
});
sequelize.authenticate().then(()=>{
console.log('Connected');
}).catch(err=>{
console.error('Connect failed');
})
docker-compose.yml
写配置文件如下:version: '3.1'
services:
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MTSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
使用如下命令生成docker
docker-compose up
const Sequelize = require('sequelize');
const sequelize = new Sequelize('数据库名称','用户名(默认:root)','密码(docker-compose.yml中设置的)', {
host:'localhost',
dialect: 'mysql'
});
sequelize.authenticate().then(()=>{
console.log('Connected');
})
.catch(err=>{
console.error('Connect failed', err);
})
const Category = sequelize.define('category', {
id: Sequelize.UUID, // 定义id字段,类型为UUID
name: Sequelize.STRING // 定义name字段,类型为String
})
const Project = sequelize.define('project', {
name: {
type: Sequelize.STRING, // 定位类型为String
allowNull: false, //不能为空
unique: true // 必须唯一,不允许重复
},
date: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW // 设置为当前时间
}
})
const Custom = sequelize.define('custom', {
name: {
type: Sequelize.STRING,
get(){
const title = this.getDataValue('title');
return `${this.getDataValue('name')} (${titile}) `
}
},
title: {
title: Sequelize.STRING,
set (val) {
this.setDataValue('title', val.toUpperCase())
}
}
})
查询所有
await Product.findAll()
查询name和data字段
await Project.findAll({ attributes: ['name', 'date'] })
const Sequelize = require('sequelize');
const sequelize = new Sequelize('custom', 'root', 'example', {
dialect:'mysql'
});
// 定义Customer模型
const Customer = sequelize.define('customer',{
id:{
type: Sequelize.UUID,
unique: true,
primaryKey: true,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
sex: {
type: Sequelize.ENUM(['男','女']),
allowNull: false
},
address:{
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING,
allowNull: false
},
phone: {
type: Sequelize.STRING
},
country:{
type: Sequelize.STRING
},
city: {
type:Sequelize.STRING
}
});
/mysql/db.js
中,对表的功能进行加工const { Customer } = require('./model/custom');
const { Op } = require('sequelize');
async function getAllCustomers() {
return Customer.findAndCountAll({
attributes: ['id', 'name', 'sex', 'fulladdress'],
order: [
['updatedAt', 'DESC']
]
})
}
async function getCustomerById(id) {
return Customer.findById(id);
}
async function getCustomerByName(name) {
return Customer.findAll({
where: {
name: {
[Op.like]: `${name}`
}
}
})
}
async function updateCustomer(id, customer) {
const item = await getCustomerById(id)
if (item) {
return item.update(customer);
} else {
throw new Error('the customer with id ${id} is not exist');
}
}
async function createCustomer(customer) {
return Customer.create(customer);
}
async function deleteCustomer(id) {
const customer = await getCustomerById(id);
if (customer) {
return customer.destroy();
}
}
/mysql/app.js
中对对应的路由设置数据库的操作方法(路由层还未抽离出来)const {
getAllCustomers,
getCustomerById,
getCustomerByName,
createCustomer,
updateCustomer,
deleteCustomer
} = require('./db');
const koa = require('koa');
const app = new koa();
const router = new require('koa-router')();
const bodyParser = require('koa-bodyparser');
app.use(async (ctx, next) => {
try {
await next();
} catch (ex) {
// ctx.type = jsonMIME;
ctx.body = {
status: -1,
message: ex.message
}
}
})
router.get('/customer', async ctx => {
const customers = await getAllCustomers();
// ctx.type = jsonMIME;
ctx.body = {
status: 0,
data: customers
};
});
router.get('/customer/:id', async ctx => {
const customer = await getCUstomerById(ctx.params.id);
// ctx.type = jsonMIME;
ctx.body = {
status: 0,
data: customer
};
});
router.get('/customer/name/:name', async ctx => {
const customer = await getCUstomerByName(ctx.params.name);
// ctx.type = jsonMIME;
ctx.body = {
status: 0,
data: customer
};
});
router.post('/customer', async ctx => {
const customer = ctx.body;
await createCustomer(customer);
// ctx.type = jsonMIME;
ctx.body = {
status: 0
};
});
router.put('/customer/:id', async ctx => {
const id = ctx.params.id;
const customer = ctx.body;
await updateCustomer(id, customer);
// ctx.type = jsonMIME;
ctx.body = {
status: 0
};
});
router.delete('/customer/:id', async ctx => {
await deleteCustomer(ctx.params.id);
// ctx.type = jsonMIME;
ctx.body = {
stauts: 0
};
});
app.use(bodyParser());
app.use(router.routes());
app.listen(3000, async () => {
console.log('Server is running at http://localhost:3000');
})