Sequelize-nodejs-9-Scopes
Scopes作用域
Scoping allows you to define commonly used queries that you can easily use later. Scopes can include all the same attributes as regular finders, where
, include
, limit
etc.
Scoping允许你去定义普遍使用的查询,这样你之后就能够简单使用。Scopes能够像一般的查询器一样包含 where
, include
, limit
等
Definition
Scopes are defined in the model definition and can be finder objects, or functions returning finder objects - except for the default scope, which can only be an object:
Scopes可以在模型定义中进行定义,也可以是查找器对象或返回查找器对象的函数-除了默认的scope,她才能只是一个对象
const Project = sequelize.define('project', {
// Attributes
}, {
defaultScope: {
where: {
active: true
}
},
scopes: {
deleted: {
where: {
deleted: true
}
},
activeUsers: {
include: [
{ model: User, where: { active: true }}
]
},
random: function () {
return {
where: {
someNumber: Math.random()
}
}
},
accessLevel: function (value) {
return {
where: {
accessLevel: {
[Op.gte]: value
}
}
}
}
}
});
You can also add scopes after a model has been defined by calling addScope
. This is especially useful for scopes with includes, where the model in the include might not be defined at the time the other model is being defined.
你可以通过调用addScope
在模型定义后添加scopes。这对带有includes的scopes十分有用,因为在其他模型被定义时include中的模型可能还没有定义
The default scope is always applied. This means, that with the model definition above, Project.findAll()
will create the following query:
缺省scope总是被提供。这意味着有着上面的模型定义,Project.findAll()
将创建出下面的查询:
SELECT * FROM projects WHERE active = true
The default scope can be removed by calling .unscoped()
, .scope(null)
, or by invoking another scope:
缺省scope可以通过调用 .unscoped()
, .scope(null)
函数或调用另一个scope来移除
Project.scope('deleted').findAll(); // Removes the default scope SELECT * FROM projects WHERE deleted = true
It is also possible to include scoped models in a scope definition. This allows you to avoid duplicating include
, attributes
or where
definitions. Using the above example, and invoking the active
scope on the included User model (rather than specifying the condition directly in that include object):
在scope定义中包含scoped模型也是可能的。这要求你避免重复 include
, attributes
或where定义。使用上面的例子并在包含的User模型中调用
active scope(而不是直接在包含对象中指明条件)
activeUsers: {
include: [
{ model: User.scope('active')}
]
}
Usage
Scopes are applied by calling .scope
on the model definition, passing the name of one or more scopes. .scope
returns a fully functional model instance with all the regular methods: .findAll
, .update
, .count
, .destroy
etc. You can save this model instance and reuse it later:
Scopes通过在模型定义中调用.scope来应用,传递一个或多个scopes的名字。
.scope
将返回一个带着传统方法 .findAll
, .update
, .count
, .destroy
等的完整的函数模型实例。你可以保存这个模型实例并在之后重新使用它
const DeletedProjects = Project.scope('deleted'); DeletedProjects.findAll();
// some time passes // let's look for deleted projects again!
DeletedProjects.findAll();
Scopes apply to .find
, .findAll
, .count
, .update
, .increment
and .destroy
.
Scopes供应 .find
, .findAll
, .count
, .update
, .increment
和 .destroy方法
Scopes which are functions can be invoked in two ways. If the scope does not take any arguments it can be invoked as normally. If the scope takes arguments, pass an object:
Scopes函数可以以两种方式被调用。如果scope不带任何参数,他将普通地调用。如果带参数,将传递对象:
Project.scope('random', { method: ['accessLevel', ]}).findAll(); SELECT * FROM projects WHERE someNumber = AND accessLevel >=
Merging
Several scopes can be applied simultaneously by passing an array of scopes to .scope
, or by passing the scopes as consecutive arguments.
几个scopes可以同时通过传递scopes数组给 .scope
来被应用,或者通过传递scopes作为连续的参数
// These two are equivalent
Project.scope('deleted', 'activeUsers').findAll();
Project.scope(['deleted', 'activeUsers']).findAll(); SELECT * FROM projects
INNER JOIN users ON projects.userId = users.id
AND users.active = true
If you want to apply another scope alongside the default scope, pass the key defaultScope
to .scope
:
如果你想要跟着缺省scope来应用其他scope,传递键defaultScope
给.scope
:
Project.scope('defaultScope', 'deleted').findAll(); SELECT * FROM projects WHERE active = true AND deleted = true
When invoking several scopes, keys from subsequent scopes will overwrite previous ones (similar to _.assign). Consider two scopes:
当调用几个scopes时,来自子序列scopes的键将复写以前的键值
{
scope1: {
where: {
firstName: 'bob',
age: {
[Op.gt]:
}
},
limit:
},
scope2: {
where: {
age: {
[Op.gt]:
}
},
limit:
}
}
Calling .scope('scope1', 'scope2')
will yield the following query
调用 .scope('scope1', 'scope2')
产生下面的查询:
WHERE firstName = 'bob' AND age > LIMIT
Note how limit
and age
are overwritten by scope2
, while firstName
is preserved. limit
, offset
, order
, paranoid
, lock
and raw
are overwritten, while where
and include
are shallowly merged. This means that identical keys in the where objects, and subsequent includes of the same model will both overwrite each other.
注明怎么通过scope2复写
limit
和age,当
firstName
保存时。
,当
where
和include被浅合并时
limit
, offset
, order
, paranoid
, lock
and raw
都被复写。这意味着在where对象中有着相同的键,并且当自序列包含相同的模型时将互相复写
The same merge logic applies when passing a find object directly to findAll on a scoped model:
当在scoped模型中直接传递一个find对象给findAll时,同样的合并逻辑使用
Project.scope('deleted').findAll({
where: {
firstName: 'john'
}
}) WHERE deleted = true AND firstName = 'john'
Here the deleted
scope is merged with the finder. If we were to pass where: { firstName: 'john', deleted: false }
to the finder, the deleted
scope would be overwritten.
这里deleted scope将与查找器合并。如果我们打算传递
where: { firstName: 'john', deleted: false }
给查找器,deleted
scope将被复写
Associations
Sequelize has two different but related scope concepts in relation to associations. The difference is subtle but important:
Sequelize有着两种不同但相关的与关联相关的scope概念。两者的不同很微弱,但却很重要
- Association scopes Allow you to specify default attributes when getting and setting associations - useful when implementing polymorphic associations. This scope is only invoked on the association between the two models, when using the
get
,set
,add
andcreate
associated model functions当getting和setting关联时,允许你指定属性-当实现多元化关联时十分有用。这个scope只有在两个模型的关联中被调用,使用get
,set
,add
和create
关联模型函数 - Scopes on associated models Allows you to apply default and other scopes when fetching associations, and allows you to pass a scoped model when creating associations. These scopes both apply to regular finds on the model and to find through the association.允许你在获取关联时请求默认或其他scopes,也允许你在创建关联时传递scoped模型。这个scopes即在模型中请求传统查询也通过关联进行查找
As an example, consider the models Post and Comment. Comment is associated to several other models (Image, Video etc.) and the association between Comment and other models is polymorphic, which means that Comment stores a commentable
column, in addition to the foreign key commentable_id
.
举例,考虑模型Post和Comment。Comment与几个模型关联(Image, Video等)且在Comment和其他模型之间的关联是多元化的,这意味着Comment存储commentable
列,除此之外commentable_id
为外键
The polymorphic association can be implemented with an association scope :
多元化关联可以使用关联scope实现:
this.Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
scope: {
commentable: 'post'
}
});
When calling post.getComments()
, this will automatically add WHERE commentable = 'post'
. Similarly, when adding new comments to a post, commentable
will automagically be set to 'post'
. The association scope is meant to live in the background without the programmer having to worry about it - it cannot be disabled. For a more complete polymorphic example, see Association scopes
当调用post.getComments()
时,他将自动添加 WHERE commentable = 'post'
。同样的,当添加新comment到post中时,commentable
将会自动设置为 'post'
。关联scope意味着存活在没有项目烦恼的后台-它不可能不可用。更多完成的多元化实例,看 Association scopes。
Consider then, that Post has a default scope which only shows active posts: where: { active: true }
. This scope lives on the associated model (Post), and not on the association like the commentable
scope did. Just like the default scope is applied when calling Post.findAll()
, it is also applied when calling User.getPosts()
- this will only return the active posts for that user.
考虑这些后,Post有了只显示活跃posts:where: { active: true }
的缺省scope。这个scope存活在关联的模型(Post)中,而不在像commentable scope做出的关联中。当调用
Post.findAll()和
时,缺省scopeUser.getPosts()
被请求-将只返回活跃posts
To disable the default scope, pass scope: null
to the getter: User.getPosts({ scope: null })
. Similarly, if you want to apply other scopes, pass an array like you would to .scope
:
为了使缺省scope无效,传递scope: null
给getter: User.getPosts({ scope: null })。相同的,如果你想要请求其他scopes,只要传递数组,就像你传递给
.scope
的那样:
User.getPosts({ scope: ['scope1', 'scope2']});
If you want to create a shortcut method to a scope on an associated model, you can pass the scoped model to the association. Consider a shortcut to get all deleted posts for a user:
如果你想要创建快捷方法去一个关联模型的scope,你可以传递scoped模型给关联。考虑快捷方式将得到所有删除的posts给用户
const Post = sequelize.define('post', attributes, {
defaultScope: {
where: {
active: true
}
},
scopes: {
deleted: {
where: {
deleted: true
}
}
}
}); User.hasMany(Post); // regular getPosts association
User.hasMany(Post.scope('deleted'), { as: 'deletedPosts' }); User.getPosts(); // WHERE active = true
User.getDeletedPosts(); // WHERE deleted = true
Sequelize-nodejs-9-Scopes的更多相关文章
- 【前端】nodejs的ORM框架sequelize的工厂化
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...
- Node.js Koa2开发微信小程序服务端
1.promise.async.await const Koa = require('koa') const app = new Koa() // 应用程序对象 有很多中间件 // 发送HTTP KO ...
- nodejs项目mysql使用sequelize支持存储emoji
nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- 从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区
背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题. 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT ...
- nodejs sequelize 对应数据库操作符的定义
const Op = Sequelize.Op [Op.and]: {a: } // 且 (a = 5) [Op.or]: [{a: }, {a: }] // (a = 5 或 a = 6) [Op. ...
- postgresql on centos (sequelize+pg+nodejs):Failed to find PostgresSQL server.Pleast double check your settings
公司的一个项目,使用的nodejs做服务端,数据库是postgresql,在本地时一切ok,放在centos时,postgresql配置ok,可以远程访问,但是nodejs在centos启动时,就会报 ...
- 项目总结,彻底掌握NodeJS中如何使用Sequelize
前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...
- 基于Nodejs的sequelize操纵数据库
## 使用基于ORM架构的sequelize操纵数据库 ### 1.技术背景 ```Sequelize是一个基于promise的关系型数据库ORM框架,*********************技术文 ...
- Nodejs ORM框架Sequelize快速入门
Nodejs ORM框架Sequelize快速入门 什么是ORM? 简单的讲就是对SQL查询语句的封装,让我们可以用OOP的方式操作数据库,优雅的生成安全.可维护的SQL代码.直观上,是一种Model ...
随机推荐
- 第三节:我的第一个Java程序
一.编写我的第一个Java程序流程: 1.新建一个文本文档:在电脑任意位置“右击”----->选择“新建”----->选择“文本文档”: 2.修改文档名与后缀名:“右击”新建的文本文档-- ...
- 采用DTO和DAO对JDBC程序进行进一步优化
采用DTO和DAO对JDBC程序进行进一步优化 DTO:数据传输对象,主要用于远程调用等需要远程调用对象的地方DAO:数据访问对象,主要实现封装数据库的访问,通过它可以把数据库中的表转换成DTO类 引 ...
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- html中内联元素和块元素的区别、用法以及联系
昨天用asp.net的BulletedList做一个导航栏,最终该控件形成的html代码是ul列表和a超链接,具体代码如下: <ul id="BulletedList1" s ...
- Progress数据库配置与应用
创建database 开始->程序->OpenEdge,选择:Desktop,进行database创建. 选择创建一个空database或直接copy一个demo的database,我们选 ...
- idea appliction context not configured for this file
File --> Project Structure
- MongoDB用户配置
MongoDB学习笔记—权限管理 阅读目录 1.MongoDB权限介绍 2 MongoDB添加管理员账户 3 MongoDB开启用户权限验证 4 MongoDB的roles角色简单介绍 5 Mongo ...
- 1.CSS基础简介
一.基础简介 1.简介 CSS(Cascading Style Sheet)可译为“层叠样式表”或“级联样式表”,它定义如何显示 HTML 元素,用于控制Web页面的外观.通过使用CSS实现页面的内容 ...
- C#中的三种timer
转 https://blog.csdn.net/hoiven/article/details/51362582 如果你需要使用规律的时间间隔重复执行一些方法,最简单的方式是使用定时器(timer). ...
- ExpressRoute 连接模型
可通过以下三种不同方式,创建本地网络和 Azure 云之间的连接:CloudExchange 归置.点对点以太网连接和任意位置之间的 (IPVPN) 连接.连接服务提供商可以提供一个或多个连接模型.可 ...