Sequelize 增删改查接口

Sequelize 基本的增删改查


//查单个 findOne({
     where:{
     查询条件}})

async function findOne() {
     
    let result = await UserList.findOne({
     where: {
     id: 1}});
    console.log(result);
}

//查所有 findAll 不传条件则查询所有
async function findAll() {
     
    let result = await UserList.findAll({
     where: {
     id: 1}});
    console.log(result);
}

//更新
async function update() {
     
    let res = await UserList.update({
     userphone: 15111111111}, {
     where: {
     id: 1}});
    console.log(res);
}

//删除
async function destroy() {
     
    let result = await UserList.destroy({
     where: {
     id: 15}});
    console.log(result);
}

//查询插入  不存在 => 插入, 存在 => 不插入
async function findCreateFind() {
     
//默认的: 查询的条件,没有就创建,有就不作为           
let result = await UserList.findCreateFind({
     defaults: {
     userphone:xxx,password:'111'}, where: {
     username:xx}});
    console.log(result);
}

更多请参考官网:sequelize官网

你可能感兴趣的:(sequelize,后端,数据库)