-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
217ff55
commit f3542d4
Showing
1 changed file
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,62 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
// import { withOptimize } from "@prisma/extension-optimize"; | ||
import { Prisma, PrismaClient } from "@prisma/client"; | ||
|
||
// export const prisma = new PrismaClient().$extends(withOptimize()); | ||
const prisma = new PrismaClient(); | ||
const sanitizeData = (data: any): any => { | ||
// Return early for null or undefined | ||
if (data == null) return data; | ||
|
||
// Handle Date objects | ||
if (data instanceof Date) { | ||
return data; | ||
} | ||
|
||
// Handle strings | ||
if (typeof data === "string") { | ||
return data.replace(/\u0000/g, ""); | ||
} | ||
|
||
// Handle objects and arrays | ||
if (typeof data === "object") { | ||
return Object.keys(data).reduce( | ||
(acc, key) => ({ | ||
...acc, | ||
[key]: sanitizeData(data[key]), | ||
}), | ||
Array.isArray(data) ? [] : {}, | ||
); | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
// Define the sanitization extension | ||
const sanitizationExtension = Prisma.defineExtension({ | ||
name: "sanitization", | ||
query: { | ||
$allModels: { | ||
async create({ args, query }) { | ||
args.data = sanitizeData(args.data); | ||
return query(args); | ||
}, | ||
async update({ args, query }) { | ||
args.data = sanitizeData(args.data); | ||
return query(args); | ||
}, | ||
async upsert({ args, query }) { | ||
args.create = sanitizeData(args.create); | ||
args.update = sanitizeData(args.update); | ||
return query(args); | ||
}, | ||
async createMany({ args, query }) { | ||
args.data = Array.isArray(args.data) | ||
? args.data.map((item) => sanitizeData(item)) | ||
: sanitizeData(args.data); | ||
return query(args); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Create extended client with sanitization | ||
const prisma = new PrismaClient().$extends(sanitizationExtension); | ||
|
||
export { prisma }; |