Recent searches

in

Collection

On this page

Represents a Collection of files, as configured under collections_config in your CloudCannon Configuration File. Returned by the collection() and collections() methods. Additionally, you can call items() to list a Collection's files, or addEventListener to react to changes.

Type

Object

Properties

collectionKeystring#

Holds the Collection's key, as configured under collections_config in your CloudCannon Configuration File.

Available on: Collection.

Show examplesHide examples

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

JavaScript
Copied to clipboard
const collection = api.collection('posts');
const key = collection.collectionKey; // 'posts'

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

Returns: A promise for the Collection's files.

Available on: Collection.

Show examplesHide examples

In this example, we list every file in the posts Collection and log each path.

JavaScript
Copied to clipboard
const posts = await api.collection('posts').items();
for (const file of posts) {
  console.log(file.path);
}
addEventListener'change' | 'delete'#

Listens for change and delete events on any file in this Collection. 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 post that changes.

JavaScript
Copied to clipboard
api.collection('posts').addEventListener('change', (event) => {
  console.log('A post 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 posts on teardown.

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