directus-extension-ensure-folders
v1.0.4
Published
Create folders for the directus files system in hirarchy. Use this directus operation extension in a directus flow to ensure that the folder hirarchy exist.
Maintainers
Readme
Ensure Folders
Directus Flow Operation: Ensure Folders
A small Directus operation extension that idempotently ensures a hierarchy of up to 5 nested folders (folder1..folder5) in the Directus folders collection. It will reuse existing folders found by name (scoped by parent) or create them if missing, threading the parent folder id through each level.

Behavior / contract
- Inputs (options):
- folder1 (string, required) — top-level folder name
- folder2 (string, optional)
- folder3 (string, optional)
- folder4 (string, optional)
- folder5 (string, optional)
- Outputs: a flat object with keys
folder1,folder2,folder3,folder4,folder5, andlastFolder. Each value is a folder id (string) ornullwhen not provided or not created. - Logic:
- Iterates i = 1..5. For each
folder{i}that is a non-empty string, it:- Looks for an existing folder with the same name and the current
parentvalue (null for top-level). - If found, uses its id; otherwise creates a new folder with
nameandparent. - Sets
parentto the found/created id for the next level.
- Looks for an existing folder with the same name and the current
- Stops early when it encounters the first empty/missing folder name and returns the current result.
- Idempotent: running the operation multiple times with the same names returns the same ids and does not create duplicates.
- Handles a race when creating a folder by retrying the lookup if creation fails with a conflict.
- Iterates i = 1..5. For each
Response shape (example)
{
"folder1": "a1b2c3...",
"folder2": "d4e5f6...",
"folder3": null,
"folder4": null,
"folder5": null,
"lastFolder": "d4e5f6..."
}lastFolderequals the id of the deepest created/found folder (the last non-empty folder). If onlyfolder1is supplied,lastFolderequalsfolder1's id.
Usage (Directus Flow Operation)
- Add the
ensure-foldersoperation to a Flow. - Provide
folder1(required) and optionallyfolder2..folder5. - The operation returns the object (above) which can be used by downstream operations.
Example payload (when invoking via API or testing in the Flow UI)
{
"folder1": "Tenants",
"folder2": "Project X",
"folder3": "CollectionName"
}Development / installation
- Place this extension in
/directus/extensions/ensure-folders(build result of this repo). - Restart Directus so it discovers the extension.
- Use the Flow builder to add the
ensure-foldersoperation.
License & Contributing
Any use of this plugin in a commercial or public product must visibly mention the original author and repository. Apache 2.0 License.
