fix(json): false error when using typed json in multi-file schemas (#1836)

This commit is contained in:
Yiming 2024-11-07 10:18:22 -08:00 committed by GitHub
parent 13f95d270a
commit 1d1fec0019
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 27 additions and 22 deletions

View file

@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "2.8.0",
"version": "2.8.1",
"description": "",
"scripts": {
"build": "pnpm -r build",

View file

@ -9,7 +9,7 @@ plugins {
}
group = "dev.zenstack"
version = "2.8.0"
version = "2.8.1"
repositories {
mavenCentral()

View file

@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.8.0",
"version": "2.8.1",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",

View file

@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "2.8.0",
"version": "2.8.1",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.8.0",
"version": "2.8.1",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {

View file

@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "2.8.0",
"version": "2.8.1",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",

View file

@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "FullStack enhancement for Prisma ORM: seamless integration from database to UI",
"version": "2.8.0",
"version": "2.8.1",
"author": {
"name": "ZenStack Team"
},

View file

@ -9,7 +9,6 @@ import {
isTypeDef,
} from '@zenstackhq/language/ast';
import {
getDataSourceProvider,
getModelFieldsWithBases,
getModelIdFields,
getModelUniqueFields,
@ -108,10 +107,6 @@ export default class DataModelValidator implements AstValidator<DataModel> {
if (!hasAttribute(field, '@json')) {
accept('error', 'Custom-typed field must have @json attribute', { node: field });
}
if (getDataSourceProvider(field.$container.$container) !== 'postgresql') {
accept('error', 'Custom-typed field is only supported with "postgresql" provider', { node: field });
}
}
}

View file

@ -39,6 +39,7 @@ import {
getAttribute,
getAttributeArg,
getAttributeArgLiteral,
getDataSourceProvider,
getInheritedFromDelegate,
getLiteral,
getRelationKeyPairs,
@ -81,6 +82,7 @@ import {
const MODEL_PASSTHROUGH_ATTR = '@@prisma.passthrough';
const FIELD_PASSTHROUGH_ATTR = '@prisma.passthrough';
const PROVIDERS_SUPPORTING_NAMED_CONSTRAINTS = ['postgresql', 'mysql', 'cockroachdb'];
const PROVIDERS_SUPPORTING_TYPEDEF_FIELDS = ['postgresql'];
// Some database providers like postgres and mysql have default limit to the length of identifiers
// Here we use a conservative value that should work for most cases, and truncate names if needed
@ -794,6 +796,7 @@ export class PrismaSchemaGenerator {
} else if (field.type.reference?.ref) {
// model, enum, or type-def
if (isTypeDef(field.type.reference.ref)) {
this.ensureSupportingTypeDefFields(this.zmodel);
fieldType = 'Json';
} else {
fieldType = field.type.reference.ref.name;
@ -846,6 +849,13 @@ export class PrismaSchemaGenerator {
return result;
}
private ensureSupportingTypeDefFields(zmodel: Model) {
const dsProvider = getDataSourceProvider(zmodel);
if (dsProvider && !PROVIDERS_SUPPORTING_TYPEDEF_FIELDS.includes(dsProvider)) {
throw new PluginError(name, `Datasource provider "${dsProvider}" does not support "@json" fields`);
}
}
private setDummyDefault(result: ModelField, field: DataModelField) {
const dummyDefaultValue = match(field.type.type)
.with('String', () => new AttributeArgValue('String', ''))

View file

@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {

View file

@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "2.8.0",
"version": "2.8.1",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",

View file

@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,

View file

@ -1,9 +1,9 @@
import { loadModelWithError } from '@zenstackhq/testtools';
import { loadModelWithError, loadSchema } from '@zenstackhq/testtools';
describe('JSON field typing', () => {
it('is only supported by postgres', async () => {
await expect(
loadModelWithError(
loadSchema(
`
type Profile {
age Int @gt(0)
@ -16,7 +16,7 @@ describe('JSON field typing', () => {
}
`
)
).resolves.toContain('Custom-typed field is only supported with "postgresql" provider');
).rejects.toThrow('Datasource provider "sqlite" does not support "@json" fields');
});
it('requires field to have @json attribute', async () => {