Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for Before/After Hooks on Create/Update/Delete, Soft Deletes, and Removing Methods #10

Open
MichaelrMentele opened this issue Feb 23, 2023 · 1 comment

Comments

@MichaelrMentele
Copy link

MichaelrMentele commented Feb 23, 2023

VERY excited for this feature. Thank you for the examples!

One area I'm confused about is adding a side-effect to a model CUD action.

This could be replacing the delete action with a custom delete action or making sure some log is always written on table creation. Is this possible?

Also in the docs it says extensions could be used for soft-deletes and for removing deletes entirely would love examples of these two usecases as well.

Also, does this work so that if you override a method on a model you can ensure that nested creates on other models will work as well? For example, if I want to make sure there is no delete method on a model and remove that operation how can I prevent a nested delete on all the other models?

// extend model to disallow deletes

db.someModel.delete <- should be undefined

db.someOtherRelatedModel.update({
    where: ...,
    data: { someModel: { delete: true } }, // shouldn't work
})
@MichaelrMentele MichaelrMentele changed the title Examples for Before/After Hooks on Create Examples for Before/After Hooks on Create/Update/Delete, Soft Deletes, and Removing Methods Feb 23, 2023
@MichaelrMentele
Copy link
Author

MichaelrMentele commented Feb 23, 2023

// extension
export default function extendDealTradeIn(prisma: PrismaClient) {
  return prisma.$extends({
    model: {
      vehicle: {
        delete: undefined,
      },
    },
  })
}
// test
  describe('vehicle', () => {
    it('delete method is undefined', async () => {
      // The direct method is removed...
      expect(db.vehicle.delete).toBeUndefined()
    })

    it('nested deletes are undefined', async () => {
      // the indirect delete is NOT prevented...
      const vehicle = await fakeVehicle()
      const vehicleCount = await db.vehicle.count({ where: { id: vehicle.id } })
      expect(vehicleCount).toBe(1)
      await db.stage.update({
        where: {
          id: vehicle.stageId,
        },
        data: {
          vehicles: {
            delete: {
              id: vehicle.id,
            },
          },
        },
      })
      const newVehicleCount = await db.vehicle.count({
        where: { id: vehicle.id },
      })
      // without the extension, this SHOULD be 0
      // with the extension this SHOULD be 1 but either way
      // in both cases the new vehicle count is 0 meaning
      // the vehicle was deleted
      expect(newVehicleCount).toBe(1)
    })
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant