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 01/13] 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" -- 2.49.1 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 02/13] 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" -- 2.49.1 From 9dbf9a0ea4213ab8d2afe8d1a9ca67f2d60b412e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 05:23:24 +0200 Subject: [PATCH 03/13] Doc fix --- src/Types/PrismaJson.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/PrismaJson.ts b/src/Types/PrismaJson.ts index 16577a9..4d15721 100644 --- a/src/Types/PrismaJson.ts +++ b/src/Types/PrismaJson.ts @@ -1,5 +1,5 @@ /** - * Prisma's Json types + * `@prisma/client`'s Json types * @module */ -- 2.49.1 From 4489b077c801a2e3742093e80dcb043a00f3183e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 05:36:44 +0200 Subject: [PATCH 04/13] Jsonifiable -> JsonValue --- src/Schema/Jsonifiable.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Jsonifiable.ts b/src/Schema/Jsonifiable.ts index 3a006cd..c55cd61 100644 --- a/src/Schema/Jsonifiable.ts +++ b/src/Schema/Jsonifiable.ts @@ -1,10 +1,10 @@ import { Schema } from "@effect/schema" -import type { Jsonifiable as TJsonifiable } from "type-fest" +import type { JsonValue } from "type-fest" export const Jsonifiable = < JsonifiableA, - JsonifiableI extends TJsonifiable, + JsonifiableI extends JsonValue, JsonifiableR, >( jsonifiable: Schema.Schema -- 2.49.1 From c5417454df734df7182e64fc708067fb47b44f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 05:47:09 +0200 Subject: [PATCH 05/13] makeJsonifiable --- src/Schema/index.ts | 1 + src/Schema/makeJsonifiable.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/Schema/makeJsonifiable.ts diff --git a/src/Schema/index.ts b/src/Schema/index.ts index 3d6c5f1..30da332 100644 --- a/src/Schema/index.ts +++ b/src/Schema/index.ts @@ -3,6 +3,7 @@ export * from "./DateTime" export * from "./encodedAsJsonValue" export * from "./Jsonifiable" export * from "./Kind" +export * from "./makeJsonifiable" export * as MobX from "./MobX" export * from "./MutableClass" export * from "./MutableTaggedClass" diff --git a/src/Schema/makeJsonifiable.ts b/src/Schema/makeJsonifiable.ts new file mode 100644 index 0000000..a436e41 --- /dev/null +++ b/src/Schema/makeJsonifiable.ts @@ -0,0 +1,13 @@ +import { Schema } from "@effect/schema" +import type { JsonValue } from "type-fest" + + +export const makeJsonifiable = < + JsonifiableA, + JsonifiableI extends JsonValue, + JsonifiableR, +>( + jsonifiable: Schema.Schema +) => + (schema: Schema.Schema) => + jsonifiable.pipe(Schema.compose(schema)) -- 2.49.1 From c7ce90c9bc928e36500caaac79e365c54f629ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 06:09:47 +0200 Subject: [PATCH 06/13] encodedAsPrismaJsonValue --- src/Schema/encodedAsJsonValue.ts | 13 ------------- src/Schema/encodedAsPrismaJsonValue.ts | 13 +++++++++++++ src/Schema/index.ts | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 src/Schema/encodedAsJsonValue.ts create mode 100644 src/Schema/encodedAsPrismaJsonValue.ts diff --git a/src/Schema/encodedAsJsonValue.ts b/src/Schema/encodedAsJsonValue.ts deleted file mode 100644 index 93bf8b1..0000000 --- a/src/Schema/encodedAsJsonValue.ts +++ /dev/null @@ -1,13 +0,0 @@ -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 a cast. - */ -export const encodedAsJsonValue = (schema: Schema.Schema) => - schema as Schema.Schema diff --git a/src/Schema/encodedAsPrismaJsonValue.ts b/src/Schema/encodedAsPrismaJsonValue.ts new file mode 100644 index 0000000..55d7cdc --- /dev/null +++ b/src/Schema/encodedAsPrismaJsonValue.ts @@ -0,0 +1,13 @@ +import type { Schema } from "@effect/schema" +import type { JsonValue } from "type-fest" +import type { PrismaJson } from "../Types" + + +/** + * Takes a schema of which the encoded value satisfies type-fest `JsonValue` 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 Prisma's `JsonValue` is poorly implemented and does not work well with some types, such as readonly arrays. + */ +export const encodedAsPrismaJsonValue = (schema: Schema.Schema) => + schema as Schema.Schema diff --git a/src/Schema/index.ts b/src/Schema/index.ts index 30da332..662dcbd 100644 --- a/src/Schema/index.ts +++ b/src/Schema/index.ts @@ -1,6 +1,6 @@ export * from "./Class" export * from "./DateTime" -export * from "./encodedAsJsonValue" +export * from "./encodedAsPrismaJsonValue" export * from "./Jsonifiable" export * from "./Kind" export * from "./makeJsonifiable" -- 2.49.1 From e8769d046d369e73cdf62d7090a730ffb43fa9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 19:54:33 +0200 Subject: [PATCH 07/13] Fix --- src/Schema/encodedAsPrismaJsonValue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/encodedAsPrismaJsonValue.ts b/src/Schema/encodedAsPrismaJsonValue.ts index 55d7cdc..9389bec 100644 --- a/src/Schema/encodedAsPrismaJsonValue.ts +++ b/src/Schema/encodedAsPrismaJsonValue.ts @@ -4,7 +4,7 @@ import type { PrismaJson } from "../Types" /** - * Takes a schema of which the encoded value satisfies type-fest `JsonValue` and returns the same schema with Prisma's `JsonValue` as its encoded type instead. + * Takes a schema of which the encoded value satisfies type-fest's `JsonValue` 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 Prisma's `JsonValue` is poorly implemented and does not work well with some types, such as readonly arrays. -- 2.49.1 From 2cd09fe2f7fff5dd22a31c475c851cc23955ed9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 20:55:14 +0200 Subject: [PATCH 08/13] Fix --- src/Schema/encodedAsPrismaJsonValue.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/encodedAsPrismaJsonValue.ts b/src/Schema/encodedAsPrismaJsonValue.ts index 9389bec..b58ac01 100644 --- a/src/Schema/encodedAsPrismaJsonValue.ts +++ b/src/Schema/encodedAsPrismaJsonValue.ts @@ -9,5 +9,5 @@ import type { PrismaJson } from "../Types" * * This is needed because Prisma's `JsonValue` is poorly implemented and does not work well with some types, such as readonly arrays. */ -export const encodedAsPrismaJsonValue = (schema: Schema.Schema) => - schema as Schema.Schema +export const encodedAsPrismaJsonValue = (schema: Schema.Schema) => + schema as unknown as Schema.Schema -- 2.49.1 From 93f9a2dee1fb0fca1b598947d486f16b5b70046f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 20:57:04 +0200 Subject: [PATCH 09/13] Revert --- src/Schema/Jsonifiable.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Jsonifiable.ts b/src/Schema/Jsonifiable.ts index c55cd61..3a006cd 100644 --- a/src/Schema/Jsonifiable.ts +++ b/src/Schema/Jsonifiable.ts @@ -1,10 +1,10 @@ import { Schema } from "@effect/schema" -import type { JsonValue } from "type-fest" +import type { Jsonifiable as TJsonifiable } from "type-fest" export const Jsonifiable = < JsonifiableA, - JsonifiableI extends JsonValue, + JsonifiableI extends TJsonifiable, JsonifiableR, >( jsonifiable: Schema.Schema -- 2.49.1 From 1f7159f4a346c539624cc884c4794d9eaae8917b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 20:58:39 +0200 Subject: [PATCH 10/13] Doc --- src/Schema/Jsonifiable.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Schema/Jsonifiable.ts b/src/Schema/Jsonifiable.ts index 3a006cd..19fffd2 100644 --- a/src/Schema/Jsonifiable.ts +++ b/src/Schema/Jsonifiable.ts @@ -2,6 +2,9 @@ import { Schema } from "@effect/schema" import type { Jsonifiable as TJsonifiable } from "type-fest" +/** + * @deprecated Use `makeJsonifiable` instead + */ export const Jsonifiable = < JsonifiableA, JsonifiableI extends TJsonifiable, -- 2.49.1 From 618028f139143583d6570988890092ad3dcb148a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 22:11:37 +0200 Subject: [PATCH 11/13] composeJsonifiable --- src/Schema/Jsonifiable.ts | 16 ---------------- src/Schema/composeJsonifiable.ts | 13 +++++++++++++ src/Schema/index.ts | 3 +-- src/Schema/makeJsonifiable.ts | 13 ------------- 4 files changed, 14 insertions(+), 31 deletions(-) delete mode 100644 src/Schema/Jsonifiable.ts create mode 100644 src/Schema/composeJsonifiable.ts delete mode 100644 src/Schema/makeJsonifiable.ts diff --git a/src/Schema/Jsonifiable.ts b/src/Schema/Jsonifiable.ts deleted file mode 100644 index 19fffd2..0000000 --- a/src/Schema/Jsonifiable.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Schema } from "@effect/schema" -import type { Jsonifiable as TJsonifiable } from "type-fest" - - -/** - * @deprecated Use `makeJsonifiable` instead - */ -export const Jsonifiable = < - JsonifiableA, - JsonifiableI extends TJsonifiable, - JsonifiableR, ->( - jsonifiable: Schema.Schema -) => - (schema: Schema.Schema) => - jsonifiable.pipe(Schema.compose(schema)) diff --git a/src/Schema/composeJsonifiable.ts b/src/Schema/composeJsonifiable.ts new file mode 100644 index 0000000..0599622 --- /dev/null +++ b/src/Schema/composeJsonifiable.ts @@ -0,0 +1,13 @@ +import { Schema } from "@effect/schema" +import type { JsonValue } from "type-fest" + + +export const composeJsonifiable = < + JsonifiableA, + JsonifiableI extends JsonValue, + JsonifiableR, +>( + jsonifiableSchema: Schema.Schema +) => + (decodedSchema: Schema.Schema) => + Schema.compose(jsonifiableSchema, decodedSchema) diff --git a/src/Schema/index.ts b/src/Schema/index.ts index 662dcbd..0342e44 100644 --- a/src/Schema/index.ts +++ b/src/Schema/index.ts @@ -1,9 +1,8 @@ export * from "./Class" +export * from "./composeJsonifiable" export * from "./DateTime" export * from "./encodedAsPrismaJsonValue" -export * from "./Jsonifiable" export * from "./Kind" -export * from "./makeJsonifiable" export * as MobX from "./MobX" export * from "./MutableClass" export * from "./MutableTaggedClass" diff --git a/src/Schema/makeJsonifiable.ts b/src/Schema/makeJsonifiable.ts deleted file mode 100644 index a436e41..0000000 --- a/src/Schema/makeJsonifiable.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Schema } from "@effect/schema" -import type { JsonValue } from "type-fest" - - -export const makeJsonifiable = < - JsonifiableA, - JsonifiableI extends JsonValue, - JsonifiableR, ->( - jsonifiable: Schema.Schema -) => - (schema: Schema.Schema) => - jsonifiable.pipe(Schema.compose(schema)) -- 2.49.1 From 7c71fdfebc539e4f2f0450dd63f42a3a6f83d237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 22:12:55 +0200 Subject: [PATCH 12/13] Version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1caed23..3a223de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thilawyn/thilalib", - "version": "0.1.10", + "version": "0.1.11", "type": "module", "files": [ "./dist" -- 2.49.1 From 3094b8bc6991aea843a37f19114eb4de1a9927d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 20 Aug 2024 22:15:29 +0200 Subject: [PATCH 13/13] Fixed tests --- src/tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests.ts b/src/tests.ts index 764e0f2..cf38378 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,7 +1,7 @@ import { Schema as S } from "@effect/schema" import { reaction, runInAction } from "mobx" import type { Simplify } from "type-fest" -import { Jsonifiable, MutableTaggedClass } from "./Schema" +import { composeJsonifiable, MutableTaggedClass } from "./Schema" import { ObservableClass } from "./Schema/MobX" import type { ExtendAll } from "./Types" @@ -26,7 +26,7 @@ class User extends MutableTaggedClass()("User", { ) {} const JsonifiableUser = User.pipe( - Jsonifiable(S.Struct({ + composeJsonifiable(S.Struct({ ...User.fields, id: S.BigInt, })) -- 2.49.1