SurrealDb.Embedded.SurrealKv
1.0.0
dotnet add package SurrealDb.Embedded.SurrealKv --version 1.0.0
NuGet\Install-Package SurrealDb.Embedded.SurrealKv -Version 1.0.0
<PackageReference Include="SurrealDb.Embedded.SurrealKv" Version="1.0.0" />
<PackageVersion Include="SurrealDb.Embedded.SurrealKv" Version="1.0.0" />
<PackageReference Include="SurrealDb.Embedded.SurrealKv" />
paket add SurrealDb.Embedded.SurrealKv --version 1.0.0
#r "nuget: SurrealDb.Embedded.SurrealKv, 1.0.0"
#:package SurrealDb.Embedded.SurrealKv@1.0.0
#addin nuget:?package=SurrealDb.Embedded.SurrealKv&version=1.0.0
#tool nuget:?package=SurrealDb.Embedded.SurrealKv&version=1.0.0
surrealdb.net
File provider of the official SurrealDB SDK for .NET, backed by SurrealKV.
Documentation
View the SDK documentation here.
How to install
dotnet add package SurrealDb.Embedded.SurrealKv
Getting started
Constructing a new SurrealDB client
You can easily create a new SurrealDB client which will provide a file instance of SurrealDB, backed by SurrealKV.
using var db = new SurrealDbKvClient("data.db");
const string TABLE = "person";
var person = new Person
{
Title = "Founder & CEO",
Name = new() { FirstName = "Tobie", LastName = "Morgan Hitchcock" },
Marketing = true
};
var created = await db.Create(TABLE, person);
Console.WriteLine(ToJsonString(created));
Dependency injection
You can use Dependency Injection with the services.AddSurreal() and .AddSurrealKvProvider() functions.
Default instance
services.
.AddSurreal("Endpoint=surrealkv://data.db")
.AddSurrealKvProvider();
Then you will be able to use the ISurrealDbClient interface or SurrealDbClient class anywhere.
public class MyClass
{
private readonly ISurrealDbClient _client;
public MyClass(ISurrealDbClient client)
{
_client = client;
}
// ...
}
Note that the default lifetime of this service is Scoped. You can override this as follows:
services.AddSurreal(options, ServiceLifetime.Scoped);
Connection String
Consider the following appsettings.json file:
{
"AllowedHosts": "*",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"SurrealDB": "Endpoint=surrealkv://data.db;Namespace=test;Database=test"
}
}
You can use the Connection String instead of having to deal with a SurrealDbOptions.
services
.AddSurreal(configuration.GetConnectionString("SurrealDB"))
.AddSurrealKvProvider();
It will automatically create a new SurrealDB using the Client endpoint and configure the client using the different values for namespace, database. Note that these values are optional but the endpoint is still required.
Multiple instances
Having a default instance for a project is enough most of the time, but there may be times when you'd like to target multiple SurrealDB instances, either at different addresses or at the same address but inside different NS/DBs. You can use multiple instances as long as you provide 1 interface per client, as in the following example.
interface IBackupSurrealDbClient : ISurrealDbClient { }
interface IMonitoringSurrealDbClient : ISurrealDbClient { }
services
.AddSurreal(configuration.GetConnectionString("SurrealDB.Main"))
.AddSurrealKvProvider();
services.AddSurreal<IBackupSurrealDbClient>(configuration.GetConnectionString("SurrealDB.Backup"));
services.AddSurreal<IMonitoringSurrealDbClient>(configuration.GetConnectionString("SurrealDB.Monitoring"));
Here you will have 3 instances:
- the default one using the memory provider, you can keep using
ISurrealDbClientinterface orSurrealDbClientclass anywhere - a remote client for backup purpose, using the
IBackupSurrealDbClientinterface - a remote client for monitoring purpose, using the
IMonitoringSurrealDbClientinterface
Use the client
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private const string Table = "weatherForecast";
private readonly ISurrealDbClient _surrealDbClient;
public WeatherForecastController(ISurrealDbClient surrealDbClient)
{
_surrealDbClient = surrealDbClient;
}
[HttpGet]
public Task<IEnumerable<WeatherForecast>> GetAll(CancellationToken cancellationToken)
{
return _surrealDbClient.Select<WeatherForecast>(Table, cancellationToken);
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(string id, CancellationToken cancellationToken)
{
var weatherForecast = await _surrealDbClient.Select<WeatherForecast>((Table, id), cancellationToken);
if (weatherForecast is null)
return NotFound();
return Ok(weatherForecast);
}
[HttpPost]
public Task<WeatherForecast> Create(CreateWeatherForecast data, CancellationToken cancellationToken)
{
var weatherForecast = new WeatherForecast
{
Date = data.Date,
Country = data.Country,
TemperatureC = data.TemperatureC,
Summary = data.Summary
};
return _surrealDbClient.Create(Table, weatherForecast, cancellationToken);
}
[HttpPut]
public Task<WeatherForecast> Update(WeatherForecast data, CancellationToken cancellationToken)
{
return _surrealDbClient.Upsert(data, cancellationToken);
}
[HttpPatch]
public Task<IEnumerable<WeatherForecast>> PatchAll(
JsonPatchDocument<WeatherForecast> patches,
CancellationToken cancellationToken
)
{
return _surrealDbClient.PatchAll(Table, patches, cancellationToken);
}
[HttpPatch("{id}")]
public Task<WeatherForecast> Patch(
string id,
JsonPatchDocument<WeatherForecast> patches,
CancellationToken cancellationToken
)
{
return _surrealDbClient.Patch((Table, id), patches, cancellationToken);
}
[HttpDelete]
public Task DeleteAll(CancellationToken cancellationToken)
{
return _surrealDbClient.Delete(Table, cancellationToken);
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(string id, CancellationToken cancellationToken)
{
bool success = await _surrealDbClient.Delete((Table, id), cancellationToken);
if (!success)
return NotFound();
return Ok();
}
}
| 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 is compatible. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ConcurrentHashSet (>= 1.3.0)
- Dahomey.Cbor (>= 1.26.3)
- Microsoft.AspNetCore.JsonPatch.SystemTextJson (>= 10.0.7)
- Microsoft.Extensions.Http (>= 10.0.7)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Spatial (>= 7.22.0)
- Semver (>= 3.0.0)
- SurrealDb.Net (>= 1.0.0)
- Websocket.Client (>= 5.3.0)
-
net8.0
- ConcurrentHashSet (>= 1.3.0)
- Dahomey.Cbor (>= 1.26.3)
- Microsoft.Extensions.Http (>= 10.0.7)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Spatial (>= 7.22.0)
- Semver (>= 3.0.0)
- SurrealDb.Net (>= 1.0.0)
- System.Collections.Immutable (>= 10.0.7)
- System.Linq.AsyncEnumerable (>= 10.0.7)
- SystemTextJsonPatch (>= 5.0.0)
- Websocket.Client (>= 5.3.0)
-
net9.0
- ConcurrentHashSet (>= 1.3.0)
- Dahomey.Cbor (>= 1.26.3)
- Microsoft.Extensions.Http (>= 10.0.7)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.1)
- Microsoft.Spatial (>= 7.22.0)
- Semver (>= 3.0.0)
- SurrealDb.Net (>= 1.0.0)
- System.Collections.Immutable (>= 10.0.7)
- System.Linq.AsyncEnumerable (>= 10.0.7)
- SystemTextJsonPatch (>= 5.0.0)
- Websocket.Client (>= 5.3.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on SurrealDb.Embedded.SurrealKv:
| Package | Downloads |
|---|---|
|
Aero.Cms.AppServer
Package Description |
|
|
AeroDB.Sable.Configuration
Encrypted .NET configuration provider backed by embedded SurrealDB SurrealKV |
GitHub repositories
This package is not used by any popular GitHub repositories.