|
| 1 | +--- |
| 2 | +title: Collections CLI Usage |
| 3 | +sidebar_label: Collections |
| 4 | +slug: /collections-cli |
| 5 | +--- |
| 6 | + |
| 7 | +The OpenFn CLI includes support for reading from and writing to |
| 8 | +[Collections](/documentation/build/collections) |
| 9 | + |
| 10 | +You can use the CLI to: |
| 11 | + |
| 12 | +- Explore the contents of Collections without running a Workflow |
| 13 | +- Experiment with query syntax to get the keys you need |
| 14 | +- Update mapping objects and lookup tables from local (or source-controlled) |
| 15 | + files |
| 16 | +- Manually remove unneeded data |
| 17 | + |
| 18 | +:::tip |
| 19 | + |
| 20 | +Got feedback? Want more Collections support in the CLI? Post a Feature Request |
| 21 | +to [community.openfn.org](https://community.openfn.org/c/feature-requests)! |
| 22 | + |
| 23 | +::: |
| 24 | + |
| 25 | +Get started with the Collections API with `openfn collections --help` |
| 26 | + |
| 27 | +You'll need a Personal Access Token (PAT) to access a collection. You'll also |
| 28 | +need to ensure a collection has been created before you can read or write to |
| 29 | +it - see |
| 30 | +[Managing Collections](http://localhost:3000/documentation/build/collections#managing-collections) |
| 31 | + |
| 32 | +:::info Want to use Collections in a CLI workflow? |
| 33 | + |
| 34 | +These docs explain how to use the `openfn collections` CLI command. |
| 35 | + |
| 36 | +If you're running an expression or workflow from the CLI, you need to use the |
| 37 | +collections adaptor - check out the |
| 38 | +[Collections Adaptor Docs](http://localhost:3000/adaptors/collections#cli-usage) |
| 39 | +for detauls |
| 40 | + |
| 41 | +::: |
| 42 | + |
| 43 | +## Getting a PAT |
| 44 | + |
| 45 | +Data inside Collections is securely stored under a Project, and access is |
| 46 | +strictly only allowed to users with access to that Project. |
| 47 | + |
| 48 | +So if you want to access a Collection, you have to tell the server who you are. |
| 49 | + |
| 50 | +We do this using Personal Access Tokens. See |
| 51 | +[Create and Manage API Tokens](/documentation/api-tokens#about-api-tokens) for |
| 52 | +more details. |
| 53 | + |
| 54 | +One you have a PAT, you need to pass it in to the CLI. The easiest way to do |
| 55 | +this is to set your `OPENFN_PAT` env var, which the CLI will use automatically. |
| 56 | + |
| 57 | +If you're using multiple access tokens, you can pass `--token` to the CLI to |
| 58 | +override the default. |
| 59 | + |
| 60 | +```bash |
| 61 | +openfn collections get my-collection \* --token $MY_OPENFN_PAT |
| 62 | +``` |
| 63 | + |
| 64 | +:::tip |
| 65 | + |
| 66 | +The rest of this guide assumes that the `OPENFN_PAT` env var has been set. So |
| 67 | +long as it has, as you're using a server which has a `my-collection` collection, |
| 68 | +all examples will work. |
| 69 | + |
| 70 | +::: |
| 71 | + |
| 72 | +## Fetching items |
| 73 | + |
| 74 | +You can fetch items from a Collection by passing a collection name and a key, or |
| 75 | +key pattern (like `*` for "everything", or `2024*` for keys starting with |
| 76 | +`2024`) |
| 77 | + |
| 78 | +```bash |
| 79 | +openfn collections get <collection-name> <key> |
| 80 | +``` |
| 81 | + |
| 82 | +For example, to get everything from `my-collection`, run: |
| 83 | + |
| 84 | +```bash |
| 85 | +openfn collections get my-collection \* |
| 86 | +``` |
| 87 | + |
| 88 | +:::tip |
| 89 | + |
| 90 | +In unix shells (MacOS or Linux), the `*` character has special meaning. So if |
| 91 | +you want to get all items, you have to escape it or quote it: |
| 92 | + |
| 93 | +``` |
| 94 | +openfn collections get my-collection \* |
| 95 | +``` |
| 96 | + |
| 97 | +Including \* in a pattern string should still work: |
| 98 | + |
| 99 | +``` |
| 100 | +openfn collections get my-collection 2024\* |
| 101 | +``` |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +Collections are saved as strings, but will be serialized to JSON in the output. |
| 106 | + |
| 107 | +By default the CLI will log returned values to stdout in your shell. To write to |
| 108 | +disk, pass `--output` or `-o` with a file path relative to your working |
| 109 | +directory: |
| 110 | + |
| 111 | +```bash |
| 112 | +openfn collections get my-collection \* -o /tmp/my_collection.json |
| 113 | +``` |
| 114 | + |
| 115 | +It's important to understand that the output works a bit differently if you're |
| 116 | +getting one item, or potentially getting many items with a pattern. |
| 117 | + |
| 118 | +A single always returns its value verbatim, without the key. |
| 119 | + |
| 120 | +So this: |
| 121 | + |
| 122 | +```bash |
| 123 | +openfn collections get my-collection item-1 |
| 124 | +``` |
| 125 | + |
| 126 | +Returns something like this: |
| 127 | + |
| 128 | +```js |
| 129 | +{ |
| 130 | + "id": "item-1" |
| 131 | + /* ... other properties of the value */ |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +If you use a key-pattern to retrieve data, the value is output in multi-item |
| 136 | +mode, which is a JSON object where the key is the item's key, and the value is |
| 137 | +the item's value: |
| 138 | + |
| 139 | +``` |
| 140 | +$ openfn collections get my-collection item-1* |
| 141 | +
|
| 142 | +{ |
| 143 | + "item-1": { |
| 144 | + "id": "item-1" |
| 145 | + /* ... other properties of the value */ |
| 146 | + }, |
| 147 | + "item-10": { |
| 148 | + "id": "item-10" |
| 149 | + /* ... other properties of the value */ |
| 150 | + }, |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Uploading items |
| 155 | + |
| 156 | +You can use the collections command to upload data to a collection. When |
| 157 | +uploading, values always from from a file on disk. In this example we'll use |
| 158 | +JSON files, but if you're uploading a single value it doesn't have to be valid |
| 159 | +JSON. |
| 160 | + |
| 161 | +The `set` command has two modes. To upload a single item, use: |
| 162 | + |
| 163 | +```bash |
| 164 | +openfn collections set <collection-name> <key> <path/to/value.json> |
| 165 | +``` |
| 166 | + |
| 167 | +This will read the data in path/to/value.json as a string, and upsert it under |
| 168 | +the provided key. Key patterns are not supported. |
| 169 | + |
| 170 | +To bulk upsert multiple values, use: |
| 171 | + |
| 172 | +```bash |
| 173 | +openfn collections set <collection-name> --items <path/to/items.json> |
| 174 | +``` |
| 175 | + |
| 176 | +The `items.json` file must contain a JSON object where the keys are item keys |
| 177 | +and the values are item values (just like the multi-get command returns): |
| 178 | + |
| 179 | +```json |
| 180 | +{ |
| 181 | + "item-1": { |
| 182 | + "id": "item-1" |
| 183 | + /* ... other properties of the value */ |
| 184 | + }, |
| 185 | + "item-10": { |
| 186 | + "id": "item-10" |
| 187 | + /* ... other properties of the value */ |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +:::tip |
| 193 | + |
| 194 | +Remember that Collections always uses an _upsert_ strategy when uploading new |
| 195 | +items. |
| 196 | + |
| 197 | +This means that if a key does not exist, it will be created and assigned a |
| 198 | +value. If it already exists, its value will be updated. ::: |
| 199 | + |
| 200 | +::: |
| 201 | + |
| 202 | +## Removing items |
| 203 | + |
| 204 | +You can also remove items from a collection with the `collections remove` |
| 205 | +command: |
| 206 | + |
| 207 | +```bash |
| 208 | +openfn collections remove <collection-name> <key> |
| 209 | +``` |
| 210 | + |
| 211 | +Key-patterns are supported and allow you to remove multiple keys. |
| 212 | + |
| 213 | +Use `--dry-run` to get a list of the keys that would be deleted without actually |
| 214 | +running the delete: |
| 215 | + |
| 216 | +```bash |
| 217 | +openfn collections remove my-collection 2024* --dry-run |
| 218 | +``` |
0 commit comments