MikroORM Create filter query based on a value in the database
Simply put, is it possible to create a filter query where I reference a value stored in the row?
For example:
orm.em.findOne(Job, {
status: 'active',
startDate: {
$gt: '$anotherDateField'
}
}
My goal is to have a user-input defined filter (the status), but also only bring back certain rows where the start date is greater than another column's value.
You can use custom SQL fragment
orm.em.findOne(Job, {
status: 'active',
// expr helper allows to escape strict typing of the method, so we can use `em.raw()`
[expr('startDate')]: {
$gt: orm.em.raw('another_date_field') // this will have to be column name, not property name
}
}
Note that your em
needs to be typed to the one exported from driver package to have access to the em.raw()
method (if you work with orm
instance, you need to type that to MikroORM<YourDriver>
so orm.em
can be properly typed).
https://mikro-orm.io/docs/entity-manager/#using-custom-sql-fragments