From 20252f2b7458506e9cc4a498a99f4e7a56f6de09 Mon Sep 17 00:00:00 2001 From: jiasheng Date: Thu, 16 Jan 2025 17:15:06 +0800 Subject: [PATCH] fix: ZModel generator support JSON Type --- .../schema/tests/schema/all-features.zmodel | 97 +++++++++++-------- packages/sdk/package.json | 2 +- packages/sdk/src/zmodel-code-generator.ts | 25 ++++- 3 files changed, 78 insertions(+), 46 deletions(-) diff --git a/packages/schema/tests/schema/all-features.zmodel b/packages/schema/tests/schema/all-features.zmodel index b567093fe..8fbe6031d 100644 --- a/packages/schema/tests/schema/all-features.zmodel +++ b/packages/schema/tests/schema/all-features.zmodel @@ -36,15 +36,15 @@ abstract model Base { * Model for a space in which users can collaborate on Lists and Todos */ model Space extends Base { - id String @id @default(uuid()) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - name String @length(4, 50) - slug String @length(4, 16) - owner User? @relation(fields: [ownerId], references: [id]) - ownerId String? - members SpaceUser[] - lists List[] + id String @id @default(uuid()) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + name String @length(4, 50) + slug String @length(4, 16) + owner User? @relation(fields: [ownerId], references: [id]) + ownerId String? + members SpaceUser[] + lists List[] unsupported Unsupported('foo') // require login @@ -66,14 +66,14 @@ model Space extends Base { * Model representing membership of a user in a space */ model SpaceUser { - id String @id @default(uuid()) + id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade) - spaceId String - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - userId String - role UserRole + space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade) + spaceId String + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + userId String + role UserRole @@unique([userId, spaceId]) // require login @@ -92,18 +92,18 @@ model SpaceUser { * Model for a user */ model User { - id String @id @default(uuid()) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - email String @unique @email - password String? @password @omit + id String @id @default(uuid()) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + email String @unique @email + password String? @password @omit emailVerified DateTime? - name String? - ownedSpaces Space[] - spaces SpaceUser[] - image String? @url - lists List[] - todos Todo[] + name String? + ownedSpaces Space[] + spaces SpaceUser[] + image String? @url + lists List[] + todos Todo[] // can be created by anyone, even not logged in @@allow('create', true) @@ -119,16 +119,16 @@ model User { * Model for a Todo list */ model List { - id String @id @default(uuid()) + id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade) - spaceId String - owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) - ownerId String - title String @length(1, 100) - private Boolean @default(false) - todos Todo[] + space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade) + spaceId String + owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) + ownerId String + title String @length(1, 100) + private Boolean @default(false) + todos Todo[] // require login @@deny('all', auth() == null) @@ -151,14 +151,14 @@ model List { * Model for a single Todo */ model Todo { - id String @id @default(uuid()) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) - ownerId String - list List @relation(fields: [listId], references: [id], onDelete: Cascade) - listId String - title String @length(1, 100) + id String @id @default(uuid()) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) + ownerId String + list List @relation(fields: [listId], references: [id], onDelete: Cascade) + listId String + title String @length(1, 100) completedAt DateTime? // require login @@ -173,7 +173,18 @@ model Todo { } view SpaceWithMembers { - id String @unique + id String @unique name String slug String } + +model Image { + id Int @id @default(autoincrement()) + metadata Json +} + +type Metadata { + width Int + height Int + format String +} \ No newline at end of file diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 1e43cb73c..68c88c22f 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/sdk", - "version": "2.11.3", + "version": "2.11.4", "description": "ZenStack plugin development SDK", "main": "index.js", "scripts": { diff --git a/packages/sdk/src/zmodel-code-generator.ts b/packages/sdk/src/zmodel-code-generator.ts index 96aaa87d9..0a82906f1 100644 --- a/packages/sdk/src/zmodel-code-generator.ts +++ b/packages/sdk/src/zmodel-code-generator.ts @@ -39,6 +39,9 @@ import { StringLiteral, ThisExpr, UnaryExpr, + TypeDef, + TypeDefField, + TypeDefFieldType, } from './ast'; import { resolved } from './utils'; @@ -177,10 +180,10 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ }`; } - private fieldType(type: DataModelFieldType) { + private fieldType(type: DataModelFieldType | TypeDefFieldType) { const baseType = type.type ? type.type - : type.unsupported + : type.$type == 'DataModelFieldType' && type.unsupported ? 'Unsupported(' + this.generate(type.unsupported.value) + ')' : type.reference?.$refText; return `${baseType}${type.array ? '[]' : ''}${type.optional ? '?' : ''}`; @@ -322,6 +325,24 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ return `${ast.type ?? ast.reference?.$refText}${ast.array ? '[]' : ''}`; } + @gen(TypeDef) + private _genearteTypeDef(ast: TypeDef) { + return `type ${ast.name} { +${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${ + ast.attributes.length > 0 + ? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n') + : '' + } +}`; + } + + @gen(TypeDefField) + private _generateTypeDefField(ast: TypeDefField) { + return `${ast.name} ${this.fieldType(ast.type)}${ + ast.attributes.length > 0 ? ' ' + ast.attributes.map((x) => this.generate(x)).join(' ') : '' + }`; + } + private argument(ast: Argument) { return this.generate(ast.value); }