Skip to content

Commit

Permalink
fix(json): false error when using typed json in multi-file schemas (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 7, 2024
1 parent 13f95d2 commit 1d1fec0
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "2.8.0",
"version": "2.8.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.8.0"
version = "2.8.1"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
isTypeDef,
} from '@zenstackhq/language/ast';
import {
getDataSourceProvider,
getModelFieldsWithBases,
getModelIdFields,
getModelUniqueFields,
Expand Down Expand Up @@ -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 });
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
getAttribute,
getAttributeArg,
getAttributeArgLiteral,
getDataSourceProvider,
getInheritedFromDelegate,
getLiteral,
getRelationKeyPairs,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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', ''))
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "2.8.0",
"version": "2.8.1",
"description": "ZenStack Test Tools",
"main": "index.js",
"private": true,
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/tests/enhancements/json/validation.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 () => {
Expand Down

0 comments on commit 1d1fec0

Please sign in to comment.