|
1 | | -# BLite.Client.Net |
| 1 | +# BLite.Client |
| 2 | + |
| 3 | +Official .NET SDK for BLite Server over gRPC. |
| 4 | + |
| 5 | +## What You Get |
| 6 | + |
| 7 | +- Typed collections (`IDocumentCollection<TId, T>`) |
| 8 | +- Dynamic BSON collections |
| 9 | +- LINQ query push-down |
| 10 | +- Transactions |
| 11 | +- Key-value API |
| 12 | +- Admin API (users and tenants) |
| 13 | + |
| 14 | +## Install |
| 15 | + |
| 16 | +```bash |
| 17 | +dotnet add package BLite.Client |
| 18 | +``` |
| 19 | + |
| 20 | +## 1. Connect To BLite Server |
| 21 | + |
| 22 | +```csharp |
| 23 | +using BLite.Client; |
| 24 | + |
| 25 | +await using var client = new BLiteClient(new BLiteClientOptions |
| 26 | +{ |
| 27 | + Host = "localhost", |
| 28 | + Port = 2626, |
| 29 | + ApiKey = "your-api-key", |
| 30 | + UseTls = false |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +`Port` must point to the gRPC endpoint (`2626` by default). |
| 35 | + |
| 36 | +## 2. Typed Collections (Recommended) |
| 37 | + |
| 38 | +```csharp |
| 39 | +using BLite.Bson; |
| 40 | + |
| 41 | +[DocumentMapper("users")] |
| 42 | +public class User |
| 43 | +{ |
| 44 | + [BsonId] |
| 45 | + public ObjectId Id { get; set; } |
| 46 | + |
| 47 | + public string Name { get; set; } = string.Empty; |
| 48 | + public int Age { get; set; } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Use the generated mapper (for example `UserMapper`) to get a typed collection: |
| 53 | + |
| 54 | +```csharp |
| 55 | +var users = client.GetDocumentCollection<ObjectId, User>(new UserMapper()); |
| 56 | + |
| 57 | +await users.InsertAsync(new User |
| 58 | +{ |
| 59 | + Id = ObjectId.NewObjectId(), |
| 60 | + Name = "Alice", |
| 61 | + Age = 30 |
| 62 | +}); |
| 63 | + |
| 64 | +var adults = await users.AsQueryable() |
| 65 | + .Where(x => x.Age >= 18) |
| 66 | + .OrderBy(x => x.Name) |
| 67 | + .ToListAsync(); |
| 68 | +``` |
| 69 | + |
| 70 | +## 3. Dynamic BSON Collections |
| 71 | + |
| 72 | +```csharp |
| 73 | +using BLite.Bson; |
| 74 | +using BLite.Proto; |
| 75 | + |
| 76 | +var sensors = client.GetDynamicCollection("sensors"); |
| 77 | + |
| 78 | +var doc = await sensors.NewDocumentAsync(["name", "temp"], b => b |
| 79 | + .AddString("name", "room-1") |
| 80 | + .AddDouble("temp", 21.5)); |
| 81 | + |
| 82 | +_ = await sensors.InsertAsync(doc); |
| 83 | + |
| 84 | +var q = new QueryDescriptor |
| 85 | +{ |
| 86 | + Collection = "sensors", |
| 87 | + Where = new BinaryFilter |
| 88 | + { |
| 89 | + Field = "temp", |
| 90 | + Op = FilterOp.Gt, |
| 91 | + Value = ScalarValue.From(20) |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +await foreach (var item in sensors.QueryAsync(q)) |
| 96 | +{ |
| 97 | + // process item |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## 4. Transactions |
| 102 | + |
| 103 | +```csharp |
| 104 | +await using var tx = await client.BeginTransactionAsync(); |
| 105 | + |
| 106 | +await users.InsertAsync(new User |
| 107 | +{ |
| 108 | + Id = ObjectId.NewObjectId(), |
| 109 | + Name = "Bob", |
| 110 | + Age = 41 |
| 111 | +}, tx); |
| 112 | + |
| 113 | +await tx.CommitAsync(); |
| 114 | +``` |
| 115 | + |
| 116 | +## 5. Key-Value API |
| 117 | + |
| 118 | +```csharp |
| 119 | +using System.Text; |
| 120 | + |
| 121 | +await client.Kv.SetAsync("session:abc", Encoding.UTF8.GetBytes("payload")); |
| 122 | +var value = await client.Kv.GetAsync("session:abc"); |
| 123 | +``` |
| 124 | + |
| 125 | +## 6. Admin API |
| 126 | + |
| 127 | +```csharp |
| 128 | +using BLite.Core.Query; |
| 129 | + |
| 130 | +var createdKey = await client.Admin.CreateUserAsync( |
| 131 | + username: "reporting", |
| 132 | + namespaceName: null, |
| 133 | + databaseId: null, |
| 134 | + permissions: |
| 135 | + [ |
| 136 | + new UserPermission |
| 137 | + { |
| 138 | + Collection = "reports", |
| 139 | + Ops = BLiteOperation.Query |
| 140 | + } |
| 141 | + ]); |
| 142 | +``` |
| 143 | + |
| 144 | +## Notes |
| 145 | + |
| 146 | +- BLite Server must be reachable on gRPC endpoint. |
| 147 | +- In production prefer `UseTls = true` and secure secret handling for API keys. |
| 148 | +- Unsupported LINQ operators are applied client-side after streaming. |
| 149 | + |
| 150 | +## Links |
| 151 | + |
| 152 | +- BLite Server: https://github.com/EntglDb/BLite.Server |
| 153 | +- BLite engine: https://github.com/EntglDb/BLite |
2 | 154 |
|
0 commit comments