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

Is it possible to require a model relationship? #83

Open
Ansis100 opened this issue Nov 9, 2023 · 3 comments
Open

Is it possible to require a model relationship? #83

Ansis100 opened this issue Nov 9, 2023 · 3 comments

Comments

@Ansis100
Copy link
Contributor

Ansis100 commented Nov 9, 2023

If I create a node using a model, the relationship does not have to be defined upon creation. Is it possible to force the relationship property to be required when creating a node using createOne?

My idea would be using beforeCreate to check if the relationship is defined and throw an error otherwise, but maybe @themetalfleece has a better option in mind? Best case I would like Typescript to handle this, not the JS runtime. Can I somehow cast the Neogma relationship types to accomplish this?

@fulcanelly
Copy link

@Ansis100 I had encountered same problem but ended up writing my own wrapper over neogma which does generate all models and types, and conducts relations validations

I tried to combine best of two worlds: ts' type safety and rails efficiency

Can you take a look ? I think it may solves your problem - neogen

@Ansis100
Copy link
Contributor Author

It looks like a great option for managing the models, thanks! However, our project is quite big and migrating to a different model scheme would take a great deal of effort. I might think about implementing it when refactoring.
Currently we're just looking to modify one of our models with this functionality.

@themetalfleece
Copy link
Owner

Hey! That's a nice idea :) It's currently not supported in neogma, however I will consider adding it.

What I'm thinking as a workaround is creating a wrapper for createOne, while changing its type.

I.e.

    type CreateParamsI = Parameters<typeof Users.createOne>[0];
    interface UsersStaticsI {
      createWithOrder: (
        userWithOrder: CreateParamsI & Required<Pick<CreateParamsI, 'Orders'>>,
      ) => Promise<UsersInstance>;
    }
   
   ...after Model definition

    Users.createWithOrder = (userWithOrder) => {
      return Users.createOne(userWithOrder);
    };
image

Let me know what do you think about this 👀

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

No branches or pull requests

3 participants