{"id":13466564,"url":"https://github.com/chanan/BlazorDB","last_synced_at":"2025-03-25T21:32:30.816Z","repository":{"id":43211105,"uuid":"132522906","full_name":"chanan/BlazorDB","owner":"chanan","description":"In memory, persisted to localstorage, database for .net Blazor browser framework","archived":true,"fork":false,"pushed_at":"2019-07-25T18:29:57.000Z","size":350,"stargazers_count":70,"open_issues_count":3,"forks_count":19,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-29T19:19:29.716Z","etag":null,"topics":["blazor","database","linq","localstorage"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chanan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-07T22:26:43.000Z","updated_at":"2023-07-18T08:01:21.000Z","dependencies_parsed_at":"2022-09-10T03:11:52.349Z","dependency_job_id":null,"html_url":"https://github.com/chanan/BlazorDB","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanan%2FBlazorDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanan%2FBlazorDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanan%2FBlazorDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanan%2FBlazorDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chanan","download_url":"https://codeload.github.com/chanan/BlazorDB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245548566,"owners_count":20633611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","database","linq","localstorage"],"created_at":"2024-07-31T15:00:46.397Z","updated_at":"2025-03-25T21:32:30.070Z","avatar_url":"https://github.com/chanan.png","language":"C#","funding_links":[],"categories":["Libraries \u0026 Extensions","C\\#"],"sub_categories":["Tools \u0026 Utilities"],"readme":"# BlazorDB\nIn memory, persisted to localstorage, database for .net Blazor browser framework\n\n## Warning\nThis library like Blazor itself is experimental and API is likely to change.\n\n## Breaking change as of V0.7.0\n\nAt this time, you will need to initialize the `Context` prior to using it. I hope that I cna get this done automatically again in a future version:\n\n```\nprotected async override Task OnInitAsync()\n{\n    await Context.Initialize();\n}\n```\n\n## Docs\n\n### Install\n\nFirst add a reference to the nuget package:\n\n[![NuGet Pre Release](https://img.shields.io/nuget/vpre/BlazorDB.svg)](https://www.nuget.org/packages/BlazorDB/)\n\nThen in `Startup.cs` in the method `ConfigureServices` add Blazor DB to the dependency injection services:\n\n```\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddBlazorDB(options =\u003e\n    {\n        options.LogDebug = true;\n        options.Assembly = typeof(Program).Assembly;\n    });\n}\n```\nSet `LogDebug` to see debug output in the browser console.\n\n### Setup\n\n**NOTE:** Models stored by BlazorDB require that an int Id property exist on the model. The Id property will be maintained by BlazorDB, you dont need to set it yourself.\n\nCreate at least one model and context for example:\n\nPerson.cs:\n\n```\npublic class Person\n{\n    public int Id { get; set; }\n    public string FirstName { get; set; }\n    public string LastName { get; set; }\n    public Address HomeAddress { get; set; }\n}\n```\n\nif the field `public int Id { get; set; }` exists it will be managed by BlazorDB\n\nand a context, for example, Context.cs:\n```\npublic class Context : StorageContext\n{\n    public StorageSet\u003cPerson\u003e People { get; set; }\n}\n```\n\n### Usage\n\nSee the full example in the sample app: https://github.com/chanan/BlazorDB/blob/master/src/Sample/Pages/Index.razor\n\nInject your context into your component:\n\n```\n@using Sample.Models\n@inject Context Context\n```\n\nCurrently, as of v0.7.0, before using the `Context` object you must initialize it. Hopefully, this requirement will go away in a future version:\n\n```\nprotected async override Task OnInitAsync()\n{\n    await Context.Initialize();\n}\n```\n\nCreate a model and add it to your Context:\n\n```\nvar person = new Person { FirstName = \"John\", LastName = \"Smith\" };\nContext.People.Add(person);\n```\n\nDo not set the Id field. It will be assigned by BlazorDB.\n\nCall SaveChanges:\n\n```\nContext.SaveChanges();\n```\n\nOnce `SaveChanges()` has been called, you may close the browser and return later, the data will be reloaded from localStorage.\n\nYou may query the data using linq for example:\n\n```\nprivate Person Person { get; set; }\nvoid onclickGetPerson()\n{\n    var query = from person in Context.People\n                where person.Id == 1\n                select person;\n    Person = query.Single();\n    StateHasChanged();\n}\n```\n\n## Associations\n\nAssociations work in the same context. If you have an object in another object that is not in the context, it will be serialized to localStorage as one \"complex\" document.\n\nFor example, in `Context.cs` only Person is in the Context and Address is not. Therefore, Person will contain Address, and Address will not be a seperate object.\n\n### One to One Association\n\nWhen an object refers to another object that are both in Context, they are stored as a reference, such that changing the reference will update both objects.\n\nFor example, `AssociationContext.cs`:\n\n\n```\npublic class AssociationContext : StorageContext\n{\n    public StorageSet\u003cPerson\u003e People { get; set; }\n    public StorageSet\u003cAddress\u003e Addresses { get; set; }\n}\n```\n\n`Person.cs` as shown above has a property `public Address HomeAddress { get; set; }`. Because unlike `Context.cs`, `AssociationContext.cs` does define `public StorageSet\u003cAddress\u003e Addresses { get; set; }` references are stored as \"foreign keys\" instead of complex objects.\n\nTherefore, like in `Associations.cshtml` example, changing the Address will Change the Person's HomeAddress:\n\n```\nContext.People[0].HomeAddress.Street = \"Changed Streeet\";\nContext.SaveChanges();\nConsole.WriteLine(\"Person address changed: {0}\", Context.People[0].HomeAddress.Street);\nConsole.WriteLine(\"Address entity changed as well: {0}\", Context.Addresses[0].Street);\nStateHasChanged();\n```\n### One to Many, Many to Many Association\n\nDefine a \"One\" association by adding a property of the other model. For example in `Person.cs`:\n\n```\npublic Address HomeAddress { get; set; }\n```\n\nDefine a \"Many\" association by adding a property of type `List\u003c\u003e` to the association. For example in `Person.cs`:\n\n```\npublic List\u003cAddress\u003e OtherAddresses { get; set; }\n```\n\nThis is association is then used in `Associations.cshtml` like so:\n\n```\nvar person = new Person { FirstName = \"Many\", LastName = \"Test\" };\nperson.HomeAddress = new Address { Street = \"221 Baker Streeet\", City = \"This should be a refrence to address since Address exists in the context\" };\nvar address1 = new Address { Street = \"Many test 1\", City = \"Saved as a reference\" };\nvar address2 = new Address { Street = \"Many test 2\", City = \"Saved as a reference\" };\nperson.OtherAddresses = new List\u003cAddress\u003e { address1, address2 };\nContext.People.Add(person);\nContext.SaveChanges();\nStateHasChanged();\n```\n\n### Maintaining Associations\n\nAs you can see in the example above BlazorDB will detect associations added to the model so no need to add them to the Context explicitly. In the example above, the address objects do not need to be explicitly added to the context, instead they are persisted when the person object is added and `SaveChanges()` is called.\n\n**Note:** At this time removing/deleting is not done automatically and needs to be done manually. A future update of BlazorDB will handle deletions properly.  \n\n## Validations\n\nYou can annotate your model's propeties with `[Required]` and `[MaxLength(int)]` to enforce required and max length on properties.\n\n## Example\n\nA Todo sample built with BlazorDB is included in the sample project:\n\n* [Todos.razor](https://github.com/chanan/BlazorDB/blob/master/src/Sample/Pages/Todos.razor)\n* [TodoItemForm.razor](https://github.com/chanan/BlazorDB/blob/master/src/Sample/Pages/TodoItemForm.razor)\n\n## Fluxor Integration Example\n\nThe [Fluxor integration example](https://github.com/chanan/BlazorDB/tree/master/src/FluxorIntegration) shows how to use BlazorDB to manage data and Fluxor to connect between the UI and the data layer.\n\n## Storage Format\n\n[Storage Format Doc](https://github.com/chanan/BlazorDB/blob/master/docs/storageFormat.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanan%2FBlazorDB","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchanan%2FBlazorDB","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanan%2FBlazorDB/lists"}