Model usage - 模型使用

    在本文中,我们将探讨 finder 方法可以做什么:

    findOrCreate - 搜索特定元素或创建它(如果不可用)

    方法 findOrCreate 可用于检查数据库中是否已存在某个元素。 如果是这种情况,则该方法将生成相应的实例。 如果元素不存在,将会被创建。

    如果是这种情况,则该方法将导致相应的实例。 如果元素不存在,将会被创建。

    假设我们有一个空的数据库,一个 User 模型有一个 usernamejob

    1. User
    2. .findOrCreate({where: {username: 'sdepold'}, defaults: {job: 'Technical Lead JavaScript'}})
    3. .spread((user, created) => {
    4. console.log(user.get({
    5. plain: true
    6. }))
    7. console.log(created)
    8. /*
    9. findOrCreate 返回一个包含已找到或创建的对象的数组,找到或创建的对象和一个布尔值,如果创建一个新对象将为true,否则为false,像这样:
    10. [ {
    11. username: 'sdepold',
    12. job: 'Technical Lead JavaScript',
    13. id: 1,
    14. createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET),
    15. updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET)
    16. },
    17. true ]
    18. 在上面的例子中,".spread" 将数组分成2部分,并将它们作为参数传递给回调函数,在这种情况下将它们视为 "user" 和 "created" 。(所以“user”将是返回数组的索引0的对象,并且 "created" 将等于 "true"。)
    19. */
    20. })

    代码创建了一个新的实例。 所以当我们已经有一个实例了 …

    1. User.create({ username: 'fnord', job: 'omnomnom' })
    2. .then(() => User.findOrCreate({where: {username: 'fnord'}, defaults: {job: 'something else'}}))
    3. .spread((user, created) => {
    4. console.log(user.get({
    5. plain: true
    6. }))
    7. console.log(created)
    8. /*
    9. 在这个例子中,findOrCreate 返回一个如下的数组:
    10. [ {
    11. username: 'fnord',
    12. job: 'omnomnom',
    13. id: 2,
    14. createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET),
    15. updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET)
    16. },
    17. false
    18. ]
    19. 由findOrCreate返回的数组通过 ".spread" 扩展为两部分,并且这些部分将作为2个参数传递给回调函数,在这种情况下将其视为 "user" 和 "created" 。(所以“user”将是返回数组的索引0的对象,并且 "created" 将等于 "false"。)
    20. */
    21. })

    …现有条目将不会更改。 看到第二个用户的 “job”,并且实际上创建操作是假的。

    findAndCountAll - 在数据库中搜索多个元素,返回数据和总计数

    这是一个方便的方法,它结合了 findAllcount(见下文),当处理与分页相关的查询时,这是有用的,你想用 limitoffset 检索数据,但也需要知道总数与查询匹配的记录数:

    处理程序成功将始终接收具有两个属性的对象:

    • count - 一个整数,总数记录匹配where语句和关联的其它过滤器
    • rows - 一个数组对象,记录在limit和offset范围内匹配where语句和关联的其它过滤器,
    1. Project
    2. .findAndCountAll({
    3. where: {
    4. title: {
    5. [Op.like]: 'foo%'
    6. }
    7. },
    8. offset: 10,
    9. limit: 2
    10. })
    11. .then(result => {
    12. console.log(result.count);
    13. console.log(result.rows);
    14. });

    它支持 include。 只有标记为 required 的 include 将被添加到计数部分:

    假设您想查找附有个人资料的所有用户:

    1. User.findAndCountAll({
    2. include: [
    3. { model: Profile, required: true}
    4. ],
    5. limit: 3
    6. });

    因为 Profile 的 include 有 required 设置,这将导致内部连接,并且只有具有 profile 的用户将被计数。 如果我们从 include 中删除required,那么有和没有 profile 的用户都将被计数。 在include中添加一个 where 语句会自动使它成为 required:

    1. User.findAndCountAll({
    2. include: [
    3. { model: Profile, where: { active: true }}
    4. ],
    5. limit: 3
    6. });

    上面的查询只会对具有 active profile 的用户进行计数,因为在将 where 语句添加到 include 时,required 被隐式设置为 true。

    传递给 findAndCountAll 的 options 对象与 findAll 相同(如下所述)。

    findAll - 搜索数据库中的多个元素

    1. // 找到多个条目
    2. Project.findAll().then(projects => {
    3. // projects 将是所有 Project 实例的数组
    4. })
    5. // 也可以:
    6. Project.all().then(projects => {
    7. // projects 将是所有 Project 实例的数组
    8. })
    9. // 搜索特定属性 - 使用哈希
    10. Project.findAll({ where: { name: 'A Project' } }).then(projects => {
    11. // projects将是一个具有指定 name 的 Project 实例数组
    12. })
    13. // 在特定范围内进行搜索
    14. Project.findAll({ where: { id: [1,2,3] } }).then(projects => {
    15. // projects将是一系列具有 id 1,2 或 3 的项目
    16. // 这实际上是在做一个 IN 查询
    17. })
    18. Project.findAll({
    19. where: {
    20. id: {
    21. [Op.and]: {a: 5}, // 且 (a = 5)
    22. [Op.or]: [{a: 5}, {a: 6}], // (a = 5 或 a = 6)
    23. [Op.gt]: 6, // id > 6
    24. [Op.gte]: 6, // id >= 6
    25. [Op.lt]: 10, // id < 10
    26. [Op.lte]: 10, // id <= 10
    27. [Op.ne]: 20, // id != 20
    28. [Op.between]: [6, 10], // 在 6 和 10 之间
    29. [Op.notBetween]: [11, 15], // 不在 11 和 15 之间
    30. [Op.in]: [1, 2], // 在 [1, 2] 之中
    31. [Op.notIn]: [1, 2], // 不在 [1, 2] 之中
    32. [Op.like]: '%hat', // 包含 '%hat'
    33. [Op.notLike]: '%hat', // 不包含 '%hat'
    34. [Op.iLike]: '%hat', // 包含 '%hat' (不区分大小写) (仅限 PG)
    35. [Op.notILike]: '%hat', // 不包含 '%hat' (仅限 PG)
    36. [Op.overlap]: [1, 2], // && [1, 2] (PG数组重叠运算符)
    37. [Op.contains]: [1, 2], // @> [1, 2] (PG数组包含运算符)
    38. [Op.contained]: [1, 2], // <@ [1, 2] (PG数组包含于运算符)
    39. [Op.any]: [2,3], // 任何数组[2, 3]::INTEGER (仅限 PG)
    40. },
    41. status: {
    42. [Op.not]: false, // status 不为 FALSE
    43. }
    44. }
    45. })

    复合过滤 / OR / NOT 查询

    你可以使用多层嵌套的 AND,OR 和 NOT 条件进行一个复合的 where 查询。 为了做到这一点,你可以使用 orandnot 运算符:

    1. Project.findOne({
    2. where: {
    3. name: 'a project',
    4. [Op.or]: [
    5. { id: [1,2,3] },
    6. { id: { [Op.gt]: 10 } }
    7. ]
    8. }
    9. })
    10. Project.findOne({
    11. where: {
    12. name: 'a project',
    13. id: {
    14. [Op.or]: [
    15. [1,2,3],
    16. { [Op.gt]: 10 }
    17. ]
    18. }
    19. }
    20. })
    1. SELECT *
    2. FROM `Projects`
    3. WHERE (
    4. )
    5. LIMIT 1;

    not 示例:

    1. Project.findOne({
    2. where: {
    3. name: 'a project',
    4. [Op.not]: [
    5. { id: [1,2,3] },
    6. { array: { [Op.contains]: [3,4,5] } }
    7. ]
    8. }
    9. });

    将生成:

    1. SELECT *
    2. FROM `Projects`
    3. WHERE (
    4. `Projects`.`name` = 'a project'
    5. AND NOT (`Projects`.`id` IN (1,2,3) OR `Projects`.`array` @> ARRAY[3,4,5]::INTEGER[])
    6. )
    7. LIMIT 1;

    要获取更多相关数据,可以使用限制,偏移,顺序和分组:

    分组和排序的语法是相同的,所以下面只用一个单独的例子来解释分组,而其余的则是排序。 您下面看到的所有内容也可以对分组进行

    1. Project.findAll({order: 'title DESC'})
    2. // 生成 ORDER BY title DESC
    3. Project.findAll({group: 'name'})
    4. // 生成 GROUP BY name

    请注意,在上述两个示例中,提供的字符串逐字插入到查询中,所以不会转义列名称。 当你向 order / group 提供字符串时,将始终如此。 如果要转义列名,您应该提供一个参数数组,即使您只想通过单个列进行 order / group

    1. something.findOne({
    2. order: [
    3. // 将返回 `name`
    4. ['name'],
    5. // 将返回 `username` DESC
    6. ['username', 'DESC'],
    7. // 将返回 max(`age`)
    8. sequelize.fn('max', sequelize.col('age')),
    9. // 将返回 max(`age`) DESC
    10. [sequelize.fn('max', sequelize.col('age')), 'DESC'],
    11. // 将返回 otherfunction(`col1`, 12, 'lalala') DESC
    12. [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],
    13. // 将返回 otherfunction(awesomefunction(`col`)) DESC,这个嵌套是可以无限的!
    14. [sequelize.fn('otherfunction', sequelize.fn('awesomefunction', sequelize.col('col'))), 'DESC']
    15. ]
    16. })

    回顾一下,order / group数组的元素可以是以下内容:

    • String - 将被引用
    • Array - 第一个元素将被引用,第二个将被逐字地追加
    • Object -
      • raw 将被添加逐字引用
      • 如果未设置 raw,一切都被忽略,查询将失败
    • Sequelize.fn 和 Sequelize.col 返回函数和引用的列名

    原始查询

    有时候,你可能会期待一个你想要显示的大量数据集,而无需操作。 对于你选择的每一行,Sequelize 创建一个具有更新,删除和获取关联等功能的实例。如果您有数千行,则可能需要一些时间。 如果您只需要原始数据,并且不想更新任何内容,您可以这样做来获取原始数据。

    1. // 你期望从数据库的一个巨大的数据集,
    2. // 并且不想花时间为每个条目构建DAO?
    3. // 您可以传递一个额外的查询参数来取代原始数据:
    4. Project.findAll({ where: { ... }, raw: true })

    count - 计算数据库中元素的出现次数

    还有一种数据库对象计数的方法:

    1. Project.count().then(c => {
    2. console.log("There are " + c + " projects!")
    3. })
    4. Project.count({ where: {'id': {[Op.gt]: 25}} }).then(c => {
    5. console.log("There are " + c + " projects with an id greater than 25.")
    6. })

    max - 获取特定表中特定属性的最大值

    这里是获取属性的最大值的方法:

    1. /*
    2. 我们假设3个具有属性年龄的对象。
    3. 第一个是10岁,
    4. 第二个是5岁,
    5. 第三个是40岁。
    6. */
    7. Project.max('age').then(max => {
    8. // 将返回 40
    9. })
    10. Project.max('age', { where: { age: { [Op.lt]: 20 } } }).then(max => {
    11. // 将会是 10
    12. })

    min - 获取特定表中特定属性的最小值

    这里是获取属性的最小值的方法:

    1. /*
    2. 我们假设3个具有属性年龄的对象。
    3. 第一个是10岁,
    4. 第二个是5岁,
    5. 第三个是40岁。
    6. */
    7. Project.min('age').then(min => {
    8. // 将返回 5
    9. })
    10. Project.min('age', { where: { age: { [Op.gt]: 5 } } }).then(min => {
    11. // 将会是 10
    12. })

    为了计算表的特定列的总和,可以使用“sum”方法。

    1. /*
    2. 我们假设3个具有属性年龄的对象。
    3. 第一个是10岁,
    4. 第二个是5岁,
    5. 第三个是40岁。
    6. */
    7. Project.sum('age').then(sum => {
    8. // 将返回 55
    9. })
    10. Project.sum('age', { where: { age: { [Op.gt]: 5 } } }).then(sum => {
    11. // 将会是 50
    12. })

    预加载

    当你从数据库检索数据时,也想同时获得与之相关联的查询,这被称为预加载。这个基本思路就是当你调用 findfindAll 时使用 include 属性。让我们假设以下设置:

    1. const User = sequelize.define('user', { name: Sequelize.STRING })
    2. const Task = sequelize.define('task', { name: Sequelize.STRING })
    3. const Tool = sequelize.define('tool', { name: Sequelize.STRING })
    4. Task.belongsTo(User)
    5. User.hasMany(Task)
    6. User.hasMany(Tool, { as: 'Instruments' })
    7. sequelize.sync().then(() => {
    8. // 这是我们继续的地方 ...
    9. })

    首先,让我们用它们的关联 user 加载所有的 task。

    1. Task.findAll({ include: [ User ] }).then(tasks => {
    2. console.log(JSON.stringify(tasks))
    3. /*
    4. [{
    5. "name": "A Task",
    6. "id": 1,
    7. "createdAt": "2013-03-20T20:31:40.000Z",
    8. "updatedAt": "2013-03-20T20:31:40.000Z",
    9. "userId": 1,
    10. "user": {
    11. "name": "John Doe",
    12. "id": 1,
    13. "createdAt": "2013-03-20T20:31:45.000Z",
    14. "updatedAt": "2013-03-20T20:31:45.000Z"
    15. }
    16. }]
    17. */
    18. })

    请注意,访问者(结果实例中的 User 属性)是单数形式,因为关联是一对一的。

    1. User.findAll({ include: [ Task ] }).then(users => {
    2. console.log(JSON.stringify(users))
    3. /*
    4. [{
    5. "name": "John Doe",
    6. "id": 1,
    7. "createdAt": "2013-03-20T20:31:45.000Z",
    8. "updatedAt": "2013-03-20T20:31:45.000Z",
    9. "tasks": [{
    10. "name": "A Task",
    11. "id": 1,
    12. "createdAt": "2013-03-20T20:31:40.000Z",
    13. "updatedAt": "2013-03-20T20:31:40.000Z",
    14. "userId": 1
    15. }]
    16. }]
    17. */
    18. })

    请注意,访问者(结果实例中的 Tasks 属性)是复数形式,因为关联是多对一的。

    如果关联是别名的(使用 as 参数),则在包含模型时必须指定此别名。 注意用户的 Tool 如何被别名为 Instruments。 为了获得正确的权限,您必须指定要加载的模型以及别名:

    您还可以通过指定与关联别名匹配的字符串来包含别名:

    1. User.findAll({ include: ['Instruments'] }).then(users => {
    2. console.log(JSON.stringify(users))
    3. /*
    4. [{
    5. "name": "John Doe",
    6. "id": 1,
    7. "createdAt": "2013-03-20T20:31:45.000Z",
    8. "updatedAt": "2013-03-20T20:31:45.000Z",
    9. "Instruments": [{
    10. "name": "Toothpick",
    11. "id": 1,
    12. "createdAt": null,
    13. "updatedAt": null,
    14. "userId": 1
    15. }]
    16. }]
    17. */
    18. })
    19. User.findAll({ include: [{ association: 'Instruments' }] }).then(users => {
    20. /*
    21. [{
    22. "id": 1,
    23. "createdAt": "2013-03-20T20:31:45.000Z",
    24. "updatedAt": "2013-03-20T20:31:45.000Z",
    25. "Instruments": [{
    26. "name": "Toothpick",
    27. "id": 1,
    28. "createdAt": null,
    29. "updatedAt": null,
    30. "userId": 1
    31. }]
    32. }]
    33. */
    34. })

    当预加载时,我们也可以使用 where 过滤关联的模型。 这将返回 Tool 模型中所有与 where 语句匹配的行的User

    1. User.findAll({
    2. include: [{
    3. model: Tool,
    4. as: 'Instruments',
    5. where: { name: { [Op.like]: '%ooth%' } }
    6. }]
    7. }).then(users => {
    8. console.log(JSON.stringify(users))
    9. /*
    10. [{
    11. "name": "John Doe",
    12. "id": 1,
    13. "createdAt": "2013-03-20T20:31:45.000Z",
    14. "updatedAt": "2013-03-20T20:31:45.000Z",
    15. "Instruments": [{
    16. "name": "Toothpick",
    17. "id": 1,
    18. "createdAt": null,
    19. "updatedAt": null,
    20. "userId": 1
    21. }]
    22. }],
    23. [{
    24. "name": "John Smith",
    25. "id": 2,
    26. "createdAt": "2013-03-20T20:31:45.000Z",
    27. "updatedAt": "2013-03-20T20:31:45.000Z",
    28. "Instruments": [{
    29. "name": "Toothpick",
    30. "id": 1,
    31. "createdAt": null,
    32. "updatedAt": null,
    33. "userId": 1
    34. }]
    35. }],
    36. */
    37. })

    当使用 include.where 过滤一个预加载的模型时,include.required 被隐式设置为 true。 这意味着内部联接完成返回具有任何匹配子项的父模型。

    使用预加载模型的顶层 WHERE

    将模型的 WHERE 条件从 ON 条件的 include 模式移动到顶层,你可以使用 '$nested.column$' 语法:

    1. User.findAll({
    2. where: {
    3. '$Instruments.name$': { [Op.iLike]: '%ooth%' }
    4. },
    5. include: [{
    6. model: Tool,
    7. as: 'Instruments'
    8. }]
    9. }).then(users => {
    10. console.log(JSON.stringify(users));
    11. /*
    12. [{
    13. "name": "John Doe",
    14. "id": 1,
    15. "createdAt": "2013-03-20T20:31:45.000Z",
    16. "updatedAt": "2013-03-20T20:31:45.000Z",
    17. "Instruments": [{
    18. "name": "Toothpick",
    19. "id": 1,
    20. "createdAt": null,
    21. "updatedAt": null,
    22. "userId": 1
    23. }]
    24. }],
    25. [{
    26. "name": "John Smith",
    27. "id": 2,
    28. "createdAt": "2013-03-20T20:31:45.000Z",
    29. "updatedAt": "2013-03-20T20:31:45.000Z",
    30. "Instruments": [{
    31. "name": "Toothpick",
    32. "id": 1,
    33. "createdAt": null,
    34. "updatedAt": null,
    35. "userId": 1
    36. }]
    37. }],
    38. */

    包括所有

    要包含所有属性,您可以使用 all:true 传递单个对象:

    1. User.findAll({ include: [{ all: true }]});

    包括软删除的记录

    如果想要加载软删除的记录,可以通过将 include.paranoid 设置为 false 来实现

    1. User.findAll({
    2. include: [{
    3. model: Tool,
    4. where: { name: { [Op.like]: '%ooth%' } },
    5. paranoid: false // query and loads the soft deleted records
    6. }]
    7. });

    排序预加载关联

    在一对多关系的情况下。

    1. Company.findAll({ include: [ Division ], order: [ [ Division, 'name' ] ] });
    2. Company.findAll({ include: [ Division ], order: [ [ Division, 'name', 'DESC' ] ] });
    3. Company.findAll({
    4. include: [ { model: Division, as: 'Div' } ],
    5. order: [ [ { model: Division, as: 'Div' }, 'name' ] ]
    6. });
    7. Company.findAll({
    8. include: [ { model: Division, as: 'Div' } ],
    9. order: [ [ { model: Division, as: 'Div' }, 'name', 'DESC' ] ]
    10. });
    11. Company.findAll({
    12. include: [ { model: Division, include: [ Department ] } ],
    13. order: [ [ Division, Department, 'name' ] ]
    14. });

    在多对多关系的情况下,您还可以通过表中的属性进行排序。

    1. Company.findAll({
    2. include: [ { model: Division, include: [ Department ] } ],
    3. order: [ [ Division, DepartmentDivision, 'name' ] ]
    4. });

    您可以使用嵌套的预加载来加载相关模型的所有相关模型:

    1. User.findAll({
    2. include: [
    3. {model: Tool, as: 'Instruments', include: [
    4. {model: Teacher, include: [ /* etc */]}
    5. ]}
    6. ]
    7. }).then(users => {
    8. console.log(JSON.stringify(users))
    9. /*
    10. [{
    11. "name": "John Doe",
    12. "id": 1,
    13. "createdAt": "2013-03-20T20:31:45.000Z",
    14. "updatedAt": "2013-03-20T20:31:45.000Z",
    15. "Instruments": [{ // 1:M and N:M association
    16. "name": "Toothpick",
    17. "id": 1,
    18. "createdAt": null,
    19. "updatedAt": null,
    20. "userId": 1,
    21. "Teacher": { // 1:1 association
    22. "name": "Jimi Hendrix"
    23. }
    24. }]
    25. }]
    26. */
    27. })

    这将产生一个外连接。 但是,相关模型上的 where 语句将创建一个内部连接,并仅返回具有匹配子模型的实例。 要返回所有父实例,您应该添加 required: false

    1. User.findAll({
    2. include: [{
    3. model: Tool,
    4. as: 'Instruments',
    5. include: [{
    6. model: Teacher,
    7. where: {
    8. school: "Woodstock Music School"
    9. },
    10. required: false
    11. }]
    12. }]
    13. }).then(users => {
    14. /* ... */
    15. })

    以上查询将返回所有用户及其所有乐器,但只会返回与 Woodstock Music School 相关的老师。

    包括所有也支持嵌套加载: