Making multi-instance Directus servers connect (mmultiserver)

Submitted by admin on Tue, 06/20/2023 - 09:43

Why not use autocomplete?

The one way to this is autocomplete field of Directus, but when we made selection of uuid of the entity we end up only with that uuid saved without title of the entity, so we cant understand that is it. To fix this and decouple one system from other I decided to make ref_ tables, that have entity id and title and to meka them sync by using Directus build in web-hook (source) and flows system (on destination).

Why do this?

We have 'core' entities with system settings, global company rules, products, sell places info, legal info, accounts, etc. This info need to be available all the time and edited only by owners. This Directus runs on protected server with LTS versions of soft, rare version upgrades, everyday backups, etc. Other 'work' and 'mcrsrv' instances of Directus used to work, data of that instances changed many times a day from any employee (work for core functionality and mcrsrv for side microservices DB). We can run them on simplier server with less backups, if some of that services offline for upgrade its ok, other business apps can work ok.

Target Directus config (Flows)

Create ref_ table

ref table view

To take data for ref_ entites we need to create the table with refs. I use separate folder and "ref_" prefix and color code for all references (see image). Icon name is "Conversion Path" color is "#B641BE"

 

ref product tables

Create Flow

Then we will create flow that will accept POST message from core server and create/delete/upgrade ref_ entity.

flows whole view

Condition: security check

security checkThe token is just generated random big string that is same sent in headers from 'core' server to 'work' server and checked here.  for more security we check remote host, so only requests from that IP will be allowed. uses this code:

{
    "$trigger": {
        "headers": {
            "token": {
                "_eq": "product-random-string-security-token"
            },
            "remote-host": {
                "_eq": "165.227.XXX.XXX"
            }
        },
        "body": {
            "collection": {
                "_eq": "product"
            }
        }
    }
}

If security check is wrong we just output message to console "security check failed with {{$trigger.headers}}" else we need to check type of the operation to perform.

Condition: if need to createneed create

{
    "$trigger": {
        "body": {
            "event": {
                "_eq": "items.create"
            }
        }
    }
}

Create:

If need to create we select needed action, select collection, set permissions "Full Access" and in "Payload" put the whole payload we received:

"{{$trigger.body.payload}}"

If its NOT create it can be delete or update, that is different actions in flows so need another condition.

Condition: if need deleteflow update sample

{
    "$trigger": {
        "body": {
            "event": {
                "_eq": "items.delete"
            }
        }
    }
}

And if delete we create delete action with IDs as "{{$trigger.body.keys[0]}}" (dont forget edit raw before adding this data). See image.

flow raw edit value

Copy URL of the main Trigger flow element and now go to "core" server that will send data when items created/updated/deleted in it database.

Source Directus (web hook)

This is the easy part, we just create web hook and POST all data about entities that we need to send data to target server.

web hook config