diff --git a/src/Common/Library.fs b/src/Common/Library.fs index 83053be..f51e81a 100644 --- a/src/Common/Library.fs +++ b/src/Common/Library.fs @@ -4,26 +4,37 @@ open System.Security.Cryptography /// The types of comparisons available for JSON fields type Comparison = + /// Equals (=) | Equal of Value: obj + /// Greater Than (>) | Greater of Value: obj + /// Greater Than or Equal To (>=) | GreaterOrEqual of Value: obj + /// Less Than (<) | Less of Value: obj + /// Less Than or Equal To (<=) | LessOrEqual of Value: obj + /// Not Equal to (<>) | NotEqual of Value: obj + /// Between (BETWEEN) | Between of Min: obj * Max: obj + /// In (IN) | In of Values: obj seq + /// In Array (PostgreSQL: |?, SQLite: EXISTS / json_each / IN) | InArray of Table: string * Values: obj seq + /// Exists (IS NOT NULL) | Exists + /// Does Not Exist (IS NULL) | NotExists @@ -53,26 +64,29 @@ type Dialect = /// The format in which an element of a JSON field should be extracted [] type FieldFormat = + /// Use ->> or #>>; extracts a text (PostgreSQL) or SQL (SQLite) value | AsSql + /// Use -> or #>; extracts a JSONB (PostgreSQL) or JSON (SQLite) value | AsJson /// Criteria for a field WHERE clause -type Field = - { /// The name of the field - Name: string +type Field = { - /// The comparison for the field - Comparison: Comparison + /// The name of the field + Name: string - /// The name of the parameter for this field - ParameterName: string option + /// The comparison for the field + Comparison: Comparison - /// The table qualifier for this field - Qualifier: string option } -with + /// The name of the parameter for this field + ParameterName: string option + + /// The table qualifier for this field + Qualifier: string option +} with /// Create a comparison against a field static member Where name (comparison: Comparison) = @@ -190,8 +204,10 @@ with /// How fields should be matched [] type FieldMatch = + /// Any field matches (OR) | Any + /// All fields match (AND) | All @@ -202,6 +218,7 @@ type FieldMatch = /// Derive parameter names (each instance wraps a counter to uniquely name anonymous fields) type ParameterName() = + /// The counter for the next field value let mutable currentIdx = -1 @@ -213,35 +230,30 @@ type ParameterName() = currentIdx <- currentIdx + 1 $"@field{currentIdx}" -#if NET6_0 -open System.Text -#endif /// Automatically-generated document ID strategies [] type AutoId = + /// No automatic IDs will be generated | Disabled + /// Generate a MAX-plus-1 numeric value for documents | Number + /// Generate a GUID for each document (as a lowercase, no-dashes, 32-character string) | Guid + /// Generate a random string of hexadecimal characters for each document | RandomString with /// Generate a GUID string - static member GenerateGuid () = + static member GenerateGuid() = System.Guid.NewGuid().ToString "N" /// Generate a string of random hexadecimal characters - static member GenerateRandomString (length: int) = -#if NET8_0_OR_GREATER + static member GenerateRandomString(length: int) = RandomNumberGenerator.GetHexString(length, lowercase = true) -#else - RandomNumberGenerator.GetBytes((length / 2) + 1) - |> Array.fold (fun (str: StringBuilder) byt -> str.Append(byt.ToString "x2")) (StringBuilder length) - |> function it -> it.Length <- length; it.ToString() -#endif /// Does the given document need an automatic ID generated? static member NeedsAutoId<'T> strategy (document: 'T) idProp = diff --git a/src/Common/README.md b/src/Common/README.md index 7047424..c0107b8 100644 --- a/src/Common/README.md +++ b/src/Common/README.md @@ -7,6 +7,7 @@ This package provides common definitions and functionality for `BitBadger.Docume ## Features - Select, insert, update, save (upsert), delete, count, and check existence of documents, and create tables and indexes for these documents +- Automatically generate IDs for documents (numeric IDs, GUIDs, or random strings) - Addresses documents via ID and via comparison on any field (for PostgreSQL, also via equality on any property by using JSON containment, or via condition on any property using JSON Path queries) - Accesses documents as your domain models (POCOs) - Uses `Task`-based async for all data access functions diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 1b3b103..60cc6e9 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -6,17 +6,14 @@ 4.0.0.0 4.0.0.0 4.0.0 - rc5 From v3.1: (see project site for breaking changes and compatibility) - Change ByField to ByFields - Support dot-access to nested document fields - Add Find*Ordered functions/methods - -Release Candidate Changes: -- from v4-rc4: Field construction functions are now generic. -- from v4-rc3: Add In/InArray field comparisons, revamp internal comparison handling. -- from v4-rc2: preserve additional ORDER BY qualifiers. -- from v4-rc1: add case-insensitive ordering. +- Add case-insensitive ordering (as of rc2) +- Preserve additional ORDER BY qualifiers (as of rc3) +- Add In / InArray comparisons (as of rc4) +- Field construction functions are generic (as of rc5) danieljsummers Bit Badger Solutions README.md diff --git a/src/Postgres/BitBadger.Documents.Postgres.fsproj b/src/Postgres/BitBadger.Documents.Postgres.fsproj index 13f8be2..79ba4a6 100644 --- a/src/Postgres/BitBadger.Documents.Postgres.fsproj +++ b/src/Postgres/BitBadger.Documents.Postgres.fsproj @@ -14,8 +14,8 @@ - - + + diff --git a/src/Postgres/Library.fs b/src/Postgres/Library.fs index 36a4798..39b3882 100644 --- a/src/Postgres/Library.fs +++ b/src/Postgres/Library.fs @@ -3,8 +3,10 @@ /// The type of index to generate for the document [] type DocumentIndex = + /// A GIN index with standard operations (all operators supported) | Full + /// A GIN index with JSONPath operations (optimized for @>, @?, @@ operators) | Optimized @@ -36,6 +38,7 @@ open Npgsql.FSharp /// Helper functions [] module private Helpers = + /// Shorthand to retrieve the data source as SqlProps let internal fromDataSource () = Configuration.dataSource () |> Sql.fromDataSource diff --git a/src/Postgres/README.md b/src/Postgres/README.md index baa1161..8e6ba1c 100644 --- a/src/Postgres/README.md +++ b/src/Postgres/README.md @@ -5,6 +5,7 @@ This package provides a lightweight document library backed by [PostgreSQL](http ## Features - Select, insert, update, save (upsert), delete, count, and check existence of documents, and create tables and indexes for these documents +- Automatically generate IDs for documents (numeric IDs, GUIDs, or random strings) - 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 diff --git a/src/Sqlite/Extensions.fs b/src/Sqlite/Extensions.fs index 52cd5e8..9f143dc 100644 --- a/src/Sqlite/Extensions.fs +++ b/src/Sqlite/Extensions.fs @@ -1,6 +1,5 @@ namespace BitBadger.Documents.Sqlite -open BitBadger.Documents open Microsoft.Data.Sqlite /// F# extensions for the SqliteConnection type @@ -263,9 +262,3 @@ type SqliteConnectionCSharpExtensions = [] static member inline DeleteByFields(conn, tableName, howMatched, fields) = WithConn.Delete.byFields tableName howMatched fields conn - - /// Delete documents by matching a comparison on a JSON field - [] - [] - static member inline DeleteByField(conn, tableName, field) = - conn.DeleteByFields(tableName, Any, [ field ]) diff --git a/src/Sqlite/Library.fs b/src/Sqlite/Library.fs index 34a5652..520f465 100644 --- a/src/Sqlite/Library.fs +++ b/src/Sqlite/Library.fs @@ -153,7 +153,7 @@ module Results = /// Create a domain item from a document, specifying the field in which the document is found [] let fromDocument<'TDoc> field (rdr: SqliteDataReader) : 'TDoc = - Configuration.serializer().Deserialize<'TDoc>(rdr.GetString(rdr.GetOrdinal(field))) + Configuration.serializer().Deserialize<'TDoc>(rdr.GetString(rdr.GetOrdinal field)) /// Create a domain item from a document [] diff --git a/src/Sqlite/README.md b/src/Sqlite/README.md index 6d069d3..7f0651e 100644 --- a/src/Sqlite/README.md +++ b/src/Sqlite/README.md @@ -5,6 +5,7 @@ This package provides a lightweight document library backed by [SQLite](https:// ## Features - Select, insert, update, save (upsert), delete, count, and check existence of documents, and create tables and indexes for these documents +- Automatically generate IDs for documents (numeric IDs, GUIDs, or random strings) - Address documents via ID or via comparison on any field - Access documents as your domain models (POCOs) - Use `Task`-based async for all data access functions @@ -72,28 +73,28 @@ Count customers in Atlanta: ```csharp // C#; parameters are table name, field, operator, and value -// Count.ByField type signature is Func> -var customerCount = await Count.ByField("customer", Field.Equal("City", "Atlanta")); +// Count.ByFields type signature is Func, Task> +var customerCount = await Count.ByFields("customer", FieldMatch.Any, [Field.Equal("City", "Atlanta")]); ``` ```fsharp // F# -// Count.byField type signature is string -> Field -> Task -let! customerCount = Count.byField "customer" (Field.Equal "City" "Atlanta") +// Count.byFields type signature is string -> FieldMatch -> Field seq -> Task +let! customerCount = Count.byFields "customer" Any [ Field.Equal "City" "Atlanta" ] ``` Delete customers in Chicago: _(no offense, Second City; just an example...)_ ```csharp // C#; parameters are same as above, except return is void -// Delete.ByField type signature is Func -await Delete.ByField("customer", Field.Equal("City", "Chicago")); +// Delete.ByFields type signature is Func, Task> +await Delete.ByFields("customer", FieldMatch.Any, [Field.Equal("City", "Chicago")]); ``` ```fsharp // F# -// Delete.byField type signature is string -> string -> Op -> obj -> Task -do! Delete.byField "customer" (Field.Equal "City" "Chicago") +// Delete.byFields type signature is string -> FieldMatch -> Field seq -> Task +do! Delete.byFields "customer" Any [ Field.Equal "City" "Chicago" ] ``` ## More Information