Node. jssequencee implements single-instance fields or batch auto-increment and auto-Subtraction

Source: Internet
Author: User
Sequencee can perform auto-increment and auto-increment operations on one or more fields of a single instance, or perform batch auto-increment and auto-increment operations on data that meet the conditions. The auto-increment and auto-increment of a single Instance field can be implemented using the corresponding method of Instance, while the batch auto-increment and auto-increment must be implemented using the literal method provided by sequencee. Let's take a look at the detailed introduction. I. Auto-increment and auto-increment for a single instance

In sequencee, an Instance indicates a row of records in the database. There are two types of instances: non-persistent instances created by Model. build () and persistent instances created by Model. create. Whether it is a persistent or non-persistent instance, there will be two methods on the increment () and decrement (), respectively used for the field value auto-increment and auto-increment operations.

Instance. increment (fields, [options])-field value auto-increment

Instance. decrement (fields, [options])-field value auto-Subtraction

For example, find a user whose id is 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'); })})

The SQL statement generated by the increment () method is as follows:

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

The default auto-increment and auto-impairment values of increment () and decrement () are 1. If you want to use other values, you can use the by parameter in Option [options.

For example, you can reduce the number and age fields by 2 in the following ways:

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

The generated SQL statement is as follows:

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

Fields parameters can also be passed in through objects, and auto-increment and auto-impairment can be specified. In this case, the options. by parameter is ignored.

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

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

The generated SQL statement is as follows:

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

Ii. Batch auto-increment and auto-Increment

Both increment () and decrement () perform auto-increment or auto-increment operations on a single instance, that is, the operation data is a row of data in the database. To perform operations similar to batch auto-increment and auto-increment operations, you cannot perform operations through the Instance:

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

In sequencee, a volume operation is generally implemented through a Model. However, the Model does not have the increment () and decrement () methods, so you cannot perform auto-increment or auto-increment as conveniently as the Instance.

In this case, we can use Model. update () and the top-level method sequencee. literal () in sequencee to implement:

Sequencee. literal (val)-create a literal object

The sequencee. literal () method is used to create a literal object. The object (val) is directly passed into the generated SQL statement without any escape.

For example, increase the age of a user whose number is greater than 10 by 1:

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

The generated SQL statement is as follows:

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

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

For more information about node. js sequencee, such as single-instance fields, batch auto-increment, and auto-increment, please pay attention to PHP!

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.