2021-12-18 11:18:35 +00:00
|
|
|
|
using LiteDB;
|
|
|
|
|
using LiteDB.Async;
|
|
|
|
|
|
|
|
|
|
namespace PettingZoo.Settings.LiteDB
|
|
|
|
|
{
|
|
|
|
|
public class BaseLiteDBRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly string databaseFilename;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseLiteDBRepository(string databaseName)
|
|
|
|
|
{
|
|
|
|
|
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
|
|
|
if (appDataPath == null)
|
|
|
|
|
throw new IOException("Could not resolve application data path");
|
|
|
|
|
|
|
|
|
|
var databasePath = Path.Combine(appDataPath, @"PettingZoo");
|
|
|
|
|
if (!Directory.CreateDirectory(databasePath).Exists)
|
|
|
|
|
throw new IOException($"Failed to create directory: {databasePath}");
|
|
|
|
|
|
|
|
|
|
databaseFilename = Path.Combine(databasePath, $"{databaseName}.litedb");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected ILiteDatabaseAsync GetDatabase()
|
|
|
|
|
{
|
2021-12-18 17:40:22 +00:00
|
|
|
|
return new LiteDatabaseAsync(databaseFilename, new BsonMapper
|
|
|
|
|
{
|
|
|
|
|
EmptyStringToNull = false
|
|
|
|
|
});
|
2021-12-18 11:18:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|