教程 > sequelize 中文教程 > 阅读:146

sequelize 模型查询(基础)——迹忆客-ag捕鱼王app官网

sequelize 提供了多种方法来协助查询数据库中的数据。

重要说明:要使用 sequelize 执行生产级别的查询,请确保你还阅读了事务指南。 事务对于确保数据完整性和提供其它好处很重要。

本篇内容将说明如何进行标准的 增删改查(crud) 查询。


简单 insert 查询

首先,一个简单的例子:

// 创建一个新用户
const jane = await user.create({ firstname: "jane", lastname: "doe" });
console.log("jane's auto-generated id:", jane.id);

model.create() 方法是使用 model.build() 构建未保存实例并使用 instance.save() 保存实例的简写形式.

也可以定义在 create 方法中的属性。 如果你基于用户填写的表单创建数据库条目,这将特别有用。 例如,使用它可以允许你将 user 模型限制为仅设置用户名和地址,而不设置管理员标志 (例如, isadmin):

const user = await user.create({
  username: 'alice123',
  isadmin: true
}, { fields: ['username'] });
// 假设 isadmin 的默认值为 false
console.log(user.username); // 'alice123'
console.log(user.isadmin); // false

简单 select 查询

你可以使用 findall 方法从数据库中读取整个表:

// 查询所有用户
const users = await user.findall();
console.log(users.every(user => user instanceof user)); // true
console.log("all users:", json.stringify(users, null, 2));
select * from ...

select 查询特定属性

选择某些特定属性,可以使用 attributes 参数:

model.findall({
  attributes: ['foo', 'bar']
});
select foo, bar from ...

可以使用嵌套数组来重命名属性:

model.findall({
  attributes: ['foo', ['bar', 'baz'], 'qux']
});
select foo, bar as baz, qux from ...

你可以使用 sequelize.fn 进行聚合:

model.findall({
  attributes: [
    'foo',
    [sequelize.fn('count', sequelize.col('hats')), 'n_hats'],
    'bar'
  ]
});
select foo, count(hats) as n_hats, bar from ...

使用聚合函数时,必须为它提供一个别名,以便能够从模型中访问它。 在上面的示例中,你可以通过 instance.n_hats 获取帽子数量.

有时,如果只想添加聚合,那么列出模型的所有属性可能会很麻烦:

// 这是获取帽子数量的烦人方法(每列都有)
model.findall({
  attributes: [
    'id', 'foo', 'bar', 'baz', 'qux', 'hats', // 我们必须列出所有属性...
    [sequelize.fn('count', sequelize.col('hats')), 'n_hats'] // 添加聚合...
  ]
});
// 这个更短,并且更不易出错. 如果以后在模型中添加/删除属性,它仍然可以正常工作
model.findall({
  attributes: {
    include: [
      [sequelize.fn('count', sequelize.col('hats')), 'n_hats']
    ]
  }
});
select id, foo, bar, baz, qux, hats, count(hats) as n_hats from ...

同样,也可以排除某些属性:

model.findall({
  attributes: { exclude: ['baz'] }
});
-- assuming all columns are 'id', 'foo', 'bar', 'baz' and 'qux'
select id, foo, bar, qux from ...

应用 where 子句

where 参数用于过滤查询。where 子句有很多运算符,可以从 op 中以 symbols 的形式使用。

基础

post.findall({
  where: {
    authorid: 2
  }
});
// select * from post where authorid = 2;

可以看到没有显式传递任何运算符(来自op),因为默认情况下 sequelize 假定进行相等比较。 上面的代码等效于:

const { op } = require("sequelize");
post.findall({
  where: {
    authorid: {
      [op.eq]: 2
    }
  }
});
// select * from post where authorid = 2;

可以传递多个校验:

post.findall({
  where: {
    authorid: 12,
    status: 'active'
  }
});
// select * from post where authorid = 12 and status = 'active';

就像在第一个示例中 sequelize 推断出 op.eq 运算符一样,在这里 sequelize 推断出调用者希望对两个检查使用 and。 上面的代码等效于:

