SODA for In-Database JavaScriptによる既存のコレクションの検出

SodaDatabase.getCollectionNames()を使用すると、特定のSodaDatabaseオブジェクトのすべての既存コレクションの名前をフェッチできます。

コレクションの数が非常に多い場合は、戻される名前の数を制限できます。また、例8-4に示すように、ユーザー定義文字列で始まるコレクションに検索を制限できます。

例8-3 既存のすべてのコレクション名のフェッチ

この例では、メソッド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}`);
  }
}

例8-4 戻されるコレクションのリストのフィルタリング

この例では、ユーザー定義文字列startWithで始まるコレクションの名前のみを出力することによって、getCollectionNames()の結果を制限します。

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}`);
  }
}