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
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.
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.
const posts = await api.collection('posts').items();
for (const file of posts) {
console.log(file.path);
}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.
api.collection('posts').addEventListener('change', (event) => {
console.log('A post changed:', event.detail.sourcePath);
});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.
const posts = api.collection('posts');
const onChange = (event) => console.log('A post changed:', event.detail.sourcePath);
posts.addEventListener('change', onChange);
posts.removeEventListener('change', onChange);