const { op } = require("sequelize");
post.findall({
  where: {
    [op.and]: [
      { authorid: 12 },
      { status: 'active' }
    ]
  }
});
// select * from post where authorid = 12 and status = 'active';

or 可以通过类似的方式轻松执行:

const { op } = require("sequelize");
post.findall({
  where: {
    [op.or]: [
      { authorid: 12 },
      { authorid: 13 }
    ]
  }
});
// select * from post where authorid = 12 or authorid = 13;

由于以上的 or 涉及相同字段 ,因此 sequelize 允许你使用稍有不同的结构,该结构更易读并且作用相同:

const { op } = require("sequelize");
post.destroy({
  where: {
    authorid: {
      [op.or]: [12, 13]
    }
  }
});
// delete from post where authorid = 12 or authorid = 13;

操作符

sequelize 提供了多种运算符.

const { op } = require("sequelize");
post.findall({
  where: {
    [op.and]: [{ a: 5 }, { b: 6 }],            // (a = 5) and (b = 6)
    [op.or]: [{ a: 5 }, { b: 6 }],             // (a = 5) or (b = 6)
    someattribute: {
      // 基本
      [op.eq]: 3,                              // = 3
      [op.ne]: 20,                             // != 20
      [op.is]: null,                           // is null
      [op.not]: true,                          // is not true
      [op.or]: [5, 6],                         // (someattribute = 5) or (someattribute = 6)
      // 使用方言特定的列标识符 (以下示例中使用 pg):
      [op.col]: 'user.organization_id',        // = "user"."organization_id"
      // 数字比较
      [op.gt]: 6,                              // > 6
      [op.gte]: 6,                             // >= 6
      [op.lt]: 10,                             // < 10
      [op.lte]: 10,                            // <= 10
      [op.between]: [6, 10],                   // between 6 and 10
      [op.notbetween]: [11, 15],               // not between 11 and 15
      // 其它操作符
      [op.all]: sequelize.literal('select 1'), // > all (select 1)
      [op.in]: [1, 2],                         // in [1, 2]
      [op.notin]: [1, 2],                      // not in [1, 2]
      [op.like]: '%hat',                       // like '%hat'
      [op.notlike]: '%hat',                    // not like '%hat'
      [op.startswith]: 'hat',                  // like 'hat%'
      [op.endswith]: 'hat',                    // like '%hat'
      [op.substring]: 'hat',                   // like '%hat%'
      [op.ilike]: '%hat',                      // ilike '%hat' (不区分大小写) (仅 pg)
      [op.notilike]: '%hat',                   // not ilike '%hat'  (仅 pg)
      [op.regexp]: '^[h|a|t]',                 // regexp/~ '^[h|a|t]' (仅 mysql/pg)
      [op.notregexp]: '^[h|a|t]',              // not regexp/!~ '^[h|a|t]' (仅 mysql/pg)
      [op.iregexp]: '^[h|a|t]',                // ~* '^[h|a|t]' (仅 pg)
      [op.notiregexp]: '^[h|a|t]',             // !~* '^[h|a|t]' (仅 pg)
      [op.any]: [2, 3],                        // any array[2, 3]::integer (仅 pg)
      [op.match]: sequelize.fn('to_tsquery', 'fat & rat') // 匹配文本搜索字符串 'fat' 和 'rat' (仅 pg)
      // 在 postgres 中, op.like/op.ilike/op.notlike 可以结合 op.any 使用:
      [op.like]: { [op.any]: ['cat', 'hat'] }  // like any array['cat', 'hat']
      // 还有更多的仅限 postgres 的范围运算符,请参见下文
    }
  }
});

op.in 的简写语法

直接将数组参数传递给 where 将隐式使用 in 运算符:

post.findall({
  where: {
    id: [1,2,3] // 等同使用 `id: { [op.in]: [1,2,3] }`
  }
});
// select ... from "posts" as "post" where "post"."id" in (1, 2, 3);

运算符的逻辑组合

运算符 op.andop.orop.not 可用于创建任意复杂的嵌套逻辑比较.

使用 op.and 和 op.or 示例

const { op } = require("sequelize");
foo.findall({
  where: {
    rank: {
      [op.or]: {
        [op.lt]: 1000,
        [op.eq]: null
      }
    },
    // rank < 1000 or rank is null
    {
      createdat: {
        [op.lt]: new date(),
        [op.gt]: new date(new date() - 24 * 60 * 60 * 1000)
      }
    },
    // createdat < [timestamp] and createdat > [timestamp]
    {
      [op.or]: [
        {
          title: {
            [op.like]: 'boat%'
          }
        },
        {
          description: {
            [op.like]: '%boat%'
          }
        }
      ]
    }
    // title like 'boat%' or description like '%boat%'
  }
});

使用 op.not 示例

project.findall({
  where: {
    name: 'some project',
    [op.not]: [
      { id: [1,2,3] },
      {
        description: {
          [op.like]: 'hello%'
        }
      }
    ]
  }
});

上面将生成:

select *
from `projects`
where (
  `projects`.`name` = 'some project'
  and not (
    `projects`.`id` in (1,2,3)
    and
    `projects`.`description` like 'hello%'
  )
)

高级查询(不仅限于列)

如果你想得到类似 where char_length("content") = 7 的结果怎么办?

post.findall({
  where: sequelize.where(sequelize.fn('char_length', sequelize.col('content')), 7)
});
// select ... from "posts" as "post" where char_length("content") = 7

请注意方法 sequelize.fn 和 sequelize.col 的用法,应分别用于指定 sql 函数调用和列。 应该使用这些方法,而不是传递纯字符串(例如 char_length(content)),因为 sequelize 需要以不同的方式对待这种情况(例如,使用其他符号转义方法)。

如果你需要更复杂的东西怎么办?

post.findall({
  where: {
    [op.or]: [
      sequelize.where(sequelize.fn('char_length', sequelize.col('content')), 7),
      {
        content: {
          [op.like]: 'hello%'
        }
      },
      {
        [op.and]: [
          { status: 'draft' },
          sequelize.where(sequelize.fn('char_length', sequelize.col('content')), {
            [op.gt]: 10
          })
        ]
      }
    ]
  }
});

上面生成了以下sql:

select
  ...
from "posts" as "post"
where (
  char_length("content") = 7
  or
  "post"."content" like 'hello%'
  or (
    "post"."status" = 'draft'
    and
    char_length("content") > 10
  )
)

仅限 postgres 的范围运算符

可以使用所有支持的运算符查询范围类型。

请记住,提供的范围值也可以定义绑定的 包含/排除。

[op.contains]: 2,            // @> '2'::integer  (pg range 包含元素运算符)
[op.contains]: [1, 2],       // @> [1, 2)        (pg range 包含范围运算符)
[op.contained]: [1, 2],      // <@ [1, 2)        (pg range 包含于运算符)
[op.overlap]: [1, 2],        // && [1, 2)        (pg range 重叠(有共同点)运算符)
[op.adjacent]: [1, 2],       // -|- [1, 2)       (pg range 相邻运算符)
[op.strictleft]: [1, 2],     // << [1, 2)        (pg range 左严格运算符)
[op.strictright]: [1, 2],    // >> [1, 2)        (pg range 右严格运算符)
[op.noextendright]: [1, 2],  // &< [1, 2)        (pg range 未延伸到右侧运算符)
[op.noextendleft]: [1, 2],   // &> [1, 2)        (pg range 未延伸到左侧运算符)

不推荐使用: 操作符别名

在 sequelize v4 中,可以指定字符串来引用运算符,而不是使用 symbols. 现在不建议使用此方法,很可能在下一个主要版本中将其删除. 如果确实需要,可以在 sequelize 构造函数中传递 operatoraliases 参数.

例如:

