资讯专栏INFORMATION COLUMN

Sequelize Model 使用

Sanchi / 1993人阅读

数据检索

查询特定元素

findOne

findById

</>复制代码

  1. // search for known ids
  2. Project.findById(123).then(function(project) {
  3. // project will be an instance of Project and stores the content of the table entry
  4. // with id 123. if such an entry is not defined you will get null
  5. })
  6. // search for attributes
  7. Project.findOne({ where: {title: "aProject"} }).then(function(project) {
  8. // project will be the first entry of the Projects table with the title "aProject" || null
  9. })
  10. Project.findOne({
  11. where: {title: "aProject"},
  12. attributes: ["id", ["name", "title"]]
  13. }).then(function(project) {
  14. // project will be the first entry of the Projects table with the title "aProject" || null
  15. // project.title will contain the name of the project
  16. })

findOrCreate
findAndCountAll

findAll

</>复制代码

  1. // find multiple entries
  2. Project.findAll().then(function(projects) {
  3. // projects will be an array of all Project instances
  4. })
  5. // also possible:
  6. Project.all().then(function(projects) {
  7. // projects will be an array of all Project instances
  8. })
  9. // search for specific attributes - hash usage
  10. Project.findAll({ where: { name: "A Project" } }).then(function(projects) {
  11. // projects will be an array of Project instances with the specified name
  12. })
  13. // search with string replacements
  14. Project.findAll({ where: ["id > ?", 25] }).then(function(projects) {
  15. // projects will be an array of Projects having a greater id than 25
  16. })
  17. // search within a specific range
  18. Project.findAll({ where: { id: [1,2,3] } }).then(function(projects) {
  19. // projects will be an array of Projects having the id 1, 2 or 3
  20. // this is actually doing an IN query
  21. })
  22. Project.findAll({
  23. where: {
  24. id: {
  25. $and: {a: 5} // AND (a = 5)
  26. $or: [{a: 5}, {a: 6}] // (a = 5 OR a = 6)
  27. $gt: 6, // id > 6
  28. $gte: 6, // id >= 6
  29. $lt: 10, // id < 10
  30. $lte: 10, // id <= 10
  31. $ne: 20, // id != 20
  32. $between: [6, 10], // BETWEEN 6 AND 10
  33. $notBetween: [11, 15], // NOT BETWEEN 11 AND 15
  34. $in: [1, 2], // IN [1, 2]
  35. $notIn: [1, 2], // NOT IN [1, 2]
  36. $like: "%hat", // LIKE "%hat"
  37. $notLike: "%hat" // NOT LIKE "%hat"
  38. $iLike: "%hat" // ILIKE "%hat" (case insensitive) (PG only)
  39. $notILike: "%hat" // NOT ILIKE "%hat" (PG only)
  40. $overlap: [1, 2] // && [1, 2] (PG array overlap operator)
  41. $contains: [1, 2] // @> [1, 2] (PG array contains operator)
  42. $contained: [1, 2] // <@ [1, 2] (PG array contained by operator)
  43. $any: [2,3] // ANY ARRAY[2, 3]::INTEGER (PG only)
  44. },
  45. status: {
  46. $not: false, // status NOT FALSE
  47. }
  48. }
  49. })

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

转载请注明本文地址: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元查看
<