Integrate PostgreSQL with common lib
- Add "byField" functions to Postgres impl
This commit is contained in:
parent
d02ea638bc
commit
8d3385b70b
|
@ -210,18 +210,6 @@ module Query =
|
||||||
let full tableName =
|
let full tableName =
|
||||||
$"""UPDATE %s{tableName} SET data = @data WHERE {whereById "@id"}"""
|
$"""UPDATE %s{tableName} SET data = @data WHERE {whereById "@id"}"""
|
||||||
|
|
||||||
/// Query to update a partial document by its ID
|
|
||||||
[<CompiledName "PartialById">]
|
|
||||||
let partialById tableName =
|
|
||||||
$"""UPDATE %s{tableName} SET data = json_patch(data, json(@data)) WHERE {whereById "@id"}"""
|
|
||||||
|
|
||||||
/// Query to update a partial document via a comparison on a JSON field
|
|
||||||
[<CompiledName "PartialByField">]
|
|
||||||
let partialByField tableName fieldName op =
|
|
||||||
sprintf
|
|
||||||
"UPDATE %s SET data = json_patch(data, json(@data)) WHERE %s"
|
|
||||||
tableName (whereByField fieldName op "@field")
|
|
||||||
|
|
||||||
/// Queries to delete documents
|
/// Queries to delete documents
|
||||||
module Delete =
|
module Delete =
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
namespace BitBadger.Documents.Postgres
|
namespace BitBadger.Documents.Postgres
|
||||||
|
|
||||||
|
open Npgsql
|
||||||
|
|
||||||
/// The type of index to generate for the document
|
/// The type of index to generate for the document
|
||||||
[<Struct>]
|
[<Struct>]
|
||||||
type DocumentIndex =
|
type DocumentIndex =
|
||||||
|
@ -91,14 +93,20 @@ module Definition =
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Query =
|
module Query =
|
||||||
|
|
||||||
/// Create a SELECT clause to retrieve the document data from the given table
|
module Definition =
|
||||||
let selectFromTable tableName =
|
|
||||||
$"SELECT data FROM %s{tableName}"
|
/// SQL statement to create a document table
|
||||||
|
[<CompiledName "EnsureTable">]
|
||||||
/// Create a WHERE clause fragment to implement an ID-based query
|
let ensureTable name =
|
||||||
let whereById paramName =
|
Query.Definition.ensureTableFor name "JSONB"
|
||||||
$"data ->> '{Configuration.idField ()}' = %s{paramName}"
|
|
||||||
|
/// SQL statement to create an index on JSON documents in the specified table
|
||||||
|
[<CompiledName "EnsureJsonIndex">]
|
||||||
|
let ensureJsonIndex (name : string) idxType =
|
||||||
|
let extraOps = match idxType with Full -> "" | Optimized -> " jsonb_path_ops"
|
||||||
|
let tableName = name.Split '.' |> Array.last
|
||||||
|
$"CREATE INDEX IF NOT EXISTS idx_{tableName} ON {name} USING GIN (data{extraOps})"
|
||||||
|
|
||||||
/// Create a WHERE clause fragment to implement a @> (JSON contains) condition
|
/// Create a WHERE clause fragment to implement a @> (JSON contains) condition
|
||||||
let whereDataContains paramName =
|
let whereDataContains paramName =
|
||||||
$"data @> %s{paramName}"
|
$"data @> %s{paramName}"
|
||||||
|
@ -127,10 +135,6 @@ module Query =
|
||||||
/// Queries for counting documents
|
/// Queries for counting documents
|
||||||
module Count =
|
module Count =
|
||||||
|
|
||||||
/// Query to count all documents in a table
|
|
||||||
let all tableName =
|
|
||||||
$"SELECT COUNT(*) AS it FROM %s{tableName}"
|
|
||||||
|
|
||||||
/// Query to count matching documents using a JSON containment query (@>)
|
/// Query to count matching documents using a JSON containment query (@>)
|
||||||
let byContains tableName =
|
let byContains tableName =
|
||||||
$"""SELECT COUNT(*) AS it FROM %s{tableName} WHERE {whereDataContains "@criteria"}"""
|
$"""SELECT COUNT(*) AS it FROM %s{tableName} WHERE {whereDataContains "@criteria"}"""
|
||||||
|
@ -142,10 +146,6 @@ module Query =
|
||||||
/// Queries for determining document existence
|
/// Queries for determining document existence
|
||||||
module Exists =
|
module Exists =
|
||||||
|
|
||||||
/// Query to determine if a document exists for the given ID
|
|
||||||
let byId tableName =
|
|
||||||
$"""SELECT EXISTS (SELECT 1 FROM %s{tableName} WHERE {whereById "@id"}) AS it"""
|
|
||||||
|
|
||||||
/// Query to determine if documents exist using a JSON containment query (@>)
|
/// Query to determine if documents exist using a JSON containment query (@>)
|
||||||
let byContains tableName =
|
let byContains tableName =
|
||||||
$"""SELECT EXISTS (SELECT 1 FROM %s{tableName} WHERE {whereDataContains "@criteria"}) AS it"""
|
$"""SELECT EXISTS (SELECT 1 FROM %s{tableName} WHERE {whereDataContains "@criteria"}) AS it"""
|
||||||
|
@ -157,28 +157,24 @@ module Query =
|
||||||
/// Queries for retrieving documents
|
/// Queries for retrieving documents
|
||||||
module Find =
|
module Find =
|
||||||
|
|
||||||
/// Query to retrieve a document by its ID
|
|
||||||
let byId tableName =
|
|
||||||
$"""{selectFromTable tableName} WHERE {whereById "@id"}"""
|
|
||||||
|
|
||||||
/// Query to retrieve documents using a JSON containment query (@>)
|
/// Query to retrieve documents using a JSON containment query (@>)
|
||||||
let byContains tableName =
|
let byContains tableName =
|
||||||
$"""{selectFromTable tableName} WHERE {whereDataContains "@criteria"}"""
|
$"""{Query.selectFromTable tableName} WHERE {whereDataContains "@criteria"}"""
|
||||||
|
|
||||||
/// Query to retrieve documents using a JSON Path match (@?)
|
/// Query to retrieve documents using a JSON Path match (@?)
|
||||||
let byJsonPath tableName =
|
let byJsonPath tableName =
|
||||||
$"""{selectFromTable tableName} WHERE {whereJsonPathMatches "@path"}"""
|
$"""{Query.selectFromTable tableName} WHERE {whereJsonPathMatches "@path"}"""
|
||||||
|
|
||||||
/// Queries to update documents
|
/// Queries to update documents
|
||||||
module Update =
|
module Update =
|
||||||
|
|
||||||
/// Query to update a document
|
|
||||||
let full tableName =
|
|
||||||
$"""UPDATE %s{tableName} SET data = @data WHERE {whereById "@id"}"""
|
|
||||||
|
|
||||||
/// Query to update a document
|
/// Query to update a document
|
||||||
let partialById tableName =
|
let partialById tableName =
|
||||||
$"""UPDATE %s{tableName} SET data = data || @data WHERE {whereById "@id"}"""
|
$"""UPDATE %s{tableName} SET data = data || @data WHERE {Query.whereById "@id"}"""
|
||||||
|
|
||||||
|
/// Query to update a document
|
||||||
|
let partialByField tableName fieldName op =
|
||||||
|
$"""UPDATE %s{tableName} SET data = data || @data WHERE {Query.whereByField fieldName op "@field"}"""
|
||||||
|
|
||||||
/// Query to update partial documents matching a JSON containment query (@>)
|
/// Query to update partial documents matching a JSON containment query (@>)
|
||||||
let partialByContains tableName =
|
let partialByContains tableName =
|
||||||
|
@ -191,10 +187,6 @@ module Query =
|
||||||
/// Queries to delete documents
|
/// Queries to delete documents
|
||||||
module Delete =
|
module Delete =
|
||||||
|
|
||||||
/// Query to delete a document by its ID
|
|
||||||
let byId tableName =
|
|
||||||
$"""DELETE FROM %s{tableName} WHERE {whereById "@id"}"""
|
|
||||||
|
|
||||||
/// Query to delete documents using a JSON containment query (@>)
|
/// Query to delete documents using a JSON containment query (@>)
|
||||||
let byContains tableName =
|
let byContains tableName =
|
||||||
$"""DELETE FROM %s{tableName} WHERE {whereDataContains "@criteria"}"""
|
$"""DELETE FROM %s{tableName} WHERE {whereDataContains "@criteria"}"""
|
||||||
|
@ -204,6 +196,31 @@ module Query =
|
||||||
$"""DELETE FROM %s{tableName} WHERE {whereJsonPathMatches "@path"}"""
|
$"""DELETE FROM %s{tableName} WHERE {whereJsonPathMatches "@path"}"""
|
||||||
|
|
||||||
|
|
||||||
|
/// Functions for creating parameters
|
||||||
|
[<AutoOpen>]
|
||||||
|
module Parameters =
|
||||||
|
|
||||||
|
/// Create an ID parameter (name "@id", key will be treated as a string)
|
||||||
|
[<CompiledName "Id">]
|
||||||
|
let idParam (key: 'TKey) =
|
||||||
|
"@id", Sql.string (string key)
|
||||||
|
|
||||||
|
/// Create a parameter with a JSON value
|
||||||
|
[<CompiledName "Json">]
|
||||||
|
let jsonParam (name: string) (it: 'TJson) =
|
||||||
|
name, Sql.jsonb (Configuration.serializer().Serialize it)
|
||||||
|
|
||||||
|
/// Create a JSON field parameter (name "@field")
|
||||||
|
[<CompiledName "Field">]
|
||||||
|
let fieldParam (value: obj) =
|
||||||
|
"@field", Sql.parameter (NpgsqlParameter("@field", value))
|
||||||
|
|
||||||
|
/// An empty parameter sequence
|
||||||
|
[<CompiledName "None">]
|
||||||
|
let noParams =
|
||||||
|
Seq.empty<string * SqlValue>
|
||||||
|
|
||||||
|
|
||||||
/// Functions for dealing with results
|
/// Functions for dealing with results
|
||||||
[<AutoOpen>]
|
[<AutoOpen>]
|
||||||
module Results =
|
module Results =
|
||||||
|
@ -215,11 +232,69 @@ module Results =
|
||||||
/// Create a domain item from a document
|
/// Create a domain item from a document
|
||||||
let fromData<'T> row : 'T =
|
let fromData<'T> row : 'T =
|
||||||
fromDocument "data" row
|
fromDocument "data" row
|
||||||
|
|
||||||
|
/// Extract a count from the column "it"
|
||||||
|
let toCount (row: RowReader) =
|
||||||
|
row.int "it"
|
||||||
|
|
||||||
|
/// Extract a true/false value from the column "it"
|
||||||
|
let toExists (row: RowReader) =
|
||||||
|
row.bool "it"
|
||||||
|
|
||||||
|
|
||||||
/// Versions of queries that accept SqlProps as the last parameter
|
/// Versions of queries that accept SqlProps as the last parameter
|
||||||
module WithProps =
|
module WithProps =
|
||||||
|
|
||||||
|
/// Commands to execute custom SQL queries
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Custom =
|
||||||
|
|
||||||
|
/// Execute a query that returns a list of results
|
||||||
|
[<CompiledName "FSharpList">]
|
||||||
|
let list<'T> query parameters (mapFunc: RowReader -> 'T) sqlProps =
|
||||||
|
Sql.query query sqlProps
|
||||||
|
|> Sql.parameters parameters
|
||||||
|
|> Sql.executeAsync mapFunc
|
||||||
|
|
||||||
|
/// Execute a query that returns a list of results
|
||||||
|
let List<'T>(query, parameters, mapFunc: System.Func<RowReader, 'T>, sqlProps) = backgroundTask {
|
||||||
|
let! results = list query (List.ofSeq parameters) mapFunc.Invoke sqlProps
|
||||||
|
return ResizeArray results
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute a query that returns one or no results (returns None if not found)
|
||||||
|
[<CompiledName "FSharpSingle">]
|
||||||
|
let single<'T> query parameters mapFunc sqlProps = backgroundTask {
|
||||||
|
let! results = list<'T> query parameters mapFunc sqlProps
|
||||||
|
return FSharp.Collections.List.tryHead results
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute a query that returns one or no results (returns null if not found)
|
||||||
|
let Single<'T when 'T: null>(
|
||||||
|
query, parameters, mapFunc: System.Func<RowReader, 'T>, sqlProps) = backgroundTask {
|
||||||
|
let! result = single<'T> query (FSharp.Collections.List.ofSeq parameters) mapFunc.Invoke sqlProps
|
||||||
|
return Option.toObj result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute a query that returns no results
|
||||||
|
[<CompiledName "NonQuery">]
|
||||||
|
let nonQuery query parameters sqlProps =
|
||||||
|
Sql.query query sqlProps
|
||||||
|
|> Sql.parameters (FSharp.Collections.List.ofSeq parameters)
|
||||||
|
|> Sql.executeNonQueryAsync
|
||||||
|
|> ignoreTask
|
||||||
|
|
||||||
|
/// Execute a query that returns a scalar value
|
||||||
|
[<CompiledName "FSharpScalar">]
|
||||||
|
let scalar<'T when 'T: struct> query parameters (mapFunc: RowReader -> 'T) sqlProps =
|
||||||
|
Sql.query query sqlProps
|
||||||
|
|> Sql.parameters parameters
|
||||||
|
|> Sql.executeRowAsync mapFunc
|
||||||
|
|
||||||
|
/// Execute a query that returns a scalar value
|
||||||
|
let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func<RowReader, 'T>, sqlProps) =
|
||||||
|
scalar<'T> query (FSharp.Collections.List.ofSeq parameters) mapFunc.Invoke sqlProps
|
||||||
|
|
||||||
/// Execute a non-query statement to manipulate a document
|
/// Execute a non-query statement to manipulate a document
|
||||||
let private executeNonQuery query (document: 'T) sqlProps =
|
let private executeNonQuery query (document: 'T) sqlProps =
|
||||||
sqlProps
|
sqlProps
|
||||||
|
@ -236,208 +311,283 @@ module WithProps =
|
||||||
|> Sql.executeNonQueryAsync
|
|> Sql.executeNonQueryAsync
|
||||||
|> ignoreTask
|
|> ignoreTask
|
||||||
|
|
||||||
/// Insert a new document
|
/// Commands to add documents
|
||||||
let insert<'T> tableName (document: 'T) sqlProps =
|
[<AutoOpen>]
|
||||||
executeNonQuery (Query.insert tableName) document sqlProps
|
module Document =
|
||||||
|
|
||||||
|
/// Insert a new document
|
||||||
|
[<CompiledName "Insert">]
|
||||||
|
let insert<'TDoc> tableName (document: 'TDoc) sqlProps =
|
||||||
|
Custom.nonQuery (Query.insert tableName) [ jsonParam "@data" document ] sqlProps
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
let save<'T> tableName (document: 'T) sqlProps =
|
[<CompiledName "Save">]
|
||||||
executeNonQuery (Query.save tableName) document sqlProps
|
let save<'TDoc> tableName (document: 'TDoc) sqlProps =
|
||||||
|
Custom.nonQuery (Query.save tableName) [ jsonParam "@data" document ] sqlProps
|
||||||
|
|
||||||
/// Commands to count documents
|
/// Commands to count documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Count =
|
module Count =
|
||||||
|
|
||||||
/// Count all documents in a table
|
/// Count all documents in a table
|
||||||
|
[<CompiledName "All">]
|
||||||
let all tableName sqlProps =
|
let all tableName sqlProps =
|
||||||
sqlProps
|
Custom.scalar (Query.Count.all tableName) [] toCount sqlProps
|
||||||
|> Sql.query (Query.Count.all tableName)
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.int "it")
|
/// Count matching documents using a JSON field comparison (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) sqlProps =
|
||||||
|
Custom.scalar (Query.Count.byField tableName fieldName op) [ fieldParam value ] toCount sqlProps
|
||||||
|
|
||||||
/// Count matching documents using a JSON containment query (@>)
|
/// Count matching documents using a JSON containment query (@>)
|
||||||
let byContains tableName (criteria: obj) sqlProps =
|
[<CompiledName "ByContains">]
|
||||||
sqlProps
|
let byContains tableName (criteria: 'TContains) sqlProps =
|
||||||
|> Sql.query (Query.Count.byContains tableName)
|
Custom.scalar (Query.Count.byContains tableName) [ jsonParam "@criteria" criteria ] toCount sqlProps
|
||||||
|> Sql.parameters [ "@criteria", Query.jsonbDocParam criteria ]
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.int "it")
|
|
||||||
|
|
||||||
/// Count matching documents using a JSON Path match query (@?)
|
/// Count matching documents using a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName jsonPath sqlProps =
|
let byJsonPath tableName jsonPath sqlProps =
|
||||||
sqlProps
|
Custom.scalar (Query.Count.byJsonPath tableName) [ "@path", Sql.string jsonPath ] toCount sqlProps
|
||||||
|> Sql.query (Query.Count.byJsonPath tableName)
|
|
||||||
|> Sql.parameters [ "@path", Sql.string jsonPath ]
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.int "it")
|
|
||||||
|
|
||||||
/// Commands to determine if documents exist
|
/// Commands to determine if documents exist
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Exists =
|
module Exists =
|
||||||
|
|
||||||
/// Determine if a document exists for the given ID
|
/// Determine if a document exists for the given ID
|
||||||
let byId tableName docId sqlProps =
|
[<CompiledName "ById">]
|
||||||
sqlProps
|
let byId tableName (docId: 'TKey) sqlProps =
|
||||||
|> Sql.query (Query.Exists.byId tableName)
|
Custom.scalar (Query.Exists.byId tableName) [ idParam docId ] toExists sqlProps
|
||||||
|> Sql.parameters [ "@id", Sql.string docId ]
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.bool "it")
|
|
||||||
|
|
||||||
|
/// Determine if a document exists using a JSON field comparison (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) sqlProps =
|
||||||
|
Custom.scalar (Query.Exists.byField tableName fieldName op) [ fieldParam value ] toExists sqlProps
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON containment query (@>)
|
/// Determine if a document exists using a JSON containment query (@>)
|
||||||
let byContains tableName (criteria: obj) sqlProps =
|
[<CompiledName "ByContains">]
|
||||||
sqlProps
|
let byContains tableName (criteria: 'TContains) sqlProps =
|
||||||
|> Sql.query (Query.Exists.byContains tableName)
|
Custom.scalar (Query.Exists.byContains tableName) [ jsonParam "@criteria" criteria ] toExists sqlProps
|
||||||
|> Sql.parameters [ "@criteria", Query.jsonbDocParam criteria ]
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.bool "it")
|
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON Path match query (@?)
|
/// Determine if a document exists using a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName jsonPath sqlProps =
|
let byJsonPath tableName jsonPath sqlProps =
|
||||||
sqlProps
|
Custom.scalar (Query.Exists.byJsonPath tableName) [ "@path", Sql.string jsonPath ] toExists sqlProps
|
||||||
|> Sql.query (Query.Exists.byJsonPath tableName)
|
|
||||||
|> Sql.parameters [ "@path", Sql.string jsonPath ]
|
|
||||||
|> Sql.executeRowAsync (fun row -> row.bool "it")
|
|
||||||
|
|
||||||
/// Commands to determine if documents exist
|
/// Commands to determine if documents exist
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Find =
|
module Find =
|
||||||
|
|
||||||
/// Retrieve all documents in the given table
|
/// Retrieve all documents in the given table
|
||||||
let all<'T> tableName sqlProps =
|
[<CompiledName "FSharpAll">]
|
||||||
sqlProps
|
let all<'TDoc> tableName sqlProps =
|
||||||
|> Sql.query (Query.selectFromTable tableName)
|
Custom.list<'TDoc> (Query.selectFromTable tableName) [] fromData<'TDoc> sqlProps
|
||||||
|> Sql.executeAsync fromData<'T>
|
|
||||||
|
|
||||||
/// Retrieve a document by its ID
|
/// Retrieve all documents in the given table
|
||||||
let byId<'T> tableName docId sqlProps = backgroundTask {
|
let All<'TDoc>(tableName, sqlProps) =
|
||||||
let! results =
|
Custom.List<'TDoc>(Query.selectFromTable tableName, [], fromData<'TDoc>, sqlProps)
|
||||||
sqlProps
|
|
||||||
|> Sql.query (Query.Find.byId tableName)
|
|
||||||
|> Sql.parameters [ "@id", Sql.string docId ]
|
|
||||||
|> Sql.executeAsync fromData<'T>
|
|
||||||
return List.tryHead results
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Execute a JSON containment query (@>)
|
/// Retrieve a document by its ID (returns None if not found)
|
||||||
let byContains<'T> tableName (criteria: obj) sqlProps =
|
[<CompiledName "FSharpById">]
|
||||||
sqlProps
|
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) sqlProps =
|
||||||
|> Sql.query (Query.Find.byContains tableName)
|
Custom.single (Query.Find.byId tableName) [ idParam docId ] fromData<'TDoc> sqlProps
|
||||||
|> Sql.parameters [ "@criteria", Query.jsonbDocParam criteria ]
|
|
||||||
|> Sql.executeAsync fromData<'T>
|
|
||||||
|
|
||||||
/// Execute a JSON Path match query (@?)
|
/// Retrieve a document by its ID (returns null if not found)
|
||||||
let byJsonPath<'T> tableName jsonPath sqlProps =
|
let ById<'TKey, 'TDoc when 'TDoc: null>(tableName, docId: 'TKey, sqlProps) =
|
||||||
sqlProps
|
Custom.Single<'TDoc>(Query.Find.byId tableName, [ idParam docId ], fromData<'TDoc>, sqlProps)
|
||||||
|> Sql.query (Query.Find.byJsonPath tableName)
|
|
||||||
|> Sql.parameters [ "@path", Sql.string jsonPath ]
|
/// Retrieve documents matching a JSON field comparison (->> =)
|
||||||
|> Sql.executeAsync fromData<'T>
|
[<CompiledName "FSharpByField">]
|
||||||
|
let byField<'TDoc> tableName fieldName op (value: obj) sqlProps =
|
||||||
|
Custom.list<'TDoc> (Query.Find.byField tableName fieldName op) [ fieldParam value ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
/// Execute a JSON containment query (@>), returning only the first result
|
/// Retrieve documents matching a JSON field comparison (->> =)
|
||||||
let firstByContains<'T> tableName (criteria: obj) sqlProps = backgroundTask {
|
let ByField<'TDoc>(tableName, fieldName, op, value: obj, sqlProps) =
|
||||||
let! results = byContains<'T> tableName criteria sqlProps
|
Custom.List<'TDoc>(
|
||||||
return List.tryHead results
|
Query.Find.byField tableName fieldName op, [ fieldParam value ], fromData<'TDoc>, sqlProps)
|
||||||
}
|
|
||||||
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
|
[<CompiledName "FSharpByContains">]
|
||||||
|
let byContains<'TDoc> tableName (criteria: obj) sqlProps =
|
||||||
|
Custom.list<'TDoc>
|
||||||
|
(Query.Find.byContains tableName) [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
/// Execute a JSON Path match query (@?), returning only the first result
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
let firstByJsonPath<'T> tableName jsonPath sqlProps = backgroundTask {
|
let ByContains<'TDoc>(tableName, criteria: obj, sqlProps) =
|
||||||
let! results = byJsonPath<'T> tableName jsonPath sqlProps
|
Custom.List<'TDoc>(
|
||||||
return List.tryHead results
|
Query.Find.byContains tableName, [ jsonParam "@criteria" criteria ], fromData<'TDoc>, sqlProps)
|
||||||
}
|
|
||||||
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
|
[<CompiledName "FSharpByJsonPath">]
|
||||||
|
let byJsonPath<'TDoc> tableName jsonPath sqlProps =
|
||||||
|
Custom.list<'TDoc>
|
||||||
|
(Query.Find.byJsonPath tableName) [ "@path", Sql.string jsonPath ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
|
let ByJsonPath<'TDoc>(tableName, jsonPath, sqlProps) =
|
||||||
|
Custom.List<'TDoc>(
|
||||||
|
Query.Find.byJsonPath tableName, [ "@path", Sql.string jsonPath ], fromData<'TDoc>, sqlProps)
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison (->> =); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
let firstByField<'TDoc> tableName fieldName op (value: obj) sqlProps =
|
||||||
|
Custom.single<'TDoc>
|
||||||
|
$"{Query.Find.byField tableName fieldName op} LIMIT 1" [ fieldParam value ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison (->> =); returns null if not found
|
||||||
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, fieldName, op, value: obj, sqlProps) =
|
||||||
|
Custom.Single<'TDoc>(
|
||||||
|
$"{Query.Find.byField tableName fieldName op} LIMIT 1", [ fieldParam value ], fromData<'TDoc>, sqlProps)
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByContains">]
|
||||||
|
let firstByContains<'TDoc> tableName (criteria: obj) sqlProps =
|
||||||
|
Custom.single<'TDoc>
|
||||||
|
$"{Query.Find.byContains tableName} LIMIT 1" [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON containment query (@>); returns null if not found
|
||||||
|
let FirstByContains<'TDoc when 'TDoc: null>(tableName, criteria: obj, sqlProps) =
|
||||||
|
Custom.Single<'TDoc>(
|
||||||
|
$"{Query.Find.byContains tableName} LIMIT 1",
|
||||||
|
[ jsonParam "@criteria" criteria ],
|
||||||
|
fromData<'TDoc>,
|
||||||
|
sqlProps)
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByJsonPath">]
|
||||||
|
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
|
||||||
|
Custom.single<'TDoc>
|
||||||
|
$"{Query.Find.byJsonPath tableName} LIMIT 1" [ "@path", Sql.string jsonPath ] fromData<'TDoc> sqlProps
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON Path match query (@?); returns null if not found
|
||||||
|
let FirstByJsonPath<'TDoc when 'TDoc: null>(tableName, jsonPath, sqlProps) =
|
||||||
|
Custom.Single<'TDoc>(
|
||||||
|
$"{Query.Find.byJsonPath tableName} LIMIT 1",
|
||||||
|
[ "@path", Sql.string jsonPath ],
|
||||||
|
fromData<'TDoc>,
|
||||||
|
sqlProps)
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Update =
|
module Update =
|
||||||
|
|
||||||
/// Update an entire document
|
/// Update an entire document
|
||||||
let full<'T> tableName docId (document: 'T) sqlProps =
|
[<CompiledName "Full">]
|
||||||
executeNonQueryWithId (Query.Update.full tableName) docId document sqlProps
|
let full tableName (docId: 'TKey) (document: 'TDoc) sqlProps =
|
||||||
|
Custom.nonQuery (Query.Update.full tableName) [ idParam docId; jsonParam "@data" document ] sqlProps
|
||||||
|
|
||||||
/// Update an entire document
|
/// Update an entire document
|
||||||
let fullFunc<'T> tableName (idFunc: 'T -> string) (document: 'T) sqlProps =
|
[<CompiledName "FSharpFullFunc">]
|
||||||
|
let fullFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) sqlProps =
|
||||||
full tableName (idFunc document) document sqlProps
|
full tableName (idFunc document) document sqlProps
|
||||||
|
|
||||||
|
/// Update an entire document
|
||||||
|
let FullFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, sqlProps) =
|
||||||
|
fullFunc tableName idFunc.Invoke document sqlProps
|
||||||
|
|
||||||
/// Update a partial document
|
/// Update a partial document
|
||||||
let partialById tableName docId (partial: obj) sqlProps =
|
[<CompiledName "PartialById">]
|
||||||
executeNonQueryWithId (Query.Update.partialById tableName) docId partial sqlProps
|
let partialById tableName (docId: 'TKey) (partial: 'TPartial) sqlProps =
|
||||||
|
Custom.nonQuery (Query.Update.partialById tableName) [ idParam docId; jsonParam "@data" partial ] sqlProps
|
||||||
|
|
||||||
|
/// Update partial documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<CompiledName "PartialByField">]
|
||||||
|
let partialByField tableName fieldName op (value: obj) (partial: 'TPartial) sqlProps =
|
||||||
|
Custom.nonQuery
|
||||||
|
(Query.Update.partialByField tableName fieldName op)
|
||||||
|
[ jsonParam "@data" partial; fieldParam value ]
|
||||||
|
sqlProps
|
||||||
|
|
||||||
/// Update partial documents using a JSON containment query in the WHERE clause (@>)
|
/// Update partial documents using a JSON containment query in the WHERE clause (@>)
|
||||||
let partialByContains tableName (criteria: obj) (partial: obj) sqlProps =
|
[<CompiledName "PartialByContains">]
|
||||||
sqlProps
|
let partialByContains tableName (criteria: 'TContains) (partial: 'TPartial) sqlProps =
|
||||||
|> Sql.query (Query.Update.partialByContains tableName)
|
Custom.nonQuery
|
||||||
|> Sql.parameters [ "@data", Query.jsonbDocParam partial; "@criteria", Query.jsonbDocParam criteria ]
|
(Query.Update.partialByContains tableName)
|
||||||
|> Sql.executeNonQueryAsync
|
[ jsonParam "@data" partial; jsonParam "@criteria" criteria ]
|
||||||
|> ignoreTask
|
sqlProps
|
||||||
|
|
||||||
/// Update partial documents using a JSON Path match query in the WHERE clause (@?)
|
/// Update partial documents using a JSON Path match query in the WHERE clause (@?)
|
||||||
let partialByJsonPath tableName jsonPath (partial: obj) sqlProps =
|
[<CompiledName "PartialByJsonPath">]
|
||||||
sqlProps
|
let partialByJsonPath tableName jsonPath (partial: 'TPartial) sqlProps =
|
||||||
|> Sql.query (Query.Update.partialByJsonPath tableName)
|
Custom.nonQuery
|
||||||
|> Sql.parameters [ "@data", Query.jsonbDocParam partial; "@path", Sql.string jsonPath ]
|
(Query.Update.partialByJsonPath tableName)
|
||||||
|> Sql.executeNonQueryAsync
|
[ jsonParam "@data" partial; "@path", Sql.string jsonPath ]
|
||||||
|> ignoreTask
|
sqlProps
|
||||||
|
|
||||||
/// Commands to delete documents
|
/// Commands to delete documents
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Delete =
|
module Delete =
|
||||||
|
|
||||||
/// Delete a document by its ID
|
/// Delete a document by its ID
|
||||||
let byId tableName docId sqlProps =
|
[<CompiledName "ById">]
|
||||||
executeNonQueryWithId (Query.Delete.byId tableName) docId {||} sqlProps
|
let byId tableName (docId: 'TKey) sqlProps =
|
||||||
|
Custom.nonQuery (Query.Delete.byId tableName) [ idParam docId ] sqlProps
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) sqlProps =
|
||||||
|
Custom.nonQuery (Query.Delete.byField tableName fieldName op) [ fieldParam value ] sqlProps
|
||||||
|
|
||||||
/// Delete documents by matching a JSON contains query (@>)
|
/// Delete documents by matching a JSON contains query (@>)
|
||||||
let byContains tableName (criteria: obj) sqlProps =
|
[<CompiledName "ByContains">]
|
||||||
sqlProps
|
let byContains tableName (criteria: 'TCriteria) sqlProps =
|
||||||
|> Sql.query (Query.Delete.byContains tableName)
|
Custom.nonQuery (Query.Delete.byContains tableName) [ jsonParam "@criteria" criteria ] sqlProps
|
||||||
|> Sql.parameters [ "@criteria", Query.jsonbDocParam criteria ]
|
|
||||||
|> Sql.executeNonQueryAsync
|
|
||||||
|> ignoreTask
|
|
||||||
|
|
||||||
/// Delete documents by matching a JSON Path match query (@?)
|
/// Delete documents by matching a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName path sqlProps =
|
let byJsonPath tableName path sqlProps =
|
||||||
sqlProps
|
Custom.nonQuery (Query.Delete.byJsonPath tableName) [ "@path", Sql.string path ] sqlProps
|
||||||
|> Sql.query (Query.Delete.byJsonPath tableName)
|
|
||||||
|> Sql.parameters [ "@path", Sql.string path ]
|
|
||||||
|> Sql.executeNonQueryAsync
|
|
||||||
|> ignoreTask
|
|
||||||
|
|
||||||
/// Commands to execute custom SQL queries
|
|
||||||
[<RequireQualifiedAccess>]
|
/// Commands to execute custom SQL queries
|
||||||
module Custom =
|
[<RequireQualifiedAccess>]
|
||||||
|
module Custom =
|
||||||
|
|
||||||
/// Execute a query that returns one or no results
|
/// Execute a query that returns a list of results
|
||||||
let single<'T> query parameters (mapFunc: RowReader -> 'T) sqlProps = backgroundTask {
|
[<CompiledName "FSharpList">]
|
||||||
let! results =
|
let list<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) =
|
||||||
Sql.query query sqlProps
|
WithProps.Custom.list<'TDoc> query parameters mapFunc (fromDataSource ())
|
||||||
|> Sql.parameters parameters
|
|
||||||
|> Sql.executeAsync mapFunc
|
|
||||||
return List.tryHead results
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Execute a query that returns a list of results
|
/// Execute a query that returns a list of results
|
||||||
let list<'T> query parameters (mapFunc: RowReader -> 'T) sqlProps =
|
let List<'TDoc>(query, parameters, mapFunc: System.Func<RowReader, 'TDoc>) =
|
||||||
Sql.query query sqlProps
|
WithProps.Custom.List<'TDoc>(query, parameters, mapFunc, fromDataSource ())
|
||||||
|> Sql.parameters parameters
|
|
||||||
|> Sql.executeAsync mapFunc
|
|
||||||
|
|
||||||
/// Execute a query that returns no results
|
/// Execute a query that returns one or no results; returns None if not found
|
||||||
let nonQuery query parameters sqlProps =
|
[<CompiledName "FSharpSingle">]
|
||||||
Sql.query query sqlProps
|
let single<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) =
|
||||||
|> Sql.parameters (List.ofSeq parameters)
|
WithProps.Custom.single<'TDoc> query parameters mapFunc (fromDataSource ())
|
||||||
|> Sql.executeNonQueryAsync
|
|
||||||
|> ignoreTask
|
/// Execute a query that returns one or no results; returns null if not found
|
||||||
|
let Single<'TDoc when 'TDoc: null>(query, parameters, mapFunc: System.Func<RowReader, 'TDoc>) =
|
||||||
/// Execute a query that returns a scalar value
|
WithProps.Custom.Single<'TDoc>(query, parameters, mapFunc, fromDataSource ())
|
||||||
let scalar<'T when 'T : struct> query parameters (mapFunc: RowReader -> 'T) sqlProps =
|
|
||||||
Sql.query query sqlProps
|
/// Execute a query that returns no results
|
||||||
|> Sql.parameters parameters
|
[<CompiledName "NonQuery">]
|
||||||
|> Sql.executeRowAsync mapFunc
|
let nonQuery query parameters =
|
||||||
|
WithProps.Custom.nonQuery query parameters (fromDataSource ())
|
||||||
|
|
||||||
|
/// Execute a query that returns a scalar value
|
||||||
|
[<CompiledName "FSharpScalar">]
|
||||||
|
let scalar<'T when 'T: struct> query parameters (mapFunc: RowReader -> 'T) =
|
||||||
|
WithProps.Custom.scalar query parameters mapFunc (fromDataSource ())
|
||||||
|
|
||||||
|
/// Execute a query that returns a scalar value
|
||||||
|
let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func<RowReader, 'T>) =
|
||||||
|
WithProps.Custom.Scalar<'T>(query, parameters, mapFunc, fromDataSource ())
|
||||||
|
|
||||||
|
|
||||||
/// Document writing functions
|
/// Document writing functions
|
||||||
[<AutoOpen>]
|
[<AutoOpen>]
|
||||||
module Document =
|
module Document =
|
||||||
|
|
||||||
/// Insert a new document
|
/// Insert a new document
|
||||||
let insert<'T> tableName (document: 'T) =
|
[<CompiledName "Insert">]
|
||||||
WithProps.insert tableName document (fromDataSource ())
|
let insert<'TDoc> tableName (document: 'TDoc) =
|
||||||
|
WithProps.Document.insert<'TDoc> tableName document (fromDataSource ())
|
||||||
|
|
||||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||||
let save<'T> tableName (document: 'T) =
|
[<CompiledName "Save">]
|
||||||
WithProps.save<'T> tableName document (fromDataSource ())
|
let save<'TDoc> tableName (document: 'TDoc) =
|
||||||
|
WithProps.Document.save<'TDoc> tableName document (fromDataSource ())
|
||||||
|
|
||||||
|
|
||||||
/// Queries to count documents
|
/// Queries to count documents
|
||||||
|
@ -445,14 +595,22 @@ module Document =
|
||||||
module Count =
|
module Count =
|
||||||
|
|
||||||
/// Count all documents in a table
|
/// Count all documents in a table
|
||||||
|
[<CompiledName "All">]
|
||||||
let all tableName =
|
let all tableName =
|
||||||
WithProps.Count.all tableName (fromDataSource ())
|
WithProps.Count.all tableName (fromDataSource ())
|
||||||
|
|
||||||
|
/// Count matching documents using a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) =
|
||||||
|
WithProps.Count.byField tableName fieldName op value (fromDataSource ())
|
||||||
|
|
||||||
/// Count matching documents using a JSON containment query (@>)
|
/// Count matching documents using a JSON containment query (@>)
|
||||||
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName criteria =
|
let byContains tableName criteria =
|
||||||
WithProps.Count.byContains tableName criteria (fromDataSource ())
|
WithProps.Count.byContains tableName criteria (fromDataSource ())
|
||||||
|
|
||||||
/// Count matching documents using a JSON Path match query (@?)
|
/// Count matching documents using a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName jsonPath =
|
let byJsonPath tableName jsonPath =
|
||||||
WithProps.Count.byJsonPath tableName jsonPath (fromDataSource ())
|
WithProps.Count.byJsonPath tableName jsonPath (fromDataSource ())
|
||||||
|
|
||||||
|
@ -462,14 +620,22 @@ module Count =
|
||||||
module Exists =
|
module Exists =
|
||||||
|
|
||||||
/// Determine if a document exists for the given ID
|
/// Determine if a document exists for the given ID
|
||||||
|
[<CompiledName "ById">]
|
||||||
let byId tableName docId =
|
let byId tableName docId =
|
||||||
WithProps.Exists.byId tableName docId (fromDataSource ())
|
WithProps.Exists.byId tableName docId (fromDataSource ())
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON containment query (@>)
|
/// Determine if documents exist using a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) =
|
||||||
|
WithProps.Exists.byField tableName fieldName op value (fromDataSource ())
|
||||||
|
|
||||||
|
/// Determine if documents exist using a JSON containment query (@>)
|
||||||
|
[<CompiledName "ByContains">]
|
||||||
let byContains tableName criteria =
|
let byContains tableName criteria =
|
||||||
WithProps.Exists.byContains tableName criteria (fromDataSource ())
|
WithProps.Exists.byContains tableName criteria (fromDataSource ())
|
||||||
|
|
||||||
/// Determine if a document exists using a JSON Path match query (@?)
|
/// Determine if documents exist using a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName jsonPath =
|
let byJsonPath tableName jsonPath =
|
||||||
WithProps.Exists.byJsonPath tableName jsonPath (fromDataSource ())
|
WithProps.Exists.byJsonPath tableName jsonPath (fromDataSource ())
|
||||||
|
|
||||||
|
@ -479,28 +645,76 @@ module Exists =
|
||||||
module Find =
|
module Find =
|
||||||
|
|
||||||
/// Retrieve all documents in the given table
|
/// Retrieve all documents in the given table
|
||||||
let all<'T> tableName =
|
[<CompiledName "FSharpAll">]
|
||||||
WithProps.Find.all<'T> tableName (fromDataSource ())
|
let all<'TDoc> tableName =
|
||||||
|
WithProps.Find.all<'TDoc> tableName (fromDataSource ())
|
||||||
|
|
||||||
/// Retrieve a document by its ID
|
/// Retrieve all documents in the given table
|
||||||
let byId<'T> tableName docId =
|
let All<'TDoc> tableName =
|
||||||
WithProps.Find.byId<'T> tableName docId (fromDataSource ())
|
WithProps.Find.All<'TDoc>(tableName, fromDataSource ())
|
||||||
|
|
||||||
/// Execute a JSON containment query (@>)
|
/// Retrieve a document by its ID; returns None if not found
|
||||||
let byContains<'T> tableName criteria =
|
[<CompiledName "FSharpById">]
|
||||||
WithProps.Find.byContains<'T> tableName criteria (fromDataSource ())
|
let byId<'TKey, 'TDoc> tableName docId =
|
||||||
|
WithProps.Find.byId<'TKey, 'TDoc> tableName docId (fromDataSource ())
|
||||||
|
|
||||||
/// Execute a JSON Path match query (@?)
|
/// Retrieve a document by its ID; returns null if not found
|
||||||
let byJsonPath<'T> tableName jsonPath =
|
let ById<'TKey, 'TDoc when 'TDoc: null>(tableName, docId: 'TKey) =
|
||||||
WithProps.Find.byJsonPath<'T> tableName jsonPath (fromDataSource ())
|
WithProps.Find.ById<'TKey, 'TDoc>(tableName, docId, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
|
[<CompiledName "FSharpByField">]
|
||||||
|
let byField<'TDoc> tableName fieldName op (value: obj) =
|
||||||
|
WithProps.Find.byField<'TDoc> tableName fieldName op value (fromDataSource ())
|
||||||
|
|
||||||
/// Execute a JSON containment query (@>), returning only the first result
|
/// Retrieve documents matching a JSON field comparison query (->> =)
|
||||||
let firstByContains<'T> tableName (criteria: obj) =
|
let ByField<'TDoc>(tableName, fieldName, op, value: obj) =
|
||||||
WithProps.Find.firstByContains<'T> tableName criteria (fromDataSource ())
|
WithProps.Find.ByField<'TDoc>(tableName, fieldName, op, value, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
|
[<CompiledName "FSharpByContains">]
|
||||||
|
let byContains<'TDoc> tableName (criteria: obj) =
|
||||||
|
WithProps.Find.byContains<'TDoc> tableName criteria (fromDataSource ())
|
||||||
|
|
||||||
/// Execute a JSON Path match query (@?), returning only the first result
|
/// Retrieve documents matching a JSON containment query (@>)
|
||||||
let firstByJsonPath<'T> tableName jsonPath =
|
let ByContains<'TDoc>(tableName, criteria: obj) =
|
||||||
WithProps.Find.firstByJsonPath<'T> tableName jsonPath (fromDataSource ())
|
WithProps.Find.ByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
|
[<CompiledName "FSharpByJsonPath">]
|
||||||
|
let byJsonPath<'TDoc> tableName jsonPath =
|
||||||
|
WithProps.Find.byJsonPath<'TDoc> tableName jsonPath (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve documents matching a JSON Path match query (@?)
|
||||||
|
let ByJsonPath<'TDoc>(tableName, jsonPath) =
|
||||||
|
WithProps.Find.ByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByField">]
|
||||||
|
let firstByField<'TDoc> tableName fieldName op (value: obj) =
|
||||||
|
WithProps.Find.firstByField<'TDoc> tableName fieldName op value (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found
|
||||||
|
let FirstByField<'TDoc when 'TDoc: null>(tableName, fieldName, op, value: obj) =
|
||||||
|
WithProps.Find.FirstByField<'TDoc>(tableName, fieldName, op, value, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON containment query (@>); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByContains">]
|
||||||
|
let firstByContains<'TDoc> tableName (criteria: obj) =
|
||||||
|
WithProps.Find.firstByContains<'TDoc> tableName criteria (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON containment query (@>); returns null if not found
|
||||||
|
let FirstByContains<'TDoc when 'TDoc: null>(tableName, criteria: obj) =
|
||||||
|
WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON Path match query (@?); returns None if not found
|
||||||
|
[<CompiledName "FSharpFirstByJsonPath">]
|
||||||
|
let firstByJsonPath<'TDoc> tableName jsonPath =
|
||||||
|
WithProps.Find.firstByJsonPath<'TDoc> tableName jsonPath (fromDataSource ())
|
||||||
|
|
||||||
|
/// Retrieve the first document matching a JSON Path match query (@?); returns null if not found
|
||||||
|
let FirstByJsonPath<'TDoc when 'TDoc: null>(tableName, jsonPath) =
|
||||||
|
WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ())
|
||||||
|
|
||||||
|
|
||||||
/// Commands to update documents
|
/// Commands to update documents
|
||||||
|
@ -508,23 +722,37 @@ module Find =
|
||||||
module Update =
|
module Update =
|
||||||
|
|
||||||
/// Update a full document
|
/// Update a full document
|
||||||
let full<'T> tableName docId (document: 'T) =
|
[<CompiledName "Full">]
|
||||||
WithProps.Update.full<'T> tableName docId document (fromDataSource ())
|
let full tableName (docId: 'TKey) (document: 'TDoc) =
|
||||||
|
WithProps.Update.full tableName docId document (fromDataSource ())
|
||||||
|
|
||||||
/// Update a full document
|
/// Update a full document
|
||||||
let fullFunc<'T> tableName idFunc (document: 'T) =
|
[<CompiledName "FSharpFullFunc">]
|
||||||
WithProps.Update.fullFunc<'T> tableName idFunc document (fromDataSource ())
|
let fullFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) =
|
||||||
|
WithProps.Update.fullFunc tableName idFunc document (fromDataSource ())
|
||||||
|
|
||||||
|
/// Update a full document
|
||||||
|
let FullFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc) =
|
||||||
|
WithProps.Update.FullFunc(tableName, idFunc, document, fromDataSource ())
|
||||||
|
|
||||||
/// Update a partial document
|
/// Update a partial document
|
||||||
let partialById tableName docId (partial: obj) =
|
[<CompiledName "PartialById">]
|
||||||
|
let partialById tableName (docId: 'TKey) (partial: 'TPartial) =
|
||||||
WithProps.Update.partialById tableName docId partial (fromDataSource ())
|
WithProps.Update.partialById tableName docId partial (fromDataSource ())
|
||||||
|
|
||||||
|
/// Update partial documents using a JSON field comparison query in the WHERE clause (->> =)
|
||||||
|
[<CompiledName "PartialByField">]
|
||||||
|
let partialByField tableName fieldName op (value: obj) (partial: 'TPartial) =
|
||||||
|
WithProps.Update.partialByField tableName fieldName op value partial (fromDataSource ())
|
||||||
|
|
||||||
/// Update partial documents using a JSON containment query in the WHERE clause (@>)
|
/// Update partial documents using a JSON containment query in the WHERE clause (@>)
|
||||||
let partialByContains tableName (criteria: obj) (partial: obj) =
|
[<CompiledName "PartialByContains">]
|
||||||
|
let partialByContains tableName (criteria: 'TCriteria) (partial: 'TPartial) =
|
||||||
WithProps.Update.partialByContains tableName criteria partial (fromDataSource ())
|
WithProps.Update.partialByContains tableName criteria partial (fromDataSource ())
|
||||||
|
|
||||||
/// Update partial documents using a JSON Path match query in the WHERE clause (@?)
|
/// Update partial documents using a JSON Path match query in the WHERE clause (@?)
|
||||||
let partialByJsonPath tableName jsonPath (partial: obj) =
|
[<CompiledName "PartialByJsonPath">]
|
||||||
|
let partialByJsonPath tableName jsonPath (partial: 'TPartial) =
|
||||||
WithProps.Update.partialByJsonPath tableName jsonPath partial (fromDataSource ())
|
WithProps.Update.partialByJsonPath tableName jsonPath partial (fromDataSource ())
|
||||||
|
|
||||||
|
|
||||||
|
@ -533,34 +761,21 @@ module Update =
|
||||||
module Delete =
|
module Delete =
|
||||||
|
|
||||||
/// Delete a document by its ID
|
/// Delete a document by its ID
|
||||||
let byId tableName docId =
|
[<CompiledName "ById">]
|
||||||
|
let byId tableName (docId: 'TKey) =
|
||||||
WithProps.Delete.byId tableName docId (fromDataSource ())
|
WithProps.Delete.byId tableName docId (fromDataSource ())
|
||||||
|
|
||||||
/// Delete documents by matching a JSON contains query (@>)
|
/// Delete documents by matching a JSON field comparison query (->> =)
|
||||||
let byContains tableName (criteria: obj) =
|
[<CompiledName "ByField">]
|
||||||
|
let byField tableName fieldName op (value: obj) =
|
||||||
|
WithProps.Delete.byField tableName fieldName op value (fromDataSource ())
|
||||||
|
|
||||||
|
/// Delete documents by matching a JSON containment query (@>)
|
||||||
|
[<CompiledName "ByContains">]
|
||||||
|
let byContains tableName (criteria: 'TContains) =
|
||||||
WithProps.Delete.byContains tableName criteria (fromDataSource ())
|
WithProps.Delete.byContains tableName criteria (fromDataSource ())
|
||||||
|
|
||||||
/// Delete documents by matching a JSON Path match query (@?)
|
/// Delete documents by matching a JSON Path match query (@?)
|
||||||
|
[<CompiledName "ByJsonPath">]
|
||||||
let byJsonPath tableName path =
|
let byJsonPath tableName path =
|
||||||
WithProps.Delete.byJsonPath tableName path (fromDataSource ())
|
WithProps.Delete.byJsonPath tableName path (fromDataSource ())
|
||||||
|
|
||||||
|
|
||||||
/// Commands to execute custom SQL queries
|
|
||||||
[<RequireQualifiedAccess>]
|
|
||||||
module Custom =
|
|
||||||
|
|
||||||
/// Execute a query that returns one or no results
|
|
||||||
let single<'T> query parameters (mapFunc: RowReader -> 'T) =
|
|
||||||
WithProps.Custom.single query parameters mapFunc (fromDataSource ())
|
|
||||||
|
|
||||||
/// Execute a query that returns a list of results
|
|
||||||
let list<'T> query parameters (mapFunc: RowReader -> 'T) =
|
|
||||||
WithProps.Custom.list query parameters mapFunc (fromDataSource ())
|
|
||||||
|
|
||||||
/// Execute a query that returns no results
|
|
||||||
let nonQuery query parameters =
|
|
||||||
WithProps.Custom.nonQuery query parameters (fromDataSource ())
|
|
||||||
|
|
||||||
/// Execute a query that returns a scalar value
|
|
||||||
let scalar<'T when 'T: struct> query parameters (mapFunc: RowReader -> 'T) =
|
|
||||||
WithProps.Custom.scalar query parameters mapFunc (fromDataSource ())
|
|
||||||
|
|
|
@ -38,6 +38,21 @@ module Query =
|
||||||
[<CompiledName "EnsureTable">]
|
[<CompiledName "EnsureTable">]
|
||||||
let ensureTable name =
|
let ensureTable name =
|
||||||
Query.Definition.ensureTableFor name "TEXT"
|
Query.Definition.ensureTableFor name "TEXT"
|
||||||
|
|
||||||
|
/// Document update queries
|
||||||
|
module Update =
|
||||||
|
|
||||||
|
/// Query to update a partial document by its ID
|
||||||
|
[<CompiledName "PartialById">]
|
||||||
|
let partialById tableName =
|
||||||
|
$"""UPDATE %s{tableName} SET data = json_patch(data, json(@data)) WHERE {Query.whereById "@id"}"""
|
||||||
|
|
||||||
|
/// Query to update a partial document via a comparison on a JSON field
|
||||||
|
[<CompiledName "PartialByField">]
|
||||||
|
let partialByField tableName fieldName op =
|
||||||
|
sprintf
|
||||||
|
"UPDATE %s SET data = json_patch(data, json(@data)) WHERE %s"
|
||||||
|
tableName (Query.whereByField fieldName op "@field")
|
||||||
|
|
||||||
|
|
||||||
/// Parameter handling helpers
|
/// Parameter handling helpers
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
namespace BitBadger.Documents.Tests.CSharp;
|
|
||||||
|
|
||||||
using Documents;
|
|
||||||
using Expecto.CSharp;
|
using Expecto.CSharp;
|
||||||
using Expecto;
|
using Expecto;
|
||||||
|
|
||||||
|
namespace BitBadger.Documents.Tests.CSharp;
|
||||||
|
|
||||||
|
using static Runner;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A test serializer that returns known values
|
/// A test serializer that returns known values
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -21,13 +22,14 @@ public static class CommonCSharpTests
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unit tests
|
/// Unit tests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Tests] public static Test Unit =
|
[Tests]
|
||||||
Runner.TestList("Common.C# Unit", new[]
|
public static Test Unit =
|
||||||
|
TestList("Common.C# Unit", new[]
|
||||||
{
|
{
|
||||||
Runner.TestSequenced(
|
TestSequenced(
|
||||||
Runner.TestList("Configuration", new[]
|
TestList("Configuration", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("UseSerializer succeeds", () =>
|
TestCase("UseSerializer succeeds", () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -49,12 +51,12 @@ public static class CommonCSharpTests
|
||||||
Configuration.UseSerializer(DocumentSerializer.Default);
|
Configuration.UseSerializer(DocumentSerializer.Default);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("Serializer returns configured serializer", () =>
|
TestCase("Serializer returns configured serializer", () =>
|
||||||
{
|
{
|
||||||
Expect.isTrue(ReferenceEquals(DocumentSerializer.Default, Configuration.Serializer()),
|
Expect.isTrue(ReferenceEquals(DocumentSerializer.Default, Configuration.Serializer()),
|
||||||
"Serializer should have been the same");
|
"Serializer should have been the same");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("UseIdField / IdField succeeds", () =>
|
TestCase("UseIdField / IdField succeeds", () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -69,89 +71,89 @@ public static class CommonCSharpTests
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})),
|
})),
|
||||||
Runner.TestList("Op", new[]
|
TestList("Op", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("EQ succeeds", () =>
|
TestCase("EQ succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.EQ.ToString(), "=", "The equals operator was not correct");
|
Expect.equal(Op.EQ.ToString(), "=", "The equals operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("GT succeeds", () =>
|
TestCase("GT succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.GT.ToString(), ">", "The greater than operator was not correct");
|
Expect.equal(Op.GT.ToString(), ">", "The greater than operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("GE succeeds", () =>
|
TestCase("GE succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.GE.ToString(), ">=", "The greater than or equal to operator was not correct");
|
Expect.equal(Op.GE.ToString(), ">=", "The greater than or equal to operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("LT succeeds", () =>
|
TestCase("LT succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.LT.ToString(), "<", "The less than operator was not correct");
|
Expect.equal(Op.LT.ToString(), "<", "The less than operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("LE succeeds", () =>
|
TestCase("LE succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.LE.ToString(), "<=", "The less than or equal to operator was not correct");
|
Expect.equal(Op.LE.ToString(), "<=", "The less than or equal to operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("NE succeeds", () =>
|
TestCase("NE succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.NE.ToString(), "<>", "The not equal to operator was not correct");
|
Expect.equal(Op.NE.ToString(), "<>", "The not equal to operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("EX succeeds", () =>
|
TestCase("EX succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.EX.ToString(), "IS NOT NULL", "The \"exists\" operator was not correct");
|
Expect.equal(Op.EX.ToString(), "IS NOT NULL", "The \"exists\" operator was not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("NEX succeeds", () =>
|
TestCase("NEX succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Op.NEX.ToString(), "IS NULL", "The \"not exists\" operator was not correct");
|
Expect.equal(Op.NEX.ToString(), "IS NULL", "The \"not exists\" operator was not correct");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Query", new[]
|
TestList("Query", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("SelectFromTable succeeds", () =>
|
TestCase("SelectFromTable succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.SelectFromTable("test.table"), "SELECT data FROM test.table",
|
Expect.equal(Query.SelectFromTable("test.table"), "SELECT data FROM test.table",
|
||||||
"SELECT statement not correct");
|
"SELECT statement not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("WhereById succeeds", () =>
|
TestCase("WhereById succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.WhereById("@id"), "data ->> 'Id' = @id", "WHERE clause not correct");
|
Expect.equal(Query.WhereById("@id"), "data ->> 'Id' = @id", "WHERE clause not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestList("WhereByField", new[]
|
TestList("WhereByField", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("succeeds when a logical operator is passed", () =>
|
TestCase("succeeds when a logical operator is passed", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.WhereByField("theField", Op.GT, "@test"), "data ->> 'theField' > @test",
|
Expect.equal(Query.WhereByField("theField", Op.GT, "@test"), "data ->> 'theField' > @test",
|
||||||
"WHERE clause not correct");
|
"WHERE clause not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("succeeds when an existence operator is passed", () =>
|
TestCase("succeeds when an existence operator is passed", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.WhereByField("thatField", Op.NEX, ""), "data ->> 'thatField' IS NULL",
|
Expect.equal(Query.WhereByField("thatField", Op.NEX, ""), "data ->> 'thatField' IS NULL",
|
||||||
"WHERE clause not correct");
|
"WHERE clause not correct");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Definition", new[]
|
TestList("Definition", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("EnsureTableFor succeeds", () =>
|
TestCase("EnsureTableFor succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Definition.EnsureTableFor("my.table", "JSONB"),
|
Expect.equal(Query.Definition.EnsureTableFor("my.table", "JSONB"),
|
||||||
"CREATE TABLE IF NOT EXISTS my.table (data JSONB NOT NULL)",
|
"CREATE TABLE IF NOT EXISTS my.table (data JSONB NOT NULL)",
|
||||||
"CREATE TABLE statement not constructed correctly");
|
"CREATE TABLE statement not constructed correctly");
|
||||||
}),
|
}),
|
||||||
Runner.TestList("EnsureKey", new[]
|
TestList("EnsureKey", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("succeeds when a schema is present", () =>
|
TestCase("succeeds when a schema is present", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Definition.EnsureKey("test.table"),
|
Expect.equal(Query.Definition.EnsureKey("test.table"),
|
||||||
"CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON test.table ((data ->> 'Id'))",
|
"CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON test.table ((data ->> 'Id'))",
|
||||||
"CREATE INDEX for key statement with schema not constructed correctly");
|
"CREATE INDEX for key statement with schema not constructed correctly");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("succeeds when a schema is not present", () =>
|
TestCase("succeeds when a schema is not present", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Definition.EnsureKey("table"),
|
Expect.equal(Query.Definition.EnsureKey("table"),
|
||||||
"CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON table ((data ->> 'Id'))",
|
"CREATE UNIQUE INDEX IF NOT EXISTS idx_table_key ON table ((data ->> 'Id'))",
|
||||||
"CREATE INDEX for key statement without schema not constructed correctly");
|
"CREATE INDEX for key statement without schema not constructed correctly");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("EnsureIndexOn succeeds for multiple fields and directions", () =>
|
TestCase("EnsureIndexOn succeeds for multiple fields and directions", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(
|
Expect.equal(
|
||||||
Query.Definition.EnsureIndexOn("test.table", "gibberish",
|
Query.Definition.EnsureIndexOn("test.table", "gibberish",
|
||||||
|
@ -161,87 +163,72 @@ public static class CommonCSharpTests
|
||||||
"CREATE INDEX for multiple field statement incorrect");
|
"CREATE INDEX for multiple field statement incorrect");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("Insert succeeds", () =>
|
TestCase("Insert succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Insert("tbl"), "INSERT INTO tbl VALUES (@data)", "INSERT statement not correct");
|
Expect.equal(Query.Insert("tbl"), "INSERT INTO tbl VALUES (@data)", "INSERT statement not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("Save succeeds", () =>
|
TestCase("Save succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Save("tbl"),
|
Expect.equal(Query.Save("tbl"),
|
||||||
$"INSERT INTO tbl VALUES (@data) ON CONFLICT ((data ->> 'Id')) DO UPDATE SET data = EXCLUDED.data",
|
$"INSERT INTO tbl VALUES (@data) ON CONFLICT ((data ->> 'Id')) DO UPDATE SET data = EXCLUDED.data",
|
||||||
"INSERT ON CONFLICT UPDATE statement not correct");
|
"INSERT ON CONFLICT UPDATE statement not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Count", new[]
|
TestList("Count", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("All succeeds", () =>
|
TestCase("All succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Count.All("tbl"), "SELECT COUNT(*) AS it FROM tbl",
|
Expect.equal(Query.Count.All("tbl"), "SELECT COUNT(*) AS it FROM tbl",
|
||||||
"Count query not correct");
|
"Count query not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("ByField succeeds", () =>
|
TestCase("ByField succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Count.ByField("tbl", "thatField", Op.EQ),
|
Expect.equal(Query.Count.ByField("tbl", "thatField", Op.EQ),
|
||||||
"SELECT COUNT(*) AS it FROM tbl WHERE data ->> 'thatField' = @field",
|
"SELECT COUNT(*) AS it FROM tbl WHERE data ->> 'thatField' = @field",
|
||||||
"JSON field text comparison count query not correct");
|
"JSON field text comparison count query not correct");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Exists", new[]
|
TestList("Exists", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("ById succeeds", () =>
|
TestCase("ById succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Exists.ById("tbl"),
|
Expect.equal(Query.Exists.ById("tbl"),
|
||||||
"SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Id' = @id) AS it",
|
"SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Id' = @id) AS it",
|
||||||
"ID existence query not correct");
|
"ID existence query not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("ByField succeeds", () =>
|
TestCase("ByField succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Exists.ByField("tbl", "Test", Op.LT),
|
Expect.equal(Query.Exists.ByField("tbl", "Test", Op.LT),
|
||||||
"SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Test' < @field) AS it",
|
"SELECT EXISTS (SELECT 1 FROM tbl WHERE data ->> 'Test' < @field) AS it",
|
||||||
"JSON field text comparison exists query not correct");
|
"JSON field text comparison exists query not correct");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Find", new[]
|
TestList("Find", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("ById succeeds", () =>
|
TestCase("ById succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Find.ById("tbl"), "SELECT data FROM tbl WHERE data ->> 'Id' = @id",
|
Expect.equal(Query.Find.ById("tbl"), "SELECT data FROM tbl WHERE data ->> 'Id' = @id",
|
||||||
"SELECT by ID query not correct");
|
"SELECT by ID query not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("ByField succeeds", () =>
|
TestCase("ByField succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Find.ByField("tbl", "Golf", Op.GE),
|
Expect.equal(Query.Find.ByField("tbl", "Golf", Op.GE),
|
||||||
"SELECT data FROM tbl WHERE data ->> 'Golf' >= @field",
|
"SELECT data FROM tbl WHERE data ->> 'Golf' >= @field",
|
||||||
"SELECT by JSON comparison query not correct");
|
"SELECT by JSON comparison query not correct");
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Update", new[]
|
TestCase("Update.Full succeeds", () =>
|
||||||
{
|
{
|
||||||
Runner.TestCase("Full succeeds", () =>
|
Expect.equal(Query.Update.Full("tbl"), "UPDATE tbl SET data = @data WHERE data ->> 'Id' = @id",
|
||||||
{
|
"UPDATE full statement not correct");
|
||||||
Expect.equal(Query.Update.Full("tbl"), "UPDATE tbl SET data = @data WHERE data ->> 'Id' = @id",
|
|
||||||
"UPDATE full statement not correct");
|
|
||||||
}),
|
|
||||||
Runner.TestCase("PartialById succeeds", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.Update.PartialById("tbl"),
|
|
||||||
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id",
|
|
||||||
"UPDATE partial by ID statement not correct");
|
|
||||||
}),
|
|
||||||
Runner.TestCase("PartialByField succeeds", () =>
|
|
||||||
{
|
|
||||||
Expect.equal(Query.Update.PartialByField("tbl", "Part", Op.NE),
|
|
||||||
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field",
|
|
||||||
"UPDATE partial by JSON comparison query not correct");
|
|
||||||
})
|
|
||||||
}),
|
}),
|
||||||
Runner.TestList("Delete", new[]
|
TestList("Delete", new[]
|
||||||
{
|
{
|
||||||
Runner.TestCase("ById succeeds", () =>
|
TestCase("ById succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Delete.ById("tbl"), "DELETE FROM tbl WHERE data ->> 'Id' = @id",
|
Expect.equal(Query.Delete.ById("tbl"), "DELETE FROM tbl WHERE data ->> 'Id' = @id",
|
||||||
"DELETE by ID query not correct");
|
"DELETE by ID query not correct");
|
||||||
}),
|
}),
|
||||||
Runner.TestCase("ByField succeeds", () =>
|
TestCase("ByField succeeds", () =>
|
||||||
{
|
{
|
||||||
Expect.equal(Query.Delete.ByField("tbl", "gone", Op.NEX),
|
Expect.equal(Query.Delete.ByField("tbl", "gone", Op.NEX),
|
||||||
"DELETE FROM tbl WHERE data ->> 'gone' IS NULL",
|
"DELETE FROM tbl WHERE data ->> 'gone' IS NULL",
|
||||||
|
|
|
@ -19,6 +19,29 @@ public static class SqliteCSharpTests
|
||||||
public static Test Unit =
|
public static Test Unit =
|
||||||
TestList("Unit", new[]
|
TestList("Unit", new[]
|
||||||
{
|
{
|
||||||
|
TestList("Query", new[]
|
||||||
|
{
|
||||||
|
TestCase("Definition.EnsureTable succeeds", () =>
|
||||||
|
{
|
||||||
|
Expect.equal(Sqlite.Query.Definition.EnsureTable("tbl"),
|
||||||
|
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)", "CREATE TABLE statement not correct");
|
||||||
|
}),
|
||||||
|
TestList("Update", new[]
|
||||||
|
{
|
||||||
|
TestCase("PartialById succeeds", () =>
|
||||||
|
{
|
||||||
|
Expect.equal(Sqlite.Query.Update.PartialById("tbl"),
|
||||||
|
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id",
|
||||||
|
"UPDATE partial by ID statement not correct");
|
||||||
|
}),
|
||||||
|
TestCase("PartialByField succeeds", () =>
|
||||||
|
{
|
||||||
|
Expect.equal(Sqlite.Query.Update.PartialByField("tbl", "Part", Op.NE),
|
||||||
|
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field",
|
||||||
|
"UPDATE partial by JSON comparison query not correct");
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
}),
|
||||||
TestList("Parameters", new[]
|
TestList("Parameters", new[]
|
||||||
{
|
{
|
||||||
TestCase("Id succeeds", () =>
|
TestCase("Id succeeds", () =>
|
||||||
|
|
|
@ -141,18 +141,6 @@ let all =
|
||||||
$"UPDATE {tbl} SET data = @data WHERE data ->> 'Id' = @id"
|
$"UPDATE {tbl} SET data = @data WHERE data ->> 'Id' = @id"
|
||||||
"UPDATE full statement not correct"
|
"UPDATE full statement not correct"
|
||||||
}
|
}
|
||||||
test "partialById succeeds" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.Update.partialById tbl)
|
|
||||||
$"UPDATE {tbl} SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id"
|
|
||||||
"UPDATE partial by ID statement not correct"
|
|
||||||
}
|
|
||||||
test "partialByField succeeds" {
|
|
||||||
Expect.equal
|
|
||||||
(Query.Update.partialByField tbl "Part" NE)
|
|
||||||
$"UPDATE {tbl} SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field"
|
|
||||||
"UPDATE partial by JSON comparison query not correct"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
testList "Delete" [
|
testList "Delete" [
|
||||||
test "byId succeeds" {
|
test "byId succeeds" {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module PostgresTests
|
module PostgresTests
|
||||||
|
|
||||||
open Expecto
|
open Expecto
|
||||||
|
open BitBadger.Documents
|
||||||
open BitBadger.Documents.Postgres
|
open BitBadger.Documents.Postgres
|
||||||
open BitBadger.Documents.Tests
|
open BitBadger.Documents.Tests
|
||||||
|
|
||||||
|
@ -30,13 +31,6 @@ let unitTests =
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Query" [
|
testList "Query" [
|
||||||
test "selectFromTable succeeds" {
|
|
||||||
Expect.equal (Query.selectFromTable PostgresDb.TableName) $"SELECT data FROM {PostgresDb.TableName}"
|
|
||||||
"SELECT statement not correct"
|
|
||||||
}
|
|
||||||
test "whereById succeeds" {
|
|
||||||
Expect.equal (Query.whereById "@id") "data ->> 'Id' = @id" "WHERE clause not correct"
|
|
||||||
}
|
|
||||||
test "whereDataContains succeeds" {
|
test "whereDataContains succeeds" {
|
||||||
Expect.equal (Query.whereDataContains "@test") "data @> @test" "WHERE clause not correct"
|
Expect.equal (Query.whereDataContains "@test") "data @> @test" "WHERE clause not correct"
|
||||||
}
|
}
|
||||||
|
@ -65,10 +59,6 @@ let unitTests =
|
||||||
"INSERT ON CONFLICT UPDATE statement not correct"
|
"INSERT ON CONFLICT UPDATE statement not correct"
|
||||||
}
|
}
|
||||||
testList "Count" [
|
testList "Count" [
|
||||||
test "all succeeds" {
|
|
||||||
Expect.equal (Query.Count.all PostgresDb.TableName) $"SELECT COUNT(*) AS it FROM {PostgresDb.TableName}"
|
|
||||||
"Count query not correct"
|
|
||||||
}
|
|
||||||
test "byContains succeeds" {
|
test "byContains succeeds" {
|
||||||
Expect.equal (Query.Count.byContains PostgresDb.TableName)
|
Expect.equal (Query.Count.byContains PostgresDb.TableName)
|
||||||
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
$"SELECT COUNT(*) AS it FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
||||||
|
@ -81,11 +71,6 @@ let unitTests =
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Exists" [
|
testList "Exists" [
|
||||||
test "byId succeeds" {
|
|
||||||
Expect.equal (Query.Exists.byId PostgresDb.TableName)
|
|
||||||
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id) AS it"
|
|
||||||
"ID existence query not correct"
|
|
||||||
}
|
|
||||||
test "byContains succeeds" {
|
test "byContains succeeds" {
|
||||||
Expect.equal (Query.Exists.byContains PostgresDb.TableName)
|
Expect.equal (Query.Exists.byContains PostgresDb.TableName)
|
||||||
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data @> @criteria) AS it"
|
$"SELECT EXISTS (SELECT 1 FROM {PostgresDb.TableName} WHERE data @> @criteria) AS it"
|
||||||
|
@ -98,10 +83,6 @@ let unitTests =
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Find" [
|
testList "Find" [
|
||||||
test "byId succeeds" {
|
|
||||||
Expect.equal (Query.Find.byId PostgresDb.TableName)
|
|
||||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id" "SELECT by ID query not correct"
|
|
||||||
}
|
|
||||||
test "byContains succeeds" {
|
test "byContains succeeds" {
|
||||||
Expect.equal (Query.Find.byContains PostgresDb.TableName)
|
Expect.equal (Query.Find.byContains PostgresDb.TableName)
|
||||||
$"SELECT data FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
$"SELECT data FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
||||||
|
@ -114,11 +95,6 @@ let unitTests =
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Update" [
|
testList "Update" [
|
||||||
test "full succeeds" {
|
|
||||||
Expect.equal (Query.Update.full PostgresDb.TableName)
|
|
||||||
$"UPDATE {PostgresDb.TableName} SET data = @data WHERE data ->> 'Id' = @id"
|
|
||||||
"UPDATE full statement not correct"
|
|
||||||
}
|
|
||||||
test "partialById succeeds" {
|
test "partialById succeeds" {
|
||||||
Expect.equal (Query.Update.partialById PostgresDb.TableName)
|
Expect.equal (Query.Update.partialById PostgresDb.TableName)
|
||||||
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Id' = @id"
|
$"UPDATE {PostgresDb.TableName} SET data = data || @data WHERE data ->> 'Id' = @id"
|
||||||
|
@ -136,10 +112,6 @@ let unitTests =
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
testList "Delete" [
|
testList "Delete" [
|
||||||
test "byId succeeds" {
|
|
||||||
Expect.equal (Query.Delete.byId PostgresDb.TableName) $"DELETE FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
|
|
||||||
"DELETE by ID query not correct"
|
|
||||||
}
|
|
||||||
test "byContains succeeds" {
|
test "byContains succeeds" {
|
||||||
Expect.equal (Query.Delete.byContains PostgresDb.TableName)
|
Expect.equal (Query.Delete.byContains PostgresDb.TableName)
|
||||||
$"DELETE FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
$"DELETE FROM {PostgresDb.TableName} WHERE data @> @criteria"
|
||||||
|
@ -266,14 +238,14 @@ let integrationTests =
|
||||||
let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } }
|
let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } }
|
||||||
do! insert PostgresDb.TableName testDoc
|
do! insert PostgresDb.TableName testDoc
|
||||||
|
|
||||||
let! before = Find.byId<JsonDocument> PostgresDb.TableName "test"
|
let! before = Find.byId<string, JsonDocument> PostgresDb.TableName "test"
|
||||||
if Option.isNone before then Expect.isTrue false "There should have been a document returned"
|
Expect.isSome before "There should have been a document returned"
|
||||||
Expect.equal before.Value testDoc "The document is not correct"
|
Expect.equal before.Value testDoc "The document is not correct"
|
||||||
|
|
||||||
let upd8Doc = { testDoc with Sub = Some { Foo = "c"; Bar = "d" } }
|
let upd8Doc = { testDoc with Sub = Some { Foo = "c"; Bar = "d" } }
|
||||||
do! save PostgresDb.TableName upd8Doc
|
do! save PostgresDb.TableName upd8Doc
|
||||||
let! after = Find.byId<JsonDocument> PostgresDb.TableName "test"
|
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "test"
|
||||||
if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update"
|
Expect.isSome after "There should have been a document returned post-update"
|
||||||
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
Expect.equal after.Value upd8Doc "The updated document is not correct"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -378,7 +350,7 @@ let integrationTests =
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.byId<JsonDocument> PostgresDb.TableName "two"
|
let! doc = Find.byId<string, JsonDocument> PostgresDb.TableName "two"
|
||||||
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
|
||||||
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
|
||||||
}
|
}
|
||||||
|
@ -386,7 +358,7 @@ let integrationTests =
|
||||||
use db = PostgresDb.BuildDb()
|
use db = PostgresDb.BuildDb()
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
let! doc = Find.byId<JsonDocument> PostgresDb.TableName "three hundred eighty-seven"
|
let! doc = Find.byId<string, JsonDocument> PostgresDb.TableName "three hundred eighty-seven"
|
||||||
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -481,9 +453,8 @@ let integrationTests =
|
||||||
|
|
||||||
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
||||||
do! Update.full PostgresDb.TableName "one" testDoc
|
do! Update.full PostgresDb.TableName "one" testDoc
|
||||||
let! after = Find.byId<JsonDocument> PostgresDb.TableName "one"
|
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
||||||
if Option.isNone after then
|
Expect.isSome after "There should have been a document returned post-update"
|
||||||
Expect.isTrue false "There should have been a document returned post-update"
|
|
||||||
Expect.equal after.Value testDoc "The updated document is not correct"
|
Expect.equal after.Value testDoc "The updated document is not correct"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
|
@ -504,9 +475,8 @@ let integrationTests =
|
||||||
|
|
||||||
do! Update.fullFunc PostgresDb.TableName (_.Id)
|
do! Update.fullFunc PostgresDb.TableName (_.Id)
|
||||||
{ Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
{ Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
let! after = Find.byId<JsonDocument> PostgresDb.TableName "one"
|
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
||||||
if Option.isNone after then
|
Expect.isSome after "There should have been a document returned post-update"
|
||||||
Expect.isTrue false "There should have been a document returned post-update"
|
|
||||||
Expect.equal after.Value { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
Expect.equal after.Value { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||||
"The updated document is not correct"
|
"The updated document is not correct"
|
||||||
}
|
}
|
||||||
|
@ -526,9 +496,8 @@ let integrationTests =
|
||||||
do! loadDocs ()
|
do! loadDocs ()
|
||||||
|
|
||||||
do! Update.partialById PostgresDb.TableName "one" {| NumValue = 44 |}
|
do! Update.partialById PostgresDb.TableName "one" {| NumValue = 44 |}
|
||||||
let! after = Find.byId<JsonDocument> PostgresDb.TableName "one"
|
let! after = Find.byId<string, JsonDocument> PostgresDb.TableName "one"
|
||||||
if Option.isNone after then
|
Expect.isSome after "There should have been a document returned post-update"
|
||||||
Expect.isTrue false "There should have been a document returned post-update"
|
|
||||||
Expect.equal after.Value.NumValue 44 "The updated document is not correct"
|
Expect.equal after.Value.NumValue 44 "The updated document is not correct"
|
||||||
}
|
}
|
||||||
testTask "succeeds when no document is updated" {
|
testTask "succeeds when no document is updated" {
|
||||||
|
|
|
@ -6,6 +6,7 @@ let allTests =
|
||||||
"BitBadger.Documents"
|
"BitBadger.Documents"
|
||||||
[ CommonTests.all
|
[ CommonTests.all
|
||||||
CommonCSharpTests.Unit
|
CommonCSharpTests.Unit
|
||||||
|
PostgresTests.all
|
||||||
SqliteTests.all
|
SqliteTests.all
|
||||||
testSequenced SqliteExtensionTests.integrationTests
|
testSequenced SqliteExtensionTests.integrationTests
|
||||||
SqliteCSharpTests.All
|
SqliteCSharpTests.All
|
||||||
|
|
|
@ -10,6 +10,28 @@ open Types
|
||||||
/// Unit tests for the SQLite library
|
/// Unit tests for the SQLite library
|
||||||
let unitTests =
|
let unitTests =
|
||||||
testList "Unit" [
|
testList "Unit" [
|
||||||
|
testList "Query" [
|
||||||
|
test "Definition.ensureTable succeeds" {
|
||||||
|
Expect.equal
|
||||||
|
(Query.Definition.ensureTable "tbl")
|
||||||
|
"CREATE TABLE IF NOT EXISTS tbl (data TEXT NOT NULL)"
|
||||||
|
"CREATE TABLE statement not correct"
|
||||||
|
}
|
||||||
|
testList "Update" [
|
||||||
|
test "partialById succeeds" {
|
||||||
|
Expect.equal
|
||||||
|
(Query.Update.partialById "tbl")
|
||||||
|
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Id' = @id"
|
||||||
|
"UPDATE partial by ID statement not correct"
|
||||||
|
}
|
||||||
|
test "partialByField succeeds" {
|
||||||
|
Expect.equal
|
||||||
|
(Query.Update.partialByField "tbl" "Part" NE)
|
||||||
|
"UPDATE tbl SET data = json_patch(data, json(@data)) WHERE data ->> 'Part' <> @field"
|
||||||
|
"UPDATE partial by JSON comparison query not correct"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
testList "Parameters" [
|
testList "Parameters" [
|
||||||
test "idParam succeeds" {
|
test "idParam succeeds" {
|
||||||
let theParam = idParam 7
|
let theParam = idParam 7
|
||||||
|
|
Loading…
Reference in New Issue
Block a user