Daniel J. Summers 2c24e2e912 Version 4 rc1 (#6)
Changes in this version:
- **BREAKING CHANGE**: All `*byField`/`*ByField` functions are now `*byFields`/`*ByFields`, and take a `FieldMatch` case before the list of fields. The `Compat` namespace in both libraries will assist in this transition. In support of this change, the `Field` parameter name is optional; the library will generate parameter names for it if they are not specified.
- **BREAKING CHANGE**: The `Query` namespaces have had some significant work, particularly from the full-query perspective. Most have been broken up into the base query and modifiers `by*` that will combine the base query with the `WHERE` clause needed to satisfy the criteria.
- **FEATURE / BREAKING CHANGE**: PostgreSQL document fields will now be cast to numeric if the parameter value passed to the query is numeric. This drove the `Query` breaking changes, as the fields need to have their intended value for the library to generate the appropriate SQL. Additionally, if code assumes the library can be given something like `8` and transform it to `"8"`, this is no longer the case.
- **FEATURE**: All `Find` queries (except `byId`/`ById`) now have a version with the `Ordered` suffix. These take a list of fields by which the query should be ordered. A new `Field` method called `Named` can assist with creating these fields. Prefixing the field name with `n:` will cast the field to numeric in PostgreSQL (and will be ignored by SQLite); adding " DESC" to the field name will sort it descending (Z-A, high to low) instead of ascending (A-Z, low to high).
- **BREAKING CHANGE** (PostgreSQL only): `fieldNameParam`/`Parameters.FieldName` are now plural. The function still only generates one parameter, but the name is now the same between PostgreSQL and SQLite. The goal of this library is to abstract the differences away as much as practical, and this furthers that end. There are functions with these names in the `Compat` namespace.
- **FEATURE**: In the F# v3 library, lists of parameters were expected to be F#'s `List` type, and the C# version took either `List<T>` or `IEnumerable<T>`. In this version, these all expect `seq`/`IEnumerable<T>`. F#'s `List` satisfies the `seq` constraints, so this should not be a breaking change.
- **FEATURE**: `Field`s now may have qualifiers; this allows tables to be aliased when joining multiple tables (as all have the same `data` column). F# users can use `with` to specify this at creation, and both F# and C# can use the `WithQualifier` method to create a field with the qualifier specified. Parameter names for fields may be specified in a similar way, substituting `ParameterName` for `Qualifier`.

Reviewed-on: #6
2024-08-19 23:30:38 +00:00
..
2024-08-19 23:30:38 +00:00
2024-08-19 23:30:38 +00:00
2024-08-19 23:30:38 +00:00
2024-04-20 22:58:41 -04:00

BitBadger.Documents.Postgres

This package provides a lightweight document library backed by PostgreSQL. It also provides streamlined functions for traditional ADO.NET functionality where relational data is required. Both C# and F# have first-class implementations.

Features

  • Select, insert, update, save (upsert), delete, count, and check existence of documents, and create tables and indexes for these documents
  • Address documents via ID, via comparison on any field, via equality on any property (using JSON containment, on a likely indexed field), or via condition on any property (using JSON Path queries)
  • Access documents as your domain models (POCOs)
  • Use Task-based async for all data access functions
  • Use building blocks for more complex queries

Getting Started

Once the package is installed, the library needs a data source. Construct an NpgsqlDataSource instance, and provide it to the library:

// C#
using BitBadger.Documents.Postgres;

//...
// Do not use "using" here; the library will handle disposing this instance
var data = new NpgsqlDataSourceBuilder("connection-string").Build();
Postgres.Configuration.UseDataSource(data);
// F#
open BitBadger.Documents.Postgres

// ...
// Do not use "use" here; the library will handle disposing this instance
let dataSource = // same as above ....

Configuration.useDataSource dataSource
// ...

By default, the library uses a System.Text.Json-based serializer configured to use the FSharp.SystemTextJson converter. To provide a different serializer (different options, more converters, etc.), construct it to implement IDocumentSerializer and provide it via Configuration.useSerializer. If custom serialization makes the serialized Id field not be Id, that will also need to be configured.

Using

Retrieve all customers:

// C#; parameter is table name
// Find.All type signature is Func<string, Task<List<TDoc>>>
var customers = await Find.All<Customer>("customer");
// F#
// Find.all type signature is string -> Task<'TDoc list>
let! customers = Find.all<Customer> "customer"

Select a customer by ID:

// C#; parameters are table name and ID
// Find.ById type signature is Func<string, TKey, Task<TDoc?>>
var customer = await Find.ById<string, Customer>("customer", "123");
// F#
// Find.byId type signature is string -> 'TKey -> Task<'TDoc option>
let! customer = Find.byId<string, Customer> "customer" "123"

(keys are treated as strings in the database)

Count customers in Atlanta (using JSON containment):

// C#; parameters are table name and object for containment query
// Count.ByContains type signature is Func<string, TCriteria, int>
var customerCount = await Count.ByContains("customer", new { City = "Atlanta" });
// F#
// Count.byContains type signature is string -> 'TCriteria -> Task<int>
let! customerCount = Count.byContains "customer" {| City = "Atlanta" |}

Delete customers in Chicago: (no offense, Second City; just an example...)

// C#; parameters are table name and JSON Path expression
// Delete.ByJsonPath type signature is Func<string, string, Task>
await Delete.ByJsonPath("customer", "$.City ? (@ == \"Chicago\")");
// F#
// Delete.byJsonPath type signature is string -> string -> Task<unit>
do! Delete.byJsonPath "customer" """$.City ? (@ == "Chicago")"""

More Information

The project site has full details on how to use this library.