olivierwilkinson / prisma-extension-soft-delete

Prisma extension for adding soft delete to Prisma models, even when using nested queries
Apache License 2.0
109 stars 16 forks source link

should i be able to include soft deleted rows when using deletedAt ? #12

Open scottsymm opened 9 months ago

scottsymm commented 9 months ago

should i be able to include soft deleted rows when using deletedAt ?

.findUnique({ where: { id: row.id, NOT: { deletedAt: null } } })

generates

 WHERE ("public"."Table"."id" = $1 AND (NOT "public"."Table"."deletedAt" IS NULL) AND "public"."Table"."deletedAt" IS NULL)
olivierwilkinson commented 9 months ago

Heya,

Thanks for raising this! That pattern currently does not work unfortunately. This is because the extension adds the configured field deletedAt if it is not found at the top level of the where object, so the query is modified to this:

.findUnique({ where: { id: row.id, NOT: { deletedAt: null }, deletedAt: null } });

I'm planning on modifying the way I handle logical operations (NOT, AND, OR) in where objects, when I do that I will make sure this case also works.

In the meantime you can use an alternate query that has the deletedAt field at the top level:

.findUnique({ where: { id: row.id, deletedAt: { not: null } });

I hope that helps! I'm going to keep this issue open until I've fixed your original query as that should work as expected 👍

dmaksimov commented 6 months ago

@olivierwilkinson Any suggestions on doing something like this for include queries?

I'm trying to select all related models (including soft deleted ones).

await prisma.appointment.findUnique({
  where: {
    id: params.id,
  },
  include: {
    services: {
      where: {
        NOT: { deletedAt: null },
      },
    },
  },
});
bighitbiker3 commented 3 months ago

@dmaksimov did you ever figure out a good solution for this?