Opening an Existing Document Collection with SODA for In-Database JavaScript

You can use the method SodaDatabase.openCollection() to open an existing document collection or to test whether a given name names an existing collection.

Example 8-2 Opening an Existing Document Collection

This example opens the collection named collectionName. It is very important to check that the collection object returned by SodaDatabase.openCollection() is not null. Rather than throwing an error, the method will return a null value should the requested collection not exist.

export function openCollection(collectionName) {

    // perform a lookup. If a connection cannot be found by that
    // name no exception nor error are thrown, but the resulting
    // collection object will be null
    const col = soda.openCollection(collectionName);
    if (col === null) {
        throw new Error(`No such collection ${collectionName}`);
    }

    // do something with the collection
}