-
-
Notifications
You must be signed in to change notification settings - Fork 138
fix(orm): support DbNull/JsonNull/AnyNull for nullable typed JSON fields #2552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { type SchemaType as $Schema } from "./schema"; | ||
| import type { FindManyArgs as $FindManyArgs, FindUniqueArgs as $FindUniqueArgs, FindFirstArgs as $FindFirstArgs, ExistsArgs as $ExistsArgs, CreateArgs as $CreateArgs, CreateManyArgs as $CreateManyArgs, CreateManyAndReturnArgs as $CreateManyAndReturnArgs, UpdateArgs as $UpdateArgs, UpdateManyArgs as $UpdateManyArgs, UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs, UpsertArgs as $UpsertArgs, DeleteArgs as $DeleteArgs, DeleteManyArgs as $DeleteManyArgs, CountArgs as $CountArgs, AggregateArgs as $AggregateArgs, GroupByArgs as $GroupByArgs, WhereInput as $WhereInput, SelectInput as $SelectInput, IncludeInput as $IncludeInput, OmitInput as $OmitInput, QueryOptions as $QueryOptions } from "@zenstackhq/orm"; | ||
| import type { SimplifiedPlainResult as $Result, SelectIncludeOmit as $SelectIncludeOmit } from "@zenstackhq/orm"; | ||
| export type FooFindManyArgs = $FindManyArgs<$Schema, "Foo">; | ||
| export type FooFindUniqueArgs = $FindUniqueArgs<$Schema, "Foo">; | ||
| export type FooFindFirstArgs = $FindFirstArgs<$Schema, "Foo">; | ||
| export type FooExistsArgs = $ExistsArgs<$Schema, "Foo">; | ||
| export type FooCreateArgs = $CreateArgs<$Schema, "Foo">; | ||
| export type FooCreateManyArgs = $CreateManyArgs<$Schema, "Foo">; | ||
| export type FooCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Foo">; | ||
| export type FooUpdateArgs = $UpdateArgs<$Schema, "Foo">; | ||
| export type FooUpdateManyArgs = $UpdateManyArgs<$Schema, "Foo">; | ||
| export type FooUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Foo">; | ||
| export type FooUpsertArgs = $UpsertArgs<$Schema, "Foo">; | ||
| export type FooDeleteArgs = $DeleteArgs<$Schema, "Foo">; | ||
| export type FooDeleteManyArgs = $DeleteManyArgs<$Schema, "Foo">; | ||
| export type FooCountArgs = $CountArgs<$Schema, "Foo">; | ||
| export type FooAggregateArgs = $AggregateArgs<$Schema, "Foo">; | ||
| export type FooGroupByArgs = $GroupByArgs<$Schema, "Foo">; | ||
| export type FooWhereInput = $WhereInput<$Schema, "Foo">; | ||
| export type FooSelect = $SelectInput<$Schema, "Foo">; | ||
| export type FooInclude = $IncludeInput<$Schema, "Foo">; | ||
| export type FooOmit = $OmitInput<$Schema, "Foo">; | ||
| export type FooGetPayload<Args extends $SelectIncludeOmit<$Schema, "Foo", true>, Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>> = $Result<$Schema, "Foo", Args, Options>; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { type SchemaType as $Schema } from "./schema"; | ||
| import type { ModelResult as $ModelResult, TypeDefResult as $TypeDefResult } from "@zenstackhq/orm"; | ||
| export type Foo = $ModelResult<$Schema, "Foo">; | ||
| export type Metadata = $TypeDefResult<$Schema, "Metadata">; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { AnyNull, DbNull, JsonNull } from '@zenstackhq/orm'; | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { schema } from './schema'; | ||
|
|
||
| // https://github.com/zenstackhq/zenstack/issues/2411 | ||
| // TypeScript errors with nullable custom JSON types when using DbNull/JsonNull/AnyNull | ||
|
|
||
| describe('Regression for issue #2411', () => { | ||
| it('should accept DbNull/JsonNull/AnyNull for nullable typed JSON fields in create/update/find', async () => { | ||
| const db = await createTestClient(schema); | ||
| const metadata = { someInt: 1, someString: 'test' }; | ||
|
|
||
| /* --------------------------------- CREATE --------------------------------- */ | ||
|
|
||
| // metadata (non nullable) - these should cause TS errors | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.create({ data: { metadata: DbNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.create({ data: { metadata: JsonNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.create({ data: { metadata: AnyNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.create({ data: { metadata: null } })).rejects.toThrow(); | ||
|
|
||
| await db.foo.create({ data: { metadata } }); // ✅ No typescript error | ||
|
|
||
| // optionalMetadata (nullable) - DbNull/JsonNull should NOT cause TS errors | ||
| await db.foo.create({ data: { metadata, optionalMetadata: DbNull } }); | ||
| await db.foo.create({ data: { metadata, optionalMetadata: JsonNull } }); | ||
| // @ts-expect-error - AnyNull is not accepted for typed JSON fields (TS + runtime rejection) | ||
| await expect(db.foo.create({ data: { metadata, optionalMetadata: AnyNull } })).rejects.toThrow(); | ||
| await db.foo.create({ data: { metadata, optionalMetadata: null } }); // ✅ No typescript error | ||
|
|
||
| /* --------------------------------- UPDATE --------------------------------- */ | ||
|
|
||
| const firstFoo = await db.foo.findFirst(); | ||
| expect(firstFoo).not.toBeNull(); | ||
| const where = { id: firstFoo!.id }; | ||
|
|
||
| // metadata (non nullable) - these should cause TS errors | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.update({ where, data: { metadata: DbNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.update({ where, data: { metadata: JsonNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.update({ where, data: { metadata: AnyNull } })).rejects.toThrow(); | ||
| // @ts-expect-error - should not be able to set a null value to the non nullable field | ||
| await expect(db.foo.update({ where, data: { metadata: null } })).rejects.toThrow(); | ||
|
|
||
| await db.foo.update({ where, data: { metadata } }); // ✅ No typescript error | ||
|
|
||
| // optionalMetadata (nullable) - DbNull/JsonNull should NOT cause TS errors | ||
| await db.foo.update({ where, data: { metadata, optionalMetadata: DbNull } }); | ||
| await db.foo.update({ where, data: { metadata, optionalMetadata: JsonNull } }); | ||
| // @ts-expect-error - AnyNull is not accepted for typed JSON fields (TS + runtime rejection) | ||
| await expect(db.foo.update({ where, data: { metadata, optionalMetadata: AnyNull } })).rejects.toThrow(); | ||
| await db.foo.update({ where, data: { metadata, optionalMetadata: null } }); // ✅ No typescript error | ||
|
|
||
| /* ---------------------------------- FIND ---------------------------------- */ | ||
|
|
||
| // metadata (non nullable) - these should cause TS errors | ||
| // @ts-expect-error - should not be able to filter by DbNull on a non nullable field | ||
| void db.foo.findMany({ where: { metadata: DbNull } }); | ||
| // @ts-expect-error - should not be able to filter by JsonNull on a non nullable field | ||
| void db.foo.findMany({ where: { metadata: JsonNull } }); | ||
| // @ts-expect-error - should not be able to filter by AnyNull on a non nullable field | ||
| void db.foo.findMany({ where: { metadata: AnyNull } }); | ||
| // @ts-expect-error - should not be able to filter by null on a non nullable field | ||
| void db.foo.findMany({ where: { metadata: null } }); | ||
|
|
||
| // optionalMetadata (nullable) - these should NOT cause TS errors | ||
| await db.foo.findMany({ where: { optionalMetadata: DbNull } }); | ||
| await db.foo.findMany({ where: { optionalMetadata: JsonNull } }); | ||
| await db.foo.findMany({ where: { optionalMetadata: AnyNull } }); | ||
| await db.foo.findMany({ where: { optionalMetadata: null } }); // ✅ No typescript error | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { type SchemaDef, type AttributeApplication, type FieldDefault, ExpressionUtils } from "@zenstackhq/schema"; | ||
| export class SchemaType implements SchemaDef { | ||
| provider = { | ||
| type: "sqlite" | ||
| } as const; | ||
| models = { | ||
| Foo: { | ||
| name: "Foo", | ||
| fields: { | ||
| id: { | ||
| name: "id", | ||
| type: "String", | ||
| id: true, | ||
| attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }] as readonly AttributeApplication[], | ||
| default: ExpressionUtils.call("cuid") as FieldDefault | ||
| }, | ||
| createdAt: { | ||
| name: "createdAt", | ||
| type: "DateTime", | ||
| attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }] as readonly AttributeApplication[], | ||
| default: ExpressionUtils.call("now") as FieldDefault | ||
| }, | ||
| updatedAt: { | ||
| name: "updatedAt", | ||
| type: "DateTime", | ||
| updatedAt: true, | ||
| attributes: [{ name: "@updatedAt" }] as readonly AttributeApplication[] | ||
| }, | ||
| metadata: { | ||
| name: "metadata", | ||
| type: "Metadata", | ||
| attributes: [{ name: "@json" }] as readonly AttributeApplication[] | ||
| }, | ||
| optionalMetadata: { | ||
| name: "optionalMetadata", | ||
| type: "Metadata", | ||
| optional: true, | ||
| attributes: [{ name: "@json" }] as readonly AttributeApplication[] | ||
| } | ||
| }, | ||
| idFields: ["id"], | ||
| uniqueFields: { | ||
| id: { type: "String" } | ||
| } | ||
| } | ||
| } as const; | ||
| typeDefs = { | ||
| Metadata: { | ||
| name: "Metadata", | ||
| fields: { | ||
| someString: { | ||
| name: "someString", | ||
| type: "String" | ||
| }, | ||
| someInt: { | ||
| name: "someInt", | ||
| type: "Int" | ||
| } | ||
| } | ||
| } | ||
| } as const; | ||
| plugins = {}; | ||
| } | ||
| export const schema = new SchemaType(); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| datasource db { | ||
| provider = "sqlite" | ||
| url = "file:./dev.db" | ||
| } | ||
|
|
||
| type Metadata { | ||
| someString String | ||
| someInt Int | ||
| } | ||
|
|
||
| model Foo { | ||
| id String @id @default(cuid()) | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
|
|
||
| metadata Metadata @json | ||
| optionalMetadata Metadata? @json | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.