Node.js Sequelize Implement single instance field or bulk self-increment, self-subtraction _node.js

Source: Internet
Author: User

Self-increase and self-reduction of single instance

In Sequelize, an instance (Instance) represents a row of records in a database. There are two types of instance: a Non-persistent Model.build() instance created by, and a Model.create() persistent instance created by such methods. Whether it is a persistent or non-persistent instance, there is a increment() , decrement() two-person method, which is used for the increment of field values and for two operations.

    1. instance.increment(fields, [options])-field value self-increasing
    2. instance.decrement(fields, [options]) -field value self-minus

For example, find a user with ID 1 and increase its age by 1:

var User = Sequelize.import ('.. /lib/model/user/user ');
User.findbyid (1). Then (function (user) {
 user.increment (' age '). Then (function (user) {
 console.log (' Success ')})

increment()the SQL statements generated by the method are as follows:

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

increment()and decrement() The default of the self increase, the value of the self minus is 1. If you want to use a different value, you can specify it by the by parameter in option parameters [options].

For example, reducing the number of users to two fields by 2 can be done in the following ways:

User.increment ([' Age ', ' number '], {by:2}). Then (function (user) {
 console.log (' success ');
})

The generated SQL is as follows:

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

The fields parameter can also be passed in by an object and specify the self increment, self decrement value. In this case, the options.by parameter is ignored.

For example, increase the number of users by 2 and age by 1:

User.increment ({age:-1, number:2}, {by:5}). Then (function (user) {
 console.log (' success ');
})

The generated SQL is as follows:

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

Second, bulk self-increase, self-reduction

increment()And decrement() both are self increasing or self reducing operations on a single instance, that is, the data being manipulated is a row of data in the database. To achieve the following bulk self-increase, self-subtraction operation, you cannot use the instance operation:

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

In Sequelize, the volume operation is usually achieved by model. But model does not increment() and decrement() method, can not be as convenient as instance or self reduction.

At this point, we can do this by using Model.update() the top-level methods in Sequelize sequelize.literal() :

sequelize.literal(val) -Create literal objects

sequelize.literal()method is used to create a literal object (val) that is passed directly into the generated SQL statement without any escaping.

For example, increase the age of 10 users by 1:

User.update ({sex:sequelize.literal (' age ' +1 ')}, {where:{number:{$gt:}}}). Then (function (user) {
 Console.log (' success ');
}

The resulting SQL statement is as follows:

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

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.