完成sequlize加koa2对数据库presql的curd操作步骤

koa2 info//新建一个koa2项目
初始化项目
npm install  

npm i sequelize

npm i pg

代码
config.js

module.exports = {
    "development": {
        "username": "postgres",
        "password": "root",
        "database": "mydb",
        "host": "127.0.0.1",
        "dialect": "postgres"
    }
    // "test": {
    //     "username": "postgres",
    //     "password": "root",
    //     "database": "database_test",
    //     "host": "127.0.0.1",
    //     "dialect": "postgres"
    // },
    // "production": {
    //     "username": "postgres",
    //     "password": "root",
    //     "database": "database_production",
    //     "host": "127.0.0.1",
    //     "dialect": "postgres"
    // }
}
models.index.js

//models/index.js
var Sequelize = require('sequelize');
var db = require('../config/config');
var sequelize = new Sequelize(db.database, db.username, db.password, db.development);


// importing models
// 新代码
var User = require("../models/user")(sequelize, Sequelize.DataTypes);


// creating associations
User.sync({ force: true }).then(async() => {
    // Table created
    return await User.create({
        firstName: 'John',
        lastName: 'Hancock'
    });
});


module.exports = {
    User
};
/models/user.js

//models/user.js
module.exports = (sequelize, DataTypes) => {
    return sequelize.define('user', {
        username: DataTypes.STRING,
        password: DataTypes.STRING
    });
};
routers/users.js

//routes/users.js
const router = require('koa-router')();
const models = require('../models/index');


router.prefix('/test')
    // 新增用户
router.post('/save', async(ctx) => {
    const { username, password } = ctx.request.body;
    const newUser = await models.User.create({
        username,
        password
    });


    ctx.body = {
        message: 'ok',
        data: newUser
    }
    console.log('新增数据完成');
});


// 获取用户列表
router.get('/', async(ctx) => {
    const users = await models.User.findAll();


    ctx.body = {
        message: 'ok',
        data: users
    }
    console.log('检查完全部数据');
});




// 获取指定用户信息
router.get('/:id', async(ctx) => {
    const { id } = ctx.params;
    const user = await models.User.findById(id);


    ctx.body = {
        message: 'ok',
        data: user
    }
});


// 更新用户信息
router.put('/:id', async(ctx) => {
    const { id } = ctx.params;
    const { username, password } = ctx.request.body;
    const user = await models.User.update({
        username,
        password
    }, {
        where: {
            id
        }
    });
    ctx.body = {
        message: 'ok',
        data: user
    }
});


// 删除用户
router.delete('/:id', async(ctx) => {
    const { id } = ctx.params;
    await models.User.destroy({
        where: {
            id
        }
    });


    ctx.body = {
        message: 'ok'
    }
});
module.exports = router;
app.js 加入

const users = require('./routes/users')
app.use(users.routes(), users.allowedMethods())

 完成后开始测试运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钓愚者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值