How i would get all records of soft delete applied...
# orm-help
j
How i would get all records of soft delete applied model?
plus one +1 1
1
m
So you ARE wanting the deleted records? Depends on how you're "soft deleting" them - are you using a boolean attribute or a date attribute? Either way, it is just going to be part of your WHERE clause:
Copy code
prisma.widget.findMany({
  where: { isDeleted: true }
});
for boolean
Copy code
prisma.widget.findMany({
  where: { deletedAt lte now }
});
for dates, and you'll have to dig a little bit for the exact syntax
That deletedAt clause could probably also check against
is not null
j
Thanks for the response, there is a middle ware applied globally on model . Soft delete middleware is inspired from Here. i want all records including deleted records for one place.
m
I kinda exploded some pent-up math geekery here. TLDR: Just search on the place id and you'll get all the records. So you're intercepting delete & deleteMany, as per this document, right? That has nothing to do with your find* methods. Which means that, since "deleted" is just any old field as far as Prisma modeling is concerned, if you run find*, then it won't matter whether the records are soft-deleted or not. Consider two disjoint, complementary sets on the Widget model: • A: active Widget records • B: soft-deleted Widget records These sets are disjoint based on their deleted attribute -
false
for active records, and `false`'s complement
true
for soft-deleted records. You can find* the active records with a where clause like: { deleted: false } You can find* the soft-deleted records with a where clause like: { deleted: true } Since the sets are disjoint (and complementary), it would suffice then to find all records where deleted is false OR where deleted is true. That would be all of them. So then, if you want "all records including deleted records for one place" - with an id placeId, then your where clause could look like:
Copy code
where: {
  OR: [
    { placeId: placeId, deleted: false }, // get active records
    { placeId: placeId, deleted: true }, // get soft-deleted records
  ]
}
which is of course logically equivalent to:
Copy code
where: { placeId: placeId }
🙌 1
n
Thanks for providing a thorough explanation Michael 🙌
i want all records including deleted records for one place.
This would be equivalent to a normal
findMany
call and deleted field won’t matter in this case.