cult-of-coders/grapher

Preventing unsetting parent.linkedField from virtual link issue

Opened this issue · 0 comments

I have two linked collections:

const parentSchema = new SimpleSchema({
    childId: {
        type: String,
        regEx: SimpleSchema.RegEx.Id,
    },
});

const Parent = new Mongo.Collection('parent');
Parent.attachSchema(parentSchema);
const Child = new Mongo.Collection('child');

Parent.addLinks({
        child: {
            type: 'one',
            collection: Child,
            field: 'childId',
        },
    });
    Child.addLinks({
        parent: {
            collection: Parent,
            inversedBy: 'child',
        },
    });

My issue here is that I don't want grapher to unset childId field in parent when removing child document because SimpleSchema errors with Child ID is required.

What I want is to provide my own handler where I have to do some checks and potentially even change childId to some other child and not remove it and definitely not nullify it.

Therefore,

Child.addLinks({
        parent: {
            collection: Parent,
            inversedBy: 'child',
            autoremove: true,
        },
    });

won't work because I'll be left without a parent.

I think there should be an option to opt out of everything related to autoremoval/unsetting of fields.

Looking into linker.js it looks like there is no opt-out from autoremove on virtual links.

First, we have _initAutoremove which respects the autoremove flag.
Second, we have _handleReferenceRemovalForVirtualLinks which is called for virtual links if autoremove is false (is this even intended behaviour?).

If _handleReferenceRemovalForVirtualLinks being called when autoremove==false is correct behaviour, then we possibly need another parameter.
If it should be only called when autoremove==true, then we have two functions for handling virtual links removal (I'm counting also the one in _initAutoremove) which seems like it is not intended.