33
loading...
This website collects cookies to deliver better user experience
const authors = await Promise.all(
[1, 2, 3].map(
async i =>
await prisma.author.create({
data: {
firstName: `First name ${i}`,
lastName: `Last name ${i}`,
},
})
)
);
const deletedAuthor = await prisma.author.delete({
where: {
id: authors[0].id,
},
});
prisma.[entity].delete
, the where field must have the primary key of the record that has to be deleted.try {
const deletedAuthor = await prisma.author.delete({
where: {
id: 1,
},
});
console.log({ deletedAuthor });
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === "P2025"
) {
console.log("Author not found");
} else console.error(error);
}
const deletedAuthorsResult = await prisma.author.deleteMany({
where: {
id: {
in: authors.map(a => a.id),
}
},
});
count
and it contains the amount of the records effected by the delete operation.export type AuthorWhereInput = {
id?: IntFilter | number
firstName?: StringFilter | string
lastName?: StringFilter | string
comments?: CommentListRelationFilter
posts?: AuthorsOnPostListRelationFilter
AND?: Enumerable<AuthorWhereInput>
OR?: Enumerable<AuthorWhereInput>
NOT?: Enumerable<AuthorWhereInput>
}
id
.IntFilter
. At this point, if you need to find your record using the id, you can pass your value to this field, else if you want to delete records using more complex search terms you can use the IntFilter
, which is composed thusexport type IntFilter = {
equals?: number
in?: Enumerable<number>
notIn?: Enumerable<number>
lt?: number
lte?: number
gt?: number
gte?: number
not?: NestedIntFilter | number
}
in
NestedIntFilter
, which is another type equal to the IntFilter
, and it allows you to create your filters using the positive search terms and check if the records respect the opposite.
As you may imagine, there are other three types similar to: StringFilter, BoolFilter and DateTimeFilter, and each one depends from the column's type that you are testing.
DateTimeFilter is equal to the IntFilter expect that works
with the Date. BoolFilter contains only the equals
and the not
operations for obvious reasons. The StringFilter in addition to the operations exposed by the IntFilter has other three operations: contains
, startsWith
and endsWith
(I won't describe these operations because I think they speak by themselves).
After this explanation, I think you have understood also the fields: firstName
, lastName
.
Now I want to go on to the posts
and comments
fields, that have the same approach.
These fields allow you to check some particular cases inside the posts or the comments related to the record. Each field allows you to do these three operations every
, some
and none
export type CommentListRelationFilter = {
every?: CommentWhereInput
some?: CommentWhereInput
none?: CommentWhereInput
}
export type AuthorsOnPostListRelationFilter = {
every?: AuthorsOnPostWhereInput
some?: AuthorsOnPostWhereInput
none?: AuthorsOnPostWhereInput
}
every
post/comment has a particular value or more particular valuessome
post/comment has a particular value or more particular valuesnone
post/comment has a particular value or more particular values
It's not so difficult to understand it but I recommend you to play with them.AND
, OR
and NOT
.const deletedAuthorsResult = await prisma.author.deleteMany({
where: {
AND: [
{
NOT: {
id: 10,
},
},
{
OR: {
firstName: {
startsWith: "name",
},
},
},
],
OR: [
{
posts: {
every: {
post: {
updatedAt: {
lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 7),
},
},
},
},
},
],
},
});
deleteMany
and I think the definition types created by the Prisma team are a good friends to prevent errors. It's obvious that they don't create the right query for you, that's your work. 💪