KDG-Net-Database
1.0.1
dotnet add package KDG-Net-Database --version 1.0.1
NuGet\Install-Package KDG-Net-Database -Version 1.0.1
<PackageReference Include="KDG-Net-Database" Version="1.0.1" />
<PackageVersion Include="KDG-Net-Database" Version="1.0.1" />
<PackageReference Include="KDG-Net-Database" />
paket add KDG-Net-Database --version 1.0.1
#r "nuget: KDG-Net-Database, 1.0.1"
#:package KDG-Net-Database@1.0.1
#addin nuget:?package=KDG-Net-Database&version=1.0.1
#tool nuget:?package=KDG-Net-Database&version=1.0.1
Getting started
This library provides a unified interface for database operations supporting both PostgreSQL and SQL Server.
PostgreSQL
Connecting to the database
- Initialize your database connection
var db = new KDG.Database.PostgreSQL("your-connection-string");
Example connection string:
Host=localhost;Port=5432;Database=mydb;Username=user;Password=password
- Fetch data using
WithConnection
andWithTransaction
var data = await db.WithConnection(async conn => {
var result = await conn.QueryAsync("select * from table");
return result;
});
DML Operations
The KDG.Database.PostgreSQL
class has corresponding insert, update, upsert, delete, and bulk insert methods
- Insert
KDG.Database.Database.PostgreSQL.Insert<T>(
Npgsql.NpgsqlTransaction t,
KDG.Database.DML.InsertConfig<T> data
)
- Update
KDG.Database.Database.PostgreSQL.Update<T>(
Npgsql.NpgsqlTransaction t,
KDG.Database.DML.UpdateConfig<T> data
)
- Upsert
KDG.Database.Database.PostgreSQL.Upsert<T>(
Npgsql.NpgsqlTransaction t,
KDG.Database.DML.UpsertConfig<T> data
)
- Delete
KDG.Database.Database.PostgreSQL.Delete<T>(
Npgsql.NpgsqlTransaction t,
KDG.Database.DML.DeleteConfig<T> data
)
Here's what a full example might look like:
var db = new KDG.Database.PostgreSQL("connection-string");
await db.WithTransaction(async t => {
await db.Insert(
t,
new KDG.Database.DML.InsertConfig<UserModel>{
Table = "your-table",
Data = new UserModel(),
Fields = new Dictionary<string, Func<UserModel, ADbValue>>{
{ "id", x => new DbGuid(x.Id) },
{ "email", x => new DbString(x.Email) }
},
}
);
return true;
});
SQL Server
Connecting to the database
- Initialize your database connection
var db = new KDG.Database.SqlServer("your-connection-string");
Example connection string:
Server=localhost;Database=mydb;User Id=sa;Password=YourPassword;TrustServerCertificate=True
- Fetch data using
WithConnection
andWithTransaction
var data = await db.WithConnection(async conn => {
var result = await conn.QueryAsync("SELECT * FROM [table]");
return result;
});
DML Operations
The KDG.Database.SqlServer
class has corresponding insert, update, upsert (MERGE), delete, and bulk insert methods.
Insert Example
await db.WithTransaction(async transaction => {
await db.Insert(transaction, new InsertConfig<UserModel> {
Table = "Users",
Data = new UserModel { Id = Guid.NewGuid(), Email = "user@example.com" },
Fields = new Dictionary<string, Func<UserModel, ADbValue>> {
{ "id", u => new DbGuid(u.Id) },
{ "email", u => new DbString(u.Email) }
}
});
return true;
});
Upsert Example (MERGE)
await db.WithTransaction(async transaction => {
await db.Upsert(transaction, new UpsertConfig<UserModel> {
Table = "Users",
Data = user,
Key = new Dictionary<string, Func<UserModel, ADbValue>> {
{ "id", u => new DbGuid(u.Id) }
},
Fields = new Dictionary<string, Func<UserModel, ADbValue>> {
{ "id", u => new DbGuid(u.Id) },
{ "email", u => new DbString(u.Email) },
{ "name", u => new DbString(u.Name) }
}
});
return true;
});
Bulk Insert Example (SqlBulkCopy)
var users = GetLargeUserList(); // Returns IEnumerable<UserModel>
await db.WithTransaction(async transaction => {
await db.BulkInsert(transaction, users, new BulkInsertConfig<UserModel> {
Table = "Users",
Fields = new Dictionary<string, Func<UserModel, ADbValue>> {
{ "id", u => new DbGuid(u.Id) },
{ "email", u => new DbString(u.Email) },
{ "name", u => new DbString(u.Name) }
}
});
return true;
});
Supported Data Types
Both PostgreSQL and SQL Server implementations support the following ADbValue
types:
DbString
- Text/NVARCHARDbNumeric
- Numeric/Decimal types (decimal, int, float)DbGuid
- UUID/UNIQUEIDENTIFIERDbBool
- Boolean/BITDbDate
- Date types (NodaTime.LocalDate)DbInstant
- Timestamp types (NodaTime.Instant)DbJson
- JSON/JSONB (PostgreSQL) or NVARCHAR(MAX) (SQL Server)DbFloat
- Real/Float typesDbNullable<T>
- Nullable versions of any type
Support
For support, please open an issue on our GitHub Issues page and provide your questions or feedback. We strive to address all inquiries promptly.
Contributing
To contribute to this project, please follow these steps:
- Fork the repository to your own GitHub account.
- Make your changes and commit them to your fork.
- Submit a pull request to the original repository with a clear description of what your changes do and why they should be included.
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. net10.0 was computed. 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. |
-
net8.0
- Dapper (>= 2.1.35)
- KDG-Net-Common (>= 0.0.3)
- Microsoft.Data.SqlClient (>= 5.2.0)
- Moq (>= 4.20.72)
- NodaTime (>= 3.1.9)
- NodaTime.Serialization.JsonNet (>= 3.0.1)
- NodaTime.Serialization.SystemTextJson (>= 1.0.0)
- Npgsql (>= 8.0.3)
- Npgsql.NodaTime (>= 8.0.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on KDG-Net-Database:
Package | Downloads |
---|---|
KDG-Net-EventSourcing
A library to support tracking mutations to the database |
GitHub repositories
This package is not used by any popular GitHub repositories.