Auto-linking via a to-many relation [workaround]

Hi, folks!
Hope, this workaround would be helpful for someone. If you have any questions or suggestions - feel free, to share them here, in comments :hugs:

Problem

There are two Databases you would like to connect via [automatic linking]
(Automatically Link entities | Fibery Help Center). However, a simple mapping is not enough since one of the linking rules involves a to-many relation:

In this example we are trying to connect Conferences and Segments via their Region and Industry Cluster:

image

A Conference has many Industry Clusters and this makes things difficult.

Solution

We need to replace = “equal” in the rule with something like “is an element of”: ```
Link Conferences to Segments if

  • they have the same Regions
  • Segment’s Cluster is an element of Conference’s Clusters collection

While we don’t have available, we have the next best thing — contains . It’s not available on UI, but we can configure such a rule via the API. This is how, for example, Commits get linked to Pull Requests in our Github integration.

Step-by-step guide

The Database that has a to-one relation participating in the auto-linking rules:
( Segment)

  1. Create a Formula Field to extract the relation’s ID:
    "{" + [Industry Cluster].[Public Id] + "}"

The Database with a to-many relation
( Conference with its collection of Clusters)

  1. Since contains works with text, we need to transform the collection into text. So we add an auxiliary Formula to extract IDs: [Industry Clusters].Join("{" + [Public Id] + "}",",") .

  1. Create a relation Field to the other Database.
    (from Conference to Segment in our case)
  2. Enable auto-linking and set it up as if all relations are to-one:

  1. Add ?dev to the end of your URL
    https://YOUR_WORKSPACE.fibery.io/fibery/space/YOUR_SPACE/database/YOUR_DATABASE?dev
  2. Open the browser console.
  3. Copy the existing auto-linking rules configuration (make sure to replace Space and DB names with yours):
    dev.schema.typeObjectsByName["Market Test/Conference"].fieldObjectsByName["Market Test/Segments"].rawMeta["link-rule/link-rule"]
  4. Replace = with q/contains and execute the command from the console:
await dev.api.executeCommandsAllOrNothing({commands: [
        {
            "command": "fibery.schema/batch",
            "args": {
                "commands": [
                    {
                        "command": "schema.field/set-meta",
                        "args": {
                            "holder-type": "Market Test/Conferences",
                            "name": "Market Test/Segments",
                            "key": "link-rule/link-rule",
                            "value": {
                                "logicalOperator": "and",
                                "operands": [
                                    {
                                        "expression": [
                                            "8cc865f6-714d-4cba-b6da-f5e2c1e0f96b",
                                            "15dd709e-3733-4d8c-bbf4-9de8a0b16a43"
                                        ],
                                        "relationExpression": [
                                            "682b92f8-584f-4c5b-b5b1-45ac23b5069c",
                                            "15dd709e-3733-4d8c-bbf4-9de8a0b16a43"
                                        ],
                                        "operator": "="
                                    },
                                    {
                                        "expression": [
                                            "0812db29-d32e-4f41-9295-7b0c21ae662f"
                                        ],
                                        "relationExpression": [
                                            "98cb3527-17d9-4870-b9fd-c0e7f56544d3"
                                        ],
                                        "operator": "q/contains"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        }
    ]
})
2 Likes