-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported Tingle.Extensions.MongoDB (#215)
- Loading branch information
1 parent
d8d18af
commit 365b5b1
Showing
51 changed files
with
3,734 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Bson", | ||
"etag", | ||
"Ksuid", | ||
"libphonenumber", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<UserSecretsId>8bd0ef6d-24c8-4ae8-8397-32053d171b84</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Tingle.Extensions.MongoDB\Tingle.Extensions.MongoDB.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Driver; | ||
|
||
Console.WriteLine("Hello World!"); | ||
|
||
var dbName = Guid.NewGuid().ToString().Replace("_", "")[..10]; | ||
var connectionString = $"mongodb://localhost:27017/{dbName}"; | ||
|
||
var services = new ServiceCollection(); | ||
services.AddLogging(); | ||
services.AddMongoDbContext<SampleDbContext>(options => | ||
{ | ||
options.UseMongoConnectionString(connectionString); | ||
}); | ||
|
||
var sp = services.BuildServiceProvider(validateScopes: true); | ||
|
||
// resolving directly | ||
using var scope = sp.CreateScope(); | ||
var provider = scope.ServiceProvider; | ||
|
||
var context = provider.GetRequiredService<SampleDbContext>(); | ||
var persons = await context.Persons.AsQueryable().ToListAsync(); | ||
Console.WriteLine($"Found {persons.Count} persons"); | ||
|
||
class Person | ||
{ | ||
[BsonId] | ||
public string? Id { get; set; } | ||
|
||
public string? Name { get; set; } | ||
} | ||
|
||
class SampleDbContext(MongoDbContextOptions<SampleDbContext> options) : MongoDbContext(options) | ||
{ | ||
public IMongoCollection<Person> Persons => Collection<Person>("Persons"); | ||
|
||
protected override void OnConfiguring(MongoDbContextOptionsBuilder optionsBuilder) | ||
{ | ||
base.OnConfiguring(optionsBuilder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
|
||
namespace Tingle.Extensions.MongoDB; | ||
|
||
/// <summary> | ||
/// Helper for performing creation. | ||
/// </summary> | ||
/// <typeparam name="TContext">The type of context to be used.</typeparam> | ||
internal class DatabaseSetup<TContext>(IServiceScopeFactory scopeFactory, ILogger<DatabaseSetup<TContext>> logger) : IHostedService where TContext : MongoDbContext | ||
{ | ||
private readonly IServiceScopeFactory scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); | ||
private readonly ILogger logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
using var scope = scopeFactory.CreateScope(); | ||
var provider = scope.ServiceProvider; | ||
|
||
// Check if explicitly told to do creation | ||
var environment = provider.GetRequiredService<IHostEnvironment>(); | ||
var configuration = provider.GetRequiredService<IConfiguration>(); | ||
if (bool.TryParse(configuration["MONGO_CREATE_DATABASE"], out var b) && b) | ||
{ | ||
// Create database | ||
logger.LogInformation("Creating MongoDB database ..."); | ||
var context = provider.GetRequiredService<TContext>(); | ||
await context.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false); | ||
logger.LogInformation("Completed MongoDB database creation."); | ||
} | ||
else | ||
{ | ||
logger.LogDebug("Database creation skipped."); | ||
return; | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Tingle.Extensions.MongoDB/Diagnostics/MongoDbContextHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using MongoDB.Driver; | ||
|
||
namespace Tingle.Extensions.MongoDB.Diagnostics; | ||
|
||
internal class MongoDbContextHealthCheck<TContext> : IHealthCheck where TContext : MongoDbContext | ||
{ | ||
private readonly IMongoDatabase database; | ||
public MongoDbContextHealthCheck(TContext context) | ||
{ | ||
database = context.Database ?? throw new ArgumentNullException(nameof(database)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (await database.ListCollectionsAsync(cancellationToken: cancellationToken).ConfigureAwait(false)) | ||
{ | ||
|
||
} | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.