diff --git a/JsonFlatFileDataStore.Test/SingleItemTests.cs b/JsonFlatFileDataStore.Test/SingleItemTests.cs index 0828957..9bc7eb4 100644 --- a/JsonFlatFileDataStore.Test/SingleItemTests.cs +++ b/JsonFlatFileDataStore.Test/SingleItemTests.cs @@ -100,6 +100,41 @@ public void GetItem_Nullable_NotFound() UTHelpers.Down(newFilePath); } + [Fact] + public void GetRoot_Dynamic() + { + var pathToJson = UTHelpers.Up(); + + var store = new DataStore(pathToJson); + + // Get root + var root = store.GetRoot(); + + Assert.Equal(1, root.user[0].id); + Assert.Equal(2.1, root.myValue); + Assert.Equal("Carrillo", root.family[0].parents[0].name); + + UTHelpers.Down(pathToJson); + } + + [Fact] + public void GetRoot_Typed() + { + var pathToJson = UTHelpers.Up(); + + // Open database (create new if file doesn't exist) + var store = new DataStore(pathToJson); + + // Get root + var root = store.GetRoot(); + + Assert.Equal(1, root.User.First().Id); + Assert.Equal(2.1, root.MyValue); + Assert.Equal("Carrillo", root.Family.First().Parents.First().Name); + + UTHelpers.Down(pathToJson); + } + [Fact] public void NotFound_Exception() { diff --git a/JsonFlatFileDataStore.Test/TestModels.cs b/JsonFlatFileDataStore.Test/TestModels.cs index 1c36c5c..183de9a 100644 --- a/JsonFlatFileDataStore.Test/TestModels.cs +++ b/JsonFlatFileDataStore.Test/TestModels.cs @@ -163,4 +163,14 @@ public class World public float CameraRotationY { get; set; } } + + public class Store + { + public List User { get; set; } + public List Movies { get; set; } + public List Family { get; set; } + public List Worlds { get; set; } + public Double MyValue { get; set; } + public List MyValues { get; set; } + } } \ No newline at end of file diff --git a/JsonFlatFileDataStore/DataStore.cs b/JsonFlatFileDataStore/DataStore.cs index 0432137..57d035a 100644 --- a/JsonFlatFileDataStore/DataStore.cs +++ b/JsonFlatFileDataStore/DataStore.cs @@ -438,6 +438,39 @@ private async Task InnerCommit(bool isOperationAsync, CommitAction commitA return actionSuccess; } + public dynamic GetRoot() + { + if (_reloadBeforeGetCollection) + { + _jsonData = JObject.Parse(ReadJsonFromFile(_filePath)); + } + + if (_jsonData == null) + return null; + + return SingleDynamicItemReadConverter(_jsonData); + } + + public T GetRoot() + { + if (_reloadBeforeGetCollection) + { + _jsonData = JObject.Parse(ReadJsonFromFile(_filePath)); + } + + if (_jsonData == null) + { + if (Nullable.GetUnderlyingType(typeof(T)) != null) + { + return default(T); + } + + throw new KeyNotFoundException(); + } + + return _jsonData.ToObject(); + } + private dynamic SingleDynamicItemReadConverter(JToken e) { switch (e) diff --git a/JsonFlatFileDataStore/IDataStore.cs b/JsonFlatFileDataStore/IDataStore.cs index ef8928b..283eaed 100644 --- a/JsonFlatFileDataStore/IDataStore.cs +++ b/JsonFlatFileDataStore/IDataStore.cs @@ -129,6 +129,19 @@ public interface IDataStore : IDisposable /// Item key /// true if items found for deletion Task DeleteItemAsync(string key); + + /// + /// Get root item + /// + /// Dynamic item + dynamic GetRoot(); + + /// + /// Get root item + /// + /// Item type + /// Typed item + T GetRoot(); } public enum ValueType diff --git a/README.md b/README.md index aab7f92..6ed9a2c 100644 --- a/README.md +++ b/README.md @@ -456,6 +456,16 @@ var counter = store.GetItem("counter"); var user = store.GetItem("myUser"); ``` +`GetRoot` will return the dynamic root object and `GetRoot` will return the root object of type T. + +```csharp +var store = new DataStore(pathToJson); +// Dynamic data +var settingsDynamic = store.GetRoot(); +// Typed data +var settings = store.GetRoot("myUser"); +``` + Typed data will throw `KeyNotFoundException` if key is not found. Dynamic data and nullable types will return null. ```csharp