Skip to content

Commit

Permalink
fix: ZModel generator support JSON Type
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashengguo committed Jan 16, 2025
1 parent 506cf99 commit 20252f2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 46 deletions.
97 changes: 54 additions & 43 deletions packages/schema/tests/schema/all-features.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}
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.11.3",
"version": "2.11.4",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 23 additions & 2 deletions packages/sdk/src/zmodel-code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import {
StringLiteral,
ThisExpr,
UnaryExpr,
TypeDef,
TypeDefField,
TypeDefFieldType,
} from './ast';
import { resolved } from './utils';

Expand Down Expand Up @@ -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 ? '?' : ''}`;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 20252f2

Please sign in to comment.