资讯专栏INFORMATION COLUMN

Sequelize Model 使用

Sanchi / 1727人阅读

数据检索

查询特定元素

findOne

findById

// search for known ids
Project.findById(123).then(function(project) {
  // project will be an instance of Project and stores the content of the table entry
  // with id 123. if such an entry is not defined you will get null
})

// search for attributes
Project.findOne({ where: {title: "aProject"} }).then(function(project) {
  // project will be the first entry of the Projects table with the title "aProject" || null
})


Project.findOne({
  where: {title: "aProject"},
  attributes: ["id", ["name", "title"]]
}).then(function(project) {
  // project will be the first entry of the Projects table with the title "aProject" || null
  // project.title will contain the name of the project
})

findOrCreate
findAndCountAll

findAll

// find multiple entries
Project.findAll().then(function(projects) {
  // projects will be an array of all Project instances
})

// also possible:
Project.all().then(function(projects) {
  // projects will be an array of all Project instances
})

// search for specific attributes - hash usage
Project.findAll({ where: { name: "A Project" } }).then(function(projects) {
  // projects will be an array of Project instances with the specified name
})

// search with string replacements
Project.findAll({ where: ["id > ?", 25] }).then(function(projects) {
  // projects will be an array of Projects having a greater id than 25
})

// search within a specific range
Project.findAll({ where: { id: [1,2,3] } }).then(function(projects) {
  // projects will be an array of Projects having the id 1, 2 or 3
  // this is actually doing an IN query
})

Project.findAll({
  where: {
    id: {
      $and: {a: 5}           // AND (a = 5)
      $or: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)
      $gt: 6,                // id > 6
      $gte: 6,               // id >= 6
      $lt: 10,               // id < 10
      $lte: 10,              // id <= 10
      $ne: 20,               // id != 20
      $between: [6, 10],     // BETWEEN 6 AND 10
      $notBetween: [11, 15], // NOT BETWEEN 11 AND 15
      $in: [1, 2],           // IN [1, 2]
      $notIn: [1, 2],        // NOT IN [1, 2]
      $like: "%hat",         // LIKE "%hat"
      $notLike: "%hat"       // NOT LIKE "%hat"
      $iLike: "%hat"         // ILIKE "%hat" (case insensitive)  (PG only)
      $notILike: "%hat"      // NOT ILIKE "%hat"  (PG only)
      $overlap: [1, 2]       // && [1, 2] (PG array overlap operator)
      $contains: [1, 2]      // @> [1, 2] (PG array contains operator)
      $contained: [1, 2]     // <@ [1, 2] (PG array contained by operator)
      $any: [2,3]            // ANY ARRAY[2, 3]::INTEGER (PG only)
    },
    status: {
      $not: false,           // status NOT FALSE
    }
  }
})

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/87946.html

相关文章

  • Sequelize Model

    摘要:定义默认值和是否为空默认时间为创建时间设置为将会在数据表中添加列如果查询时该列为数据库会抛出错误如果你想在查询前检查该值是否为,看一下下面的验证部分可以是或如果多个列是相同就会变成会创建索引也可以这么创建索引主键自动增量在可以有可以通过属性 定义Model import sequelize from sequelize var Foo = sequelize.define(foo, ...

    andong777 评论0 收藏0
  • 使用TS+Sequelize实现更简洁的CRUD

    摘要:哈哈,这又是为什么呢细心的同学可能会发现,的返回值是一个类型的,所以上边并没有属性,的两个属性也是如此。我们通过在函数上边添加一个范型的定义,并且添加限制保证传入的范型类型一定是继承自的,在返回值转换其类型为,就可以实现功能了。 如果是经常使用Node来做服务端开发的童鞋,肯定不可避免的会操作数据库,做一些增删改查(CRUD,Create Read Update Delete)的操作,...

    JayChen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<