Skip to content

fix:Inherited fields from abstract model should be on the top #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2023
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
37 changes: 17 additions & 20 deletions packages/schema/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,29 @@ export function mergeBaseModel(model: Model) {
.forEach((decl) => {
const dataModel = decl as DataModel;

dataModel.superTypes.forEach((superType) => {
const superTypeDecl = superType.ref;
if (superTypeDecl) {
superTypeDecl.fields.forEach((field) => {
const cloneField = Object.assign({}, field);
const mutable = cloneField as Mutable<AstNode>;
// update container
mutable.$container = dataModel;
dataModel.fields.push(mutable as DataModelField);
});

superTypeDecl.attributes.forEach((attr) => {
const cloneAttr = Object.assign({}, attr);
const mutable = cloneAttr as Mutable<AstNode>;
// update container
mutable.$container = dataModel;
dataModel.attributes.push(mutable as DataModelAttribute);
});
}
});
dataModel.fields = dataModel.superTypes
.flatMap((superType) => updateContainer(superType.ref!.fields, dataModel))
.concat(dataModel.fields);

dataModel.attributes = dataModel.superTypes
.flatMap((superType) => updateContainer(superType.ref!.attributes, dataModel))
.concat(dataModel.attributes);
});

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

function updateContainer<T extends AstNode>(nodes: T[], container: AstNode): Mutable<T>[] {
return nodes.map((node) => {
const cloneField = Object.assign({}, node);
const mutable = cloneField as Mutable<T>;
// update container
mutable.$container = container;
return mutable;
});
}

function toStaticPolicy(
operation: PolicyOperationKind,
allows: DataModelAttribute[],
Expand Down
10 changes: 8 additions & 2 deletions packages/schema/tests/generator/prisma-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,11 @@ describe('Prisma generator test', () => {
updatedAt DateTime @updatedAt
}

model Post extends Base {
abstract model Content {
title String
}

model Post extends Base, Content {
published Boolean @default(false)
}
`);
Expand All @@ -301,6 +304,9 @@ describe('Prisma generator test', () => {
const post = dmmf.datamodel.models[0];
expect(post.name).toBe('Post');
expect(post.fields.length).toBe(5);
expect(post.fields[0].name).toBe('id');
expect(post.fields[3].name).toBe('title');
expect(post.fields[4].name).toBe('published');
});

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

expect(post?.documentation?.replace(/\s/g, '')).toBe(
`@@allow('delete', ownerId == auth()) @@allow('read', owner == auth())`.replace(/\s/g, '')
`@@allow('read', owner == auth()) @@allow('delete', ownerId == auth())`.replace(/\s/g, '')
);

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