Skip to content

Commit

Permalink
FIX-2314 Support LiteDB (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbruvik authored Mar 19, 2024
1 parent 0909ab8 commit a720277
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 4 deletions.
24 changes: 22 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ cp appsettings.json mysettings.json

### Database for storing urls to WitsmlServers

Using [MongoDB](Docker/MongoDb/README.md)
Using [CosmosDB](Scripts/Azure/README.md)
Using [MongoDB](Docker/MongoDb/README.md), [CosmosDB](Scripts/Azure/README.md) or [LiteDB](#using-litedb)

### Using LiteDb

LiteDb is a serverless database stored in a single file. To use LiteDb, add this to the configuration in `mysettings.json`:
```json
{
"LiteDb": {
"Name": "witsml-explorer-db.db"
}
}
```
Note that the name should contain the .db extension. It can be a full path to the database file if you want to store it in a different folder.

## Running
The database, backend and frontend must be running at the same time for WE to work properly.
Expand Down Expand Up @@ -152,6 +163,7 @@ dotnet user-secrets set "Witsml:Password" "<password>"
```

A db configuration is needed if running tests that uses the database (**NB** Use same name as in mySettings.json) :
MongoDB:
```json
{
"MongoDb": {
Expand All @@ -160,6 +172,14 @@ A db configuration is needed if running tests that uses the database (**NB** Use
}
}
```
LiteDB:
```json
{
"LiteDb": {
"Name": "witsml-explorer-db.db"
}
}
```

To run a given test, open the test file that contains it and remove the `Skip` part. E.g replace
``` c#
Expand Down
2 changes: 1 addition & 1 deletion Docs/OAUTH.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ To use Azure keyvault, create your keyvault (named `witsmlexp-servers-kv` in the
| witsmlcreds--server2user--password | My server2 test [CRUD] | ||
| witsmlcreds--server2user--userid | My server2 test [CRUD] | ||

Credentials will be mapped on URL from secrets with the server list. `Server` entry in MongoDB or CosmosDB will have the property `roles`. The app role assigned to a server will be compared to the role claims in the JWT provided in the Authorization header. If a user has been assigned the same application role, system credentials will be made available to the user. An API call will use the system credentials if the system username is set in the `WitsmlTargetUsername` or `WitsmlSourceUsername` header.
Credentials will be mapped on URL from secrets with the server list. `Server` entry in the database will have the property `roles`. The app role assigned to a server will be compared to the role claims in the JWT provided in the Authorization header. If a user has been assigned the same application role, system credentials will be made available to the user. An API call will use the system credentials if the system username is set in the `WitsmlTargetUsername` or `WitsmlSourceUsername` header.

**example server json in list**

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Please see our [contributing.md](/CONTRIBUTING.md).
Please see our [auth.md](/Docs/AUTH.md).

## Database
Please see [mongoDb](Docker/MongoDb/README.md) or [cosmosDb](Scripts/Azure/README.md) readme.
Please see [mongoDb](Docker/MongoDb/README.md), [cosmosDb](Scripts/Azure/README.md) or [LiteDb](Contributing.md#using-litedb) readme.

## Run locally
Please see our [docker setup](/Docker/README.md).
Expand Down
5 changes: 5 additions & 0 deletions Src/WitsmlExplorer.Api/Configuration/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private static void AddRepository<TDocument, T>(IServiceCollection services, ICo
Log.Information("Detected database config for CosmosDB");
services.AddSingleton<IDocumentRepository<TDocument, T>, CosmosRepository<TDocument, T>>();
}
else if (!string.IsNullOrEmpty(configuration["LiteDb:Name"]))
{
Log.Information("Detected database config for LiteDB");
services.AddSingleton<IDocumentRepository<TDocument, T>, LiteDbRepository<TDocument, T>>();
}
else
{
Log.Error(MissingDatabaseConfigMessage);
Expand Down
57 changes: 57 additions & 0 deletions Src/WitsmlExplorer.Api/Repositories/LiteRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Threading.Tasks;

using LiteDB;

using Microsoft.Extensions.Configuration;

namespace WitsmlExplorer.Api.Repositories
{
public class LiteDbRepository<TDocument, TDocumentId> : IDocumentRepository<TDocument, TDocumentId> where TDocument : DbDocument<TDocumentId>
{
private readonly ILiteCollection<TDocument> _collection;

public LiteDbRepository(IConfiguration configuration)
{
var filename = configuration["LiteDb:Name"];
var db = new LiteDatabase(@$"Filename={filename};Connection=shared");
var collectionName = $"{typeof(TDocument).Name}s";
_collection = db.GetCollection<TDocument>(collectionName);
}

public Task InitClientAsync()
{
return Task.CompletedTask;
}

public Task<TDocument> GetDocumentAsync(TDocumentId id)
{
var document = _collection.FindById(new BsonValue(id));
return Task.FromResult(document);
}

public Task<ICollection<TDocument>> GetDocumentsAsync()
{
var documents = _collection.FindAll();
return Task.FromResult<ICollection<TDocument>>(new List<TDocument>(documents));
}

public Task<TDocument> UpdateDocumentAsync(TDocumentId id, TDocument document)
{
_collection.Update(document);
return GetDocumentAsync(id);
}

public Task<TDocument> CreateDocumentAsync(TDocument document)
{
_collection.Insert(document);
return GetDocumentAsync(document.Id);
}

public Task DeleteDocumentAsync(TDocumentId id)
{
_collection.Delete(new BsonValue(id));
return Task.CompletedTask;
}
}
}
1 change: 1 addition & 0 deletions Src/WitsmlExplorer.Api/WitsmlExplorer.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="LiteDB" Version="5.0.19" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.31.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;

using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Repositories;

using Xunit;
using Xunit.Abstractions;

namespace WitsmlExplorer.IntegrationTests.Api.Repositories
{
public class LiteDbServerRepositoryTests
{
private readonly IDocumentRepository<Server, Guid> _repo;
private readonly ITestOutputHelper _output;

public LiteDbServerRepositoryTests(ITestOutputHelper output)
{
IConfiguration configuration = ConfigurationReader.GetConfig();
_repo = new LiteDbRepository<Server, Guid>(configuration);
_output = output;
}

[Fact(Skip = "Should only be run manually")]
public async Task GetAllServers()
{
IEnumerable<Server> servers = await _repo.GetDocumentsAsync();
foreach (Server server in servers)
{
_output.WriteLine(server.ToString());
}
}

[Fact(Skip = "Should only be run manually")]
public async Task AddServer()
{
Server newServer = new()
{
Name = "<insert servername>",
Url = new Uri("<insert url>"),
Description = ""
};
Server server = await _repo.CreateDocumentAsync(newServer);
_output.WriteLine($"Inserted server {server}");
}

[Fact(Skip = "Should only be run manually")]
public async Task RemoveServer()
{
Guid serverId = new("<insert server id>");
await _repo.DeleteDocumentAsync(serverId);
_output.WriteLine($"Removed server");
}
}
}

0 comments on commit a720277

Please sign in to comment.