How to compare database fields in FeathersJS - feathersjs

I need to find all records where columnA <= columnB. I don't see any mention of this feature in the documentation.
Is it possible? How?

Since you are using Sequelize, as mentioned in this answer you should be able to accomplish this using sequelize.col. This condition can be added as a before find hook like this:
app.service('users').before({
find(hook) {
const { query } = hook.params;
query.columnA = { $gte: sequelize.col('columnB') };
}
});

Related

Can someone help me in convert this MySql query to sequelize?

I am new to sequelize. I am not sure how to convert this MySql query so that I can use it in my node.js file.
MySql query:
SELECT Rtrim(Ltrim(childstatus)),TIMESTAMPDIFF(d,dob,now(3))
INTO #childstatus, #Ageday
FROM childdetails where registno=#registno
I have sequelize model for childdetails. I am not sure how to structure this query.
You can use Sequelize.fn to call these two functions indicating them in attributes option like this:
const details = await ChildDetials.findAll({
attributes: [
[Sequelize.fn('RTrim', Sequelize.fn('LTrim', Sequelize.col('childstatus'))), 'childsttaus'],
[Sequelize.fn('TIMESTAMPDIFF', Sequelize.literal('d'), Sequelize.col('dob'), Sequelize.fn('now', 3)), 'Ageday']
],
where: {
registno: registno
}
})

Query across two tables in sequelize

I am quite new to sequelize and mySQL and feel like I have tried everything in order to pass a search term ('query') to both the books table (searching against titles) and the authors table (searching against first_name or last_name). In the event of matching any of those values substrings it is to return the whole book and author information as a JSON object. When I just have the query focused on book title, it returns everything just fine. The problem comes in when I try to pass in Author columns. I have tried aliasing, nesting where clauses, everything I can think of to do and nothing I come across on here or online seems to help me figure it out.
search: (req, res) => {
const { query } = req.query;
Book.findAll({
include: [Author],
where: {
[Op.or]: [
{ title: { [Op.substring]: query } },
]},
})
.then((Book) => res.json(Book))
.catch((err) => {
console.log(err);
res.status(500).json(err);
});
},
Here is the working code. In the where clause, I want to do { first_name: { [Op.substring]: query } }, for example but it isn't accessing the Author table. In the include statement I have tried aliasing and calling it in the where clause, but that throws a aliasing error saying I have already declared an alias (Author) but when I try to use that as { 'Author.first_name' { [Op.substring]: query } }, it returns that there is no Book.Author.first_name.
I am probably missing something simple, so anyone that might be able to help, let me know what I am doing wrong here!
Solved and it was super easy. I was missing the syntax for accessing the separate table which is '$Author.first_name$'.

Sequelize.js - Invalid date problem in where clause [duplicate]

