import { Collection , Db , IndexOptions , MongoClient } from 'mongodb' ; import { configureIndexesForCollection } from './create-indexes-for-collection' ; export type FieldOrSpec = string | { [ fieldName ] : any ; } ; export interface MongoIndexSpec { fieldOrSpec : FieldOrSpec ; options ? : IndexOptions ; } async function createCollectionIfNotExists ( name , db ) { const collections = await db . collections ( ) ; const collection = collections . find ( col => col . collectionName === name ) ; return collection || db . createCollection ( name ) ; } export async function configureIndexes ( client , collectionIndexes ) { const db = client . db ( ) ; await Promise . all ( Object . keys ( collectionIndexes ) . map ( async ( collectionName ) => { const collection = await createCollectionIfNotExists ( collectionName , db ) ; const indexes = collectionIndexes [ collectionName ] ; await configureIndexesForCollection ( collection , indexes ) ; } ) ) ; }