sequelize curd操作 查找 模糊匹配

本文介绍了 Sequelize 框架中常见的 CRUD(创建、读取、更新、删除)操作,包括如何创建记录、更新记录、删除记录以及查询单条和全部数据。此外,还详细讲解了如何进行模糊查询,帮助开发者高效地在 Sequelize 中进行数据管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

create

    async createUtopiaService(info) {
        let { user_id,title,content,detail,type,language } = info
        await this.deleteUtopiaService(language)
        return await UtopiaModel.create({
            title,
            content,
            detail,
            type,
            language,
            created_by: user_id
        })
    }

update

    async updateUtopiaService (info) {
        let { user_id,title,content,detail,type,language } = info
        return await UtopiaModel.update({
            title,
            content,
            detail,
            type,
            created_by: user_id,
            updated_at: CommonUtils.getCurrentDateTime()
        }, {
            where: {
                language,
                deleted_at: null
            }
        })
    }

delete

 async deleteUtopiaService (language = 0) {
        return await UtopiaModel.update({
            deleted_at: CommonUtils.getCurrentDateTime()
        }, {
            where: {
                language,
                deleted_at: null
            }
        })
    }

findOne

  async getUtopiaService (language = 0) {
        return await UtopiaModel.findOne({
            where: {
                language,
                deleted_at: null
            },
            attributes: ['id','title','content','detail']
        })
    }

findAll

 async getTop10Banner (language = 0) {
        return await BannerModel.findAll({
            where: {
                status: 1,
                language,
                deleted_at: null
            },
            attributes: [
                'id','name','alias','cover','target','target_type'
            ],
            order: [
                ['sort_code','asc'],
                ['updated_at','desc']
            ],
            limit: 10
        })
    }

模糊查询

User.findAll({
 raw: true,
  order: [
      ['name', 'DESC']
  ],  // 排序
  where: {
    // name: 'cheny', // 精确查询
    mobile_no: {
      // 模糊查询
      [Op.like]:'%' +mobile_no + '%'
    }
  },
  attributes:['id','name']// 控制查询字段
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值