Skip to content
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

Fix crud-generator position group for ref-fields #2613

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Ben-Ho
Copy link
Contributor

@Ben-Ho Ben-Ho commented Oct 8, 2024

CRUD-Generator position-feature generates wrong code if groupByFields contains Ref field.

Ref field requires to generate baseEntity.refEntity.id (or baseEntity.refEntity?.id if optional) to call position-functions from service, but was always baseEntity.refEntity.

Type needs to be string for Ref and prop.type for Scalars and Embedded. OneToMany and ManyToMany shouldn't be possible at all.

@Ben-Ho Ben-Ho self-assigned this Oct 8, 2024
@Ben-Ho Ben-Ho force-pushed the fix-crud-generator-position branch from 1240f92 to cb9b121 Compare October 8, 2024 11:00
.map(
(prop) =>
`${prop.name}${prop.nullable ? `?` : ``}: ${
[ReferenceType.EMBEDDED, ReferenceType.SCALAR].includes(prop.reference) ? prop.type : "string"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnnyomair @nsams I think we don't want to support OneToMany and ManyToMany fields in groupByFields. Should I throw an error?

packages/api/cms-api/src/generator/generate-crud.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@johnnyomair johnnyomair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add an example for this? Maybe a unit test?

@Ben-Ho
Copy link
Contributor Author

Ben-Ho commented Oct 30, 2024

@johnnyomair I added an example in demo. I also found another bug when a dedicatedResolverArg is used for position-group (which are probably one of the most used fields)

@johnnyomair johnnyomair self-requested a review October 31, 2024 10:47
Copy link
Collaborator

@johnnyomair johnnyomair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a changeset for this fix.

@InjectRepository(ProductVariant) private readonly repository: EntityRepository<ProductVariant>,
) {}

async incrementPositions(group: { product: string }, lowestPosition: number, highestPosition?: number) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better if the product entity was passed here, and not the ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be much more complicated. in this case grouping is done by references to other entities. they can be configured differently. most of the time it will probably be something like this:

@ManyToOne(() => MyEntity, { ref: true })
myReferencedEntity?: Ref<MyEntity>;

but it could also be a eager reference. They require different code. id is always present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants