Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/sdk/src/ts-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,26 @@ export class TsSchemaGenerator {

private createModelsObject(model: Model, lite: boolean): ts.Expression {
return ts.factory.createObjectLiteralExpression(
model.declarations
.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'))
.map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite))),
this.getAllDataModels(model).map((dm) =>
ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite)),
),
true,
);
}

private getAllDataModels(model: Model) {
return model.declarations.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'));
}

private getAllTypeDefs(model: Model) {
return model.declarations.filter((d): d is TypeDef => isTypeDef(d) && !hasAttribute(d, '@@ignore'));
}

private createTypeDefsObject(model: Model, lite: boolean): ts.Expression {
return ts.factory.createObjectLiteralExpression(
model.declarations
.filter((d): d is TypeDef => isTypeDef(d))
.map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite))),
this.getAllTypeDefs(model).map((td) =>
ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite)),
),
true,
);
}
Expand Down Expand Up @@ -1337,7 +1345,7 @@ export class TsSchemaGenerator {
);

// generate: export type Model = $ModelResult<Schema, 'Model'>;
const dataModels = model.declarations.filter(isDataModel);
const dataModels = this.getAllDataModels(model);
for (const dm of dataModels) {
let modelType = ts.factory.createTypeAliasDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
Expand All @@ -1355,7 +1363,7 @@ export class TsSchemaGenerator {
}

// generate: export type TypeDef = $TypeDefResult<Schema, 'TypeDef'>;
const typeDefs = model.declarations.filter(isTypeDef);
const typeDefs = this.getAllTypeDefs(model);
for (const td of typeDefs) {
let typeDef = ts.factory.createTypeAliasDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
Expand Down Expand Up @@ -1492,7 +1500,7 @@ export class TsSchemaGenerator {
}

private generateInputTypes(model: Model, options: TsSchemaGeneratorOptions) {
const dataModels = model.declarations.filter(isDataModel);
const dataModels = this.getAllDataModels(model);
const statements: ts.Statement[] = [];

// generate: import { SchemaType as $Schema } from './schema';
Expand Down
30 changes: 30 additions & 0 deletions tests/e2e/orm/client-api/ignore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { DefaultModelResult } from '@zenstackhq/orm';
import { createTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';
import { schema } from '../schemas/basic';

describe('Ignored models and fields test', () => {
it('correctly ignores fields', async () => {
const db = await createTestClient(schema);
db.user.findFirst({
// @ts-expect-error
where: { password: 'abc' },
});

const user = await db.user.create({ data: { email: 'u1@test.com' } });
// @ts-expect-error
expect(user.password).toBeUndefined();

const u: DefaultModelResult<typeof schema, 'User'> = {} as any;
// @ts-expect-error
noop(u.password);
});

it('correctly ignores models', async () => {
const db = await createTestClient(schema);
// @ts-expect-error
expect(db.foo).toBeUndefined();
});
});

function noop(_value: unknown) {}
25 changes: 16 additions & 9 deletions tests/e2e/orm/schemas/basic/schema.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ type CommonFields {
}

model User with CommonFields {
email String @unique
name String?
role Role @default(USER)
posts Post[]
profile Profile?
meta Json?
email String @unique
name String?
password String @ignore
role Role @default(USER)
posts Post[]
profile Profile?
meta Json?

// Access policies
@@allow('all', auth().id == id)
Expand All @@ -46,9 +47,9 @@ model Post with CommonFields {
}

model Comment with CommonFields {
content String
post Post? @relation(fields: [postId], references: [id], onUpdate: Cascade, onDelete: Cascade)
postId String?
content String
post Post? @relation(fields: [postId], references: [id], onUpdate: Cascade, onDelete: Cascade)
postId String?
}

model Profile with CommonFields {
Expand All @@ -57,3 +58,9 @@ model Profile with CommonFields {
user User? @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade)
userId String? @unique
}

model Foo {
id String @id
@@ignore
}