From 4b8a83bc463b6b399be41bad1107968e2d18b144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 05:14:42 +0200 Subject: [PATCH] encodedAsJsonValue --- src/Schema/encodedAsJsonValue.ts | 12 ++++++++++++ src/Schema/index.ts | 1 + 2 files changed, 13 insertions(+) create mode 100644 src/Schema/encodedAsJsonValue.ts diff --git a/src/Schema/encodedAsJsonValue.ts b/src/Schema/encodedAsJsonValue.ts new file mode 100644 index 0000000..b569854 --- /dev/null +++ b/src/Schema/encodedAsJsonValue.ts @@ -0,0 +1,12 @@ +import type { Schema } from "@effect/schema" +import type { Jsonifiable } from "type-fest" + + +/** + * Takes a schema of which the encoded value satisfies `Jsonifiable` and returns the same schema with Prisma's `JsonValue` as its encoded type instead. + * This allows you use to use jsonifiable schemas to strongly type `Json` database fields in Prisma. + * + * This is needed because type-fest's `Jsonifiable` and `JsonValue` types do not satisfy Prisma's `JsonValue`, and as such we need to perform an unsafe cast. + */ +export const encodedAsJsonValue = (schema: Schema.Schema) => + schema as Schema.Schema diff --git a/src/Schema/index.ts b/src/Schema/index.ts index 8e5942f..3d6c5f1 100644 --- a/src/Schema/index.ts +++ b/src/Schema/index.ts @@ -1,5 +1,6 @@ export * from "./Class" export * from "./DateTime" +export * from "./encodedAsJsonValue" export * from "./Jsonifiable" export * from "./Kind" export * as MobX from "./MobX"