NScript.Storage.LiteDB
8.0.2
dotnet add package NScript.Storage.LiteDB --version 8.0.2
NuGet\Install-Package NScript.Storage.LiteDB -Version 8.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NScript.Storage.LiteDB" Version="8.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NScript.Storage.LiteDB" Version="8.0.2" />
<PackageReference Include="NScript.Storage.LiteDB" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NScript.Storage.LiteDB --version 8.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NScript.Storage.LiteDB, 8.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=NScript.Storage.LiteDB&version=8.0.2
#tool nuget:?package=NScript.Storage.LiteDB&version=8.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NScript.Storage.LiteDB
提供基于 LiteDB 的操作类,方便进行数据库操作,适合实体与实体之间没有复杂关联关系的数据存储。
数据建模
数据建模示例如下,其中,Id 项以 guid 方式存储主键。BookIndexer 的 EnsureIndex 约定了索引项。FileName 项元数据规定了该类实体存储的数据库文件(根据存储策略的不同,也可能是 collection)的名称。
[LiteDBSet(FileName = "books.db", Indexer = typeof(BookIndexer))]
public class Book
{
public class BookIndexer : Indexer<Book>
{
public override void EnsureIndex(ILiteCollection<Book> col)
{
col.EnsureIndex(x => x.Name);
}
}
public String Id { get; set; }
public String Name { get; set; }
}
[LiteDBSet(FileName = "books2.db", Indexer = typeof(Book2Indexer))]
public class Book2
{
public class Book2Indexer : Indexer<Book2>
{
public override void EnsureIndex(ILiteCollection<Book2> col)
{
col.EnsureIndex(x => x.Name);
}
}
public String Id { get; set; }
public String Name { get; set; }
}
存储策略
提供了三类存储策略:
- SingleFileDataService: 所有的实体类存储在一个数据库文件中。实体类存储在 FileName 元数据所指定的 collection 中。
- ShardingSingleFileDataService: 所有的实体类分片存储,每片存储在一个数据库文件中。实体类存储在对应分片的 FileName 元数据所指定的 collection 中。
- MultiFileDataService:每个实体类单独存储在一个数据库文件中。文件名称为 FileName 元数据所指定的名称。
SingleFileDataService 示例
var time = DateTime.Now.ToFileTime();
var service1 = new SingleFileDataService<Book>($"single_{time}","storage_single");
service1.Insert(new Book() { Name = "book1", Id = Guid.NewGuid().ToString() });
var service2 = new SingleFileDataService<Book2>($"single_{time}", "storage_single");
service2.Insert(new Book2() { Name = "book2", Id = Guid.NewGuid().ToString() });
var find = service1.FindOne(x => x.Name == "book1");
bool deleteResult = service1.Delete(find.Id);
var find2 = service2.FindOne(x => x.Name == "book2");
bool deleteResult2 = service2.Delete(find2.Id);
在 storage_single
目录下可以看到文件:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/1/10 17:15 73728 single_133809741190933375.db
数据都存储在该数据库文件中。
ShardingSingleFileDataService 示例
var now = DateTime.Now;
var time = now.ToFileTime();
var service1 = new ShardingSingleFileDataService<Book>($"sharding_{time}", ShardingStrategy.ByDay, now, "storage_sharding");
service1.Insert(new Book() { Name = "book1", Id = Guid.NewGuid().ToString() });
var service2 = new ShardingSingleFileDataService<Book2>($"sharding_{time}", ShardingStrategy.ByDay, now, "storage_sharding");
service2.Insert(new Book2() { Name = "book2", Id = Guid.NewGuid().ToString() });
var find = service1.FindOne(x => x.Name == "book1");
bool deleteResult = service1.Delete(find.Id);
var find2 = service2.FindOne(x => x.Name == "book2");
bool deleteResult2 = service2.Delete(find2.Id);
在 storage_sharding
目录下可以看到文件:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/1/10 16:46 73728 sharding_133809723850606572#d#20250110.db
数据都存储在该数据库文件中。#d 表示是按天分片,20250110 表示分片id是 20250110。
MultiFileDataService 示例
var time = DateTime.Now.ToFileTime();
var service1 = new MultiFileDataService<Book>($"multi_{time}", "storage_multi");
service1.Insert(new Book() { Name = "book1", Id = Guid.NewGuid().ToString() });
var service2 = new MultiFileDataService<Book2>($"multi_{time}", "storage_multi");
service2.Insert(new Book2() { Name = "book2", Id = Guid.NewGuid().ToString() });
var find = service1.FindOne(x => x.Name == "book1");
bool deleteResult = service1.Delete(find.Id);
var find2 = service2.FindOne(x => x.Name == "book2");
bool deleteResult2 = service2.Delete(find2.Id);
在 storage_multi
目录下可以看到文件:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/1/10 16:42 40960 books#multi_133809721266276099.db
-a--- 2025/1/10 16:42 40960 books2#multi_133809721266276099.db
可以看到,不同的类的实例,放在不同的文件中。
数据的更新
数据更新代码示例:
service1.UpdateOne(x => x.Name == "book1", x => {
// update your data
});
并发说明
LiteDB 不支持多线程访问同一个数据库文件,遇到这种情况,请自行加锁。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- LiteDB (>= 5.0.16)
- NScript.Storage (>= 8.0.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.