Daniel Uhm
10/06/2021, 4:21 PMimport { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
prisma.$use(async (params, next) => {
const bypassSoftDeleted: string[] = [];
if (params.model && !bypassSoftDeleted.includes(params.model)) {
if (!['create', 'update', 'upsert', 'delete'].includes(params.action)) {
if (!params.args.where) params.args.where = {};
if (!params.args.where['deletedAt']) {
params.args.where['deletedAt'] = null;
}
}
if (['delete', 'deleteMany'].includes(params.action)) {
if (params.action === 'delete') params.action = 'update';
if (params.action === 'deleteMany') params.action = 'updateMany';
if (!params.args.data) params.args.data = {};
params.args.data['deletedAt'] = new Date();
}
}
return next(params);
});
export { prisma };
The middleware above is a soft delete. Thanks for reading.Ryan
10/07/2021, 6:01 AMMaybe serverless-offline creates a Prisma client again in the process of recompiling this fileYeah that’s most probably the case. Could you try this and see if that works? Also re-start the database so that old connections are killed.
Daniel Uhm
10/07/2021, 6:03 AM