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

Default values or hooks for relationship properties? #86

Open exoup opened 11 months ago

exoup commented 11 months ago

I have a User model with a relationships property that looks like this:

Surveys: {
            model: Survey,
            direction: 'out',
            name: 'IS_PARTICIPANT',
            properties: {
                Feedback: {
                    property: 'feedback',
                    schema: {
                        type: 'string'
                    }
                },
                Complete: {
                    property: 'complete',
                    schema: {
                        type: 'boolean'
                    }
                }
            }
        }

Is there a way to give these a default value or a beforeCreate hook? When I go to create a user node and associate it with a Survey node, I get a Neo4j error about missing feedback and complete parameters.

I've tried adding a default value but it does not work.

I was creating my user node like this:

    const user = User.createOne({
        name: 'John Doe',
        email: 'john.doe@acme.co',
        department: 'Engineering',
        title: 'Software Engineer',
    });
    await user.relateTo({
        alias: 'Surveys',
        where: {
            target: {
                code: "ABC123"
            }
        }
    })
themetalfleece commented 10 months ago

Hey there,

I'm afraid you can't provide default values for relationships, only for the node itself. I'm noting this, it's a useful feature 👍

marisb11 commented 5 months ago

Hi, sorry, maybe nothing changed :D about default attribute of relationship properties schema.

So, if relationship props are defined by the same Ischema as model props, it just doesn't mean, that { /*Default value / default?: any;} is implemented for relationships and i cant just use :

properties: { someProp: { property: 'someProp', schema: { type: 'string' default: 'me' // by interface ISchema {....default:any;} } }, .

it just doesn't work out.

Thanks,

themetalfleece commented 5 months ago

@marisb11 Hey there, I'm afraid that nothing has changed 😓 The default property comes from the validation package (called revalidator), which is a bit outdated and not very easy to adjust. So basically, that property is pretty much useless and should be ignored (both for the model and relationships).