|
| 1 | +# Directus Sync |
| 2 | + |
| 3 | +The `directus-sync` command-line interface (CLI) provides a set of tools for managing and synchronizing the schema and |
| 4 | +collections within Directus across different environments. |
| 5 | + |
| 6 | +# Usage |
| 7 | + |
| 8 | +The CLI is available using the `npx` command. |
| 9 | + |
| 10 | +```shell |
| 11 | +npx directus-sync <command> [options] |
| 12 | +``` |
| 13 | + |
| 14 | +Here's how to use each command in the CLI: |
| 15 | + |
| 16 | +## Global Options |
| 17 | + |
| 18 | +These options can be used with any command to configure the operation of `directus-sync`: |
| 19 | + |
| 20 | +- `-d, --debug` |
| 21 | + Display additional logging information. Useful for debugging or verifying what `directus-sync` is doing under the |
| 22 | + hood. |
| 23 | + |
| 24 | +- `-u, --directus-url <directusUrl>` |
| 25 | + Specify the Directus instance URL. Alternatively, set the `DIRECTUS_URL` environment variable. |
| 26 | + |
| 27 | +- `-t, --directus-token <directusToken>` |
| 28 | + Provide the Directus access token. Alternatively, set the `DIRECTUS_TOKEN` environment variable. |
| 29 | + |
| 30 | +- `--no-split` |
| 31 | + Indicates whether the schema snapshot should be split into multiple files. By default, snapshots are split. |
| 32 | + |
| 33 | +- `--dump-path <dumpPath>` |
| 34 | + Set the base path for the dump. This must be an absolute path. The default |
| 35 | + is `"./directus-config"`. |
| 36 | + |
| 37 | +- `--collections-path <collectionPath>` |
| 38 | + Specify the path for the collections dump, relative to the dump path. The default is `"collections"`. |
| 39 | + |
| 40 | +- `--snapshot-path <snapshotPath>` |
| 41 | + Specify the path for the schema snapshot dump, relative to the dump path. The default is `"snapshot"`. |
| 42 | + |
| 43 | +- `-h, --help` |
| 44 | + Display help information for the `directus-sync` commands. |
| 45 | + |
| 46 | +## Commands |
| 47 | + |
| 48 | +### Pull |
| 49 | + |
| 50 | +```shell |
| 51 | +npx directus-sync pull |
| 52 | +``` |
| 53 | + |
| 54 | +Retrieves the current schema and collections from Directus and stores them locally. This command does not modify the |
| 55 | +database. |
| 56 | + |
| 57 | +### Diff |
| 58 | + |
| 59 | +```shell |
| 60 | +npx directus-sync diff |
| 61 | +``` |
| 62 | + |
| 63 | +Analyzes and describes the difference (diff) between your local schema and collections and the state of the Directus |
| 64 | +instance. This command is non-destructive and does not apply any changes to the database. |
| 65 | + |
| 66 | +### Push |
| 67 | + |
| 68 | +```shell |
| 69 | +npx directus-sync push |
| 70 | +``` |
| 71 | + |
| 72 | +Applies the changes from your local environment to the Directus instance. This command pushes your local schema and |
| 73 | +collection configurations to Directus, updating the instance to reflect your local state. |
| 74 | + |
| 75 | +### Untrack |
| 76 | + |
| 77 | +```shell |
| 78 | +npx directus-sync untrack --collection <collection> --id <id> |
| 79 | +``` |
| 80 | + |
| 81 | +Removes tracking from an element within Directus. You must specify the collection and the ID of the element you wish to |
| 82 | +stop tracking. |
| 83 | + |
| 84 | +### Tracked Elements |
| 85 | + |
| 86 | +`directus-sync` tracks the following Directus collections: |
| 87 | + |
| 88 | +- dashboards |
| 89 | +- flows |
| 90 | +- operations |
| 91 | +- panels |
| 92 | +- permissions |
| 93 | +- roles |
| 94 | +- settings |
| 95 | +- webhooks |
| 96 | + |
| 97 | +For these collections, data changes are committed to the code, allowing for replication on other Directus instances. A |
| 98 | +mapping table links Directus instance IDs with SyncIDs, managed by the `directus-extension-sync`. |
| 99 | + |
| 100 | +## Dependency: `directus-extension-sync` |
| 101 | + |
| 102 | +To utilize the `directus-sync` tool, it is imperative to install the `directus-extension-sync` extension on your |
| 103 | +Directus instance. This extension acts as a bridge between `directus-sync` and Directus, managing the crucial mapping |
| 104 | +table that correlates the SyncIDs with Directus's internal IDs. |
| 105 | + |
| 106 | +### Installation |
| 107 | + |
| 108 | +The `directus-extension-sync` must be added to each Directus instance involved in the synchronization process, whether |
| 109 | +as a source or a destination. Follow the installation instructions provided in |
| 110 | +the `directus-extension-sync` [repository](https://www.npmjs.com/package/directus-extension-sync) |
| 111 | +to add this extension to your Directus setup. |
| 112 | + |
| 113 | +## How It Works |
| 114 | + |
| 115 | +`directus-sync` operates on a tagging system similar to Terraform, where each trackable element within Directus is |
| 116 | +assigned a unique synchronization identifier (SyncID). This system is key to enabling version control for the |
| 117 | +configurations and schema within Directus. Here is a step-by-step explanation of how `directus-sync` functions: |
| 118 | + |
| 119 | +### Tagging and Tracking |
| 120 | + |
| 121 | +Upon execution of the `pull` command, `directus-sync` will: |
| 122 | + |
| 123 | +1. Scan the specified Directus collections, which include dashboards, flows, operations, panels, permissions, roles, |
| 124 | + settings, and webhooks. |
| 125 | +2. Assign a SyncID to each element within these collections if it doesn't already have one. |
| 126 | +3. Commit the data of these collections into code, allowing for versioning and tracking of configuration changes. |
| 127 | + |
| 128 | +This SyncID tagging facilitates the replication of configurations across different instances of Directus while |
| 129 | +maintaining the integrity and links between different entities. |
| 130 | + |
| 131 | +### Mapping Table |
| 132 | + |
| 133 | +Since it's not possible to add tags directly to entities within Directus, `directus-sync` uses a mapping table that |
| 134 | +correlates the SyncIDs with the internal IDs used by Directus. This mapping is essential for the synchronization |
| 135 | +process, as it ensures that each element can be accurately identified and updated across different environments. |
| 136 | + |
| 137 | +### Synchronization Process |
| 138 | + |
| 139 | +The synchronization process is split into two main commands: |
| 140 | + |
| 141 | +- `diff`: This command performs a comparison between the local JSON files generated by `pull` and the current state of a |
| 142 | + Directus instance. It outlines the elements that need to be created, updated, or deleted to achieve synchronization. |
| 143 | + |
| 144 | +- `push`: This command executes the actual synchronization plan, applying the necessary changes to the Directus |
| 145 | + instance. It handles dependencies and circular dependencies carefully by potentially running the synchronization |
| 146 | + process multiple times until the Directus instance is fully in sync with the JSON definitions. |
| 147 | + |
| 148 | +### Schema Management |
| 149 | + |
| 150 | +The Directus schema, which defines the data modeling and user interface, is managed by the Directus API. However, for |
| 151 | +better code repository management, `directus-sync` stores the schema elements in separate files organized within a clear |
| 152 | +directory structure. This separation allows developers to easily track changes to the schema and apply version control |
| 153 | +principles to the database structure. |
| 154 | + |
| 155 | +### Non-Tracked Elements and Ignored Fields |
| 156 | + |
| 157 | +Elements that are not meant to be tracked, such as user activities and logs, are not affected by the synchronization |
| 158 | +process. Certain fields are specifically ignored during synchronization because they are not relevant for version |
| 159 | +control purposes, such as creation dates and the identity of the user who created an entity. |
| 160 | + |
| 161 | +### Strengths of `directus-sync` |
| 162 | + |
| 163 | +The strength of `directus-sync` lies in its ability to maintain consistent and reproducible configurations across |
| 164 | +multiple environments. It ensures that only the necessary changes are made, avoiding unnecessary recreation of |
| 165 | +configurations and maintaining the relationships between tracked and non-tracked entities. This selective updating is |
| 166 | +what makes `directus-sync` a robust tool for managing Directus instances in a team or multi-environment setup. |
| 167 | + |
| 168 | +By following these mechanisms, `directus-sync` streamlines the development workflow, allowing for local changes to be |
| 169 | +efficiently deployed to various environments, all while keeping the Directus instances synchronized and |
| 170 | +version-controlled. |
0 commit comments