forked from CommunityToolkit/Datasync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(CommunityToolkit#245) Added MongoDB Repository and tests. WIP.
- Loading branch information
1 parent
16f8496
commit 6ed9c47
Showing
17 changed files
with
732 additions
and
3 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
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
18 changes: 18 additions & 0 deletions
18
src/CommunityToolkit.Datasync.Server.MongoDB/CommunityToolkit.Datasync.Server.MongoDB.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Description>A repository for the server-side of the Datasync Toolkit that uses MongoDB for storage.</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="CommunityToolkit.Datasync.Server.Test" /> | ||
<InternalsVisibleTo Include="CommunityToolkit.Datasync.Server.MongoDB.Test" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CommunityToolkit.Datasync.Server.Abstractions\CommunityToolkit.Datasync.Server.Abstractions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
166 changes: 166 additions & 0 deletions
166
src/CommunityToolkit.Datasync.Server.MongoDB/MongoDBRepository.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,166 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using CommunityToolkit.Datasync.Server.Abstractions.Http; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace CommunityToolkit.Datasync.Server.MongoDB; | ||
|
||
/// <summary> | ||
/// A repository implementation that stored data in a LiteDB database. | ||
/// </summary> | ||
/// <typeparam name="TEntity">The entity type to store in the database.</typeparam> | ||
public class MongoDBRepository<TEntity> : IRepository<TEntity> where TEntity : MongoTableData | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="MongoDBRepository{TEntity}"/> using the provided MongoDB database. | ||
/// </summary> | ||
/// <remarks> | ||
/// The collection name is based on the entity type. | ||
/// </remarks> | ||
/// <param name="database">The <see cref="IMongoDatabase"/> to use for storing entities.</param> | ||
public MongoDBRepository(IMongoDatabase database) : this(database.GetCollection<TEntity>(typeof(TEntity).Name.ToLowerInvariant() + "s")) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="MongoDBRepository{TEntity}"/> using the provided database connection | ||
/// and collection name. | ||
/// </summary> | ||
/// <param name="collection">The <see cref="IMongoCollection{TDocument}"/> to use for storing entities.</param> | ||
public MongoDBRepository(IMongoCollection<TEntity> collection) | ||
{ | ||
Collection = collection; | ||
// TODO: Ensure that there is an index on the right properties. | ||
} | ||
|
||
/// <summary> | ||
/// The collection within the LiteDb database that stores the entities. | ||
/// </summary> | ||
public virtual IMongoCollection<TEntity> Collection { get; } | ||
|
||
/// <summary> | ||
/// The mechanism by which an Id is generated when one is not provided. | ||
/// </summary> | ||
public Func<TEntity, string> IdGenerator { get; set; } = _ => Guid.NewGuid().ToString("N"); | ||
|
||
/// <summary> | ||
/// The mechanism by which a new version byte array is generated. | ||
/// </summary> | ||
public Func<byte[]> VersionGenerator { get; set; } = () => Guid.NewGuid().ToByteArray(); | ||
|
||
/// <summary> | ||
/// Updates the system properties for the provided entity on write. | ||
/// </summary> | ||
/// <param name="entity">The entity to update.</param> | ||
protected void UpdateEntity(TEntity entity) | ||
{ | ||
entity.UpdatedAt = DateTimeOffset.UtcNow; | ||
entity.Version = VersionGenerator.Invoke(); | ||
} | ||
|
||
/// <summary> | ||
/// Checks that the provided ID is valid. | ||
/// </summary> | ||
/// <param name="id">The ID of the entity to check.</param> | ||
/// <exception cref="HttpException">Thrown if the ID is not valid.</exception> | ||
protected static void CheckIdIsValid(string id) | ||
{ | ||
if (string.IsNullOrEmpty(id)) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status400BadRequest); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns a filter definition for finding a single document. | ||
/// </summary> | ||
/// <param name="id">The ID of the document to find.</param> | ||
/// <returns>The filter definition to find the document.</returns> | ||
protected FilterDefinition<TEntity> GetFilterById(string id) | ||
=> Builders<TEntity>.Filter.Eq(x => x.Id, id); | ||
|
||
/// <summary> | ||
/// Returns the document with the provided ID, or null if it doesn't exist. | ||
/// </summary> | ||
/// <param name="id">The ID of the document to find.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param> | ||
/// <returns>The document, or null if not found.</returns> | ||
protected async ValueTask<TEntity?> FindDocumentByIdAsync(string id, CancellationToken cancellationToken = default) | ||
{ | ||
return await Collection.Find(GetFilterById(id)).FirstOrDefaultAsync(cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual ValueTask<IQueryable<TEntity>> AsQueryableAsync(CancellationToken cancellationToken = default) | ||
=> ValueTask.FromResult(Collection.AsQueryable()); | ||
|
||
/// <inheritdoc/> | ||
public virtual async ValueTask CreateAsync(TEntity entity, CancellationToken cancellationToken = default) | ||
{ | ||
if (string.IsNullOrEmpty(entity.Id)) | ||
{ | ||
entity.Id = IdGenerator.Invoke(entity); | ||
} | ||
|
||
TEntity? existingEntity = await FindDocumentByIdAsync(entity.Id, cancellationToken).ConfigureAwait(false); | ||
if (existingEntity is not null) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status409Conflict) { Payload = existingEntity }; | ||
} | ||
|
||
UpdateEntity(entity); | ||
await Collection.InsertOneAsync(entity, options: null, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual async ValueTask DeleteAsync(string id, byte[]? version = null, CancellationToken cancellationToken = default) | ||
{ | ||
CheckIdIsValid(id); | ||
|
||
TEntity storedEntity = await FindDocumentByIdAsync(id, cancellationToken).ConfigureAwait(false) | ||
?? throw new HttpException(HttpStatusCodes.Status404NotFound); | ||
if (version?.Length > 0 && !storedEntity.Version.SequenceEqual(version)) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status412PreconditionFailed) { Payload = storedEntity }; | ||
} | ||
|
||
DeleteResult result = await Collection.DeleteOneAsync(GetFilterById(id), cancellationToken); | ||
if (result.DeletedCount == 0) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status404NotFound); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual async ValueTask<TEntity> ReadAsync(string id, CancellationToken cancellationToken = default) | ||
{ | ||
CheckIdIsValid(id); | ||
|
||
return await FindDocumentByIdAsync(id, cancellationToken).ConfigureAwait(false) | ||
?? throw new HttpException(HttpStatusCodes.Status404NotFound); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual async ValueTask ReplaceAsync(TEntity entity, byte[]? version = null, CancellationToken cancellationToken = default) | ||
{ | ||
CheckIdIsValid(entity.Id); | ||
|
||
TEntity storedEntity = await FindDocumentByIdAsync(entity.Id, cancellationToken).ConfigureAwait(false) | ||
?? throw new HttpException(HttpStatusCodes.Status404NotFound); | ||
if (version?.Length > 0 && !storedEntity.Version.SequenceEqual(version)) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status412PreconditionFailed) { Payload = storedEntity }; | ||
} | ||
|
||
UpdateEntity(entity); | ||
ReplaceOptions<TEntity> options = new() { IsUpsert = false }; | ||
ReplaceOneResult result = await Collection.ReplaceOneAsync(GetFilterById(entity.Id), entity, options, cancellationToken); | ||
if (result.IsModifiedCountAvailable && result.ModifiedCount == 0) | ||
{ | ||
throw new HttpException(HttpStatusCodes.Status404NotFound); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/CommunityToolkit.Datasync.Server.MongoDB/MongoTableData.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,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
namespace CommunityToolkit.Datasync.Server.MongoDB; | ||
|
||
/// <summary> | ||
/// An implementation of the <see cref="ITableData"/> interface for | ||
/// handling entities in a MongoDB database. | ||
/// </summary> | ||
public class MongoTableData : ITableData | ||
{ | ||
/// <inheritdoc /> | ||
[BsonId] | ||
public string Id { get; set; } = string.Empty; | ||
|
||
/// <inheritdoc /> | ||
public bool Deleted { get; set; } = false; | ||
|
||
/// <inheritdoc /> | ||
public DateTimeOffset? UpdatedAt { get; set; } = DateTimeOffset.UnixEpoch; | ||
|
||
/// <inheritdoc /> | ||
public byte[] Version { get; set; } = []; | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(ITableData? other) | ||
=> other != null && Id == other.Id && Version.SequenceEqual(other.Version); | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
...Toolkit.Datasync.Server.MongoDB.Test/CommunityToolkit.Datasync.Server.MongoDB.Test.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\CommunityToolkit.Datasync.Server.MongoDB\CommunityToolkit.Datasync.Server.MongoDB.csproj" /> | ||
<ProjectReference Include="..\CommunityToolkit.Datasync.TestCommon\CommunityToolkit.Datasync.TestCommon.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.msbuild"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
55 changes: 55 additions & 0 deletions
55
tests/CommunityToolkit.Datasync.Server.MongoDB.Test/CosmosMongoRepository_Tests.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,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using CommunityToolkit.Datasync.TestCommon; | ||
using CommunityToolkit.Datasync.TestCommon.Databases; | ||
using Microsoft.EntityFrameworkCore; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace CommunityToolkit.Datasync.Server.MongoDB.Test; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class CosmosMongoRepository_Tests(ITestOutputHelper output) : RepositoryTests<MongoDBMovie>(), IAsyncLifetime | ||
{ | ||
#region Setup | ||
private readonly Random random = new(); | ||
private List<MongoDBMovie> movies = []; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
if (!string.IsNullOrEmpty(ConnectionStrings.CosmosMongo)) | ||
{ | ||
Context = await MongoDBContext.CreateContextAsync(ConnectionStrings.CosmosMongo, output); | ||
this.movies = await Context.Movies.Find(new BsonDocument()).ToListAsync(); | ||
} | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
if (Context is not null) | ||
{ | ||
await Context.DisposeAsync(); | ||
} | ||
} | ||
|
||
public MongoDBContext Context { get; set; } | ||
|
||
protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.CosmosMongo); | ||
|
||
protected override async Task<MongoDBMovie> GetEntityAsync(string id) | ||
=> await Context.Movies.Find(Builders<MongoDBMovie>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync(); | ||
|
||
protected override async Task<int> GetEntityCountAsync() | ||
=> (int)(await Context.Movies.CountDocumentsAsync(Builders<MongoDBMovie>.Filter.Empty)); | ||
|
||
protected override Task<IRepository<MongoDBMovie>> GetPopulatedRepositoryAsync() | ||
=> Task.FromResult<IRepository<MongoDBMovie>>(new MongoDBRepository<MongoDBMovie>(Context.Movies)); | ||
|
||
protected override Task<string> GetRandomEntityIdAsync(bool exists) | ||
=> Task.FromResult(exists ? this.movies[this.random.Next(this.movies.Count)].Id : Guid.NewGuid().ToString()); | ||
#endregion | ||
} |
Oops, something went wrong.