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

Accessible entity ids in relational custom fields #2031

Open
Draykee opened this issue Feb 10, 2023 · 4 comments
Open

Accessible entity ids in relational custom fields #2031

Draykee opened this issue Feb 10, 2023 · 4 comments
Assignees
Labels
design 📐 This issue deals with high-level design of a feature type: feature ✨ @vendure/core
Milestone

Comments

@Draykee
Copy link
Contributor

Draykee commented Feb 10, 2023

Is your feature request related to a problem? Please describe.

Usecase:
We want to add a relation Product to User (called owner) to implement an ownership. For access controll we only need the user id from the product to compare it with the activeUserId of the RequestContext. Right now, the user needs to be eagerly loaded to access the id, therefore it would be awesome if we could just access the id directly from the column in the entity table

Instead of:
product.customFields.owner.id

We could directly access the id via:
product.customFields.ownerId

Describe the solution you'd like

For own entities we can achieve this in TypeORM by adding a relational definition and a defintion of the relation id to an entity:

@OneToOne(() => User, { onDelete: 'CASCADE' })
owner: User; // This will add the column ownerId to the entity

@EntityId()
ownerId: ID; // With this we can directly access the data from the column

This TypeORM definition should be created by a custom fields configuration like this:

config.customFields.Product.push({
            name: 'owner',
            nullable: true,
            type: 'relation',
            entity: User,
            public: false,
            readonly: true,
            withId: true, // a option like this could add the extra definition
            
        });

To prevent breaking changes, the option shall be false by default for the next minor versions.

@michaelbromley
Copy link
Member

Hey! It took me a while to get around to this but yes, this is a very good suggestion.

@michaelbromley michaelbromley moved this to 📅 Planned in Vendure OS Roadmap Jun 17, 2024
@dlhck dlhck removed the next minor label Sep 24, 2024
@dlhck dlhck removed the status in Vendure OS Roadmap Sep 24, 2024
@dlhck dlhck moved this to 📅 Planned in Vendure OS Roadmap Sep 24, 2024
@dlhck dlhck added this to the v3.1 milestone Sep 24, 2024
@dlhck
Copy link
Collaborator

dlhck commented Sep 24, 2024

This is a great feature request. Very interesting for multi-vendor applications or larger teams with defined ownership structure on data management.

@dlhck dlhck added the design 📐 This issue deals with high-level design of a feature label Sep 24, 2024
@halykon halykon self-assigned this Oct 22, 2024
michaelbromley added a commit that referenced this issue Nov 26, 2024
Attempting to implement #2031 but it is proving
quite difficult to make everything work.

I added a new e2e test, but the current implementation breaks
existing tests as well as not fully passing the new test.
@michaelbromley
Copy link
Member

This is proving much more difficult to implement correctly than I had imagined. I spent a couple of hours trying to debug the problems but I'm not there yet. The WIP is in the branch linked above. I need to return to this when I have more time to dedicate to debugging it.

@michaelbromley michaelbromley modified the milestones: v3.1.0, v3.2.0 Nov 29, 2024
@michaelbromley
Copy link
Member

Moved to the v3.2 milestone because more time is needed to resolve the issues in the WIP branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design 📐 This issue deals with high-level design of a feature type: feature ✨ @vendure/core
Projects
Status: 📅 Planned
Development

No branches or pull requests

4 participants