mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
litedb init
This commit is contained in:
parent
9cb141ef62
commit
08c3aff60b
1 changed files with 15 additions and 3 deletions
|
@ -9,20 +9,30 @@ namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
public class LiteDbStorageService : IStorageService
|
public class LiteDbStorageService : IStorageService
|
||||||
{
|
{
|
||||||
private readonly LiteCollection<JsonItem> _collection;
|
|
||||||
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||||
};
|
};
|
||||||
|
private readonly string _dbPath;
|
||||||
|
private LiteCollection<JsonItem> _collection;
|
||||||
|
|
||||||
public LiteDbStorageService(string dbPath)
|
public LiteDbStorageService(string dbPath)
|
||||||
{
|
{
|
||||||
var db = new LiteDatabase($"Filename={dbPath};");
|
_dbPath = dbPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init()
|
||||||
|
{
|
||||||
|
if(_collection == null)
|
||||||
|
{
|
||||||
|
var db = new LiteDatabase($"Filename={_dbPath};");
|
||||||
_collection = db.GetCollection<JsonItem>("json_items");
|
_collection = db.GetCollection<JsonItem>("json_items");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Task<T> GetAsync<T>(string key)
|
public Task<T> GetAsync<T>(string key)
|
||||||
{
|
{
|
||||||
|
Init();
|
||||||
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
|
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
|
||||||
if(item == null)
|
if(item == null)
|
||||||
{
|
{
|
||||||
|
@ -33,6 +43,7 @@ namespace Bit.Core.Services
|
||||||
|
|
||||||
public Task SaveAsync<T>(string key, T obj)
|
public Task SaveAsync<T>(string key, T obj)
|
||||||
{
|
{
|
||||||
|
Init();
|
||||||
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
|
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
|
||||||
_collection.Upsert(new JsonItem(key, data));
|
_collection.Upsert(new JsonItem(key, data));
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
|
@ -40,6 +51,7 @@ namespace Bit.Core.Services
|
||||||
|
|
||||||
public Task RemoveAsync(string key)
|
public Task RemoveAsync(string key)
|
||||||
{
|
{
|
||||||
|
Init();
|
||||||
_collection.Delete(i => i.Id == key);
|
_collection.Delete(i => i.Id == key);
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue