Hey! I am currently doing something like ```const ...
# orm-help
s
Hey! I am currently doing something like
Copy code
const lastLesson = await db.lesson.findFirst({ where: { date: { gt: new Date() } }});
but I have a field called
lesson.duration
. So I would ideally want to do only get it if the duration has passed, maybe something like this
Copy code
const lastLesson = await db.lesson.findFirst({ where: { date: { gt: addMinutes(new Date(), 
this.duration) } }});
basically can I get the where to know context about the current row somehow so it can know what the duration is while checking, rather than having to loop? Is this possible?
m
Did you try it?
r
This should work, as long as
addMinutes
returns a valid Date object.
plus one +1 1
m
If you can read the duration with this.duration, then it will work in the prisma query also.
s
It works fine if I do
addMinutes(new Date(), 60)
. When I try to use
this.duration
I get
Cannot read property 'duration' of undefined
, even though it exists on the row.
m
Are you able to print this.duration on console?
You just need to ensure that this.duration is defined.
s
Sorry ,maybe I wasn’t clear. So
this.duration
was just my placeholder. I have a model which looks like
Copy code
model lesson {
  id          String            @id @default(uuid())
  date        DateTime
  duration    Int?              @default(60)
}
And then a function (in an api route) that only contains
Copy code
const getNextLesson = async (req, res) => {
   return db.lesson.findFirst({
      where: {
        date: { gt: addMinutes(new Date(), 60) },
      },
    });
  }
And I want to replace that
60
so it gets it from the database instead. I am not defining
this.duration
anywhere manually.
m
Hmm, you mean the duration value is in the record that you are finding with this query?
s
yeah!
m
I’ve not tried it, but it’s an interesting use case. Let’s see if someone else replies.
r
Are you searching for the record first and then using it? Because referencing one’s own fields isn’t possible yet.
s
ah thanks, it seems like that is pretty much what I am trying to do. Searching and then re-searching in a while until the conditions are met may be the easiest way to do this for now. Thanks team!
💯 1