I have a entity with a date property
@Entity()
export class Foo {
// ...other properties
@Property({ type: DateType })
date = new Date()
}
And I want to make a query to find all Foo
that are from some specific day.
e.g.
async getByDate(someDate: Date) {
const foo = await this.repository.find({
// What should go to get Foo from the same day as `someDate` ?
})
}
I couldn't find in the documentation how to do it.
I would also like to do things like "find Foo
from some week" or "find Foo
from from some month"
If you use the
DateType
, you are mapping it to theDate
object, so you should query byDate
objects. The time part of theDate
object will be truncated - that happens in theDateType
so feel free to check its source code, it's quite straightforward.https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/types/DateType.ts
From the implementation you can also see that in fact it does support querying by strings too, so this will work as well:
To filter for a whole week, you need to first find the start and end of the week (will keep that for you), and use combination of
$gte
and$lt
operators: