Gathering detailed insights and metrics for directus-extension-schema-sync
Gathering detailed insights and metrics for directus-extension-schema-sync
Gathering detailed insights and metrics for directus-extension-schema-sync
Gathering detailed insights and metrics for directus-extension-schema-sync
The better way to sync your Directus schema and data between environments
npm install directus-extension-schema-sync
64.2
Supply Chain
97.9
Quality
87.8
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
124 Stars
107 Commits
5 Forks
1 Watching
11 Branches
17 Contributors
Updated on 25 Nov 2024
TypeScript (87.9%)
JavaScript (12.1%)
Cumulative downloads
Total Downloads
Last day
-56.3%
55
Compared to previous day
Last week
-25.1%
451
Compared to previous week
Last month
25.2%
2,115
Compared to previous month
Last year
886%
18,714
Compared to previous year
1
The better way to sync your Directus schema, configuration and selected data between environments.
Splits the schema file into multiple files once per collection, for easier management in git and working with the schema files in general.
Sync chosen data such as
Automatically export and import both the schema and data when you make changes via Directus or in the json data files
Upon installing configure what data you wish to export from the DB and be able to commit to your repository. Then on the other environments you can import the schema and data automatically.
Auto Import the schema and data when Directus starts up. This is useful for production environments. Using a locking mechanism, only one instance of Directus will import the schema and data.
IMPORTANT Always be careful with what you export and commit to your repository.
Depending on how you already are using Directus you can either install this plugin using a custom Docker image, or npm.
NOTE: Installing via marketplace is not recommended as you would still need to execute the install command as well as configure the config files.
Follow the instructions below to install the extension, ensure to first install the extension on your local environment and then export the schema and data. This will create the necessary files for the extension to work. Only once you have the files can you update your .env file with the SCHEMA_SYNC
variable.
If you don't already have a Dockerfile, you can use the following instructions to get started.
Create a folder called schema-sync
on root. This will soon contain the config and data files for what you want to import and export.
Update your Dockerfile to include the following:
1RUN pnpm install directus-extension-schema-sync 2# This is for when building for production 3COPY ./schema-sync ./schema-sync
In your docker-compose
file we need to add the schema-sync
so that we can commit it to git, and so you can edit/change the config files and have it in sync with the docker container
1// docker-compose.yaml 2volumes: 3 - ./schema-sync:/directus/schema-sync
(re)Build and run your container.
Once it is running, run the following command (from host) to install the extension's columns in the database and add the config folder.
Replace the `my-directus` with the name of your service running directus if it is different
1// docker exec -it <container> <command> 2docker compose exec -it my-directus npx directus schema-sync install --force
We are using force since we do want to replace the `schema-sync` folder already added as a volumn
npm install directus-extension-schema-sync
directus schema-sync install
to install the extension's columns in the database and add the config folderconfig.js
in the schema directory and add your collections you want to syncdirectus schema-sync export
to export the schema and data from the Directus APIView and edit the schema-sync/*config.js file to include the collections you want to sync.
To automatically import and export the schema and data, set the SCHEMA_SYNC
environment variable to IMPORT
, EXPORT
or BOTH
.
In production it is advised to set SCHEMA_SYNC
to IMPORT
and in local development to BOTH
.
Order matters when importing and exporting. For example if you have a collection (A) with a relation to another collection (B), then ensure in the config that collection (B) comes first. This is so when we import, we first import B, then A. Deletions happen afterward in the reverse order.
You can create additional config files with the other config files, and set the name on the SCHEMA_SYNC_CONFIG
env variable. For example to include a test data set used during testing in CI/CD. Additional config files need to export syncCustomCollections
object like in the config.js
file.
View the comments in the config.js
file for more information.
This does not work out of the box due to Directus masking the exported password. In order to export the hashed value you can add the following to the schema-sync/directus_config.js
file within the directus_users
object.
1onExport: async (item, itemsSrv) => { 2 if (item.password && item.password === '**********') { 3 const user = await itemsSrv.knex.select('password').from('directus_users').where('id', item.id).first(); 4 if (user) { 5 item.password = user.password; 6 } 7 } 8 9 return item; 10}, 11// And then to import the password 12onImport: async (item, itemsSrv) => { 13 if (item.password && item.password.startsWith('$argon')) { 14 await itemsSrv.knex('directus_users').update('password', item.password).where('id', item.id); 15 item.password = '**********'; 16 } 17 18 return item; 19},
Variable | Description | Default |
---|---|---|
SCHEMA_SYNC | Set to automatically do IMPORT, EXPORT or BOTH | null |
SCHEMA_SYNC_CONFIG | (optional) An additional config file to use in addition, eg. test_config.js | null |
SCHEMA_SYNC_SPLIT | (optional) Splits the schema file into multiple files once per collection | true |
Besides auto importing and exporting, you can also run the commands manually.
npx directus schema-sync [command]
Command | Description |
---|---|
export | Export the schema and data from the Directus API |
import | Import the schema and data to the Directus API (options: merge ) |
hash | Recalculate the hash for all the data files (already happens after export) |
install | Install the extension's columns in the database and add the config folder (options: force ) |
export-schema | Export only the schema (options: --split |
import-schema | Import only the schema |
Update the schema-sync/directus_config.js
file with the following:
Replace directus_roles
Add directus_policies
Replace directus_permissions
Add directus_access
…
directus_roles: {
watch: ['roles'],
linkedFields: ['parent'],
query: {
sort: ['name'],
},
},
directus_policies: {
watch: ['policies'],
query: {
sort: ['name'],
},
},
directus_permissions: {
watch: ['permissions', 'collections', 'fields'],
excludeFields: ['id'],
getKey: o => `${o.policy}-${o.collection}-${o.action}`,
query: {
sort: ['policy', 'collection', 'action'],
},
},
directus_access: {
watch: ['access'],
excludeFields: ['id'],
getKey: o => `${o.role ?? o.user ?? 'public'}-${o.policy}`,
query: {
sort: ['role', 'policy'],
filter: {
user: { _null: true },
},
},
},
…
Contributions are welcome. Please open an issue or pull request.
View changelog for more information. CHANGELOG.md
No vulnerabilities found.
No security vulnerabilities found.