Skip to main content

How to handle missing References

Introduction​

In dcupl, managing references between models can be challenging, especially if data quality varies. There are several strategies to handle missing references, tailored to specific use cases or requirements.

Validity​

When dealing with references, you have the option to set their validity as either strict or loose. With a loose reference, the value is retained even if the target is not found. On the other hand, a strict reference turns to undefined when its target is missing. The choice of validity should align with the needs of your application.

{
"key": "modelA",
"references": [
{
"key": "b_loose",
"model": "modelB",
"type": "singleValued",
"validity": "loose"
},
{
"key": "b_strict",
"model": "modelB",
"type": "singleValued",
"validity": "strict"
}
]
}

// if a.b_loose points to a missing reference the value of b_loose remains an object
{ "key": "a1", "b_loose": { "key": "some_missing_key" } }

// if a.b_strict points to a missing reference it's value is undefined
{ "key": "a1" } // b_strict is not present since it's undefined

Reference Metadata​

When you activate reference metadata in your project, dcupl enhances your references with an additional parameter. This parameter indicates the status of the reference as either a hit, miss, or auto_hit (the latter denotes a successful reference generated from a "supportsAutogeneration" action). Utilizing reference metadata is particularly beneficial when working with loose references, as it provides clearer insights into the reference's resolution status.

const dcupl = new Dcupl({
referenceMetadata: {
enabled: true,
},
});

Using the same model as above the responses looks like this:

// if a.b_loose points to a missing reference the value of b_loose remains an object
[
{
"key": "a1",
"b_loose": { "key": "some_missing_key", "_dcupl_ref_": "miss" }
},
{
"key": "a2",
"b_loose": { "key": "valid_key", "_dcupl_ref_": "hit" }
},
]

An Example query for "missed References" might looks like this:

const q = dcupl.query.generate('modelA', {
attribute: 'b_loose',
operator: 'find',
value: { _dcupl_ref_: 'miss' },
});
const missing_b_Loose = dcupl.query.execute(q);