Skip to content

Commit a9922ab

Browse files
ymc9Copilot
andauthored
fix: ts schema generation for ignored models and fields (#440)
* fix: ts schema generation for ignored models and fields * Update tests/e2e/orm/client-api/ignore.test.ts Co-authored-by: Copilot <[email protected]> * Update tests/e2e/orm/client-api/ignore.test.ts Co-authored-by: Copilot <[email protected]> * fix test --------- Co-authored-by: Copilot <[email protected]>
1 parent 023f192 commit a9922ab

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

packages/sdk/src/ts-schema-generator.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,26 @@ export class TsSchemaGenerator {
296296

297297
private createModelsObject(model: Model, lite: boolean): ts.Expression {
298298
return ts.factory.createObjectLiteralExpression(
299-
model.declarations
300-
.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'))
301-
.map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite))),
299+
this.getAllDataModels(model).map((dm) =>
300+
ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite)),
301+
),
302302
true,
303303
);
304304
}
305305

306+
private getAllDataModels(model: Model) {
307+
return model.declarations.filter((d): d is DataModel => isDataModel(d) && !hasAttribute(d, '@@ignore'));
308+
}
309+
310+
private getAllTypeDefs(model: Model) {
311+
return model.declarations.filter((d): d is TypeDef => isTypeDef(d) && !hasAttribute(d, '@@ignore'));
312+
}
313+
306314
private createTypeDefsObject(model: Model, lite: boolean): ts.Expression {
307315
return ts.factory.createObjectLiteralExpression(
308-
model.declarations
309-
.filter((d): d is TypeDef => isTypeDef(d))
310-
.map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite))),
316+
this.getAllTypeDefs(model).map((td) =>
317+
ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite)),
318+
),
311319
true,
312320
);
313321
}
@@ -1337,7 +1345,7 @@ export class TsSchemaGenerator {
13371345
);
13381346

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

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

14941502
private generateInputTypes(model: Model, options: TsSchemaGeneratorOptions) {
1495-
const dataModels = model.declarations.filter(isDataModel);
1503+
const dataModels = this.getAllDataModels(model);
14961504
const statements: ts.Statement[] = [];
14971505

14981506
// generate: import { SchemaType as $Schema } from './schema';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { DefaultModelResult } from '@zenstackhq/orm';
2+
import { createTestClient } from '@zenstackhq/testtools';
3+
import { describe, expect, it } from 'vitest';
4+
import { schema } from '../schemas/basic';
5+
6+
describe('Ignored models and fields test', () => {
7+
it('correctly ignores fields', async () => {
8+
const db = await createTestClient(schema);
9+
db.user.findFirst({
10+
// @ts-expect-error
11+
where: { password: 'abc' },
12+
});
13+
14+
const user = await db.user.create({ data: { email: '[email protected]' } });
15+
// @ts-expect-error
16+
expect(user.password).toBeUndefined();
17+
18+
const u: DefaultModelResult<typeof schema, 'User'> = {} as any;
19+
// @ts-expect-error
20+
noop(u.password);
21+
});
22+
23+
it('correctly ignores models', async () => {
24+
const db = await createTestClient(schema);
25+
// @ts-expect-error
26+
expect(db.foo).toBeUndefined();
27+
});
28+
});
29+
30+
function noop(_value: unknown) {}

tests/e2e/orm/schemas/basic/schema.zmodel

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ type CommonFields {
1919
}
2020

2121
model User with CommonFields {
22-
email String @unique
23-
name String?
24-
role Role @default(USER)
25-
posts Post[]
26-
profile Profile?
27-
meta Json?
22+
email String @unique
23+
name String?
24+
password String @ignore
25+
role Role @default(USER)
26+
posts Post[]
27+
profile Profile?
28+
meta Json?
2829

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

4849
model Comment with CommonFields {
49-
content String
50-
post Post? @relation(fields: [postId], references: [id], onUpdate: Cascade, onDelete: Cascade)
51-
postId String?
50+
content String
51+
post Post? @relation(fields: [postId], references: [id], onUpdate: Cascade, onDelete: Cascade)
52+
postId String?
5253
}
5354

5455
model Profile with CommonFields {
@@ -57,3 +58,9 @@ model Profile with CommonFields {
5758
user User? @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade)
5859
userId String? @unique
5960
}
61+
62+
model Foo {
63+
id String @id
64+
@@ignore
65+
}
66+

0 commit comments

Comments
 (0)