I am using Sequelize as my backend ORM. Now I wish to do some WHERE operations on a Date.
More specifically, I want to get all data where a date is between now and 7 days ago.
The problem is that the documentation does not specify which operations you can do on Datatypes.DATE
Can anyone point me in the right direction?
Just like Molda says, you can use $gt, $lt, $gte or $lte with a date:
model.findAll({
where: {
start_datetime: {
$gte: moment().subtract(7, 'days').toDate()
}
}
})
If you're using v5 of Sequelize, you've to include Op because the key was moved into Symbol
const { Op } = require('sequelize')
model.findAll({
where: {
start_datetime: {
[Op.gte]: moment().subtract(7, 'days').toDate()
}
}
})
See more sequelize documentation here
I had to import the Operators symbols from sequelize and use like so.
const { Op } = require('sequelize')
model.findAll({
where: {
start_datetime: {
[Op.gte]: moment().subtract(7, 'days').toDate()
}
}
})
According to the docs, for security reasons this is considered best practise.
See http://docs.sequelizejs.com/manual/tutorial/querying.html for more info.
Using Sequelize without any aliases improves security. Some frameworks
automatically parse user input into js objects and if you fail to
sanitize your input it might be possible to inject an Object with
string operators to Sequelize.
(...)
For better security it is highly advised to use Sequelize.Op and not
depend on any string alias at all. You can limit alias your
application will need by setting operatorsAliases option, remember to
sanitize user input especially when you are directly passing them to
Sequelize methods.
You can also use Sequelize.literal() to perform dates manipulation in SQL.
The following code works with Postgres, but I'm quite sure something similar could be done in other systems as well:
model.findAll({
where: {
start_datetime: {
$gte: Sequelize.literal('NOW() - INTERVAL \'7d\''),
}
}
})
This solution is without the moment.js library.
Between 7 days ago and now
const sevenDaysAgo = new Date(new Date().setDate(new Date().getDate() - 7));
models.instagram.findAll({
where: {
my_date: {
$gt: sevenDaysAgo,
$lt: new Date(),
},
},
});
Between now and 7 days from now
const sevenDaysFromNow = new Date(new Date().setDate(new Date().getDate() + 7));
models.instagram.findAll({
where: {
my_date: {
$gt: new Date(),
$lt: sevenDaysFromNow,
},
},
});
Notes:
$gt stands for "greater than". You could use $gte instead of $gt. $gte stands for "greater than or equal to". Same for $lte of course.
Technically speaking, you need both $lt and $gt to make sure that the date isn't into the future (per the original question).
Other answers show the use of [Sequelize.Op.gt] instead of $gt. Use that if on Sequelize v5.
Based on the other answers and comments, a way without using third party packages would be:
const sevenDaysFromNowResults = await db.ModelName.findAll({
where: {
createdAt: {
[Sequelize.Op.gte]: new Date(new Date() - (7 * 24 * 60 * 60 * 1000)) // seven days ago
}
}
})
The seven days are calculated using Date utility in milliseconds (days * 24hrs * 60mins * 60secs * 1000ms)

Sequelize creating SQL for DATE type

I`m using sequelize with mysql and I want to know if there is a way to use sequelize to create SQLs like these:
SELECT * FROM table WHERE DAY(dateAttribute) = 'chosen_day'
SELECT * FROM table WHERE MONTH(dateAttribute) = 'chosen_month'
I`ve tried to do something like this:
this.test= function () {
let searchingDate = "2017-05%";
return model.table.findAll({
where: {
dateAttribute: searchingDate
}
})
}
This works but it gives a deprecation warning saying that it will be remove in future versions.
Thanks for any help!
Found the solution!
So I used the sequelize.fn() with the Op.and function from sequelize. So the solution was:
this.test = function () {
return model.tableName.findAll({
attributes: ['myAttribute'],
where: {
dateAttribute: {
[sequelize.Op.and]: [
sequelize.where(sequelize.fn('MONTH', sequelize.col('dateAttribute')), 5),
sequelize.where(sequelize.fn('DAY', sequelize.col('dateAttribute')), 5),
]
},
otherAttribute: 'someValue'
}
})
}
This code generated this SQL query:
SELECT myAttribute FROM tableName WHERE (MONTH('dateAttribute') = 5 AND DAY('dateAttribute') = 5) AND otherAttribute = 'someValue'
I hope to save you guys from doing this long researching that I made!
Remember be HAPPY!

Updating multiple records using sequelize and nodejs?

Using node.js (v0.10.12) and sequelize (2.0.0-dev9), I am wanting to update attributes within an existing database table. From anther SO discussion I am able to update only one record at a time. I encapsulated the call in a for loop but that updated the same record throughout the for loop.
Could someone help me modify this code to update all records that match the search requirements? Below is my code:
global.db.Task.find({ where: {"Cd": "match"} && {"Desc": null}
}).on('success', function(task) {
if (task) { // if the record exists in the db
task.updateAttributes({
Desc: "New Upate"
}).success(function() {});
}
})
According to the docs you need to do a BulkCreate: (Not tested)
global.db.Task.update(
{Desc: 'New Upate'}, //set attribute
{cd: 'match', Desc: null} //where criteria
).success(function(affectedRows) { ... });