Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
feat(sbomservice): Add placeholder for SHA256SUMS in LNM v1 fixtures docs(devportal): Create README for SDK archives in public directory build(devportal): Implement offline bundle build script test(devportal): Add link checker script for validating links in documentation test(devportal): Create performance check script for dist folder size test(devportal): Implement accessibility check script using Playwright and Axe docs(devportal): Add SDK quickstart guide with examples for Node.js, Python, and cURL feat(excititor): Implement MongoDB storage for airgap import records test(findings): Add unit tests for export filters hash determinism feat(findings): Define attestation contracts for ledger web service feat(graph): Add MongoDB options and service collection extensions for graph indexing test(graph): Implement integration tests for MongoDB provider and service collection extensions feat(zastava): Define configuration options for Zastava surface secrets build(tests): Create script to run Concelier linkset tests with TRX output
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using MongoDB.Driver;
|
|
|
|
namespace StellaOps.Graph.Indexer.Infrastructure;
|
|
|
|
public static class MongoServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddGraphMongoDatabase(
|
|
this IServiceCollection services,
|
|
Action<MongoDatabaseOptions> configure)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
ArgumentNullException.ThrowIfNull(configure);
|
|
|
|
services.Configure(configure);
|
|
|
|
services.AddSingleton<IMongoClient>(sp =>
|
|
{
|
|
var opts = sp.GetRequiredService<IOptions<MongoDatabaseOptions>>().Value;
|
|
Validate(opts);
|
|
return new MongoClient(opts.ConnectionString);
|
|
});
|
|
|
|
services.AddSingleton<IMongoDatabase>(sp =>
|
|
{
|
|
var opts = sp.GetRequiredService<IOptions<MongoDatabaseOptions>>().Value;
|
|
Validate(opts);
|
|
return sp.GetRequiredService<IMongoClient>().GetDatabase(opts.DatabaseName);
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
private static void Validate(MongoDatabaseOptions options)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(options.ConnectionString))
|
|
{
|
|
throw new InvalidOperationException("Mongo connection string must be provided.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(options.DatabaseName))
|
|
{
|
|
throw new InvalidOperationException("Mongo database name must be provided.");
|
|
}
|
|
}
|
|
}
|