Hello there i have a small doubt i am using nest ...
# orm-help
t
Hello there i have a small doubt i am using nest js with prisma i wanted to know that how can i manage includeDeleted flag for any table i want all users data, and i have used soft delete middleware as discussed Here
1
n
Hey Tanuj 👋 Welcome to our community! Can you please elaborate on what manage includeDeleted flag for any table would mean? Do you want to include deleted flag in all your tables?
t
Hello @Nurul i want all data with deleted flag true in findmany query my current scenario as @Michael Jay said in one of his conversation i have tried below
Copy code
const users = await this.prisma.user.findMany({
      where: {
        OR: [
          {
            completed_profile: true,
            deleted: false,
          },
          {
            completed_profile: true,
            deleted: true,
          },
        ],
      },
    });
and i have take reference of soft delete middleware for users model now, this thing also didn't work i am getting only those records who have deleted as false value
m
@Tanuj Doshi I would try this where clause. There's some discussion below which may be useful to you, but I didn't want to obfuscate the appropriate where clause in discussion.
Copy code
where: {
  completed_profile: true,
  deleted: true
}
Discussion If you remember in the other person's use case, they wanted to get back ALL records (soft-deleted AND not deleted). And the point that I was trying to make was that you can OR with deleted: false & deleted: true, but that's the same thing as not putting the deleted clause in there at all. In your case, your where clause is exactly equivalent to:
Copy code
where: { completed_profile: true }
It seems like your use case is about getting "all data with deleted flag true". This means that your where clause should NOT include the deleted: false. And thus:
Copy code
where: {
  completed_profile: true,
  deleted: true
}
In other words, I interpret your use case as "get all users who have completed their profile and whose accounts are deleted." Now let's talk about your other issue. You say that you are getting only those records who have deleted: false. This is unexpected behavior. Your query SHOULD return those records, but it should also return the records that have deleted: true. The first thing that I would check is whether your table does indeed have any deleted users to return to you. You can easily check out your table with Prisma Studio. If you DON'T have any deleted users who have completed their profile, then naturally your query would have returned only those that aren't deleted. So with the query you've written, if you aren't getting any deleted records back, here's what I think the most likely reasons are, in order. 1. You don't actually have any records that meet the qualifying clause: "has completed profile and is deleted". Check your DB to find out if this is the case. 2. Your query returned both records that have deleted: false AND records that have deleted: true, but they were clumped in a way that it looked like you were only getting deleted: false records back. To check this, you could run a filter on the returned array:
users.filter(user => user.deleted)
. This would be the same thing as just writing the new query clause as I specified above. Another thing to check, which could maybe indicate a bug in your BE logic, is to run a query with the where clause ONLY checking on deleted: true. You could potentially be changing the completed_profile value to false when you set deleted to true, for example. Which would mean you had no records that matched completed_profile: true AND deleted: true.
💯 1
🙌 1