What's the best approach to get an object/s by the...
# orm-help
s
What's the best approach to get an object/s by the month they were created. Like get all the object for the month of may and year 2022.
n
Do you mean that you are storing full dates and want to filter on the month/year of the date?
s
Yes, I am using this.
Copy code
createdAt      DateTime   @default(now())
  updatedAt      DateTime   @updatedAt
n
Are you using PostgreSQL?
s
Copy code
const events = await prisma.posts.findMany({
    where: {
      userId : 1,
      createdAt: {
        gte: new Date("May 2022"),
        lt:  new Date("June 2022")
      }
    },
    skip: 0,
    take: 50
  });
Right now I have tried this. Works, but was wondering if there are better solutions. Yes, I am using Postgres.
The problem with above is I will have to know what the next month is and in cases like December will also need to specify which year it is.
n
For filtering gte and lt operators are the preferred way without using raw queries
s
Maybe Prisma could add features like this from Django? https://docs.djangoproject.com/en/dev/ref/models/querysets/#year
Copy code
Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6)
n
Could you create a feature request for this here?
👍 1