const { sequelize, op } = require("sequelize");
const sequelize = new sequelize('sqlite::memory:', {
  operatorsaliases: {
    $gt: op.gt
  }
});
// 现在我们可以在 where 子句中使用 `$gt` 代替 `[op.gt]`:
foo.findall({
  where: {
    $gt: 6 // 就像使用 [op.gt]
  }
});

简单 update 查询

update 查询也接受 where 参数,就像上面的读取查询一样.

// 将所有没有姓氏的人更改为 "doe"
await user.update({ lastname: "doe" }, {
  where: {
    lastname: null
  }
});

简单 delete 查询

delete 查询也接受 where 参数,就像上面的读取查询一样。

// 删除所有名为 "jane" 的人 
await user.destroy({
  where: {
    firstname: "jane"
  }
});

要销毁所有内容,可以使用 truncate sql:

// 截断表格
await user.destroy({
  truncate: true
});

批量创建

sequelize 提供了 model.bulkcreate 方法,以允许仅一次查询即可一次创建多个记录。

通过接收数组对象而不是单个对象,model.bulkcreate 的用法与 model.create 非常相似。

const captains = await captain.bulkcreate([
  { name: 'jack sparrow' },
  { name: 'davy jones' }
]);
console.log(captains.length); // 2
console.log(captains[0] instanceof captain); // true
console.log(captains[0].name); // 'jack sparrow'
console.log(captains[0].id); // 1 // (或另一个自动生成的值)

但是,默认情况下,bulkcreate 不会在要创建的每个对象上运行验证(而 create 可以做到)。 为了使 bulkcreate 也运行这些验证,必须通过validate: true 参数。 但这会降低性能。 用法示例:

const foo = sequelize.define('foo', {
  bar: {
    type: datatypes.text,
    validate: {
      len: [4, 6]
    }
  }
});
// 这不会引发错误,两个实例都将被创建
await foo.bulkcreate([
  { name: 'abc123' },
  { name: 'name too long' }
]);
// 这将引发错误,不会创建任何内容
await foo.bulkcreate([
  { name: 'abc123' },
  { name: 'name too long' }
], { validate: true });

如果你直接从用户获取值,那么限制实际插入的列可能会有所帮助。 为了做到这一点,bulkcreate() 接受一个 fields 参数,该参数须为你要定义字段的数组(其余字段将被忽略)。

await user.bulkcreate([
  { username: 'foo' },
  { username: 'bar', admin: true }
], { fields: ['username'] });
// foo 和 bar 都不会是管理员.

排序和分组

sequelize 提供了 order and group 参数,来与 order by 和 group by 一起使用。

排序

order 参数采用一系列 项 来让 sequelize 方法对查询进行排序。 这些 项 本身是 [column, direction] 形式的数组。 该列将被正确转义,并且将在有效方向列表中进行验证(例如 asc, desc, nulls first 等)。

