tractr / directus-sync

A CLI tool for synchronizing the schema and configuration of Directus across various environments.
GNU General Public License v3.0
209 stars 8 forks source link

Sync only specific collections & no snapshot #44

Closed NilsBaumgartner1994 closed 4 months ago

NilsBaumgartner1994 commented 5 months ago

Is it possible to only sync for example the roles and not other collections ? And how to not create a snapshot, since having a large database will take long.

EdouardDem commented 5 months ago

Hi. Yes, absolutely. You can use hooks to override the dumped data. Simply return an empty array for the collections you want to exclude. This will behave as if nothing has been pulled from Directus, and thus, nothing will be pushed.

module.exports = {
  hooks: {
    flows: { onDump: () => [] },
    operations: { onDump: () => [] },
    dashboards: { onDump: () => [] },
    panels: { onDump: () => [] },
    // etc.
  },
};
EdouardDem commented 5 months ago

Unfortunately, there are no options yet to change the snapshot of the database.

However, you can edit the files stored in the directus-config/snapshot folder after the pull command. You have to delete all collections except for the directus_sync_id_map.json.

Create a shell script clean-snapshot.sh with the following content:

#!/usr/bin/env bash

# Remove all JSON files except directus_sync_id_map.json from the collections directory.
find directus-config/snapshot/collections -type f ! -name 'directus_sync_id_map.json' -exec rm {} +

# Remove sub-folders (unless directus_sync_id_map) from the fields directory.
find directus-config/snapshot/fields -mindepth 1 -type d ! -name 'directus_sync_id_map' -exec rm -r {} +

# Remove all subfolders except those related to directus_sync_id_map from the relations directory.
find directus-config/snapshot/relations -mindepth 1 -type d ! -name 'directus_sync_id_map' -exec rm -r {} +

Make it executable with chmod +x clean-snapshot.sh and run both commands together:

npx directus-sync pull && ./clean-snapshot.sh

Hope this helps!

NilsBaumgartner1994 commented 5 months ago

It’s because I would like to synchronize for my product the roles and permissions across different instances but well the data itself should be separated.

NilsBaumgartner1994 commented 5 months ago

It would be nice, to have also the option other way round: to tell which collections and fields i would like to synchronize.

I realy think that your approach using directus-sync is a huge improvement and a necessary addition.

EdouardDem commented 5 months ago

@NilsBaumgartner1994 Thanks for your words!

to tell which collections and fields i would like to synchronize.

The example above (https://github.com/tractr/directus-sync/issues/44#issuecomment-2057742460) should work for this use case. You can also exclude (or change) some fields for specific collections :

module.exports = {
  hooks: {
    flows: { onDump: () => [] },
    operations: { onDump: () => [] },
    permissions: {
      onDump: (permission) => {
        permission.validation = {};
        delete permission.presets;
        return permission;
      }
    }
    // etc.
  },
};

There are others hooks (onSave, onLoad and onQuery) that could help your case.

EdouardDem commented 4 months ago

@NilsBaumgartner1994 I've just published a new version with features you might be interested in:

You can check the release note: https://github.com/tractr/directus-sync/releases/tag/directus-sync%401.5.2