using MongoDB.Bson;
using MongoDB.Driver;

namespace Managing.Infrastructure.Databases.MongoDb
{
    public static class MongoHelpers
    {
        public static async Task EnsureIndexExists(this IMongoDatabase database, string collectionName, string indexName)
        {
            var collection = database.GetCollection<BsonDocument>(collectionName);
            var index = new BsonDocument
            {
                {indexName, 1}
            };

            var indexModel = new CreateIndexModel<BsonDocument>(index, new CreateIndexOptions { Unique = true });
            await collection.Indexes.CreateOneAsync(indexModel).ConfigureAwait(false);
        }
    }
}
