Sequelize 中文文档 v4 - Querying - 查询
Querying - 查詢
此系列文章的應用示例已發布于 GitHub: sequelize-docs-Zh-CN. 可以 Fork 幫助改進或 Star 關注更新. 歡迎 Star.
屬性
想要只選擇某些屬性,可以使用 attributes 選項。 通常是傳遞一個數組:
Model.findAll({attributes: ['foo', 'bar'] }); SELECT foo, bar ...屬性可以使用嵌套數組來重命名:
Model.findAll({attributes: ['foo', ['bar', 'baz']] }); SELECT foo, bar AS baz ...也可以使用 sequelize.fn 來進行聚合:
Model.findAll({attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }); SELECT COUNT(hats) AS no_hats ...使用聚合功能時,必須給它一個別名,以便能夠從模型中訪問它。 在上面的例子中,您可以使用 instance.get('no_hats') 獲得帽子數量。
有時,如果您只想添加聚合,則列出模型的所有屬性可能令人厭煩:
// This is a tiresome way of getting the number of hats... Model.findAll({attributes: ['id', 'foo', 'bar', 'baz', 'quz', [sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] });// This is shorter, and less error prone because it still works if you add / remove attributes Model.findAll({attributes: { include: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] } }); SELECT id, foo, bar, baz, quz, COUNT(hats) AS no_hats ...同樣,它也可以刪除一些指定的屬性:
Model.findAll({attributes: { exclude: ['baz'] } }); SELECT id, foo, bar, quz ...Where
無論您是通過 findAll/find 或批量 updates/destroys 進行查詢,都可以傳遞一個 where 對象來過濾查詢。
where 通常用 attribute:value 鍵值對獲取一個對象,其中 value 可以是匹配等式的數據或其他運算符的鍵值對象。
也可以通過嵌套 or 和 and 運算符 的集合來生成復雜的 AND/OR 條件。
基礎
const Op = Sequelize.Op;Post.findAll({where: {authorId: 2} }); // SELECT * FROM post WHERE authorId = 2Post.findAll({where: {authorId: 12,status: 'active'} }); // SELECT * FROM post WHERE authorId = 12 AND status = 'active';Post.destroy({where: {status: 'inactive'} }); // DELETE FROM post WHERE status = 'inactive';Post.update({updatedAt: null, }, {where: {deletedAt: {[Op.ne]: null}} }); // UPDATE post SET updatedAt = null WHERE deletedAt NOT NULL;Post.findAll({where: sequelize.where(sequelize.fn('char_length', sequelize.col('status')), 6) }); // SELECT * FROM post WHERE char_length(status) = 6;操作符
Sequelize 可用于創建更復雜比較的符號運算符 -
const Op = Sequelize.Op[Op.and]: {a: 5} // 且 (a = 5) [Op.or]: [{a: 5}, {a: 6}] // (a = 5 或 a = 6) [Op.gt]: 6, // id > 6 [Op.gte]: 6, // id >= 6 [Op.lt]: 10, // id < 10 [Op.lte]: 10, // id <= 10 [Op.ne]: 20, // id != 20 [Op.eq]: 3, // = 3 [Op.not]: true, // 不是 TRUE [Op.between]: [6, 10], // 在 6 和 10 之間 [Op.notBetween]: [11, 15], // 不在 11 和 15 之間 [Op.in]: [1, 2], // 在 [1, 2] 之中 [Op.notIn]: [1, 2], // 不在 [1, 2] 之中 [Op.like]: '%hat', // 包含 '%hat' [Op.notLike]: '%hat' // 不包含 '%hat' [Op.iLike]: '%hat' // 包含 '%hat' (不區分大小寫) (僅限 PG) [Op.notILike]: '%hat' // 不包含 '%hat' (僅限 PG) [Op.regexp]: '^[h|a|t]' // 匹配正則表達式/~ '^[h|a|t]' (僅限 MySQL/PG) [Op.notRegexp]: '^[h|a|t]' // 不匹配正則表達式/!~ '^[h|a|t]' (僅限 MySQL/PG) [Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (僅限 PG) [Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (僅限 PG) [Op.like]: { [Op.any]: ['cat', 'hat']} // 包含任何數組['cat', 'hat'] - 同樣適用于 iLike 和 notLike [Op.overlap]: [1, 2] // && [1, 2] (PG數組重疊運算符) [Op.contains]: [1, 2] // @> [1, 2] (PG數組包含運算符) [Op.contained]: [1, 2] // <@ [1, 2] (PG數組包含于運算符) [Op.any]: [2,3] // 任何數組[2, 3]::INTEGER (僅限PG)[Op.col]: 'user.organization_id' // = 'user'.'organization_id', 使用數據庫語言特定的列標識符, 本例使用 PG范圍選項
所有操作符都支持支持的范圍類型查詢。
請記住,提供的范圍值也可以定義綁定的 inclusion/exclusion。
// 所有上述相等和不相等的操作符加上以下內容:[Op.contains]: 2 // @> '2'::integer (PG range contains element operator) [Op.contains]: [1, 2] // @> [1, 2) (PG range contains range operator) [Op.contained]: [1, 2] // <@ [1, 2) (PG range is contained by operator) [Op.overlap]: [1, 2] // && [1, 2) (PG range overlap (have points in common) operator) [Op.adjacent]: [1, 2] // -|- [1, 2) (PG range is adjacent to operator) [Op.strictLeft]: [1, 2] // << [1, 2) (PG range strictly left of operator) [Op.strictRight]: [1, 2] // >> [1, 2) (PG range strictly right of operator) [Op.noExtendRight]: [1, 2] // &< [1, 2) (PG range does not extend to the right of operator) [Op.noExtendLeft]: [1, 2] // &> [1, 2) (PG range does not extend to the left of operator)組合
{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%'運算符別名
Sequelize 允許將特定字符串設置為操作符的別名 -
const Op = Sequelize.Op; const operatorsAliases = {$gt: Op.gt } const connection = new Sequelize(db, user, pass, { operatorsAliases })[Op.gt]: 6 // > 6 $gt: 6 // 等同于使用 Op.gt (> 6)運算符安全性
使用沒有任何別名的 Sequelize 可以提高安全性。
一些框架會自動將用戶輸入解析為js對象,如果您無法清理輸入,則可能會將具有字符串運算符的對象注入到 Sequelize。
不帶任何字符串別名將使運算符不太可能被注入,但您應該始終正確驗證和清理用戶輸入。
由于向后兼容性原因Sequelize默認設置以下別名 -
$eq, $ne, $gte, $gt, $lte, $lt, $not, $in, $notIn, $is, $like, $notLike, $iLike, $notILike, $regexp, $notRegexp, $iRegexp, $notIRegexp, $between, $notBetween, $overlap, $contains, $contained, $adjacent, $strictLeft, $strictRight, $noExtendRight, $noExtendLeft, $and, $or, $any, $all, $values, $col
目前,以下遺留別名也被設置,但計劃在不久的將來完全刪除 -
ne, not, in, notIn, gte, gt, lte, lt, like, ilike, $ilike, nlike, $notlike, notilike, .., between, !.., notbetween, nbetween, overlap, &&, @>, <@
為了更好的安全性,建議使用 Sequelize.Op,而不是依賴任何字符串別名。 您可以通過設置operatorsAliases選項來限制應用程序需要的別名,請記住要清理用戶輸入,特別是當您直接將它們傳遞給 Sequelize 方法時。
const Op = Sequelize.Op;// 不用任何操作符別名使用 sequelize const connection = new Sequelize(db, user, pass, { operatorsAliases: false });// 只用 $and => Op.and 操作符別名使用 sequelize const connection2 = new Sequelize(db, user, pass, { operatorsAliases: { $and: Op.and } });如果你使用默認別名并且不限制它們,Sequelize會發出警告。如果您想繼續使用所有默認別名(不包括舊版別名)而不發出警告,您可以傳遞以下運算符參數 -
const Op = Sequelize.Op; const operatorsAliases = {$eq: Op.eq,$ne: Op.ne,$gte: Op.gte,$gt: Op.gt,$lte: Op.lte,$lt: Op.lt,$not: Op.not,$in: Op.in,$notIn: Op.notIn,$is: Op.is,$like: Op.like,$notLike: Op.notLike,$iLike: Op.iLike,$notILike: Op.notILike,$regexp: Op.regexp,$notRegexp: Op.notRegexp,$iRegexp: Op.iRegexp,$notIRegexp: Op.notIRegexp,$between: Op.between,$notBetween: Op.notBetween,$overlap: Op.overlap,$contains: Op.contains,$contained: Op.contained,$adjacent: Op.adjacent,$strictLeft: Op.strictLeft,$strictRight: Op.strictRight,$noExtendRight: Op.noExtendRight,$noExtendLeft: Op.noExtendLeft,$and: Op.and,$or: Op.or,$any: Op.any,$all: Op.all,$values: Op.values,$col: Op.col };const connection = new Sequelize(db, user, pass, { operatorsAliases });JSONB
JSONB 可以以三種不同的方式進行查詢。
嵌套對象
{meta: {video: {url: {[Op.ne]: null}}} }嵌套鍵
{"meta.audio.length": {[Op.gt]: 20} }外包裹
{"meta": {[Op.contains]: {site: {url: 'http://google.com'}}} }關系 / 關聯
// 找到所有具有至少一個 task 的 project,其中 task.state === project.state Project.findAll({include: [{model: Task,where: { state: Sequelize.col('project.state') }}] })分頁 / 限制
// 獲取10個實例/行 Project.findAll({ limit: 10 })// 跳過8個實例/行 Project.findAll({ offset: 8 })// 跳過5個實例,然后取5個 Project.findAll({ offset: 5, limit: 5 })排序
order 需要一個條目的數組來排序查詢或者一個 sequelize 方法。一般來說,你將要使用任一屬性的 tuple/array,并確定排序的正反方向。
Subtask.findAll({order: [// 將轉義用戶名,并根據有效的方向參數列表驗證DESC['title', 'DESC'],// 將按最大值排序(age)sequelize.fn('max', sequelize.col('age')),// 將按最大順序(age) DESC[sequelize.fn('max', sequelize.col('age')), 'DESC'],// 將按 otherfunction 排序(`col1`, 12, 'lalala') DESC[sequelize.fn('otherfunction', sequelize.col('col1'), 12, 'lalala'), 'DESC'],// 將使用模型名稱作為關聯的名稱排序關聯模型的 created_at。[Task, 'createdAt', 'DESC'],// Will order through an associated model's created_at using the model names as the associations' names.[Task, Project, 'createdAt', 'DESC'],// 將使用關聯的名稱由關聯模型的created_at排序。['Task', 'createdAt', 'DESC'],// Will order by a nested associated model's created_at using the names of the associations.['Task', 'Project', 'createdAt', 'DESC'],// Will order by an associated model's created_at using an association object. (優選方法)[Subtask.associations.Task, 'createdAt', 'DESC'],// Will order by a nested associated model's created_at using association objects. (優選方法)[Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],// Will order by an associated model's created_at using a simple association object.[{model: Task, as: 'Task'}, 'createdAt', 'DESC'],// 嵌套關聯模型的 created_at 簡單關聯對象排序[{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') })如果這篇文章對您有幫助, 感謝 下方點贊 或 Star GitHub: sequelize-docs-Zh-CN 支持, 謝謝.
總結
以上是生活随笔為你收集整理的Sequelize 中文文档 v4 - Querying - 查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装centos7
- 下一篇: PHPUnit 3.4.10 在wind