Skip to content

Commit

Permalink
more reliable fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Nov 22, 2024
1 parent fcc2909 commit 7d53ccb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
5 changes: 5 additions & 0 deletions packages/language/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ declare module './generated/ast' {
* Indicates whether the model is already merged with the base types
*/
$baseMerged?: boolean;

/**
* All fields including those marked with `@ignore`
*/
$allFields?: DataModelField[];
}
}

Expand Down
15 changes: 14 additions & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
import { isDataModel, isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
import { getDataModelAndTypeDefs, getLiteral, hasAttribute } from '@zenstackhq/sdk';
import colors from 'colors';
import fs from 'fs';
Expand Down Expand Up @@ -117,6 +117,9 @@ export async function loadDocument(fileName: string, validateOnly = false): Prom
// finally relink all references
const relinkedModel = await relinkAll(model, services);

// filter out data model fields marked with `@ignore`
filterIgnoredFields(relinkedModel);

return relinkedModel;
}

Expand Down Expand Up @@ -368,6 +371,16 @@ async function relinkAll(model: Model, services: ZModelServices) {
return newDoc.parseResult.value as Model;
}

function filterIgnoredFields(model: Model) {
model.declarations.forEach((decl) => {
if (!isDataModel(decl)) {
return;
}
decl.$allFields = [...decl.fields];
decl.fields = decl.fields.filter((f) => !hasAttribute(f, '@ignore'));
});
}

export async function showNotification() {
try {
const fetchResult = await fetch(CLI_CONFIG_ENDPOINT, {
Expand Down
10 changes: 4 additions & 6 deletions packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,16 @@ export const ${typeDef.name}Schema = ${refineFuncName}(${noRefineSchema});
});
this.sourceFiles.push(sf);
sf.replaceWithText((writer) => {
const allFields = model.fields.filter((f) => !hasAttribute(f, '@ignore'));

const scalarFields = allFields.filter(
const scalarFields = model.fields.filter(
(field) =>
// id fields are always included
isIdField(field) ||
// regular fields only
(!isDataModel(field.type.reference?.ref) && !isForeignKeyField(field))
);

const relations = allFields.filter((field) => isDataModel(field.type.reference?.ref));
const fkFields = allFields.filter((field) => isForeignKeyField(field));
const relations = model.fields.filter((field) => isDataModel(field.type.reference?.ref));
const fkFields = model.fields.filter((field) => isForeignKeyField(field));

this.addPreludeAndImports(model, writer, output);

Expand Down Expand Up @@ -463,7 +461,7 @@ export const ${typeDef.name}Schema = ${refineFuncName}(${noRefineSchema});
const refineFuncName = this.createRefineFunction(model, writer);

// delegate discriminator fields are to be excluded from mutation schemas
const delegateDiscriminatorFields = allFields.filter((field) => isDiscriminatorField(field));
const delegateDiscriminatorFields = model.fields.filter((field) => isDiscriminatorField(field));
const omitDiscriminators =
delegateDiscriminatorFields.length > 0
? `.omit({ ${delegateDiscriminatorFields.map((f) => `${f.name}: true`).join(', ')} })`
Expand Down
87 changes: 44 additions & 43 deletions tests/integration/tests/schema/todo.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ plugin zod {
* Model for a space in which users can collaborate on Lists and Todos
*/
model Space {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @length(4, 50)
slug String @unique @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 @unique @length(4, 16)
owner User? @relation(fields: [ownerId], references: [id])
ownerId String?
members SpaceUser[]
lists List[]

// require login
@@deny('all', auth() == null)
Expand All @@ -47,14 +47,14 @@ model Space {
* 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 String
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
role String
@@unique([userId, spaceId])

// require login
Expand All @@ -71,18 +71,19 @@ 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?
bio String @ignore
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 @@ -98,17 +99,17 @@ 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[]
revision Int @default(0)
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[]
revision Int @default(0)

// require login
@@deny('all', auth() == null)
Expand All @@ -131,14 +132,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 Down

0 comments on commit 7d53ccb

Please sign in to comment.