Discovering Existing Collections with SODA for In-Database JavaScript
You can use SodaDatabase.getCollectionNames() to fetch the names of all existing collections for a given SodaDatabase object.
If the number of collections is very large, you can limit the number of names returned. Additionally, the lookup can be limited to collections starting with a user-defined string as demonstrated by Example: Filtering the List of Returned Collections.
Example: Fetching All Existing Collection Names
This example prints the names of all existing collections using the method getCollectionNames().
export function printCollectionNames(){
// loop over all collections in the current user's schema
const allCollections = soda.getCollectionNames();
for (const col of allCollections){
console.log(`- ${col}`);
}
}
Example: Filtering the List of Returned Collections
This example limits the results of getCollectionNames() by only printing the names of collections that begin with a user-defined string, startWith.
export function printSomeCollectionNames(numHits, startWith) {
// loop over all collections in the current schema, limited
// to those that start with a specific character sequence and
// a maximum number of hits returned
const allCollections = soda.getCollectionNames(
{
limit: numHits,
startsWith: startWith
}
);
for (const col of allCollections){
console.log(`-${col}`);
}
}