Skip to content

Commit 6d1afc1

Browse files
authored
fix:Inherited fields from abstract model should be on the top (#487)
fix #486
1 parent c2e9795 commit 6d1afc1

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

packages/schema/src/utils/ast-utils.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,29 @@ export function mergeBaseModel(model: Model) {
5858
.forEach((decl) => {
5959
const dataModel = decl as DataModel;
6060

61-
dataModel.superTypes.forEach((superType) => {
62-
const superTypeDecl = superType.ref;
63-
if (superTypeDecl) {
64-
superTypeDecl.fields.forEach((field) => {
65-
const cloneField = Object.assign({}, field);
66-
const mutable = cloneField as Mutable<AstNode>;
67-
// update container
68-
mutable.$container = dataModel;
69-
dataModel.fields.push(mutable as DataModelField);
70-
});
71-
72-
superTypeDecl.attributes.forEach((attr) => {
73-
const cloneAttr = Object.assign({}, attr);
74-
const mutable = cloneAttr as Mutable<AstNode>;
75-
// update container
76-
mutable.$container = dataModel;
77-
dataModel.attributes.push(mutable as DataModelAttribute);
78-
});
79-
}
80-
});
61+
dataModel.fields = dataModel.superTypes
62+
.flatMap((superType) => updateContainer(superType.ref!.fields, dataModel))
63+
.concat(dataModel.fields);
64+
65+
dataModel.attributes = dataModel.superTypes
66+
.flatMap((superType) => updateContainer(superType.ref!.attributes, dataModel))
67+
.concat(dataModel.attributes);
8168
});
8269

8370
// remove abstract models
8471
model.declarations = model.declarations.filter((x) => !(x.$type == 'DataModel' && x.isAbstract));
8572
}
8673

74+
function updateContainer<T extends AstNode>(nodes: T[], container: AstNode): Mutable<T>[] {
75+
return nodes.map((node) => {
76+
const cloneField = Object.assign({}, node);
77+
const mutable = cloneField as Mutable<T>;
78+
// update container
79+
mutable.$container = container;
80+
return mutable;
81+
});
82+
}
83+
8784
function toStaticPolicy(
8885
operation: PolicyOperationKind,
8986
allows: DataModelAttribute[],

packages/schema/tests/generator/prisma-generator.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ describe('Prisma generator test', () => {
280280
updatedAt DateTime @updatedAt
281281
}
282282
283-
model Post extends Base {
283+
abstract model Content {
284284
title String
285+
}
286+
287+
model Post extends Base, Content {
285288
published Boolean @default(false)
286289
}
287290
`);
@@ -301,6 +304,9 @@ describe('Prisma generator test', () => {
301304
const post = dmmf.datamodel.models[0];
302305
expect(post.name).toBe('Post');
303306
expect(post.fields.length).toBe(5);
307+
expect(post.fields[0].name).toBe('id');
308+
expect(post.fields[3].name).toBe('title');
309+
expect(post.fields[4].name).toBe('published');
304310
});
305311

306312
it('custom aux field names', async () => {
@@ -360,7 +366,7 @@ describe('Prisma generator test', () => {
360366
const post = dmmf.datamodel.models.find((m) => m.name === 'Post');
361367

362368
expect(post?.documentation?.replace(/\s/g, '')).toBe(
363-
`@@allow('delete', ownerId == auth()) @@allow('read', owner == auth())`.replace(/\s/g, '')
369+
`@@allow('read', owner == auth()) @@allow('delete', ownerId == auth())`.replace(/\s/g, '')
364370
);
365371

366372
const todo = dmmf.datamodel.models.find((m) => m.name === 'Todo');

0 commit comments

Comments
 (0)