Giter Club home page Giter Club logo

prisma-cascade-delete's Introduction

Prisma Cascade Delete

Prisma (v2) is one of the hottest ORM's right now (you can see some of the reasion I think so here). It still has some important holes, though. Cascade delete is one of the most proeminent ones, and most of the "automatic" workarounds have some issue.

This package exposes a cascadeDelete function that uses the information available at the prisma models to perform cascade deletes on one-to-many and many-to-many (with a relation table) relations. The cascade goes as deep as the relation goes (see example below).

Installation

npm install prisma-cascade-delete

Usage

import { cascadeDelete } from "prisma-cascade-delete";

cascadeDelete(prisma, modelName, where);
// prisma is the prisma-client instance from your app
// modelName must be the same as used in the prisma schema (e.g. "User")
// where is an object accepted by prisma queries as a *where* statement

Example

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
  author    User    @relation(fields: [authorId], references: [id])
  authorId  Int
}

model Tag {
  id        Int      @id @default(autoincrement())
  name      String
}

model TagsInPost {
    id       Int     @id @default(autoincrement())
    post     Post    @relation(fields: [postId], references: [id])
    postId   Int
    tag      Tag     @relation(fields: [tagId], references: [id])
    tagId    Int

}

In the schema above, we would expect that if we delete a User, all its Posts and all the Tags associated to this posts would be deleted as well. However, there is no way to set this up.

That's where cascadeDelete comes in;

import { cascadeDelete } from "prisma-cascade-delete";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

cascadeDelete(prisma, "User", { id: 1 });

This function will generate

const userDelete = prisma.user.delete({ where: { id: 1 } });
const postsDelete = prisma.post.deleteMany({ where: { userId: 1 } });
const postTagDelete = prisma.tagsInPost.deleteMany({
  where: { OR: [{ postId: 1 }, { postId: 2 }] },
}); 
// Imagining that the user posted posts 1 and 2

prisma.$transaction([postTageDelete,postsDelete,userDelete])

prisma-cascade-delete's People

Contributors

felixhaeberle avatar pedrovrima avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.