subtask.findall({
  order: [
    // 将转义 title 并针对有效方向列表进行降序排列
    ['title', 'desc'],
    // 将按最大年龄进行升序排序
    sequelize.fn('max', sequelize.col('age')),
    // 将按最大年龄进行降序排序
    [sequelize.fn('max', sequelize.col('age')), 'desc'],
    // 将按 otherfunction(`col1`, 12, 'lalala') 进行降序排序
    [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'desc'],
    // 将使用模型名称作为关联名称按关联模型的 createdat 排序.
    [task, 'createdat', 'desc'],
    // 将使用模型名称作为关联名称通过关联模型的 createdat 排序.
    [task, project, 'createdat', 'desc'],
    // 将使用关联名称按关联模型的 createdat 排序.
    ['task', 'createdat', 'desc'],
    // 将使用关联的名称按嵌套的关联模型的 createdat 排序.
    ['task', 'project', 'createdat', 'desc'],
    // 将使用关联对象按关联模型的 createdat 排序. (首选方法)
    [subtask.associations.task, 'createdat', 'desc'],
    // 将使用关联对象按嵌套关联模型的 createdat 排序. (首选方法)
    [subtask.associations.task, task.associations.project, 'createdat', 'desc'],
    // 将使用简单的关联对象按关联模型的 createdat 排序.
    [{model: task, as: 'task'}, 'createdat', 'desc'],
    // 将由嵌套关联模型的 createdat 简单关联对象排序.
    [{model: task, as: 'task'}, {model: project, as: 'project'}, 'createdat', 'desc']
  ],
  // 将按最大年龄降序排列
  order: sequelize.literal('max(age) desc'),
  // 如果忽略方向,则默认升序,将按最大年龄升序排序
  order: sequelize.fn('max', sequelize.col('age')),
  // 如果省略方向,则默认升序, 将按年龄升序排列
  order: sequelize.col('age'),
  // 将根据方言随机排序(但不是 fn('rand') 或 fn('random'))
  order: sequelize.random()
});
foo.findone({
  order: [
    // 将返回 `name`
    ['name'],
    // 将返回 `username` desc
    ['username', 'desc'],
    // 将返回 max(`age`)
    sequelize.fn('max', sequelize.col('age')),
    // 将返回 max(`age`) desc
    [sequelize.fn('max', sequelize.col('age')), 'desc'],
    // 将返回 otherfunction(`col1`, 12, 'lalala') desc
    [sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'desc'],
    // 将返回 otherfunction(awesomefunction(`col`)) desc, 这种嵌套可能是无限的!
    [sequelize.fn('otherfunction', sequelize.fn('awesomefunction', sequelize.col('col'))), 'desc']
  ]
});

回顾一下,order 数组的元素可以如下:

  • 一个字符串 (它将被自动引用)
  • 一个数组, 其第一个元素将被引用,第二个将被逐字追加
  • 一个具有 raw 字段的对象:
    • raw 内容将不加引用地逐字添加
    • 其他所有内容都将被忽略,如果未设置 raw,查询将失败
  • 调用 sequelize.fn (这将在 sql 中生成一个函数调用)
  • 调用 sequelize.col (这将引用列名)

分组

分组和排序的语法相同,只是分组不接受方向作为数组的最后一个参数(不存在 asc, desc, nulls first 等)。

你还可以将字符串直接传递给 group,该字符串将直接(普通)包含在生成的 sql 中。 请谨慎使用,请勿与用户生成的内容一起使用。

project.findall({ group: 'name' });
// 生成 'group by name'

限制和分页

使用 limit 和 offset 参数可以进行 限制/分页:

// 提取10个实例/行
project.findall({ limit: 10 });
// 跳过8个实例/行
project.findall({ offset: 8 });
// 跳过5个实例,然后获取5个实例
project.findall({ offset: 5, limit: 5 });

通常这些与 order 参数一起使用。


实用方法

sequelize 还提供了一些实用方法。

count

count 方法仅计算数据库中元素出现的次数.

console.log(`这有 ${await project.count()} 个项目`);
const amount = await project.count({
  where: {
    id: {
      [op.gt]: 25
    }
  }
});
console.log(`这有 ${amount} 个项目 id 大于 25`);

max, min 和 sum

sequelize 还提供了 maxminsum 便捷方法。

假设我们有三个用户,分别是10、5和40岁.

await user.max('age'); // 40
await user.max('age', { where: { age: { [op.lt]: 20 } } }); // 10
await user.min('age'); // 5
await user.min('age', { where: { age: { [op.gt]: 5 } } }); // 10
await user.sum('age'); // 55
await user.sum('age', { where: { age: { [op.gt]: 5 } } }); // 50

increment, decrement

sequelize 还提供了 increment 简便方法。

假设我们有一个用户, 他的年龄是 10 岁。

await user.increment({age: 5}, { where: { id: 1 } }) // 将年龄增加到15岁
await user.increment({age: -5}, { where: { id: 1 } }) // 将年龄降至5岁

查看笔记

扫码一下
查看教程更方便
网站地图