Recent searches

in

Dataset

On this page

Represents a Dataset, as configured under data_config in your CloudCannon Configuration File. Returned by the dataset() method. Additionally, you can call items() to read a Dataset's file or files, or addEventListener to react to changes.

Type

Object

Properties

datasetKeystring#

Holds the Dataset's key, as configured under data_config in your CloudCannon Configuration File.

Available on: Dataset.

Show examplesHide examples

In this example, we read a Dataset's key from its handle.

JavaScript
Copied to clipboard
const dataset = api.dataset('locales');
const key = dataset.datasetKey; // 'locales'

Returns the Dataset's file or files: a single File when the Dataset is configured as one file, or an array of File objects when it's a folder. A falsy key (an empty string or undefined⁠) or a non-matching key (a valid string that matches no configured Dataset) never resolves the returned promise, so always pass a real Dataset key. A Collection differs here: its items() resolves to an empty array for a non-matching key.

Returns: A promise for the Dataset's file, or array of files.

Available on: Dataset.

Show examplesHide examples

In this example, we read the locales Dataset's file or files, normalized to an array.

JavaScript
Copied to clipboard
const result = await api.dataset('locales').items();
const items = Array.isArray(result) ? result : [result];
for (const file of items) {
  console.log(file.path);
}
addEventListener'change' | 'delete'#

Listens for change and delete events on any file in this Dataset. change fires when a file is created or updated, and delete when one is removed. Remove the listener with removeEventListener when your integration is torn down.

Available on: API Object, Collection, Dataset, File.

Show examplesHide examples

In this example, we log the path of any locale that changes.

JavaScript
Copied to clipboard
api.dataset('locales').addEventListener('change', (event) => {
  console.log('A locale changed:', event.detail.sourcePath);
});
removeEventListener'change' | 'delete'#

Removes a change or delete listener previously added with addEventListener.

Available on: API Object, Collection, Dataset, File.

Show examplesHide examples

In this example, we stop listening for changes to locales on teardown.

JavaScript
Copied to clipboard
const locales = api.dataset('locales');
const onChange = (event) => console.log('A locale changed:', event.detail.sourcePath);
locales.addEventListener('change', onChange);
locales.removeEventListener('change', onChange);
Open in a new tab