skip to Main Content

I’m using Prisma with a MySQL database. This is my code:

  const thisYear = Number("2023")
  const newYear = thisYear + 1

  const startDate = format(new Date(thisYear, 6, 1), 'yyyy-MM-dd HH:mm:ss')
  const endDate = format(new Date(newYear, 5, 30), 'yyyy-MM-dd HH:mm:ss')
  console.log("startDate, endDate: ", startDate, endDate)

  const bucketTransactions = await prisma.bucket_transaction.groupBy({
       by: ['bucket_id'],
       _sum: {
           amount: true,
       },
       where: {
           date_added: {
               gte: startDate,
               lte: endDate,
           },
           amount: {
               gt: 0,
           },
           donation_id: {
               gt: 0,
           },
       },
   });

I get this error:

Invalid value for argument `gte`: input contains invalid characters. Expected ISO-8601 DateTime.

This is the value of startDate and endDate in the console log:

startDate, endDate:  2023-07-01 00:00:00 2024-06-30 00:00:00

Do you know what’s wrong with my code?

I ran this directly into the SQL terminal and it worked:

select * from bucket_transaction WHERE date_added BETWEEN "2023-07-01 00:00:00" and "2024-06-30 00:00:00" AND amount > 0 AND donation_id > 0;

Why doesn’t it work with Prisma?

2

Answers


  1. Chosen as BEST ANSWER

    I had to change the startDate and the endDate into a new Date object like this:

           where: {
               date_added: {
                   gte: new Date(startDate),
                   lte: new Date(endDate),
               }
           },
    

  2. You can change

    const startDate = format(new Date(thisYear, 6, 1), 'yyyy-MM-dd HH:mm:ss')
    

    to

    const startDate = new Date(thisYear, 6, 1)
    

    or

    const startDate = new Date(thisYear, 6, 1).toISOString()
    

    Prisma work with the date directly or with the .toISOString()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search