SqlWeave 1.0.1
dotnet add package SqlWeave --version 1.0.1
NuGet\Install-Package SqlWeave -Version 1.0.1
<PackageReference Include="SqlWeave" Version="1.0.1" />
<PackageVersion Include="SqlWeave" Version="1.0.1" />
<PackageReference Include="SqlWeave" />
paket add SqlWeave --version 1.0.1
#r "nuget: SqlWeave, 1.0.1"
#:package SqlWeave@1.0.1
#addin nuget:?package=SqlWeave&version=1.0.1
#tool nuget:?package=SqlWeave&version=1.0.1
SqlWeave
SqlWeave is a C# library that enables mapping and grouping relational data (primarily from SQL queries) to complex typed objects. Inspired by an existing JavaScript function, this library uses Source Generators and Interceptors to generate efficient code that transforms flat data into hierarchical structures.
The name metaphor perfectly reflects its purpose: "weaving" SQL data threads into complex, typed object structures.
β¨ Features
- π High Performance: Uses Source Generators and C# 13 Interceptors for optimized code generation
- π Type Safe: Full compile-time type checking and IntelliSense support
- π― Intuitive API: Familiar syntax inspired by functional programming patterns
- ποΈ Complex Grouping: Support for nested collections and multi-level aggregations
- π§ Flexible: Configurable naming conventions and type conversions
- π Rich Aggregations: Sum, Count, Average, Min, Max with conditional support
π Quick Start
Installation
# Core library
dotnet add package SqlWeave
# PostgreSQL support
dotnet add package SqlWeave.Npgsql
Basic Usage
using Npgsql;
using SqlWeave.Npgsql;
await using var connection = new NpgsqlConnection(connectionString);
var vehicles = await connection.SqlWeave<Vehicle>(@"
SELECT v.id, v.make, v.model, m.date, m.description, m.cost
FROM vehicles v
LEFT JOIN maintenance m ON v.id = m.vehicle_id
WHERE v.make = @make AND v.year >= @year",
new { make = "Toyota", year = 2020 },
(item, agg) => new Vehicle(
Id: agg.Key(item.Id),
Make: item.Make,
Model: item.Model,
TotalMaintenanceCost: agg.Sum(item.Cost),
MaintenanceCount: agg.Count(),
MaintenanceHistory: agg.Items<MaintenanceRecord>(() => new MaintenanceRecord(
Date: agg.Key(item.Date),
Description: item.Description,
Cost: item.Cost
))
));
Model Definition
public record MaintenanceRecord(
DateOnly Date,
string Description,
decimal Cost
);
public record Vehicle(
Guid Id,
string Make,
string Model,
decimal TotalMaintenanceCost,
int MaintenanceCount,
List<MaintenanceRecord> MaintenanceHistory
);
π― Core Concepts
Grouping Keys
// Simple key
Id: agg.Key(item.Id)
// Composite key
GroupKey: agg.Key(item.VehicleId, item.Year)
// Generated key
YearGroup: agg.Key(item => item.Date.Year)
// Skip null handling
Id: agg.Key(item.Id, skipNull: true)
Aggregations
// Numeric aggregations
TotalCost: agg.Sum(item.Cost),
MaintenanceCount: agg.Count(),
AvgCost: agg.Avg(item.Cost),
MinCost: agg.Min(item.Cost),
MaxCost: agg.Max(item.Cost),
// Conditional aggregations
ExpensiveMaintenance: agg.Sum(item.Cost, where: x => x.Cost > 100),
RecentCount: agg.Count(where: x => x.Date > DateTime.Now.AddMonths(-6))
Nested Collections
MaintenanceHistory: agg.Items<MaintenanceRecord>(() => new MaintenanceRecord(
Date: agg.Key(item.Date),
Description: item.Description,
Cost: item.Cost
), skipNull: true)
βοΈ Configuration
Naming Conventions
// Global configuration
SqlWeaveConfig.DefaultNamingConvention = NamingConvention.SnakeCase;
// Per-query configuration
var vehicles = await connection.SqlWeave<Vehicle>(sql, param, transform)
.WithNaming(NamingConvention.SnakeCase);
Supported conventions:
ExactMatch: Exact name matchingSnakeCase:vehicle_makeβVehicleMakeCamelCase:vehicleMakeβVehicleMake
Type Conversions
Automatic conversions supported:
stringβenum(by name or numeric value)intβenumDateTimeβDateOnly/TimeOnlystringβGuid- Numeric conversions (
intβdecimal,floatβdouble) DBNullβnullfor nullable types
π§ Requirements
- .NET 9 or later
- C# 13 (required for Interceptors)
- PostgreSQL (with SqlWeave.Npgsql package)
π Documentation
- Deployment Guide - Complete guide for packaging and publishing
- Technical Documentation - Detailed technical specifications
- Examples - Usage examples and sample projects
πΊοΈ Roadmap
- β Phase 1: Core functionality with Source Generators and Interceptors
- β Phase 2: PostgreSQL integration and basic aggregations
- π Phase 3: Advanced features (streaming, more database providers)
- π Phase 4: Performance optimizations and enterprise features
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
β Support
If you find SqlWeave useful, please consider giving it a star on GitHub! It helps us understand that the project is valuable to the community.
SqlWeave - Weaving SQL data into beautiful, typed objects β¨
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 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. |
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SqlWeave:
| Package | Downloads |
|---|---|
|
SqlWeave.Npgsql
PostgreSQL extension for SqlWeave - provides extension methods for NpgsqlConnection to use SqlWeave functionality. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|