From 21d1ede3c8ff579c297372bced3e94057cd84874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 05:22:19 +0200 Subject: [PATCH] PrismaJson --- src/Schema/encodedAsJsonValue.ts | 5 ++-- src/Types/PrismaJson.ts | 51 ++++++++++++++++++++++++++++++++ src/Types/index.ts | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Types/PrismaJson.ts diff --git a/src/Schema/encodedAsJsonValue.ts b/src/Schema/encodedAsJsonValue.ts index b569854..93bf8b1 100644 --- a/src/Schema/encodedAsJsonValue.ts +++ b/src/Schema/encodedAsJsonValue.ts @@ -1,12 +1,13 @@ import type { Schema } from "@effect/schema" import type { Jsonifiable } from "type-fest" +import type { PrismaJson } from "../Types" /** * 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. + * This is needed because type-fest's `Jsonifiable` and `JsonValue` types do not satisfy Prisma's `JsonValue`, and as such we need to perform a cast. */ export const encodedAsJsonValue = (schema: Schema.Schema) => - schema as Schema.Schema + schema as Schema.Schema diff --git a/src/Types/PrismaJson.ts b/src/Types/PrismaJson.ts new file mode 100644 index 0000000..16577a9 --- /dev/null +++ b/src/Types/PrismaJson.ts @@ -0,0 +1,51 @@ +/** + * Prisma's Json types + * @module + */ + + +/** + * From https://github.com/sindresorhus/type-fest/ + * Matches a JSON object. + * This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. + */ +export type JsonObject = {[Key in string]?: JsonValue} + +/** + * From https://github.com/sindresorhus/type-fest/ + * Matches a JSON array. + */ +export interface JsonArray extends Array {} + +/** + * From https://github.com/sindresorhus/type-fest/ + * Matches any valid JSON value. + */ +export type JsonValue = string | number | boolean | JsonObject | JsonArray | null + +/** + * Matches a JSON object. + * Unlike `JsonObject`, this type allows undefined and read-only properties. + */ +export type InputJsonObject = {readonly [Key in string]?: InputJsonValue | null} + +/** + * Matches a JSON array. + * Unlike `JsonArray`, readonly arrays are assignable to this type. + */ +export interface InputJsonArray extends ReadonlyArray {} + +/** + * Matches any valid value that can be used as an input for operations like + * create and update as the value of a JSON field. Unlike `JsonValue`, this + * type allows read-only arrays and read-only object properties and disallows + * `null` at the top level. + * + * `null` cannot be used as the value of a JSON field because its meaning + * would be ambiguous. Use `Prisma.JsonNull` to store the JSON null value or + * `Prisma.DbNull` to clear the JSON value and set the field to the database + * NULL value instead. + * + * @see https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values + */ +export type InputJsonValue = string | number | boolean | InputJsonObject | InputJsonArray | { toJSON(): unknown } diff --git a/src/Types/index.ts b/src/Types/index.ts index 78e48e0..e6b8475 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -1,4 +1,5 @@ export * from "./CommonKeys" export * from "./Extend" export * from "./Merge" +export * as PrismaJson from "./PrismaJson" export * from "./StaticType"