SqlWeave 1.1.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package SqlWeave --version 1.1.0
                    
NuGet\Install-Package SqlWeave -Version 1.1.0
                    
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="SqlWeave" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SqlWeave" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SqlWeave" />
                    
Project file
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 SqlWeave --version 1.1.0
                    
#r "nuget: SqlWeave, 1.1.0"
                    
#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.
#:package SqlWeave@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SqlWeave&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SqlWeave&version=1.1.0
                    
Install as a Cake Tool

SqlWeave

NuGet NuGet License: MIT

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 matching
  • SnakeCase: vehicle_make β†’ VehicleMake
  • CamelCase: vehicleMake β†’ VehicleMake

Type Conversions

Automatic conversions supported:

  • string β†’ enum (by name or numeric value)
  • int β†’ enum
  • DateTime β†’ DateOnly/TimeOnly
  • string β†’ Guid
  • Numeric conversions (int β†’ decimal, float β†’ double)
  • DBNull β†’ null for nullable types

πŸ”§ Requirements

  • .NET 9 or later
  • C# 13 (required for Interceptors)
  • PostgreSQL (with SqlWeave.Npgsql package)

πŸ“– Documentation

πŸ—ΊοΈ 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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