How to perform arithmetic operations in Sequelize?

How to do this in the future?

SELECT ProductID, Name, ListPrice, ListPrice * 1.15 AS NewPrice  
FROM Production

I tried:

db.Production.findAndCountAll(
    attributes: {
        include: [
            ['ListPrice * 1.15', 'NewPrice']
        ]
    }
).then(function(orders){
    return res.jsonp(output);
})

But that will not work.

This is the request I expect:

SELECT Production.ProductID, Production.Name, Production.ListPrice, Production.ListPrice * 1.15 AS NewPrice  
FROM Production

Instead, I see this query:

SELECT Production.ProductID, Production.Name, Production.ListPrice, ListPrice * 1.15 AS NewPrice  
FROM Production
+4
source share
1 answer

You can use Sequelize.literal . Here is the code:

db.Production.findAndCountAll(
    attributes: [
        [Sequelize.literal('ListPrice * 1.15'), 'NewPrice'],
    ]
).then(function(orders){
    return res.jsonp(output);
})
+2
source

Source: https://habr.com/ru/post/1651849/


All Articles