Soft Delete with Nestjs

Hi everyone How I can implement soft deletes using Prisma and Nestjs? I found on the docs but is using middlewares, and for what I know, they are deprecated. Thanks for your time
13 Replies
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
Anyone can help me please?
Nurul
Nurul•4w ago
Hey @Daniel Sousa @TutoDS 👋 Did you see this extension? https://github.com/olivierwilkinson/prisma-extension-soft-delete You can use this extension in your NestJS setup.
GitHub
GitHub - olivierwilkinson/prisma-extension-soft-delete: Prisma exte...
Prisma extension for adding soft delete to Prisma models, even when using nested queries - GitHub - olivierwilkinson/prisma-extension-soft-delete: Prisma extension for adding soft delete to Prisma...
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
I don’t have the delete field only deleted_at and deleted_by
Nurul
Nurul•4w ago
I believe it's not mandatory to use delete field. You can pass your own. In your case it would be deleted_at
No description
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
Is psosible to create an extension to fill created_by automatically with the logged user?
Nurul
Nurul•4w ago
Yes, you can do that by using query extension. It will look something like this:
const prisma = new PrismaClient().$extends({
query: {
$allModels: {
create: ({ args, query }) => {
args.data = {
...args.data,
createdBy: { connect: { id: getCurrentUserId() } },
};
return query(args);
},
createMany: ({ args, query }) => {
args.data = args.data.map((item) => ({
...item,
createdBy: { connect: { id: getCurrentUserId() } },
}));
return query(args);
},
},
},
});
const prisma = new PrismaClient().$extends({
query: {
$allModels: {
create: ({ args, query }) => {
args.data = {
...args.data,
createdBy: { connect: { id: getCurrentUserId() } },
};
return query(args);
},
createMany: ({ args, query }) => {
args.data = args.data.map((item) => ({
...item,
createdBy: { connect: { id: getCurrentUserId() } },
}));
return query(args);
},
},
},
});
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
Thanks, but you can get the user from the request.user?
Nurul
Nurul•4w ago
That is an implementation detail. The getCurrentUserId function should return the user id. This may come form your incoming request.
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
Thanks, I need to see to get the request from Nestjs on Prisma service Hi @Nurul (Prisma) Sorry to bother you again
model Customer {
id String @id @default(uuid())
code String @default(uuid())
name String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt() @map("updated_at")
deletedAt DateTime? @map("deleted_at")
createdBy String @map("created_by")
updatedBy String @map("updated_by")
deletedBy String @map("deleted_by")

userCreatedById User @relation("CustomerCreatedByUser", fields: [createdBy], references: [id], onDelete: Cascade)
userUpdatedById User @relation("CustomerUpdatedByUser", fields: [updatedBy], references: [id], onDelete: Cascade)
userDeletedById User @relation("CustomerDeletedByUser", fields: [deletedBy], references: [id], onDelete: Cascade)

@@map("customers")
}
model Customer {
id String @id @default(uuid())
code String @default(uuid())
name String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt() @map("updated_at")
deletedAt DateTime? @map("deleted_at")
createdBy String @map("created_by")
updatedBy String @map("updated_by")
deletedBy String @map("deleted_by")

userCreatedById User @relation("CustomerCreatedByUser", fields: [createdBy], references: [id], onDelete: Cascade)
userUpdatedById User @relation("CustomerUpdatedByUser", fields: [updatedBy], references: [id], onDelete: Cascade)
userDeletedById User @relation("CustomerDeletedByUser", fields: [deletedBy], references: [id], onDelete: Cascade)

@@map("customers")
}
This is my current customers schema, but when I try to create a new one, I need to insert the updatedBy, the deletedBy. How I can make it optional? Or, what is the best way to handle these cases?
Nurul
Nurul•4w ago
In this case you just need to add ? to the fields like this:
updatedBy String? @map("updated_by")
deletedBy String? @map("deleted_by")
updatedBy String? @map("updated_by")
deletedBy String? @map("deleted_by")
This would make both the fields optional.
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•4w ago
And how to handle the relation?
Daniel Sousa @TutoDS
Daniel Sousa @TutoDSOP•3w ago
@Nurul (Prisma) I try the extends that you suggest me but I got this error
No description
Nurul
Nurul•3w ago
Can you share the entire updated User model and the prisma query with me?

Did you find this page helpful?