Sequelize-nodejs-6-Instances
Instances实例
Building a non-persistent instance构建非持久性实例
In order to create instances of defined classes just do as follows. You might recognize the syntax if you coded Ruby in the past. Using the build
-method will return an unsaved object, which you explicitly have to save.
为了创建定义类的实例,可以像下面写的去做。在过去,如果你编写Ruby代码,你可能意识到他的语法。使用内置方法将返回没存储的对象,你需要显示存储:
const project = Project.build({
title: 'my awesome project',
description: 'woot woot. this will make me a rich man'
}) const task = Task.build({
title: 'specify the project idea',
description: 'bla',
deadline: new Date()
})
Built instances will automatically get default values when they were defined:
构建实例将自动得到定义的默认值
// first define the model
const Task = sequelize.define('task', {
title: Sequelize.STRING,
rating: { type: Sequelize.STRING, defaultValue: }
}) // now instantiate an object
const task = Task.build({title: 'very important task'}) task.title // ==> 'very important task'
task.rating // ==> 3,没有声明的值将设置为默认值
To get it stored in the database, use the save
-method and catch the events ... if needed:
为了将其存储在数据库中,使用save
方法,如果需要,还要捕获其事件
project.save().then(() => {
// my nice callback stuff
}) task.save().catch(error => {
// mhhh, wth!
}) // you can also build, save and access the object with chaining:
Task
.build({ title: 'foo', description: 'bar', deadline: new Date() })
.save()
.then(anotherTask => {
// you can now access the currently saved task with the variable anotherTask... nice!
})
.catch(error => {
// Ooops, do some error-handling
})
Creating persistent instances创建持久实例
While an instance created with .build()
requires an explicit .save()
call to be stored in the database, .create()
omits that requirement altogether and automatically stores your instance's data once called.
上面肥持久实例的创建使用的是.build()
并需要显示使用.save()
进行存储。这里持久实例的创建使用的是.create()
,一旦调用,将会自动存储你的实例数据
Task.create({ title: 'foo', description: 'bar', deadline: new Date() }).then(task => {
// you can now access the newly created task via the variable task
})
It is also possible to define which attributes can be set via the create method. This can be especially very handy if you create database entries based on a form which can be filled by a user. Using that would for example allow you to restrict the User
model to set only a username and an address but not an admin flag:
通过create方法也可以定义能够被设置的属性。如果你基于一种被用户填满的形式来创建一个数据库条目,那么这将使其变得十分方便。例如,使用这个将允许你去限制User
模型只被设置username和address,而不设置admin标记
User.create({ username: 'barfooz', isAdmin: true }, { fields: [ 'username' ] }).then(user => {
// let's assume the default of isAdmin is false:
console.log(user.get({
plain: true
})) // => { username: 'barfooz', isAdmin: false }
})
从上面的结果可知,因为设置isAdmin为不能够被设置的属性,所以即使在创建的时候设置了这个属性,它的值也不会变化。
Updating / Saving / Persisting an instance
Now lets change some values and save changes to the database... There are two ways to do that:
更改一些值并将变化保存到数据库,下面有两种方法:
// way 1
task.title = 'a very different title now'
task.save().then(() => {}) // way 2
task.update({
title: 'a very different title now'
}).then(() => {})
It's also possible to define which attributes should be saved when calling save
, by passing an array of column names. This is useful when you set attributes based on a previously defined object. E.g. if you get the values of an object via a form of a web app. Furthermore this is used internally for update
. This is how it looks like:
在调用save的时候,是可以定义那些属性应该被存储的,通过传递列名数组。当你基于以前定义的对象设置属性时,它是十分有用的,例如,如果你是通过 web app的形式得到一个对象的值的时候。而且,这个将会被update
内部使用:
task.title = 'foooo'
task.description = 'baaaaaar'
task.save({fields: ['title']}).then(() => {
// title will now be 'foooo' but description is the very same as before
}) // The equivalent call using update looks like this,两者等价:
task.update({ title: 'foooo', description: 'baaaaaar'}, {fields: ['title']}).then(() => {
// title will now be 'foooo' but description is the very same as before
})
由结果可知,因为设置了description属性不能够被存储,所以在上面的例子中,其属性不变
When you call save
without changing any attribute, this method will execute nothing;
当你调用save,但是没有改变任何属性时,将不会发生任何事情。
Destroying / Deleting persistent instances
Once you created an object and got a reference to it, you can delete it from the database. The relevant method is destroy
:
一旦你创建一个对象并得到其引用,你可以从数据库删掉它。相关的方法是-destroy:
Task.create({ title: 'a task' }).then(task => {//task就是该对象的引用
// now you see me...
return task.destroy();//然后就能够通过引用调用destroy方法删除它
}).then(() => {
// now i'm gone :)
})
If the paranoid
options is true, the object will not be deleted, instead the deletedAt
column will be set to the current timestamp. To force the deletion, you can pass force: true
to the destroy call:
如果paranoid
选项设置为true,对象将不能被删除,但是deletedAt
列将会被设置成当前的时间戳。为了强制删除,你可以传递force: true
给destroy调用:
task.destroy({ force: true })
Working in bulk (creating, updating and destroying multiple rows at once)一次执行多个
In addition to updating a single instance, you can also create, update, and delete multiple instances at once. The functions you are looking for are called
除了更新单个实例,你也可以一次create, update, and delete多个实例。下面的函数将被调用
Model.bulkCreate
Model.update
Model.destroy
Since you are working with multiple models, the callbacks will not return DAO instances. BulkCreate will return an array of model instances/DAOs, they will however, unlike create
, not have the resulting values of autoIncrement attributes. update
and destroy
will return the number of affected rows.
当你执行的是多个模型,回调将不会返回DAO实例。BulkCreate酱返回模型实例/DAOs数组,不像create
,没有自动增长属性的结果值。update
和destroy将返回被影响的行的数量。
First lets look at bulkCreate
首先是bulkCreate函数:
User.bulkCreate([
{ username: 'barfooz', isAdmin: true },
{ username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false }
]).then(() => { // Notice: There are no arguments here, as of right now you'll have to...
return User.findAll();
}).then(users => {
console.log(users) // ... in order to get the array of user objects
})
To update several rows at once:
一次更新多行:
Task.bulkCreate([
{subject: 'programming', status: 'executing'},
{subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'}
]).then(() => {
return Task.update(
{ status: 'inactive' }, /* set attributes' value */
{ where: { subject: 'programming' }} /* where criteria */
);
}).spread((affectedCount, affectedRows) => {
// .update returns two values in an array, therefore we use .spread
// Notice that affectedRows will only be defined in dialects which support returning: true // affectedCount will be 2
return Task.findAll();
}).then(tasks => {
console.log(tasks) // the 'programming' tasks will both have a status of 'inactive'
})
And delete them:
然后删除他们:
Task.bulkCreate([
{subject: 'programming', status: 'executing'},
{subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'}
]).then(() => {
return Task.destroy({
where: {
subject: 'programming'
},
truncate: true /* this will ignore where and truncate the table instead */
});
}).then(affectedRows => {
// affectedRows will be 2
return Task.findAll();
}).then(tasks => {
console.log(tasks) // no programming, just reading :(
})
If you are accepting values directly from the user, it might be beneficial to limit the columns that you want to actually insert.bulkCreate()
accepts an options object as the second parameter. The object can have a fields
parameter, (an array) to let it know which fields you want to build explicitly
如果你从user中直接接受值,那么限制你真正想要插入的列是十分有用的。bulkCreate()
接受选项对象作为第二个参数。对象能有fields
参数,用于显式地了解你想要构建的域值:
User.bulkCreate([
{ username: 'foo' },
{ username: 'bar', admin: true}//该数据将失败,因为admin不能设置
], { fields: ['username'] }).then(() => {
// nope bar, you can't be admin!
})
bulkCreate
was originally made to be a mainstream/fast way of inserting records, however, sometimes you want the luxury of being able to insert multiple rows at once without sacrificing model validations even when you explicitly tell Sequelize which columns to sift through. You can do by adding a validate: true
property to the options object.
一开始,bulkCreate
被设置为主流/快速的方法去插入记录,可是有时你想要能够一次插入多行而不用牺牲模型的验证功能,甚至是当你显式地告诉Sequelize哪一列被选中。你可以通过添加validate: true
属性到options对象中去实现:
const Tasks = sequelize.define('task', {
name: {
type: Sequelize.STRING,
validate: {
notNull: { args: true, msg: 'name cannot be null' }
}
},
code: {
type: Sequelize.STRING,
validate: {
len: [, ]
}
}
}) Tasks.bulkCreate([
{name: 'foo', code: ''},//code长度大于range
{code: ''},//失败,因为name不能为null
{name: 'bar', code: ''}
], { validate: true }).catch(errors => {
/* console.log(errors) would look like:
[
{ record:
...
name: 'SequelizeBulkRecordError',
message: 'Validation error',
errors:
{ name: 'SequelizeValidationError',
message: 'Validation error',
errors: [Object] } },
{ record:
...
name: 'SequelizeBulkRecordError',
message: 'Validation error',
errors:
{ name: 'SequelizeValidationError',
message: 'Validation error',
errors: [Object] } }
]
*/
})
Values of an instance
If you log an instance you will notice, that there is a lot of additional stuff. In order to hide such stuff and reduce it to the very interesting information, you can use the get
-attribute. Calling it with the option plain
= true will only return the values of an instance.
如果你记录实例的日志你就会注意到,这里有着一些额外的问题。为了隐藏这些问题并在非常有意思的信息中减少它的出现,你可以使用get
-attribute。调用它并设置选项为plain
= true,将会只返回实例的值:
Person.create({
name: 'Rambow',
firstname: 'John'
}).then(john => {
console.log(john.get({
plain: true
}))
}) // result: // { name: 'Rambow',
// firstname: 'John',
// id: 1,
// createdAt: Tue, 01 May 2012 19:12:16 GMT,
// updatedAt: Tue, 01 May 2012 19:12:16 GMT
// }
Hint:You can also transform an instance into JSON by using JSON.stringify(instance)
. This will basically return the very same as values
.
提示:你也可以通过使用JSON.stringify(instance)
将实例转成JSON格式。基本上是返回相同的值的。
Reloading instances
If you need to get your instance in sync, you can use the method reload
. It will fetch the current data from the database and overwrite the attributes of the model on which the method has been called on.
如果你想要同步得到你的实例,你可以使用方法reload
。他将从数据库获取当前的数据并覆写之前调用过方法的模型的属性。
Person.findOne({ where: { name: 'john' } }).then(person => {
person.name = 'jane'
console.log(person.name) // 'jane' person.reload().then(() => {
console.log(person.name) // 'john'
})
})
Incrementing
In order to increment values of an instance without running into concurrency issues, you may use increment
.
为了增加一个实例的值并不出现并发问题,你可以使用increment,下面有三种使用情况
First of all you can define a field and the value you want to add to it.
第一种,你可以定义一个值域以及你想要添加的值:
User.findById().then(user => {
return user.increment('my-integer-field', {by: })
}).then(user => {
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
// In other dialects, you'll want to call user.reload() to get the updated instance...
})
Second, you can define multiple fields and the value you want to add to them.
第二种,定义多个值域和一个你想要添加给它们的值
User.findById().then(user => {
return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: })
}).then(/* ... */)
Third, you can define an object containing fields and its increment values.
第三种,定义包含值域的对象及其对应的增量值
User.findById().then(user => {
return user.increment({
'my-integer-field': ,
'my-very-other-field':
})
}).then(/* ... */)
Decrementing
In order to decrement values of an instance without running into concurrency issues, you may use decrement
.
为了减少一个实例的值并不出现并发问题,你可以使用decrement
,下面有三种使用情况
First of all you can define a field and the value you want to add to it.
第一种,你可以定义一个值域以及你想要添加的值:
User.findById().then(user => {
return user.decrement('my-integer-field', {by: })
}).then(user => {
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
// In other dialects, you'll want to call user.reload() to get the updated instance...
})
Second, you can define multiple fields and the value you want to add to them.
第二种,定义多个值域和一个你想要添加给它们的值
User.findById().then(user => {
return user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: })
}).then(/* ... */)
Third, you can define an object containing fields and its decrement values.
第三种,定义包含值域的对象及其对应的增量值
User.findById().then(user => {
return user.decrement({
'my-integer-field': ,
'my-very-other-field':
})
}).then(/* ... */)
Sequelize-nodejs-6-Instances的更多相关文章
- 【前端】nodejs的ORM框架sequelize的工厂化
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...
- 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. ...
- nodejs 最受欢迎的orm sequelize
传送门 # 视频教程 https://nodelover.me/course/sequelize/ # 官方文档 http://docs.sequelizejs.com/manual/tutorial ...
- 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 ...
随机推荐
- Swift 函数调用到底写不写参数名
最近真正开始学 Swift,在调用函数的时候遇到一个问题:到底写不写函数名? 我们来看两个个例子: // 1 func test(a: Int, b: Int) ->Int { return a ...
- 小tip: 某简单的字符重叠与图形生成----张鑫旭
引言 字符重叠不是什么稀奇的东西. 如1像素错位模拟阴影效果: 或者powerFloat中展示的带边框三角: 以及其他很多. 但是技术这东西不是豆腐,老了可以吃,臭了也可以吃:那我这里还拿着个说事作甚 ...
- PHP中文件操作(1)--打开/读取文件
1.打开文件(fopen) 语法:resource $fp=fopen(文件地址,模式),返回的是文件指针(file pointer) 模式 含义 r 只读 w 写(清空重写) a 追加 $fp = ...
- php 截取字符串指定长度
---恢复内容开始--- 一.直接取整,舍弃小数,保留整数:intval(): intval(9.21); /*结果是9*/ intval(9.89); /*结果是9*/ intval(string) ...
- 前台提交数据(表单数据、Json数据及上传文件)的类型
MIME (Multipurpose Internet Mail Extensions) 是描述内容类型的互联网标准.Clients use this content type or media ty ...
- linux中文字体
◆ 背景说明 报表,在windows下,展现.导出都正常,在linux下,字体变大了.比如,单元格的大小设计好后,里面的字当好能一行显示完,将报表放到linux下后,字变大了,一行显示不完了,变 ...
- 2018-10-13 21:30:51 conversion of number systems
2018-10-13 21:30:51 c language 二进制.八进制和十六进制: 1) 整数部分 十进制整数转换为 N 进制整数采用“除 N 取余,逆序排列”法. 十进制数字 36926 转 ...
- oracle exp dmp
exp help=yconn scott/tiger;select * from tab;create table student(sno int, sname varchar2(10), sage ...
- 如何计算tomcat线程池大小?
背景 在我们的日常开发中都涉及到使用tomcat做为服务器,但是我们该设置多大的线程池呢?以及根据什么原则来设计这个线程池呢? 接下来,我将介绍本人是怎么设计以及计算的. 目标 确定tomcat服务器 ...
- MVC $.Ajax()+Json实现数据库访问并显示数据
我们在使用搜索引擎时经常会看到这样一个效果 在输出输入相关文字时会有与之对应的相关提醒,作为一个MVC初学者我也做了一个简单版的“搜索工具”,分享给初学mvc和ajax的童鞋(各位大神勿喷),也加深我 ...