Skip to content

Commit 20252f2

Browse files
committed
fix: ZModel generator support JSON Type
1 parent 506cf99 commit 20252f2

File tree

3 files changed

+78
-46
lines changed

3 files changed

+78
-46
lines changed

packages/schema/tests/schema/all-features.zmodel

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ abstract model Base {
3636
* Model for a space in which users can collaborate on Lists and Todos
3737
*/
3838
model Space extends Base {
39-
id String @id @default(uuid())
40-
createdAt DateTime @default(now())
41-
updatedAt DateTime @updatedAt
42-
name String @length(4, 50)
43-
slug String @length(4, 16)
44-
owner User? @relation(fields: [ownerId], references: [id])
45-
ownerId String?
46-
members SpaceUser[]
47-
lists List[]
39+
id String @id @default(uuid())
40+
createdAt DateTime @default(now())
41+
updatedAt DateTime @updatedAt
42+
name String @length(4, 50)
43+
slug String @length(4, 16)
44+
owner User? @relation(fields: [ownerId], references: [id])
45+
ownerId String?
46+
members SpaceUser[]
47+
lists List[]
4848
unsupported Unsupported('foo')
4949

5050
// require login
@@ -66,14 +66,14 @@ model Space extends Base {
6666
* Model representing membership of a user in a space
6767
*/
6868
model SpaceUser {
69-
id String @id @default(uuid())
69+
id String @id @default(uuid())
7070
createdAt DateTime @default(now())
7171
updatedAt DateTime @updatedAt
72-
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
73-
spaceId String
74-
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
75-
userId String
76-
role UserRole
72+
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
73+
spaceId String
74+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
75+
userId String
76+
role UserRole
7777
@@unique([userId, spaceId])
7878

7979
// require login
@@ -92,18 +92,18 @@ model SpaceUser {
9292
* Model for a user
9393
*/
9494
model User {
95-
id String @id @default(uuid())
96-
createdAt DateTime @default(now())
97-
updatedAt DateTime @updatedAt
98-
email String @unique @email
99-
password String? @password @omit
95+
id String @id @default(uuid())
96+
createdAt DateTime @default(now())
97+
updatedAt DateTime @updatedAt
98+
email String @unique @email
99+
password String? @password @omit
100100
emailVerified DateTime?
101-
name String?
102-
ownedSpaces Space[]
103-
spaces SpaceUser[]
104-
image String? @url
105-
lists List[]
106-
todos Todo[]
101+
name String?
102+
ownedSpaces Space[]
103+
spaces SpaceUser[]
104+
image String? @url
105+
lists List[]
106+
todos Todo[]
107107

108108
// can be created by anyone, even not logged in
109109
@@allow('create', true)
@@ -119,16 +119,16 @@ model User {
119119
* Model for a Todo list
120120
*/
121121
model List {
122-
id String @id @default(uuid())
122+
id String @id @default(uuid())
123123
createdAt DateTime @default(now())
124124
updatedAt DateTime @updatedAt
125-
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
126-
spaceId String
127-
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
128-
ownerId String
129-
title String @length(1, 100)
130-
private Boolean @default(false)
131-
todos Todo[]
125+
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
126+
spaceId String
127+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
128+
ownerId String
129+
title String @length(1, 100)
130+
private Boolean @default(false)
131+
todos Todo[]
132132

133133
// require login
134134
@@deny('all', auth() == null)
@@ -151,14 +151,14 @@ model List {
151151
* Model for a single Todo
152152
*/
153153
model Todo {
154-
id String @id @default(uuid())
155-
createdAt DateTime @default(now())
156-
updatedAt DateTime @updatedAt
157-
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
158-
ownerId String
159-
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
160-
listId String
161-
title String @length(1, 100)
154+
id String @id @default(uuid())
155+
createdAt DateTime @default(now())
156+
updatedAt DateTime @updatedAt
157+
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
158+
ownerId String
159+
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
160+
listId String
161+
title String @length(1, 100)
162162
completedAt DateTime?
163163

164164
// require login
@@ -173,7 +173,18 @@ model Todo {
173173
}
174174

175175
view SpaceWithMembers {
176-
id String @unique
176+
id String @unique
177177
name String
178178
slug String
179179
}
180+
181+
model Image {
182+
id Int @id @default(autoincrement())
183+
metadata Json
184+
}
185+
186+
type Metadata {
187+
width Int
188+
height Int
189+
format String
190+
}

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/sdk",
3-
"version": "2.11.3",
3+
"version": "2.11.4",
44
"description": "ZenStack plugin development SDK",
55
"main": "index.js",
66
"scripts": {

packages/sdk/src/zmodel-code-generator.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import {
3939
StringLiteral,
4040
ThisExpr,
4141
UnaryExpr,
42+
TypeDef,
43+
TypeDefField,
44+
TypeDefFieldType,
4245
} from './ast';
4346
import { resolved } from './utils';
4447

@@ -177,10 +180,10 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
177180
}`;
178181
}
179182

180-
private fieldType(type: DataModelFieldType) {
183+
private fieldType(type: DataModelFieldType | TypeDefFieldType) {
181184
const baseType = type.type
182185
? type.type
183-
: type.unsupported
186+
: type.$type == 'DataModelFieldType' && type.unsupported
184187
? 'Unsupported(' + this.generate(type.unsupported.value) + ')'
185188
: type.reference?.$refText;
186189
return `${baseType}${type.array ? '[]' : ''}${type.optional ? '?' : ''}`;
@@ -322,6 +325,24 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
322325
return `${ast.type ?? ast.reference?.$refText}${ast.array ? '[]' : ''}`;
323326
}
324327

328+
@gen(TypeDef)
329+
private _genearteTypeDef(ast: TypeDef) {
330+
return `type ${ast.name} {
331+
${ast.fields.map((x) => this.indent + this.generate(x)).join('\n')}${
332+
ast.attributes.length > 0
333+
? '\n\n' + ast.attributes.map((x) => this.indent + this.generate(x)).join('\n')
334+
: ''
335+
}
336+
}`;
337+
}
338+
339+
@gen(TypeDefField)
340+
private _generateTypeDefField(ast: TypeDefField) {
341+
return `${ast.name} ${this.fieldType(ast.type)}${
342+
ast.attributes.length > 0 ? ' ' + ast.attributes.map((x) => this.generate(x)).join(' ') : ''
343+
}`;
344+
}
345+
325346
private argument(ast: Argument) {
326347
return this.generate(ast.value);
327348
}

0 commit comments

Comments
 (0)