olivierwilkinson / prisma-soft-delete-middleware

Middleware for adding soft delete to Prisma models, even when using nested queries
Apache License 2.0
103 stars 6 forks source link

[Suggestion] Auto configuration, based on field #6

Open Ottavio97 opened 1 year ago

Ottavio97 commented 1 year ago

Hi Olivier, first of all, thanks for your middleware.

It would be a cool feature to autoconfigure models with soft-delete, wherever they contain the field configured in your middleware.

Imagine if you can put a new config parameter like autoConfigure: true, with this implementation I made for my project:

this.$use(
      createSoftDeleteMiddleware({
        models: Prisma.dmmf.datamodel.models
          .filter((m) => m.fields.some((f) => f.name == 'deletedAt'))
          .map((md) => ({ [md.name]: true }))
          .reduce((prev, next) => ({ ...prev, ...next }), {}),
        defaultConfig: {
          field: 'deletedAt',
          createValue: (deleted) => {
            if (deleted) return new Date();
            return null;
          },
        },
      }),

It may be really useful and more automatic. Thanks again :)

olivierwilkinson commented 1 year ago

Hi there,

I'm glad you are finding the middleware helpful 🎉

That's an interesting idea, I can see this being a useful option! There are some other issues I need to look into first but I'll get round to this as soon as I have the bandwidth 😄