当前位置:网站首页>Simple use of sequenize (add, delete, search, modify)

Simple use of sequenize (add, delete, search, modify)

2020-12-06 08:36:38 osc_ a949oaqo

One 、 explain

It uses mysql database , The database is called koa2_weibo_db, There's a total of 2 A watch , a sheet blogs surface , a sheet users surface

  • blogs
     Insert picture description here

  • users
     Insert picture description here

Two 、 install

npm i mysql2 sequelize -d 

3、 ... and 、 Create connection

const Sequelize = require('sequelize')
const conf = {
   
   
    host:'localhost',
    dialect:'mysql'
}
const seq = new Sequelize('koa2_weibo_db','root','root',conf)
module.exports = seq

Four 、 Creating models

// Introduce modules 
const Sequelize = require('sequelize')
const seq = require('./seq')

// establish  User  Model . The name of the data table is users
const  User  = seq.define('user',{
   
   
    //id Automatically created , And automatically incrementing 
    userName:{
   
   
        type:Sequelize.STRING,//vachar(255)
        allowNull:false
    },
    password:{
   
   
        type:Sequelize.STRING,//vachar(255)
        allowNull:false
    },
    nickName:{
   
   
        type:Sequelize.STRING,
        commit:' nickname '
    }
})

// establish  Blog  Model . The name of the data table is blogs
const  Blog  = seq.define('blogs',{
   
   
    //id Automatically created , And automatically incrementing 
    title:{
   
   
        type:Sequelize.STRING,//vachar(255)
        allowNull:false
    },
    content:{
   
   
        type:Sequelize.TEXT,
        allowNull:false
    },
    userId:{
   
   
        type:Sequelize.INTEGER,
        allowNull:false
    }
})
module.exports = {
   
   
    User,
    Blog
}

5、 ... and 、 increase

 const {
   
    Blog, User } = require('./mode')
 
 !(async function(){
   
   
     // Create user 
     const zhangsan = await User.create({
   
   
         userName:'zhangsan',
         password:'123',
         nickName:' Zhang San '
     })
     const zhangsanId = zhangsan.dataValues.id 
     console.log('zhangshan:',zhangsan.dataValues)

     const lisi = await User.create({
   
   
        userName:'lisi',
        password:'123',
        nickName:' Li Si '
    })
    const lisiId = lisi.dataValues.id

    const blog1 = await Blog.create({
   
   
        title:' title 1',
        content:' Content 1',
        userId:zhangsanId
    })
    const blog2 = await Blog.create({
   
   
        title:' title 2',
        content:' Content 2',
        userId:zhangsanId
    })
    const blog3 = await Blog.create({
   
   
        title:' title 3',
        content:' Content 3',
        userId:lisiId
    })
    const blog4 = await Blog.create({
   
   
        title:' title 4',
        content:' Content 4',
        userId:lisiId
    })
 })()

6、 ... and 、 Delete

const {
   
    User, Blog} = require('./mode') 
!(async function(){
   
   
    const destroyRes = await User.destroy({
   
   
        where:{
   
   
            id:2
        }
    })
    console.log('destroy......',destroyRes)
})()

7、 ... and 、 lookup

const {
   
    Blog, User } = require('./mode')
 
!(async function(){
   
   
     A record 
    const zhangsan = await User.findOne({
   
   
        where:{
   
   
            userName:'zhangsan'
        }
    })
    console.log('zhangsan:',zhangsan.dataValues)
    
     Query specific columns 
    const zhangsanName = await User.findOne({
   
   
        attributes:['userName','nickName'],
        where:{
   
   
            userName:'zhangsan'
        }
    })
    console.log('zhangsanName:',zhangsanName.dataValues)

     Query a list 
    const zhangsanBlogList = await Blog.findAll({
   
   
        where:{
   
   
            userId:1
        },
        order:[
            ['id','desc']
        ]
    })
    console.log('zhangsanBlogList:',zhangsanBlogList.map(blog => blog.dataValues))

     Pagination 
    const blogPageList = await Blog.findAll({
   
   
        limit:2,
        offset:0,
        order:[
            ['id','desc']
        ]
    })
    console.log('zhangsanBlogList:',blogPageList.map(blog => blog.dataValues))

     Total number of queries 
    const blogListAndCount = await Blog.findAndCountAll({
   
   
        limit:2,
        offset:0,
        order:[
            ['id','desc']
        ]
    })
    console.log(blogListAndCount.count)
    console.log('..............................................')
    console.log(blogListAndCount.rows.map(blog => blog.dataValues))
})()

8、 ... and 、 change

const {
   
    User } = require('./mode') 
!(async function(){
   
   
    const updateRes = await User.update({
   
   
        nickName:' Zhang San '
    },{
   
   
        where:{
   
   
            userName:'zhangsan'
        }
    })
    console.log('updating......',updateRes)
})()

版权声明
本文为[osc_ a949oaqo]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206083356623k.html