Home Web Front-end JS Tutorial node.js Sequelize implements single instance field or batch auto-increment and auto-decrement

node.js Sequelize implements single instance field or batch auto-increment and auto-decrement

Dec 28, 2016 am 11:16 AM

一、单实例自增、自减

在Sequelize中,一个实例(Instance)表示数据库中的一行记录。Instance有两种:由Model.build()创建的非持久化实例,和由Model.create()等方法创建的持久化实例。无论是持久化还是非持久化实例,都会有increment() 、decrement()两人上方法,分别用于字段值的自增和自减两种操作。

instance.increment(fields, [options]) - 字段值自增

instance.decrement(fields, [options]) - 字段值自减

如,查找id为1的用户,并将其年龄自增1:

1

2

3

4

5

6

var User = sequelize.import('../lib/model/user/user');

User.findById(1).then(function(user){

 user.increment('age').then(function(user){

 console.log('success');

 })

})

Copy after login

其中increment()方法生成的SQL语句如下:

1

UPDATE `user` SET `age`=`age` + 1 WHERE `id` = 1

Copy after login

increment()和decrement()默认的自增、自减值是1。如果希望使用其它值,可在选项参数[options]中通过by参数指定。

如,将用户的number、age两个字段减小2,可以通过以下方式实现:

1

2

3

user.increment(['age', 'number'], {by:2}).then(function(user){

 console.log('success');

})

Copy after login

生成的SQL如下:

1

UPDATE `user` SET `age`=`age` + 2,`number`=`number` + 2 WHERE `id` = 1

Copy after login

fields参数还可以通过对象传入,并指定自增、自减值。这种情况下,会忽略options.by参数。

如,将用户的number增加2、age减小1:

1

2

3

user.increment({age:-1, number:2}, {by:5}).then(function(user){

 console.log('success');

})

Copy after login

生成的SQL如下:

1

UPDATE `user` SET `age`=`age` + -1,`number`=`number` + 2 WHERE `id` = 1

Copy after login

二、 批量自增、自减

increment()和decrement()都是针对单个实例进行自增或自减操作的,也就是说操作的数据为数据库中的一行数据。要实现类似如下批量自增、自减操作,就无法通过Instance操作:

1

UPDATE `user` SET `age`=`age` + 1 WHERE `number` > 10;

Copy after login

在Sequelize中,指量操作一般是通过模型(Model)来实现。但Model并没有increment()和decrement()方法,无法像Instance那样方便的进行自增或自减。

这时,我们可以通过Model.update()并借助sequelize中的顶级方法sequelize.literal()来实现:

sequelize.literal(val) - 创建字面量对象

sequelize.literal()方法用于创建一个字面量对象,该对象(val)会被直接传入所生成的SQL语句中,而不会进行任何转义。

如,将number大于10的用户年龄增加1:

1

2

3

User.update({sex:sequelize.literal('`age` +1')}, {where:{number:{$gt:10}}}).then(function(user){

 console.log('success');

})

Copy after login

生成的SQL语句如下:

1

UPDATE `user` SET `age`=`age` +1 WHERE `number` > 10

Copy after login

   

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多node.js Sequelize实现单实例字段或批量自增、自减相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles