49 lines
1.0 KiB
TypeScript
49 lines
1.0 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
|
|
export function toPrismaJsonValue(value: unknown): Prisma.InputJsonValue {
|
|
if (value === null) {
|
|
return "null";
|
|
}
|
|
|
|
if (typeof value === "string" || typeof value === "boolean") {
|
|
return value;
|
|
}
|
|
|
|
if (typeof value === "number") {
|
|
return Number.isFinite(value) ? value : String(value);
|
|
}
|
|
|
|
if (typeof value === "bigint") {
|
|
return value.toString();
|
|
}
|
|
|
|
if (value instanceof Date) {
|
|
return value.toISOString();
|
|
}
|
|
|
|
if (value instanceof Error) {
|
|
return {
|
|
name: value.name,
|
|
message: value.message,
|
|
stack: value.stack ?? ""
|
|
};
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
return value.map((item) => toPrismaJsonValue(item));
|
|
}
|
|
|
|
if (typeof value === "object") {
|
|
const output: Record<string, Prisma.InputJsonValue> = {};
|
|
|
|
for (const [key, raw] of Object.entries(value as Record<string, unknown>)) {
|
|
if (raw === undefined) continue;
|
|
output[key] = toPrismaJsonValue(raw);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
return String(value);
|
|
}
|