themetalfleece / neogma

Object-Graph-Mapping neo4j framework, Fully-typed with TypeScript, for easy and flexible node and relationship operations
https://themetalfleece.github.io/neogma/
MIT License
124 stars 12 forks source link

Is it possible to require a model relationship? #83

Open Ansis100 opened 1 year ago

Ansis100 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 👀