├── .gitattributes ├── .gitignore ├── EzDatabase.csproj ├── EzDatabase.sln ├── EzDatabase.xml ├── EzDatabase ├── AsyncExtension │ └── AsyncExtensions.cs ├── Database.cs └── DatabaseCategory.cs ├── README.md └── docs └── index.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /EzDatabase.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | 6 | 7 | 8 | ..\EzDatabase.xml 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /EzDatabase.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30709.132 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EzDatabase", "EzDatabase.csproj", "{A73DA17E-6BE5-4399-98F7-7FAF83FCEAB3}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {A73DA17E-6BE5-4399-98F7-7FAF83FCEAB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {A73DA17E-6BE5-4399-98F7-7FAF83FCEAB3}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {A73DA17E-6BE5-4399-98F7-7FAF83FCEAB3}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {A73DA17E-6BE5-4399-98F7-7FAF83FCEAB3}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {59469ADB-50FC-40D3-B1A0-BC682136E1EA} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /EzDatabase.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | EzDatabase 5 | 6 | 7 | 8 | 9 | Extensions for creating and reading files asynchronously 10 | 11 | 12 | 13 | 14 | Asynchronously saves or overwrites a json file in this category 15 | 16 | The base category 17 | The name of the file to create 18 | The object to serialize and store 19 | 20 | 21 | 22 | Asynchronously gets a json file in this category 23 | 24 | The type of the json file 25 | The base category 26 | The name of the json file 27 | The deserialized object from the json file 28 | 29 | 30 | 31 | Asynchronously gets a list of deseralized json files in this category 32 | 33 | The type of the json files 34 | The base category 35 | A list of deserialized objects 36 | 37 | 38 | 39 | Saves a file in this category with the specified name, extension and data 40 | 41 | The base category 42 | The name of the file 43 | The extension of the file with a period (for example: ".jpg") 44 | The data to be stored in the file 45 | 46 | 47 | 48 | Saves a file in this category with the specified name, extension and data 49 | 50 | The base category 51 | The name of the file 52 | The extension of the file with a period (for example: ".jpg") 53 | The data to be stored in the file 54 | 55 | 56 | 57 | Saves a file in this category with the specified name, extension and data 58 | 59 | The base category 60 | The name of the file 61 | The extension of the file with a period (for example: ".jpg") 62 | The data to be stored in the file 63 | 64 | 65 | 66 | Represents a database object 67 | 68 | 69 | 70 | 71 | Gets the name of the database 72 | 73 | 74 | 75 | 76 | A list of the categories of the database 77 | 78 | 79 | 80 | 81 | Creates a new database 82 | 83 | The name of the database to create 84 | 85 | 86 | 87 | Gets or creates a category in the database 88 | 89 | The name of the category to create 90 | The category created 91 | 92 | 93 | 94 | Gets or creates a category in the database 95 | 96 | The name of the category to get 97 | The category requested 98 | 99 | 100 | 101 | Gets a list of all the categories in the database 102 | 103 | A list of all the categories 104 | 105 | 106 | 107 | Deletes a category in the database 108 | WARNING: This irreversibly deletes the category and all the data inside of it 109 | 110 | The name of the category to delete 111 | 112 | 113 | 114 | Represents a database category 115 | 116 | 117 | 118 | 119 | Gets the name of the category 120 | 121 | 122 | 123 | 124 | Gets the base database this category is in 125 | 126 | 127 | 128 | 129 | Gets the path of this category 130 | 131 | 132 | 133 | 134 | If a subcategory, gets the parent category, otherwise returns null 135 | 136 | 137 | 138 | 139 | A list of all the subcategories in this category 140 | 141 | 142 | 143 | 144 | Creates a subcategory in this category 145 | 146 | Get name of the category to create 147 | The category created 148 | 149 | 150 | 151 | Gets or creates a subcategory in this category 152 | 153 | The name of the category to get or create 154 | The category created or retrieved 155 | 156 | 157 | 158 | Gets a list of all the subcategories in this category 159 | 160 | A list of subcategories 161 | 162 | 163 | 164 | Deletes a subcategory in this category 165 | 166 | The name of the subcategory to delete 167 | 168 | 169 | 170 | Saves or overwrites a json file in this category 171 | 172 | The name of the file to create 173 | The object to serialize and store 174 | 175 | 176 | 177 | Gets a json file in this category 178 | 179 | The type of the json file 180 | The name of the json file 181 | The deserialized object from the json file 182 | 183 | 184 | 185 | Gets a json file in this category 186 | 187 | The name of the json file 188 | A FileInfo object corresponding to the json file 189 | 190 | 191 | 192 | Gets a list of names of all json files in this category 193 | 194 | A list of the names of json files in this category 195 | 196 | 197 | 198 | Gets a list of deseralized json files in this category 199 | 200 | The type of the json files 201 | A list of deserialized objects 202 | 203 | 204 | 205 | Deletes a json file in this category 206 | 207 | 208 | 209 | 210 | 211 | Saves a file in this category with the specified name, extension and data 212 | 213 | The name of the file 214 | The extension of the file with a period (for example: ".jpg") 215 | The data to be stored in the file 216 | 217 | 218 | 219 | Saves a file in this category with the specified name, extension and data 220 | 221 | The name of the file 222 | The extension of the file with a period (for example: ".jpg") 223 | The data to be stored in the file 224 | 225 | 226 | 227 | Saves a file in this category with the specified name, extension and data 228 | 229 | The name of the file 230 | The extension of the file with a period (for example: ".jpg") 231 | The data to be stored in the file 232 | 233 | 234 | 235 | Gets a file in this category with the specified name and extension 236 | 237 | The name of the file 238 | The extension of the file with a period (for example: ".jpg") 239 | 240 | 241 | 242 | Gets a list of all the files in this category 243 | 244 | A list of files in this category 245 | 246 | 247 | 248 | Gets a list of all the files with the specified extension in this category 249 | 250 | 251 | A list of files with the specified extension 252 | 253 | 254 | 255 | Deleted the specified file in the category 256 | 257 | The name of the file to delete 258 | The extension of the file to delete with a period (for example: ".jpg") 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /EzDatabase/AsyncExtension/AsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Newtonsoft.Json; 6 | using System.IO; 7 | using System.Net; 8 | using System.Linq; 9 | 10 | namespace EzDatabase.AsyncExtension 11 | { 12 | /// 13 | /// Extensions for creating and reading files asynchronously 14 | /// 15 | public static class AsyncExtensions 16 | { 17 | /// 18 | /// Asynchronously saves or overwrites a json file in this category 19 | /// 20 | /// The base category 21 | /// The name of the file to create 22 | /// The object to serialize and store 23 | public static async Task SaveJsonAsync(this DatabaseCategory category, string name, object data) 24 | { 25 | var json = JsonConvert.SerializeObject(data); 26 | await File.WriteAllTextAsync($"{category.FullPath}\\{name}.json", json); 27 | } 28 | 29 | /// 30 | /// Asynchronously gets a json file in this category 31 | /// 32 | /// The type of the json file 33 | /// The base category 34 | /// The name of the json file 35 | /// The deserialized object from the json file 36 | public static async Task GetJsonAsync(this DatabaseCategory category, string name) 37 | { 38 | var FilePath = $"{category.FullPath}\\{name}.json"; 39 | var text = await File.ReadAllTextAsync(FilePath); 40 | return JsonConvert.DeserializeObject(text); 41 | } 42 | 43 | /// 44 | /// Asynchronously gets a list of deseralized json files in this category 45 | /// 46 | /// The type of the json files 47 | /// The base category 48 | /// A dictionary with the key being the name of the file and the value a the object 49 | public static async Task> GetAllJsonAsync(this DatabaseCategory category) 50 | { 51 | var info = new DirectoryInfo(category.FullPath); 52 | var files = info.GetFiles(); 53 | var dictionary = new Dictionary(); 54 | foreach (var file in files) 55 | { 56 | if (file.Extension == ".json") 57 | { 58 | var text = await File.ReadAllTextAsync(file.FullName); 59 | dictionary.Add(Path.GetFileNameWithoutExtension(file.Name), JsonConvert.DeserializeObject(text)); 60 | } 61 | } 62 | return dictionary; 63 | } 64 | 65 | /// 66 | /// Asynchronously saves the given text in a .txt file 67 | /// 68 | /// The base category 69 | /// The name of the file 70 | /// The text to be written in the file 71 | public static async Task SaveTextAsync(this DatabaseCategory category, string name, string text) 72 | { 73 | await File.WriteAllTextAsync($"{category.FullPath}\\{name}.txt", text); 74 | } 75 | 76 | /// 77 | /// Asynchronously gets the text from the given file 78 | /// 79 | /// The base category 80 | /// The name of the file 81 | /// The text from the file 82 | public static async Task GetTextAsync(this DatabaseCategory category, string name) 83 | { 84 | var text = await File.ReadAllTextAsync($"{category.FullPath}\\{name}.txt"); 85 | return text; 86 | } 87 | 88 | /// 89 | /// Asynchronously gets a list of all text files from the category 90 | /// 91 | /// /// The base category 92 | /// A dictionary with the key being the name of the file and the value the text in the file 93 | public static async Task> GetAllTextAsync(this DatabaseCategory category) 94 | { 95 | var info = new DirectoryInfo(category.FullPath); 96 | var files = info.GetFiles(); 97 | var dictionary = new Dictionary(); 98 | foreach (var file in files) 99 | { 100 | if (file.Extension == ".txt") 101 | { 102 | var text = await File.ReadAllTextAsync(file.FullName); 103 | dictionary.Add(Path.GetFileNameWithoutExtension(file.Name), text); 104 | } 105 | } 106 | return dictionary; 107 | } 108 | 109 | /// 110 | /// Asynchronously saves a file in this category with the specified name, extension and data 111 | /// 112 | /// The base category 113 | /// The name of the file 114 | /// The extension of the file with a period (for example: ".jpg") 115 | /// The data to be stored in the file 116 | public static async Task SaveFileAsync(this DatabaseCategory category, string name, string extension, string data) 117 | { 118 | await File.WriteAllTextAsync($"{category.FullPath}\\{name}{extension}", data); 119 | } 120 | /// 121 | /// Asynchronously saves a file in this category with the specified name, extension and data 122 | /// 123 | /// The base category 124 | /// The name of the file 125 | /// The extension of the file with a period (for example: ".jpg") 126 | /// The data to be stored in the file 127 | public static async Task SaveFileAsync(this DatabaseCategory category, string name, string extension, byte[] data) 128 | { 129 | await File.WriteAllBytesAsync($"{category.FullPath}\\{name}{extension}", data); 130 | } 131 | /// 132 | /// Asynchronously saves a file in this category with the specified name, extension and data 133 | /// 134 | /// The base category 135 | /// The name of the file 136 | /// The extension of the file with a period (for example: ".jpg") 137 | /// The data to be stored in the file 138 | public static async Task SaveFileAsync(this DatabaseCategory category, string name, string extension, Stream data) 139 | { 140 | var path = $"{category.FullPath}\\{name}{extension}"; 141 | 142 | if (File.Exists(path)) 143 | { 144 | File.Delete(path); 145 | } 146 | 147 | using FileStream fileStream = File.Create(path); 148 | await data.CopyToAsync(fileStream); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /EzDatabase/Database.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace EzDatabase 7 | { 8 | /// 9 | /// Represents a database object 10 | /// 11 | public class Database 12 | { 13 | /// 14 | /// Gets the name of the database 15 | /// 16 | public string Name { get; internal set; } 17 | 18 | /// 19 | /// Gets the path of this database 20 | /// 21 | public string FullPath { get; internal set; } 22 | 23 | /// 24 | /// Gets the directory this database is associated with 25 | /// 26 | public DirectoryInfo BaseDirectory { get; internal set; } 27 | 28 | internal string BaseDirectoryPath; 29 | 30 | /// 31 | /// Creates a new database 32 | /// 33 | /// The name of the database to create 34 | /// The path to make the database in 35 | public Database(string name, string basepath = "") 36 | { 37 | Name = name; 38 | BaseDirectoryPath = basepath; 39 | 40 | Initialize(); 41 | } 42 | 43 | internal void Initialize() 44 | { 45 | DirectoryInfo directory; 46 | if(BaseDirectoryPath == "") 47 | { 48 | directory = Directory.CreateDirectory($"{Name}"); 49 | } 50 | else 51 | { 52 | directory = Directory.CreateDirectory($"{BaseDirectoryPath}\\{Name}"); 53 | } 54 | FullPath = directory.FullName; 55 | BaseDirectory = directory; 56 | } 57 | 58 | /// 59 | /// Gets or creates a category in the database 60 | /// 61 | /// The name of the category to create 62 | /// The category created 63 | public DatabaseCategory CreateCategory(string name) 64 | { 65 | return new DatabaseCategory(this, name); 66 | } 67 | 68 | /// 69 | /// Gets a category in the database, returns null if no category of the name is found 70 | /// 71 | /// The name of the category to get 72 | /// The category requested, null if not found 73 | public DatabaseCategory GetCategory(string name) 74 | { 75 | if (!Directory.Exists($"{FullPath}\\{name}")) return null; 76 | return new DatabaseCategory(this, name); 77 | } 78 | 79 | /// 80 | /// Gets a list of all the categories in the database 81 | /// 82 | /// A list of all the categories 83 | public IReadOnlyList GetCategories() 84 | { 85 | var info = new DirectoryInfo(FullPath); 86 | var directories = info.GetDirectories(); 87 | var result = new List(); 88 | foreach (var directory in directories) 89 | { 90 | result.Add(new DatabaseCategory(this, directory.Name)); 91 | } 92 | return result; 93 | } 94 | 95 | /// 96 | /// Deletes a category in the database 97 | /// WARNING: This irreversibly deletes the category and all the data inside of it 98 | /// 99 | /// The name of the category to delete 100 | public void DeleteCategory(string name) 101 | { 102 | Directory.Delete($"{FullPath}\\{name}", true); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /EzDatabase/DatabaseCategory.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Newtonsoft.Json; 5 | using System; 6 | using EzDatabase.AsyncExtension; 7 | 8 | namespace EzDatabase 9 | { 10 | /// 11 | /// Represents a database category 12 | /// 13 | public class DatabaseCategory 14 | { 15 | /// 16 | /// Gets the name of the category 17 | /// 18 | public string Name { get; internal set; } 19 | 20 | /// 21 | /// Gets the base database this category is in 22 | /// 23 | public Database BaseDatabase { get; internal set; } 24 | 25 | /// 26 | /// Gets the full path of this category 27 | /// 28 | public string FullPath { get; internal set; } 29 | 30 | /// 31 | /// Gets the directory this category is associated with 32 | /// 33 | public DirectoryInfo BaseDirectory { get; internal set; } 34 | 35 | /// 36 | /// If a subcategory, gets the parent category, otherwise returns null 37 | /// 38 | public DatabaseCategory ParentCategory { get; internal set; } 39 | 40 | internal DatabaseCategory(Database database, string name) 41 | { 42 | Name = name; 43 | BaseDatabase = database; 44 | ParentCategory = null; 45 | 46 | Initailize(); 47 | } 48 | 49 | internal DatabaseCategory(DatabaseCategory category, string name) 50 | { 51 | Name = name; 52 | BaseDatabase = category.BaseDatabase; 53 | ParentCategory = category; 54 | 55 | InitializeSubCategory(); 56 | } 57 | 58 | internal void Initailize() 59 | { 60 | FullPath = $"{BaseDatabase.FullPath}\\{Name}"; 61 | var directory = Directory.CreateDirectory(FullPath); 62 | FullPath = directory.FullName; 63 | BaseDirectory = directory; 64 | } 65 | 66 | internal void InitializeSubCategory() 67 | { 68 | FullPath = $"{ParentCategory.FullPath}\\{Name}"; 69 | var directory = Directory.CreateDirectory(FullPath); 70 | FullPath = directory.FullName; 71 | BaseDirectory = directory; 72 | } 73 | 74 | /// 75 | /// Creates a subcategory in this category 76 | /// 77 | /// Get name of the category to create 78 | /// The category created 79 | public DatabaseCategory CreateSubCategory(string name) 80 | { 81 | return new DatabaseCategory(this, name); 82 | } 83 | 84 | /// 85 | /// Gets a subcategory in this category, returns null if no category of the name is found 86 | /// 87 | /// The name of the category to get 88 | /// The category retrieved, null if no category is found 89 | public DatabaseCategory GetSubCategory(string name) 90 | { 91 | if (!Directory.Exists($"{FullPath}\\{name}")) return null; 92 | return new DatabaseCategory(this, name); 93 | } 94 | 95 | /// 96 | /// Gets a list of all the subcategories in this category 97 | /// 98 | /// A list of subcategories 99 | public IReadOnlyList GetSubCategories() 100 | { 101 | var info = new DirectoryInfo(FullPath); 102 | var directories = info.GetDirectories(); 103 | var result = new List(); 104 | foreach (var directory in directories) 105 | { 106 | result.Add(new DatabaseCategory(this, directory.Name)); 107 | } 108 | return result; 109 | } 110 | 111 | /// 112 | /// Deletes a subcategory in this category 113 | /// WARNING: This irreversibly deletes the category and all the data inside of it 114 | /// 115 | /// The name of the subcategory to delete 116 | public void DeleteSubCategory(string name) 117 | { 118 | Directory.Delete($"{FullPath}\\{name}", true); 119 | } 120 | 121 | /// 122 | /// Saves or overwrites a json file in this category 123 | /// 124 | /// The name of the file to create 125 | /// The object to serialize and store 126 | public void SaveJson(string name, object data) 127 | { 128 | var json = JsonConvert.SerializeObject(data); 129 | File.WriteAllText($"{FullPath}\\{name}.json", json); 130 | } 131 | 132 | /// 133 | /// Gets a json file in this category 134 | /// 135 | /// The type of the json file 136 | /// The name of the json file 137 | /// The deserialized object from the json file 138 | public T GetJson(string name) 139 | { 140 | var FilePath = $"{FullPath}\\{name}.json"; 141 | var text = File.ReadAllText(FilePath); 142 | return JsonConvert.DeserializeObject(text); 143 | } 144 | /// 145 | /// Gets a json file in this category 146 | /// 147 | /// The name of the json file 148 | /// A FileInfo object corresponding to the json file 149 | public FileInfo GetJson(string name) 150 | { 151 | return new FileInfo($"{FullPath}\\{name}.json"); 152 | } 153 | 154 | /// 155 | /// Gets whether a json file with the specified name exists in this category 156 | /// 157 | /// The name of the json file to check for 158 | /// A bool indicating whether the json file exists 159 | public bool HasJson(string name) 160 | { 161 | return File.Exists($"{FullPath}\\{name}.json"); 162 | } 163 | 164 | /// 165 | /// Gets a list of names of all json files in this category 166 | /// 167 | /// A list of the names of json files in this category 168 | public IReadOnlyList GetAllJson() 169 | { 170 | var info = new DirectoryInfo(FullPath); 171 | var files = info.GetFiles(); 172 | var filenames = new List(); 173 | foreach (var file in files) 174 | { 175 | if (file.Extension == ".json") 176 | { 177 | filenames.Add(Path.GetFileNameWithoutExtension(file.Name)); 178 | } 179 | } 180 | return filenames; 181 | } 182 | /// 183 | /// Gets a dictionary of all json files in the category deserialized to the given Type 184 | /// 185 | /// The type to deserialize to 186 | /// A dictionary with the key being the name of the file and the value a the object 187 | public IReadOnlyDictionary GetAllJson() 188 | { 189 | var info = new DirectoryInfo(FullPath); 190 | var files = info.GetFiles(); 191 | var dictionary = new Dictionary(); 192 | foreach (var file in files) 193 | { 194 | if (file.Extension == ".json") 195 | { 196 | dictionary.Add(Path.GetFileNameWithoutExtension(file.Name), JsonConvert.DeserializeObject(File.ReadAllText(file.FullName))); 197 | } 198 | } 199 | return dictionary; 200 | } 201 | 202 | /// 203 | /// Deletes a json file in this category 204 | /// 205 | /// 206 | public void DeleteJson(string name) 207 | { 208 | File.Delete($"{FullPath}\\{name}.json"); 209 | } 210 | 211 | /// 212 | /// Saves the given text in a .txt file 213 | /// 214 | /// The name of the file 215 | /// The text to be written in the file 216 | public void SaveText(string name, string text) 217 | { 218 | File.WriteAllText($"{FullPath}\\{name}.txt", text); 219 | } 220 | 221 | /// 222 | /// Gets the text from the given file 223 | /// 224 | /// The name of the file 225 | /// The text from the file 226 | public string GetText(string name) 227 | { 228 | return File.ReadAllText($"{FullPath}\\{name}.txt"); 229 | } 230 | 231 | /// 232 | /// Gets whether a text file with the specified name exists in this category 233 | /// 234 | /// The name of the text file to check for 235 | /// A bool indicating whether the text file with the specified name exists 236 | public bool HasText(string name) 237 | { 238 | return File.Exists($"{FullPath}\\{name}.txt"); 239 | } 240 | 241 | /// 242 | /// Gets a list of all text files from the category 243 | /// 244 | /// A dictionary with the key being the name of the file and the value the text in the file 245 | public IReadOnlyDictionary GetAllText() 246 | { 247 | var info = new DirectoryInfo(FullPath); 248 | var files = info.GetFiles(); 249 | var dictionary = new Dictionary(); 250 | foreach (var file in files) 251 | { 252 | if (file.Extension == ".txt") 253 | { 254 | dictionary.Add(Path.GetFileNameWithoutExtension(file.Name), File.ReadAllText(file.FullName)); 255 | } 256 | } 257 | return dictionary; 258 | } 259 | 260 | /// 261 | /// Deletes the specified text file 262 | /// 263 | /// The name of the text file to delete 264 | public void DeleteText(string name) 265 | { 266 | File.Delete($"{FullPath}\\{name}.txt"); 267 | } 268 | 269 | /// 270 | /// Saves a file in this category with the specified name, extension and data 271 | /// 272 | /// The name of the file 273 | /// The extension of the file with a period (for example: ".jpg") 274 | /// The data to be stored in the file 275 | public void SaveFile(string name, string extension, string data) 276 | { 277 | File.WriteAllText($"{FullPath}\\{name}{extension}", data); 278 | } 279 | /// 280 | /// Saves a file in this category with the specified name, extension and data 281 | /// 282 | /// The name of the file 283 | /// The extension of the file with a period (for example: ".jpg") 284 | /// The data to be stored in the file 285 | public void SaveFile(string name, string extension, byte[] data) 286 | { 287 | File.WriteAllBytes($"{FullPath}\\{name}{extension}", data); 288 | } 289 | /// 290 | /// Saves a file in this category with the specified name, extension and data 291 | /// 292 | /// The name of the file 293 | /// The extension of the file with a period (for example: ".jpg") 294 | /// The data to be stored in the file 295 | public void SaveFile(string name, string extension, Stream data) 296 | { 297 | var path = $"{FullPath}\\{name}{extension}"; 298 | 299 | if (File.Exists(path)) 300 | { 301 | File.Delete(path); 302 | } 303 | 304 | using FileStream fileStream = File.Create(path); 305 | data.CopyTo(fileStream); 306 | } 307 | 308 | /// 309 | /// Gets a file in this category with the specified name and extension 310 | /// 311 | /// The name of the file 312 | /// The extension of the file with a period (for example: ".jpg") 313 | public FileInfo GetFile(string name, string extension) 314 | { 315 | return new FileInfo($"{FullPath}\\{name}{extension}"); 316 | } 317 | 318 | /// 319 | /// Gets whether a file with the specified name and extension exists in this category 320 | /// 321 | /// The name of the file to look for 322 | /// The extension of the file to look for with a period (for example: ".jpg") 323 | /// A bool indicating whether the file with the specified name and extension exists in this category 324 | public bool HasFile(string name, string extension) 325 | { 326 | return File.Exists($"{FullPath}\\{name}{extension}"); 327 | } 328 | 329 | /// 330 | /// Gets a list of all the files in this category 331 | /// 332 | /// A list of files in this category 333 | public IReadOnlyList GetAllFiles() 334 | { 335 | var info = new DirectoryInfo(FullPath); 336 | return info.GetFiles(); 337 | } 338 | /// 339 | /// Gets a list of all the files with the specified extension in this category 340 | /// 341 | /// The extension of the files to find with a period (for example: ".jpg") 342 | /// A list of files with the specified extension 343 | public IReadOnlyList GetAllFiles(string extension) 344 | { 345 | var info = new DirectoryInfo(FullPath); 346 | var files = info.GetFiles(); 347 | return files.Where(file => file.Extension == extension).ToList(); 348 | } 349 | 350 | /// 351 | /// Deleted the specified file in the category 352 | /// 353 | /// The name of the file to delete 354 | /// The extension of the file to delete with a period (for example: ".jpg") 355 | public void DeleteFile(string name, string extension) 356 | { 357 | File.Delete($"{FullPath}\\{name}{extension}"); 358 | } 359 | 360 | /// 361 | /// Deletes all files in this category 362 | /// 363 | public void Clear() 364 | { 365 | foreach(var file in GetAllFiles()) 366 | { 367 | file.Delete(); 368 | } 369 | } 370 | /// 371 | /// Deletes all files with the extension in this category 372 | /// 373 | /// The extension of the files to delete 374 | public void Clear(string extension) 375 | { 376 | foreach(var file in GetAllFiles(extension)) 377 | { 378 | file.Delete(); 379 | } 380 | } 381 | 382 | /// 383 | /// Deletes all files and subcategories in this category 384 | /// 385 | public void ClearAll() 386 | { 387 | Directory.Delete(FullPath, true); 388 | Directory.CreateDirectory(FullPath); 389 | } 390 | } 391 | } 392 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EzDatabase 2 | [![Discord Server](https://img.shields.io/discord/801857343930761281?label=Discord)](https://discord.gg/https://discord.gg/2ZhXXVJYhU) 3 | 4 | An easy to use json based I/O database for C# 5 | 6 | ## This project is no longer actively maintained. Use PostgreSQL with entity framework instead. It's probably several times more efficient at scale. 7 | 8 | # How it works 9 | Instead of having to bother yourself with SQL and add unnecessary dependencies to your project, you can use EzDatabase to make a simple file based database and store your info 10 | in simple JSON format, or even store images, or anything you can normally store in your computer as a file! 11 | 12 | It utitlizes Newtonsoft.Json to serialize and deserailize objects and then store them in .json format 13 | 14 | To create some other type of file, you can simply specify the name and extension and store a string, byte array or stream inside 15 | # How to use 16 | to get example usage and documentation, check out the [Docs](https://idoeverything.github.io/EzDatabase/) 17 | # Contributing and getting help 18 | This is still under heavy development, and there are likely many bugs. If you would like to contribute, feel free to open a [pull request](https://github.com/IDoEverything/EzDatabase/pulls). If you have any questions, you can [join our discord server](https://discord.gg/2ZhXXVJYhU) or [open a discussion](https://github.com/IDoEverything/EzDatabase/discussions) 19 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Welcome to the EzDatabase docs! 2 | EzDatabase is a easy to use alternative to other database storage solutions like SQL that directly saves your data in simple files and folders, for ease of access and usage 3 | # Example usage 4 | Currently there is no way to directly install the package, so you can instead clone the project, build it and reference the dlls in your project 5 | ## Making a database 6 | Once you have the dependencies installed, you can initiate a database like this: 7 | ```cs 8 | Database database = new Database("Database Name"); 9 | ``` 10 | You can then create a category inside the db: 11 | ```cs 12 | DatabaseCategory category = database.CreateCategory("A category"); 13 | ``` 14 | You can also make subcategories inside categories 15 | ```cs 16 | DatabaseCategory subcategory = category.CreateSubCategory("a subcategory"); 17 | ``` 18 | And use it as a regular category 19 | ## Storing data 20 | To store an object as json inside the category, you can simply do 21 | ```cs 22 | category.SaveJson(MyObject); 23 | ``` 24 | To store a file, you can do 25 | ```cs 26 | //to store a string 27 | category.SaveFile("filename", ".txt", "my text"); 28 | 29 | //to store a byte array 30 | category.SaveFile("filename", ".mp3", ByteArray); 31 | 32 | //to store a Stream 33 | category.SaveFile("filename", ".jpg", MyStream); 34 | ``` 35 | To store simple text (as .txt), you can do 36 | ```cs 37 | category.SaveText("filename", "my text"); 38 | ``` 39 | ## Reading and processing data 40 | You can retrieve json data from your database 41 | ```cs 42 | //directly deserializes the json file 43 | ClassType data = category.GetJson("filename"); 44 | 45 | //gets the FileInfo for the json file 46 | FileInfo filedata = category.GetJson("filename"); 47 | ``` 48 | You can also get a list of all the json files in the category 49 | ```cs 50 | //get a list of names of json files in the category 51 | IReadOnlyList jsoninfo = category.GetAllJson(); 52 | 53 | //or directly deserialize all the data at once into a dictionary 54 | //the key is the file name 55 | IReadOnlyDictionary jsondata = category.GetAllJson(); 56 | ``` 57 | To get files in your category 58 | ```cs 59 | //to get a single file 60 | FileInfo file = category.GetFile("my image", ".jpg"); 61 | 62 | //gets a list of all files in the category 63 | IReadOnlyList files = category.GetAllFiles(); 64 | 65 | //or all files with the specified extension 66 | IReadOnlyList jpgfiles = category.GetAllFiles(".jpg"); 67 | ``` 68 | To get text (.txt) files in your category 69 | ```cs 70 | //to get data from a single file 71 | string textfile = category.GetText("filename"); 72 | 73 | //to get all text files 74 | //the key is the file name 75 | IReadOnlyDictionary textfiles = category.GetAllText(); 76 | ``` 77 | ## Deleting data 78 | To delete a json file 79 | ```cs 80 | category.DeleteJson("filename"); 81 | ``` 82 | To delete a file 83 | ```cs 84 | category.DeleteFile("filename", ".jpg"); 85 | ``` 86 | To delete a text file 87 | ```cs 88 | category.DeleteText("filename"); 89 | ``` --------------------------------------------------------------------------------