Skip to content

Commit aa7630c

Browse files
committed
fix computed fields
1 parent cb39843 commit aa7630c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

packages/runtime/src/client/executor/name-mapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class QueryNameMapper extends OperationNodeTransformer {
252252
this.requireCurrentModel(contextNode);
253253
const modelDef = requireModel(this.schema, this.currentModel!);
254254
const scalarFields = Object.entries(modelDef.fields)
255-
.filter(([, fieldDef]) => !fieldDef.relation)
255+
.filter(([, fieldDef]) => !fieldDef.relation && !fieldDef.computed)
256256
.map(([fieldName]) => fieldName);
257257
return scalarFields;
258258
}

samples/blog/zenstack/schema.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// DO NOT MODIFY THIS FILE //
33
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
44
//////////////////////////////////////////////////////////////////////////////////////////////
5-
import { type SchemaDef, type OperandExpression } from "@zenstackhq/runtime/schema";
5+
import { type SchemaDef, type OperandExpression, Expression } from "@zenstackhq/runtime/schema";
66
import path from "node:path";
77
import url from "node:url";
88
import SQLite from "better-sqlite3";
@@ -17,35 +17,40 @@ export const schema = {
1717
},
1818
models: {
1919
User: {
20-
dbTable: "User",
2120
fields: {
2221
id: {
2322
type: "String",
2423
id: true,
24+
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: Expression.call("cuid") }] }],
2525
default: { call: "cuid" }
2626
},
2727
createdAt: {
2828
type: "DateTime",
29+
attributes: [{ name: "@default", args: [{ name: "value", value: Expression.call("now") }] }],
2930
default: { call: "now" }
3031
},
3132
updatedAt: {
3233
type: "DateTime",
33-
updatedAt: true
34+
updatedAt: true,
35+
attributes: [{ name: "@updatedAt" }]
3436
},
3537
email: {
3638
type: "String",
37-
unique: true
39+
unique: true,
40+
attributes: [{ name: "@unique" }]
3841
},
3942
name: {
4043
type: "String",
4144
optional: true
4245
},
4346
postCount: {
4447
type: "Int",
48+
attributes: [{ name: "@computed" }],
4549
computed: true
4650
},
4751
role: {
4852
type: "Role",
53+
attributes: [{ name: "@default", args: [{ name: "value", value: Expression.ref("Role", "USER") }] }],
4954
default: "USER"
5055
},
5156
posts: {
@@ -71,11 +76,11 @@ export const schema = {
7176
}
7277
},
7378
Profile: {
74-
dbTable: "Profile",
7579
fields: {
7680
id: {
7781
type: "String",
7882
id: true,
83+
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: Expression.call("cuid") }] }],
7984
default: { call: "cuid" }
8085
},
8186
bio: {
@@ -89,12 +94,14 @@ export const schema = {
8994
user: {
9095
type: "User",
9196
optional: true,
97+
attributes: [{ name: "@relation", args: [{ name: "fields", value: Expression.array([Expression.ref("Profile", "userId")]) }, { name: "references", value: Expression.array([Expression.ref("User", "id")]) }] }],
9298
relation: { opposite: "profile", fields: ["userId"], references: ["id"] }
9399
},
94100
userId: {
95101
type: "String",
96102
unique: true,
97-
optional: true
103+
optional: true,
104+
attributes: [{ name: "@unique" }]
98105
}
99106
},
100107
idFields: ["id"],
@@ -104,20 +111,22 @@ export const schema = {
104111
}
105112
},
106113
Post: {
107-
dbTable: "Post",
108114
fields: {
109115
id: {
110116
type: "String",
111117
id: true,
118+
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: Expression.call("cuid") }] }],
112119
default: { call: "cuid" }
113120
},
114121
createdAt: {
115122
type: "DateTime",
123+
attributes: [{ name: "@default", args: [{ name: "value", value: Expression.call("now") }] }],
116124
default: { call: "now" }
117125
},
118126
updatedAt: {
119127
type: "DateTime",
120-
updatedAt: true
128+
updatedAt: true,
129+
attributes: [{ name: "@updatedAt" }]
121130
},
122131
title: {
123132
type: "String"
@@ -126,10 +135,13 @@ export const schema = {
126135
type: "String"
127136
},
128137
published: {
129-
type: "Boolean"
138+
type: "Boolean",
139+
attributes: [{ name: "@default", args: [{ name: "value", value: Expression.literal(false) }] }],
140+
default: false
130141
},
131142
author: {
132143
type: "User",
144+
attributes: [{ name: "@relation", args: [{ name: "fields", value: Expression.array([Expression.ref("Post", "authorId")]) }, { name: "references", value: Expression.array([Expression.ref("User", "id")]) }] }],
133145
relation: { opposite: "posts", fields: ["authorId"], references: ["id"] }
134146
},
135147
authorId: {

0 commit comments

Comments
 (0)