{"id":17022628,"url":"https://github.com/denperidge/directus-to-data","last_synced_at":"2026-02-27T19:06:50.029Z","repository":{"id":247794703,"uuid":"826863453","full_name":"Denperidge/directus-to-data","owner":"Denperidge","description":"A minimal utility to save a specific Collection from Directus into a local JSON file!","archived":false,"fork":false,"pushed_at":"2025-06-21T14:38:47.000Z","size":101,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-25T01:47:51.850Z","etag":null,"topics":["directus","directus-cms","npm-package"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/directus-to-data","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Denperidge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-07-10T14:27:08.000Z","updated_at":"2025-06-21T14:37:59.000Z","dependencies_parsed_at":"2024-10-14T07:10:57.051Z","dependency_job_id":"412044bb-5192-4c62-883c-f9aad745686b","html_url":"https://github.com/Denperidge/directus-to-data","commit_stats":null,"previous_names":["denperidge/directus-to-data"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/Denperidge/directus-to-data","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Denperidge%2Fdirectus-to-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Denperidge%2Fdirectus-to-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Denperidge%2Fdirectus-to-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Denperidge%2Fdirectus-to-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Denperidge","download_url":"https://codeload.github.com/Denperidge/directus-to-data/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Denperidge%2Fdirectus-to-data/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29909285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T17:28:36.873Z","status":"ssl_error","status_checked_at":"2026-02-27T17:28:20.970Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directus","directus-cms","npm-package"],"created_at":"2024-10-14T07:10:49.713Z","updated_at":"2026-02-27T19:06:49.981Z","avatar_url":"https://github.com/Denperidge.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Directus to Data\nA minimal utility to save a specific Collection from Directus into a local JSON file!\n\n## How-to\n### Install\n```bash\n# npm\nnpm install --save-dev directus-to-data\n# Yarn\nyarn add -D directus-to-data\n```\n\n### Usage\n#### Usage: CLI\n```bash\nnpx directus-to-data -u https://cms.example.com -t DAE_DOJ?1-edOJQHDS -c MyCollection  # Basic usage\nnpx directus-to-data -i ../.directus.json  # config file usage\nnpx directus-to-data --help  # View all options\n```\n\nYou can alternatively configure `directus-to-data` using environment variables\nor a config file. See [Reference - Options](#options) or [directusToData.js](directusToData.js) for further documentation.\n\n#### Usage: Code\n\u003e Note: the following example is written for use with 11ty, but it can easily be adapted for any usecase.\n\n```js\n// Import the package\nconst directusToData = require(\"directus-to-data\");\n\nmodule.exports = function (eleventyConfig) {\n    directusToData({\n        cmsUrl: \"https://cms.example.com\",\n        collectionOutput: \"src/_data/{{collectionName}}.json\",\n        collectionName: \"MyCollection\",\n        staticToken: \"DAE_DOJ?1-edOJQHDS\"\n    });\n    /**\n     * And done! As soon as this function is run,\n     * the data will be downloaded and saved in src/_data/MyCollection.json\n     */\n}\n```\n\nYou can alternatively configure `directus-to-data` using environment variables\nor a config file. See [Reference - Options](#options) or [directusToData.js](directusToData.js) for further documentation.\n\n#### Usage: Config file \n```js\n// .data.mjs\ndirectusToData({\n  configFilename: \".directus.json\",\n  collectionName: [\"FirstCollection\", \"SecondCollection\"],\n  collectionOutput: \"src/_data/{{collectionName}}.json\",\n});\n\n// .directus.json\n{\n  \"cmsUrl\": \"https://cms.example.com\",\n  \"staticToken\": \"DAE_DOJ?1-edOJQHDS\"\n}\n```\n\nYou can alternatively configure `directus-to-data` using environment variables.\nSee [Reference - Options](#options) or [directusToData.js](directusToData.js) for further documentation.\n\n#### Usage: Scripts\n```json\n# package.json\n{\n  \"scripts\": {\n    \"data\": \"directus-to-data -c CollectionOne -c CollectionTwo --collection-output src/_data/{{collectionName}}.json\",\n    \"_schema\": \"directus-to-data -c CollectionOne -c CollectionTwo --collection-output NO_WRITE --assets-output NO_WRITE\",\n    \"schema:backup\": \"yarn _schema --backup-schema .directus.schema.json\",\n    \"schema:restore\": \"yarn _schema --restore-schema .directus.schema.json\",\n    \"schema:apply\": \"yarn schema:restore --apply-schema\"\n  }\n}\n\n# .directus.json\n{\n    \"cmsUrl\": \"https://cms.example.com\",\n    \"staticToken\": \"DAE_DOJ?1-edOJQHDS\"\n}\n```\nThis should probably be used in combination with a [config file](#usage-config-file), as to not unnecessarily put access tokens in your console logs.\n\n### Specifying fields \u0026 downloading assets/files\n- To specify fields for a collection, use the `collectionName:selectorString` syntax.\n- If any objects with an `id` \u0026 `filename_download` key are found, directus-to-data will download those assets. You can modify the [assetsOutput option](#options) to specify a subdirectory \n```bash\n# This will fetch CollectionOne fully, and CollectionTwo all first-descendant values, with the image field also requesting its own id \u0026 filename_download properties, as such downloading the files\ndirectus-to-data --assets-output src/assets/{{filename}} -c CollectionOne -c CollectionTwo:*,image.id,image.filename_download --collection-output src/_data/{{collectionName}}.json \n```\n\n### Backup \u0026 restore collection schema\n\u003e Note: due to Directus limitations, this requires credentials for an admin account of your local CMS instance\n\nBy default, Directus supports [exporting \u0026 importing a schema](https://docs.directus.io/reference/system/schema). However, this by default exports \u0026 imports *all* tables. To better support shared/multi-site instances \u0026 supporting the ability to have others be able to quickly import a schema for projects, directus-to-data only backups \u0026 restores your schemas based on the *collectionNames* that have been passed (see [#options](#options)), ignoring all other collection changes.\n\n```bash\n# Backup the collections' schemas\nnpx directus-to-data --backup-schema schema.json -c \"CollectionOne\" -c \"CollectionTwo\" --cms-url https://cms.example.com --staticToken DAE_DOJ?1-edOJQHDS\n\n# View the changes that would be made from restoring the provided schema to the CMS\nnpx directus-to-data --restore-schema schema.json -c \"CollectionOne\" -c \"CollectionTwo\" --cms-url https://cms.example.com --staticToken DAE_DOJ?1-edOJQHDS\n# Note: staticToken should be configured through env variables or the config file. It is shown here for clarity\n\n# If you're sure, apply changes\nnpx directus-to-data --apply-schema --restore-schema schema.json -c \"CollectionOne\" -c \"CollectionTwo\" --cms-url https://cms.example.com --staticToken DAE_DOJ?1-edOJQHDS\n```\n\n\n### CI Integration example\nThe following file can be used in combination with Directus Flow's webhook/request url integration for an automatically updating website.\nSee [GitHub API - Create a workflow dispatch event](https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event)\n\n```yml\n# .github/workflows/directus.yml\nname: Update data from Directus webhook\non:\n  workflow_dispatch:  # Manual run\nconcurrency:  # If multiple builds are going, keep most recent\n  group: directus\n  cancel-in-progress: true\njobs:\n  update-data:\n    runs-on: ubuntu-latest\n    permissions:\n      contents: write\n    steps:\n      - name: Checkout\n        uses: actions/checkout@v4\n      - uses: actions/setup-node@v4\n      - name: Install directus-to-data\n        run: yarn add directus-to-data\n      - name: Fetch data\n        run: npx directus-to-data\n        env:\n          CMS_URL: ${{ secrets.CMS_URL }}\n          COLLECTION_NAME: MyCollection\n          STATIC_TOKEN: ${{ secrets.STATIC_TOKEN }}\n          OUTPUT_FILENAME: src/_data/{{collectionName}}.json\n      - uses: EndBug/add-and-commit@v9\n        with:\n          add: 'src/_data/*'  # git add\n          default_author: github_actions  # Commit as github-actions bot\n          message: 'CMS update (${{ github.event.repository.updated_at}})'\n\n  # Example with a local GitHub Pages deploy workflow\n  # Make sure to check the configuration below to ensure it can be called from a different workflow\n  deploy:\n    uses: ./.github/workflows/deploy.yml\n    needs: update-data\n    permissions:\n      contents: read\n      pages: write\n      id-token: write\n    secrets: inherit  # Pass secrets to called workflow\n```\nPlease see the comments marked with !IMPORTANT in the following example,\nto ensure your deploy CI has the correct settings.\n```yml\n# .github/workflows/deploy.yml\nname: Deploy\n\non:\n  push:  # Runs on pushes targeting the default branch\n    branches: [\"main\"]\n  workflow_dispatch: # Allows you to run this workflow manually from the Actions tab\n  # !IMPORTANT Allows this workflow to be called from other workflows\n  workflow_call:\n\n# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages\npermissions:\n  contents: read\n  pages: write\n  id-token: write\n\n# Allow one concurrent deployment\nconcurrency:\n  group: \"pages\"\n  cancel-in-progress: true\n\njobs:\n  deploy:\n    environment:\n      name: github-pages\n      url: ${{ steps.deployment.outputs.page_url }}\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n        with:  #!IMPORTANT Select latest ref instead of event trigger\n          ref: ${{ github.ref }}\n      - uses: actions/setup-node@v4\n          \n      - name: Setup Pages\n        uses: actions/configure-pages@v5\n        \n      - name: Install pre-requirements\n        run: yarn install\n\n      - name: Build\n        run: yarn build\n        \n      - name: Upload artifact\n        uses: actions/upload-pages-artifact@v3\n        with:\n          # Upload entire repository\n          path: 'dist/'\n\n      - name: Deploy to GitHub Pages\n        id: deployment\n        uses: actions/deploy-pages@v4\n```\n\n### Directus flow GitHub builds\n\u003e Note: this is currently a horridly manual setup, which is not what I like. But alas.\n1. Create personal access tokens for your repository. See https://github.com/settings/tokens . If the repo is part of an organisation, it may need to enable the ability to allow personal access tokens\n   - Limit it to the relevant repositories\n   - Enable `Actions \u003e Read and write`/`Read and Write access to actions`\n3. Logged in as an admin, navigate to Settings \u003e Flows (`https://cms.example.com/admin/settings/flows`) and create a new flow. The trigger can be anything, but (unless you modify the scripts), you wanna turn off `Collection Page \u003e Requires Selection`\n4. Add a `run script`. Modify the example below and add it to the sript\n    ```js\n    module.exports = async function(data) {\n    \t// Fallbacks for automatic vs manual trigger\n        const changedCollection = data[\"$trigger\"][\"collection\"] || data[\"$trigger\"][\"body\"][\"collection\"];\n        let owner;\n        let repo;\n        let auth;\n        if (changedCollection == \"CollectionOne\") {\n        \towner = \"MyAccount\";\n            repo = \"MyRepo\";\n            auth = \"YOUR_GITHUB_PERSONAL_ACCESS_TOKEN\";\n        } else if (changedCollection == \"CollectionTwo\") {\n        \towner = \"MyOrganisation\";\n            repo = \"OrgRepo\";\n            auth = \"YOUR_GITHUB_PERSONAL_ACCESS_TOKEN\";\n        } else {\n        \tthrow Error(changedCollection + \" not configured. Data: \" + JSON.stringify(data));\n        }\n    \treturn {\n            \"owner\": owner,\n            \"repo\": repo,\n            \"auth\": \"Bearer \" + auth\n        };\n    }\n    ```\n5. Add a `Webhook/Request URL` block after\n   - Method: `POST`\n   - URL: `https://api.github.com/repos/{{$last.owner}}/{{$last.repo}}/actions/workflows/directus.yml/dispatches`\n   - Headers:\n     - `Accept`: `application/vnd.github+json`\n     - `Authorization`: `{{$last.auth}}`\n     - `X-GitHub-Api-Version`: `2022-11-28`\n   - Request Body: `{\"ref\":\"main\"}` \n6. Optionally, add a Log to console with `{{$last}}`\n\n\n\n## Reference\n### Options\n| Description | Function/`.directus.json` parameter | CLI option            | env var    | default value | example value           |\n| ----------- | ----------------------------------- | --------------------- | ---------- | ------------- | ----------------------- |\n| url of your Directus instance | `cmsUrl`                            | `-u, --cms-url \u003curl\u003e` |  `CMS_URL` | Not set       | https://cms.example.com |\n| static token for user login | `staticToken`       | `-t, --static-token \u003ctoken\u003e` | `STATIC_TOKEN` | Not set | DAE_DOJ?1-edOJQHDS |\n| name(s) of the collection(s) you want to save locally. can be passed as string, array or a JSON array string | `collectionName` | `-c, --collection-name, --collection \u003cname\u003e` | `COLLECTION_NAME` | Not set | MyCollection or [\"CollectionOne\", \"CollectionTwo\"] or '[\"CollectionOne\", \"CollectionTwo\"]'\n| where to save the JSON file. you can use the `{{collectionName}}` template string value, which will be replaced with the passed collection name. Optionally, set it to `NO_WRITE` to disable writing to disk | `collectionOutput` | `-o, --collection-output, --output \u003cfilename\u003e` | `OUTPUT_FILENAME` | {{collectionName}}.json | src/_data/{{collectionName}}.json |\n| where to save the asset files. you can use the {{filename}} template string value, which will be replaced with the passed asset download filename \u0026 extension. Optionally, set it to \"NO_WRITE\" to disable writing to disk | `assetsOutput` | `-a, --assets-output, --assets \u003cfilename\u003e` | `ASSETS_OUTPUT` | {{filename}} | src/assets/{{filename}} | \n| which encoding to use when reading/writing. Passed directly to Node.js' fs functions | `encoding` | `-e, --encoding \u003cencoding\u003e` | `ENCODING` | utf-8 | ascii |\n| value to pass to JSON.stringify 'space' parameter to prettify JSON output. Disabled if set to 0 or false. Set to 4 by default if a truthy non-number value or -1 is passed. If a different number is passed, that will be used instead | `prettify` | `-p, --prettify \u003cspace\u003e` | `PRETTIFY` | 4 | 0, false, 6 |\n| path towards file you want to store the collections' schema to. This command ignores collections whose names were not passed | `backupSchema`\\*\\* | `-b, --backup-schema \u003cfilename\u003e` | `BACKUP_SCHEMA` | `null` | `schema.json` |\n| path towards schema you want to apply to the CMS. **This overrides default behaviour.** This command ignores collections whose names were not passed | `restoreSchema`\\*\\* | `-r, --restore-schema \u003cfilename\u003e` | `RESTORE_SCHEMA` | `null` | `schema.json` |\n| for use with --restore-schema/-r. Apply the schema differences to the CMS instead of only displaying the differences | `applySchema`\\*\\* | `--apply-schema` | `APPLY_SCHEMA` | `false` | `true` |\n| for use with --restore-schema/-r. Applies --force to the SchemaDiff, ignoring schema and deployment Directus version differences | `forceSchema`\\*\\* | `--force-schema` | `FORCE_SCHEMA` | `false` | `true` |\n| path towards directus-to-data's json config | `configFilename`* | `-i, --config-filename, --config \u003cfilename\u003e` | `CONFIG_FILENAME` | .directus.json | ../directus-to-data.json |\n| Optionally, pass a callback function. It will be invoked with callback(data) | `callback`* | N/A | N/A | `function(data){}` | `function(data) { console.log(data); }` |\n| Optionally, pass your own instance of @directus/sdk | `directusSdk`* | N/A | N/A | `require(\"@directus/sdk\")` | `customDirectusSdkInstance` |\n\n- \\*: Does **not** support .directus.json\n- \\*\\*: Requires an admin account\n\n## Explanation\n### Why use `-i` for config-filename\n-c was already taken and -i can stand for input. Best justification I have.\n\n### Why not set the defaults in commander?\nWith how the base function is coded, setting defaults in the regular way in commander overwrites any values set by other means, making the default values always override others.\n\n\n## License\nThis project is released into the public domain under the [Unlicense](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenperidge%2Fdirectus-to-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenperidge%2Fdirectus-to-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenperidge%2Fdirectus-to-data/lists"}