diff --git a/src/Postgres/Library.fs b/src/Postgres/Library.fs
index edf03af..97cb339 100644
--- a/src/Postgres/Library.fs
+++ b/src/Postgres/Library.fs
@@ -1,5 +1,9 @@
namespace BitBadger.Documents.Postgres
+open System.IO
+open System.Text
+open Npgsql.FSharp
+
/// The type of index to generate for the document
[]
type DocumentIndex =
@@ -302,3 +306,60 @@ module Results =
[]
let toExists (row: RowReader) =
row.bool "it"
+
+ /// Extract a JSON document, specifying the field in which the document is found
+ /// The field name containing the JSON document
+ /// A row reader set to the row with the document to be extracted
+ /// The JSON from the given field (an empty object if no field exists)
+ []
+ let jsonFromDocument field (row: RowReader) =
+ row.stringOrNone field |> Option.defaultValue "{}"
+
+ /// Extract a JSON document
+ /// A row reader set to the row with the document to be extracted
+ /// The JSON from the row (an empty object if no field exists)
+ []
+ let jsonFromData row =
+ jsonFromDocument "data" row
+
+ /// Create a JSON array of items for the results of a query
+ /// The mapping function to extract JSON from the query's results
+ /// The query from which JSON should be extracted
+ /// A JSON array as a string; no results will produce an empty array ("[]")
+ []
+ let toJsonArray (mapFunc: RowReader -> string) sqlProps =
+ let output = StringBuilder("[")
+ sqlProps
+ |> Sql.iter (fun it ->
+ if output.Length > 2 then ignore (output.Append ",")
+ mapFunc it |> output.Append |> ignore)
+ output.Append("]").ToString()
+
+ /// Create a JSON array of items for the results of a query
+ /// The mapping function to extract JSON from the query's results
+ /// The query from which JSON should be extracted
+ /// A JSON array as a string; no results will produce an empty array ("[]")
+ let ToJsonArray(mapFunc: System.Func, sqlProps) =
+ toJsonArray mapFunc.Invoke sqlProps
+
+ /// Write a JSON array of items for the results of a query to the given StreamWriter
+ /// The StreamWriter to which results should be written
+ /// The mapping function to extract JSON from the query's results
+ /// The query from which JSON should be extracted
+ []
+ let writeJsonArray (writer: StreamWriter) (mapFunc: RowReader -> string) sqlProps =
+ writer.Write "["
+ let mutable isFirst = true
+ sqlProps
+ |> Sql.iter (fun it ->
+ if isFirst then isFirst <- false else writer.Write ","
+ mapFunc it |> writer.Write)
+ writer.Write "]"
+
+ /// Write a JSON array of items for the results of a query to the given StreamWriter
+ /// The StreamWriter to which results should be written
+ /// The mapping function to extract JSON from the query's results
+ /// The query from which JSON should be extracted
+ let WriteJsonArray(writer, mapFunc: System.Func, sqlProps) =
+ writeJsonArray writer mapFunc.Invoke sqlProps
+
\ No newline at end of file
diff --git a/src/Postgres/WithProps.fs b/src/Postgres/WithProps.fs
index da7bc86..fcb7360 100644
--- a/src/Postgres/WithProps.fs
+++ b/src/Postgres/WithProps.fs
@@ -1,6 +1,7 @@
/// Versions of queries that accept SqlProps as the last parameter
module BitBadger.Documents.Postgres.WithProps
+open System.IO
open BitBadger.Documents
open Npgsql.FSharp
@@ -14,7 +15,7 @@ module Custom =
/// The query to retrieve the results
/// Parameters to use for the query
/// The mapping function between the document and the domain item
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// A list of results for the given query
[]
let list<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) sqlProps =
@@ -26,22 +27,64 @@ module Custom =
/// The query to retrieve the results
/// Parameters to use for the query
/// The mapping function between the document and the domain item
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// A list of results for the given query
let List<'TDoc>(query, parameters, mapFunc: System.Func, sqlProps) = backgroundTask {
let! results = list<'TDoc> query parameters mapFunc.Invoke sqlProps
return ResizeArray results
}
+ /// Execute a query that returns a JSON array of results
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ /// A JSON array of results for the given query
+ []
+ let jsonArray query parameters (mapFunc: RowReader -> string) sqlProps =
+ Sql.query query sqlProps
+ |> Sql.parameters (FSharpList.ofSeq parameters)
+ |> toJsonArray mapFunc
+
+ /// Execute a query that returns a JSON array of results
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ /// A JSON array of results for the given query
+ let JsonArray(query, parameters, mapFunc: System.Func, sqlProps) =
+ jsonArray query parameters mapFunc.Invoke sqlProps
+
+ /// Execute a query, writing its results to the given StreamWriter
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The StreamWriter to which the results should be written
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ []
+ let writeJsonArray query parameters writer (mapFunc: RowReader -> string) sqlProps =
+ Sql.query query sqlProps
+ |> Sql.parameters (FSharpList.ofSeq parameters)
+ |> writeJsonArray writer mapFunc
+
+ /// Execute a query, writing its results to the given StreamWriter
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The StreamWriter to which the results should be written
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ let WriteJsonArray(query, parameters, writer, mapFunc: System.Func, sqlProps) =
+ writeJsonArray query parameters writer mapFunc.Invoke sqlProps
+
/// Execute a query that returns one or no results
/// The query to retrieve the results
/// Parameters to use for the query
/// The mapping function between the document and the domain item
- /// The SqlProps to use to execute the query
- /// Some with the first matching result, or None if not found
+ /// The SqlProps to use to execute the query
+ /// Some with the first matching result, or None if not found
[]
let single<'TDoc> query parameters mapFunc sqlProps = backgroundTask {
- let! results = list<'TDoc> query parameters mapFunc sqlProps
+ let! results = list<'TDoc> $"{query} LIMIT 1" parameters mapFunc sqlProps
return FSharpList.tryHead results
}
@@ -49,18 +92,38 @@ module Custom =
/// The query to retrieve the results
/// Parameters to use for the query
/// The mapping function between the document and the domain item
- /// The SqlProps to use to execute the query
- /// The first matching result, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first matching result, or null if not found
let Single<'TDoc when 'TDoc: null and 'TDoc: not struct>(
query, parameters, mapFunc: System.Func, sqlProps) = backgroundTask {
let! result = single<'TDoc> query parameters mapFunc.Invoke sqlProps
return Option.toObj result
}
+ /// Execute a query that returns one or no JSON documents
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ /// The JSON document with the first matching result, or an empty document if not found
+ []
+ let jsonSingle query parameters mapFunc sqlProps =
+ let results = jsonArray $"%s{query} LIMIT 1" parameters mapFunc sqlProps
+ if results = "[]" then "{}" else results[1..results.Length - 1]
+
+ /// Execute a query that returns one or no JSON documents
+ /// The query to retrieve the results
+ /// Parameters to use for the query
+ /// The mapping function between the document and the domain item
+ /// The SqlProps to use to execute the query
+ /// The JSON document with the first matching result, or an empty document if not found
+ let JsonSingle(query, parameters, mapFunc: System.Func, sqlProps) =
+ jsonSingle query parameters mapFunc.Invoke sqlProps
+
/// Execute a query that returns no results
/// The query to retrieve the results
/// Parameters to use for the query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let nonQuery query parameters sqlProps =
Sql.query query sqlProps
@@ -72,7 +135,7 @@ module Custom =
/// The query to retrieve the value
/// Parameters to use for the query
/// The mapping function to obtain the value
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The scalar value for the query
[]
let scalar<'T when 'T: struct> query parameters (mapFunc: RowReader -> 'T) sqlProps =
@@ -84,7 +147,7 @@ module Custom =
/// The query to retrieve the value
/// Parameters to use for the query
/// The mapping function to obtain the value
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The scalar value for the query
let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func, sqlProps) =
scalar<'T> query parameters mapFunc.Invoke sqlProps
@@ -94,7 +157,7 @@ module Definition =
/// Create a document table
/// The table whose existence should be ensured (may include schema)
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let ensureTable name sqlProps = backgroundTask {
do! Custom.nonQuery (Query.Definition.ensureTable name) [] sqlProps
@@ -104,7 +167,7 @@ module Definition =
/// Create an index on documents in the specified table
/// The table to be indexed (may include schema)
/// The type of document index to create
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let ensureDocumentIndex name idxType sqlProps =
Custom.nonQuery (Query.Definition.ensureDocumentIndex name idxType) [] sqlProps
@@ -113,7 +176,7 @@ module Definition =
/// The table to be indexed (may include schema)
/// The name of the index to create
/// One or more fields to be indexed
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let ensureFieldIndex tableName indexName fields sqlProps =
Custom.nonQuery (Query.Definition.ensureIndexOn tableName indexName fields PostgreSQL) [] sqlProps
@@ -125,7 +188,7 @@ module Document =
/// Insert a new document
/// The table into which the document should be inserted (may include schema)
/// The document to be inserted
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let insert<'TDoc> tableName (document: 'TDoc) sqlProps =
let query =
@@ -149,7 +212,7 @@ module Document =
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
/// The table into which the document should be saved (may include schema)
/// The document to be saved
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let save<'TDoc> tableName (document: 'TDoc) sqlProps =
Custom.nonQuery (Query.save tableName) [ jsonParam "@data" document ] sqlProps
@@ -160,37 +223,37 @@ module Count =
/// Count all documents in a table
/// The table in which documents should be counted (may include schema)
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The count of the documents in the table
[]
let all tableName sqlProps =
Custom.scalar (Query.count tableName) [] toCount sqlProps
- /// Count matching documents using JSON field comparisons (->> =, etc.)
+ /// Count matching documents using JSON field comparisons (->> =, etc.)
/// The table in which documents should be counted (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The count of matching documents in the table
[]
let byFields tableName howMatched fields sqlProps =
Custom.scalar
(Query.byFields (Query.count tableName) howMatched fields) (addFieldParams fields []) toCount sqlProps
- /// Count matching documents using a JSON containment query (@>)
+ /// Count matching documents using a JSON containment query (@>)
/// The table in which documents should be counted (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The count of the documents in the table
[]
let byContains tableName (criteria: 'TContains) sqlProps =
Custom.scalar
(Query.byContains (Query.count tableName)) [ jsonParam "@criteria" criteria ] toCount sqlProps
- /// Count matching documents using a JSON Path match query (@?)
+ /// Count matching documents using a JSON Path match query (@?)
/// The table in which documents should be counted (may include schema)
/// The JSON Path expression to be matched
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// The count of the documents in the table
[]
let byJsonPath tableName jsonPath sqlProps =
@@ -204,17 +267,17 @@ module Exists =
/// Determine if a document exists for the given ID
/// The table in which existence should be checked (may include schema)
/// The ID of the document whose existence should be checked
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// True if a document exists, false if not
[]
let byId tableName (docId: 'TKey) sqlProps =
Custom.scalar (Query.exists tableName (Query.whereById docId)) [ idParam docId ] toExists sqlProps
- /// Determine if a document exists using JSON field comparisons (->> =, etc.)
+ /// Determine if a document exists using JSON field comparisons (->> =, etc.)
/// The table in which existence should be checked (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// True if any matching documents exist, false if not
[]
let byFields tableName howMatched fields sqlProps =
@@ -224,10 +287,10 @@ module Exists =
toExists
sqlProps
- /// Determine if a document exists using a JSON containment query (@>)
+ /// Determine if a document exists using a JSON containment query (@>)
/// The table in which existence should be checked (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// True if any matching documents exist, false if not
[]
let byContains tableName (criteria: 'TContains) sqlProps =
@@ -237,10 +300,10 @@ module Exists =
toExists
sqlProps
- /// Determine if a document exists using a JSON Path match query (@?)
+ /// Determine if a document exists using a JSON Path match query (@?)
/// The table in which existence should be checked (may include schema)
/// The JSON Path expression to be matched
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// True if any matching documents exist, false if not
[]
let byJsonPath tableName jsonPath sqlProps =
@@ -250,13 +313,13 @@ module Exists =
toExists
sqlProps
-/// Commands to retrieve documents
+/// Commands to retrieve documents as domain objects
[]
module Find =
/// Retrieve all documents in the given table
/// The table from which documents should be retrieved (may include schema)
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents from the given table
[]
let all<'TDoc> tableName sqlProps =
@@ -264,7 +327,7 @@ module Find =
/// Retrieve all documents in the given table
/// The table from which documents should be retrieved (may include schema)
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents from the given table
let All<'TDoc>(tableName, sqlProps) =
Custom.List<'TDoc>(Query.find tableName, [], fromData<'TDoc>, sqlProps)
@@ -272,7 +335,7 @@ module Find =
/// Retrieve all documents in the given table ordered by the given fields in the document
/// The table from which documents should be retrieved (may include schema)
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents from the given table, ordered by the given fields
[]
let allOrdered<'TDoc> tableName orderFields sqlProps =
@@ -281,7 +344,7 @@ module Find =
/// Retrieve all documents in the given table ordered by the given fields in the document
/// The table from which documents should be retrieved (may include schema)
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents from the given table, ordered by the given fields
let AllOrdered<'TDoc>(tableName, orderFields, sqlProps) =
Custom.List<'TDoc>(
@@ -290,8 +353,8 @@ module Find =
/// Retrieve a document by its ID
/// The table from which a document should be retrieved (may include schema)
/// The ID of the document to retrieve
- /// The SqlProps to use to execute the query
- /// Some with the document if found, None otherwise
+ /// The SqlProps to use to execute the query
+ /// Some with the document if found, None otherwise
[]
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) sqlProps =
Custom.single (Query.byId (Query.find tableName) docId) [ idParam docId ] fromData<'TDoc> sqlProps
@@ -299,17 +362,17 @@ module Find =
/// Retrieve a document by its ID
/// The table from which a document should be retrieved (may include schema)
/// The ID of the document to retrieve
- /// The SqlProps to use to execute the query
- /// The document if found, null otherwise
+ /// The SqlProps to use to execute the query
+ /// The document if found, null otherwise
let ById<'TKey, 'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, docId: 'TKey, sqlProps) =
Custom.Single<'TDoc>(
Query.byId (Query.find tableName) docId, [ idParam docId ], fromData<'TDoc>, sqlProps)
- /// Retrieve documents matching JSON field comparisons (->> =, etc.)
+ /// Retrieve documents matching JSON field comparisons (->> =, etc.)
/// The table from which documents should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given fields
[]
let byFields<'TDoc> tableName howMatched fields sqlProps =
@@ -319,11 +382,11 @@ module Find =
fromData<'TDoc>
sqlProps
- /// Retrieve documents matching JSON field comparisons (->> =, etc.)
+ /// Retrieve documents matching JSON field comparisons (->> =, etc.)
/// The table from which documents should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given fields
let ByFields<'TDoc>(tableName, howMatched, fields, sqlProps) =
Custom.List<'TDoc>(
@@ -333,14 +396,14 @@ module Find =
sqlProps)
///
- /// Retrieve documents matching JSON field comparisons (->> =, etc.) ordered by the given fields in
+ /// Retrieve documents matching JSON field comparisons (->> =, etc.) ordered by the given fields in
/// the document
///
/// The table from which documents should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given fields, ordered by the other given fields
[]
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
@@ -351,14 +414,14 @@ module Find =
sqlProps
///
- /// Retrieve documents matching JSON field comparisons (->> =, etc.) ordered by the given fields in
- /// the document
+ /// Retrieve documents matching JSON field comparisons (->> =, etc.) ordered by the given fields in the
+ /// document
///
/// The table from which documents should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given fields, ordered by the other given fields
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, sqlProps) =
Custom.List<'TDoc>(
@@ -367,20 +430,20 @@ module Find =
fromData<'TDoc>,
sqlProps)
- /// Retrieve documents matching a JSON containment query (@>)
+ /// Retrieve documents matching a JSON containment query (@>)
/// The table from which documents should be retrieved (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given containment query
[]
let byContains<'TDoc> tableName (criteria: obj) sqlProps =
Custom.list<'TDoc>
(Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps
- /// Retrieve documents matching a JSON containment query (@>)
+ /// Retrieve documents matching a JSON containment query (@>)
/// The table from which documents should be retrieved (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given containment query
let ByContains<'TDoc>(tableName, criteria: obj, sqlProps) =
Custom.List<'TDoc>(
@@ -390,13 +453,12 @@ module Find =
sqlProps)
///
- /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the
- /// document
+ /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
///
/// The table from which documents should be retrieved (may include schema)
/// The document to match with the containment query
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given containment query, ordered by the given fields
[]
let byContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
@@ -407,13 +469,12 @@ module Find =
sqlProps
///
- /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the
- /// document
+ /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document
///
/// The table from which documents should be retrieved (may include schema)
/// The document to match with the containment query
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given containment query, ordered by the given fields
let ByContainsOrdered<'TDoc>(tableName, criteria: obj, orderFields, sqlProps) =
Custom.List<'TDoc>(
@@ -422,20 +483,20 @@ module Find =
fromData<'TDoc>,
sqlProps)
- /// Retrieve documents matching a JSON Path match query (@?)
+ /// Retrieve documents matching a JSON Path match query (@?)
/// The table from which documents should be retrieved (may include schema)
/// The JSON Path expression to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given JSON Path expression
[]
let byJsonPath<'TDoc> tableName jsonPath sqlProps =
Custom.list<'TDoc>
(Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] fromData<'TDoc> sqlProps
- /// Retrieve documents matching a JSON Path match query (@?)
+ /// Retrieve documents matching a JSON Path match query (@?)
/// The table from which documents should be retrieved (may include schema)
/// The JSON Path expression to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given JSON Path expression
let ByJsonPath<'TDoc>(tableName, jsonPath, sqlProps) =
Custom.List<'TDoc>(
@@ -445,12 +506,12 @@ module Find =
sqlProps)
///
- /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
+ /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
///
/// The table from which documents should be retrieved (may include schema)
/// The JSON Path expression to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given JSON Path expression, ordered by the given fields
[]
let byJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
@@ -461,12 +522,12 @@ module Find =
sqlProps
///
- /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
+ /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document
///
/// The table from which documents should be retrieved (may include schema)
/// The JSON Path expression to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
/// All documents matching the given JSON Path expression, ordered by the given fields
let ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, sqlProps) =
Custom.List<'TDoc>(
@@ -475,12 +536,12 @@ module Find =
fromData<'TDoc>,
sqlProps)
- /// Retrieve the first document matching JSON field comparisons (->> =, etc.)
+ /// Retrieve the first document matching JSON field comparisons (->> =, etc.)
/// The table from which a document should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
- /// Some with the first document, or None if not found
+ /// The SqlProps to use to execute the query
+ /// Some with the first document, or None if not found
[]
let firstByFields<'TDoc> tableName howMatched fields sqlProps =
Custom.single<'TDoc>
@@ -489,12 +550,12 @@ module Find =
fromData<'TDoc>
sqlProps
- /// Retrieve the first document matching JSON field comparisons (->> =, etc.)
+ /// Retrieve the first document matching JSON field comparisons (->> =, etc.)
/// The table from which a document should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
- /// The first document, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document, or null if not found
let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields, sqlProps) =
Custom.Single<'TDoc>(
$"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1",
@@ -503,17 +564,15 @@ module Find =
sqlProps)
///
- /// Retrieve the first document matching JSON field comparisons (->> =, etc.) ordered by the given
+ /// Retrieve the first document matching JSON field comparisons (->> =, etc.) ordered by the given
/// fields in the document
///
/// The table from which a document should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- ///
- /// Some with the first document ordered by the given fields, or None if not found
- ///
+ /// The SqlProps to use to execute the query
+ /// Some with the first document ordered by the given fields, or None if not found
[]
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps =
Custom.single<'TDoc>
@@ -523,15 +582,15 @@ module Find =
sqlProps
///
- /// Retrieve the first document matching JSON field comparisons (->> =, etc.) ordered by the given
+ /// Retrieve the first document matching JSON field comparisons (->> =, etc.) ordered by the given
/// fields in the document
///
/// The table from which a document should be retrieved (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- /// The first document ordered by the given fields, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document ordered by the given fields, or null if not found
let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
tableName, howMatched, queryFields, orderFields, sqlProps) =
Custom.Single<'TDoc>(
@@ -540,11 +599,11 @@ module Find =
fromData<'TDoc>,
sqlProps)
- /// Retrieve the first document matching a JSON containment query (@>)
+ /// Retrieve the first document matching a JSON containment query (@>)
/// The table from which a document should be retrieved (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
- /// Some with the first document, or None if not found
+ /// The SqlProps to use to execute the query
+ /// Some with the first document, or None if not found
[]
let firstByContains<'TDoc> tableName (criteria: obj) sqlProps =
Custom.single<'TDoc>
@@ -553,11 +612,11 @@ module Find =
fromData<'TDoc>
sqlProps
- /// Retrieve the first document matching a JSON containment query (@>)
+ /// Retrieve the first document matching a JSON containment query (@>)
/// The table from which a document should be retrieved (may include schema)
/// The document to match with the containment query
- /// The SqlProps to use to execute the query
- /// The first document, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document, or null if not found
let FirstByContains<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, criteria: obj, sqlProps) =
Custom.Single<'TDoc>(
$"{Query.byContains (Query.find tableName)} LIMIT 1",
@@ -566,16 +625,14 @@ module Find =
sqlProps)
///
- /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in
- /// the document
+ /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the
+ /// document
///
/// The table from which a document should be retrieved (may include schema)
/// The document to match with the containment query
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- ///
- /// Some with the first document ordered by the given fields, or None if not found
- ///
+ /// The SqlProps to use to execute the query
+ /// Some with the first document ordered by the given fields, or None if not found
[]
let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps =
Custom.single<'TDoc>
@@ -585,14 +642,14 @@ module Find =
sqlProps
///
- /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in
- /// the document
+ /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the
+ /// document
///
/// The table from which a document should be retrieved (may include schema)
/// The document to match with the containment query
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- /// The first document ordered by the given fields, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document ordered by the given fields, or null if not found
let FirstByContainsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
tableName, criteria: obj, orderFields, sqlProps) =
Custom.Single<'TDoc>(
@@ -601,11 +658,11 @@ module Find =
fromData<'TDoc>,
sqlProps)
- /// Retrieve the first document matching a JSON Path match query (@?)
+ /// Retrieve the first document matching a JSON Path match query (@?)
/// The table from which a document should be retrieved (may include schema)
/// The JSON Path expression to match
- /// The SqlProps to use to execute the query
- /// Some with the first document, or None if not found
+ /// The SqlProps to use to execute the query
+ /// Some with the first document, or None if not found
[]
let firstByJsonPath<'TDoc> tableName jsonPath sqlProps =
Custom.single<'TDoc>
@@ -614,11 +671,11 @@ module Find =
fromData<'TDoc>
sqlProps
- /// Retrieve the first document matching a JSON Path match query (@?)
+ /// Retrieve the first document matching a JSON Path match query (@?)
/// The table from which a document should be retrieved (may include schema)
/// The JSON Path expression to match
- /// The SqlProps to use to execute the query
- /// The first document, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document, or null if not found
let FirstByJsonPath<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, jsonPath, sqlProps) =
Custom.Single<'TDoc>(
$"{Query.byPathMatch (Query.find tableName)} LIMIT 1",
@@ -627,16 +684,14 @@ module Find =
sqlProps)
///
- /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
+ /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
/// document
///
/// The table from which a document should be retrieved (may include schema)
/// The JSON Path expression to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- ///
- /// Some with the first document ordered by the given fields, or None if not found
- ///
+ /// The SqlProps to use to execute the query
+ /// Some with the first document ordered by the given fields, or None if not found
[]
let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps =
Custom.single<'TDoc>
@@ -646,14 +701,14 @@ module Find =
sqlProps
///
- /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
+ /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the
/// document
///
/// The table from which a document should be retrieved (may include schema)
/// The JSON Path expression to match
/// Fields by which the results should be ordered
- /// The SqlProps to use to execute the query
- /// The first document ordered by the given fields, or null if not found
+ /// The SqlProps to use to execute the query
+ /// The first document ordered by the given fields, or null if not found
let FirstByJsonPathOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
tableName, jsonPath, orderFields, sqlProps) =
Custom.Single<'TDoc>(
@@ -662,6 +717,406 @@ module Find =
fromData<'TDoc>,
sqlProps)
+/// Commands to retrieve documents as JSON
+[]
+module Json =
+
+ /// Retrieve all documents in the given table as a JSON array
+ /// The table from which documents should be retrieved (may include schema)
+ /// The SqlProps to use to execute the query
+ /// All documents from the given table as a JSON array
+ []
+ let all tableName sqlProps =
+ Custom.jsonArray (Query.find tableName) [] jsonFromData sqlProps
+
+ /// Write all documents in the given table to the given StreamWriter
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The SqlProps to use to execute the query
+ /// All documents from the given table as a JSON array
+ []
+ let writeAll tableName writer sqlProps =
+ Custom.writeJsonArray (Query.find tableName) [] writer jsonFromData sqlProps
+
+ ///
+ /// Retrieve all documents in the given table as a JSON array, ordered by the given fields in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// All documents from the given table as a JSON array, ordered by the given fields
+ []
+ let allOrdered tableName orderFields sqlProps =
+ Custom.jsonArray (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] jsonFromData sqlProps
+
+ ///
+ /// Write all documents in the given table to the given StreamWriter, ordered by the given fields in the
+ /// document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// All documents from the given table as a JSON array, ordered by the given fields
+ []
+ let writeAllOrdered tableName writer orderFields sqlProps =
+ Custom.writeJsonArray
+ (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] writer jsonFromData sqlProps
+
+ /// Retrieve a JSON document by its ID
+ /// The table from which a document should be retrieved (may include schema)
+ /// The ID of the document to retrieve
+ /// The SqlProps to use to execute the query
+ /// The JSON document if found, an empty JSON document otherwise
+ []
+ let byId<'TKey> tableName (docId: 'TKey) sqlProps =
+ Custom.jsonSingle (Query.byId (Query.find tableName) docId) [ idParam docId ] jsonFromData sqlProps
+
+ /// Write a JSON document to the given StreamWriter by its ID
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The ID of the document to retrieve
+ /// The SqlProps to use to execute the query
+ []
+ let writeById<'TKey> tableName (writer: StreamWriter) (docId: 'TKey) sqlProps =
+ byId tableName docId sqlProps |> writer.Write
+
+ /// Retrieve JSON documents matching JSON field comparisons (->> =, etc.)
+ /// The table from which documents should be retrieved (may include schema)
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// The SqlProps to use to execute the query
+ /// All JSON documents matching the given fields
+ []
+ let byFields tableName howMatched fields sqlProps =
+ Custom.jsonArray
+ (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching JSON field comparisons (->> =, etc.)
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// The SqlProps to use to execute the query
+ []
+ let writeByFields tableName writer howMatched fields sqlProps =
+ Custom.writeJsonArray
+ (Query.byFields (Query.find tableName) howMatched fields)
+ (addFieldParams fields [])
+ writer
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Retrieve JSON documents matching JSON field comparisons (->> =, etc.) ordered by the given fields
+ /// in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// All JSON documents matching the given fields, ordered by the other given fields
+ []
+ let byFieldsOrdered tableName howMatched queryFields orderFields sqlProps =
+ Custom.jsonArray
+ (Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL)
+ (addFieldParams queryFields [])
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching JSON field comparisons (->> =, etc.)
+ /// ordered by the given fields in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeByFieldsOrdered tableName writer howMatched queryFields orderFields sqlProps =
+ Custom.writeJsonArray
+ (Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL)
+ (addFieldParams queryFields [])
+ writer
+ jsonFromData
+ sqlProps
+
+ /// Retrieve JSON documents matching a JSON containment query (@>)
+ /// The table from which documents should be retrieved (may include schema)
+ /// The document to match with the containment query
+ /// The SqlProps to use to execute the query
+ /// All JSON documents matching the given containment query
+ []
+ let byContains tableName (criteria: obj) sqlProps =
+ Custom.jsonArray
+ (Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching a JSON containment query (@>)
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The document to match with the containment query
+ /// The SqlProps to use to execute the query
+ []
+ let writeByContains tableName writer (criteria: obj) sqlProps =
+ Custom.writeJsonArray
+ (Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] writer jsonFromData sqlProps
+
+ ///
+ /// Retrieve JSON documents matching a JSON containment query (@>) ordered by the given fields in the
+ /// document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The document to match with the containment query
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// All documents matching the given containment query, ordered by the given fields
+ []
+ let byContainsOrdered tableName (criteria: obj) orderFields sqlProps =
+ Custom.jsonArray
+ (Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
+ [ jsonParam "@criteria" criteria ]
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching a JSON containment query (@>) ordered
+ /// by the given fields in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The document to match with the containment query
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeByContainsOrdered tableName writer (criteria: obj) orderFields sqlProps =
+ Custom.writeJsonArray
+ (Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
+ [ jsonParam "@criteria" criteria ]
+ writer
+ jsonFromData
+ sqlProps
+
+ /// Retrieve JSON documents matching a JSON Path match query (@?)
+ /// The table from which documents should be retrieved (may include schema)
+ /// The JSON Path expression to match
+ /// The SqlProps to use to execute the query
+ /// All JSON documents matching the given JSON Path expression
+ []
+ let byJsonPath tableName jsonPath sqlProps =
+ Custom.jsonArray
+ (Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching a JSON Path match query (@?)
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The JSON Path expression to match
+ /// The SqlProps to use to execute the query
+ []
+ let writeByJsonPath tableName writer jsonPath sqlProps =
+ Custom.writeJsonArray
+ (Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] writer jsonFromData sqlProps
+
+ ///
+ /// Retrieve JSON documents matching a JSON Path match query (@?) ordered by the given fields in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The JSON Path expression to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// All JSON documents matching the given JSON Path expression, ordered by the given fields
+ []
+ let byJsonPathOrdered tableName jsonPath orderFields sqlProps =
+ Custom.jsonArray
+ (Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
+ [ "@path", Sql.string jsonPath ]
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write JSON documents to the given StreamWriter matching a JSON Path match query (@?) ordered by
+ /// the given fields in the document
+ ///
+ /// The table from which documents should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The JSON Path expression to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeByJsonPathOrdered tableName writer jsonPath orderFields sqlProps =
+ Custom.writeJsonArray
+ (Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL)
+ [ "@path", Sql.string jsonPath ]
+ writer
+ jsonFromData
+ sqlProps
+
+ /// Retrieve the first JSON document matching JSON field comparisons (->> =, etc.)
+ /// The table from which a document should be retrieved (may include schema)
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByFields tableName howMatched fields sqlProps =
+ Custom.jsonSingle
+ (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) jsonFromData sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching JSON field comparisons
+ /// (->> =, etc.)
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByFields tableName (writer: StreamWriter) howMatched fields sqlProps =
+ firstByFields tableName howMatched fields sqlProps |> writer.Write
+
+ ///
+ /// Retrieve the first JSON document matching JSON field comparisons (->> =, etc.) ordered by the given
+ /// fields in the document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps =
+ Custom.jsonSingle
+ $"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL}"
+ (addFieldParams queryFields [])
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching JSON field comparisons
+ /// (->> =, etc.) ordered by the given fields in the document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// Whether to match any or all of the field conditions
+ /// The field conditions to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByFieldsOrdered tableName (writer: StreamWriter) howMatched queryFields orderFields sqlProps =
+ firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps |> writer.Write
+
+ /// Retrieve the first JSON document matching a JSON containment query (@>)
+ /// The table from which a document should be retrieved (may include schema)
+ /// The document to match with the containment query
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByContains tableName (criteria: obj) sqlProps =
+ Custom.jsonSingle
+ (Query.byContains (Query.find tableName)) [ jsonParam "@criteria" criteria ] jsonFromData sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching a JSON containment query (@>)
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The document to match with the containment query
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByContains tableName (writer: StreamWriter) (criteria: obj) sqlProps =
+ firstByContains tableName criteria sqlProps |> writer.Write
+
+ ///
+ /// Retrieve the first JSON document matching a JSON containment query (@>) ordered by the given fields in
+ /// the document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The document to match with the containment query
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByContainsOrdered tableName (criteria: obj) orderFields sqlProps =
+ Custom.jsonSingle
+ $"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL}"
+ [ jsonParam "@criteria" criteria ]
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching a JSON containment query (@>)
+ /// ordered by the given fields in the document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The document to match with the containment query
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByContainsOrdered tableName (writer: StreamWriter) (criteria: obj) orderFields sqlProps =
+ firstByContainsOrdered tableName criteria orderFields sqlProps |> writer.Write
+
+ /// Retrieve the first JSON document matching a JSON Path match query (@?)
+ /// The table from which a document should be retrieved (may include schema)
+ /// The JSON Path expression to match
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByJsonPath tableName jsonPath sqlProps =
+ Custom.jsonSingle
+ (Query.byPathMatch (Query.find tableName)) [ "@path", Sql.string jsonPath ] jsonFromData sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching a JSON Path match query (@?)
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The JSON Path expression to match
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByJsonPath tableName (writer: StreamWriter) jsonPath sqlProps =
+ firstByJsonPath tableName jsonPath sqlProps |> writer.Write
+
+ ///
+ /// Retrieve the first JSON document matching a JSON Path match query (@?) ordered by the given fields in the
+ /// document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The JSON Path expression to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ /// The first matching JSON document if found, an empty JSON document otherwise
+ []
+ let firstByJsonPathOrdered tableName jsonPath orderFields sqlProps =
+ Custom.jsonSingle
+ $"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL}"
+ [ "@path", Sql.string jsonPath ]
+ jsonFromData
+ sqlProps
+
+ ///
+ /// Write the first JSON document to the given StreamWriter matching a JSON Path match query (@?)
+ /// ordered by the given fields in the document
+ ///
+ /// The table from which a document should be retrieved (may include schema)
+ /// The StreamWriter to which the results should be written
+ /// The JSON Path expression to match
+ /// Fields by which the results should be ordered
+ /// The SqlProps to use to execute the query
+ []
+ let writeFirstByJsonPathOrdered tableName (writer: StreamWriter) jsonPath orderFields sqlProps =
+ firstByJsonPathOrdered tableName jsonPath orderFields sqlProps |> writer.Write
+
/// Commands to update documents
[]
module Update =
@@ -670,7 +1125,7 @@ module Update =
/// The table in which a document should be updated (may include schema)
/// The ID of the document to be updated (replaced)
/// The new document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byId tableName (docId: 'TKey) (document: 'TDoc) sqlProps =
Custom.nonQuery
@@ -682,7 +1137,7 @@ module Update =
/// The table in which a document should be updated (may include schema)
/// The function to obtain the ID of the document
/// The new document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) sqlProps =
byId tableName (idFunc document) document sqlProps
@@ -693,7 +1148,7 @@ module Update =
/// The table in which a document should be updated (may include schema)
/// The function to obtain the ID of the document
/// The new document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, sqlProps) =
byFunc tableName idFunc.Invoke document sqlProps
@@ -705,20 +1160,20 @@ module Patch =
/// The table in which a document should be patched (may include schema)
/// The ID of the document to patch
/// The partial document to patch the existing document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byId tableName (docId: 'TKey) (patch: 'TPatch) sqlProps =
Custom.nonQuery
(Query.byId (Query.patch tableName) docId) [ idParam docId; jsonParam "@data" patch ] sqlProps
///
- /// Patch documents using a JSON field comparison query in the WHERE clause (->> =, etc.)
+ /// Patch documents using a JSON field comparison query in the WHERE clause (->> =, etc.)
///
/// The table in which documents should be patched (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// The partial document to patch the existing document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byFields tableName howMatched fields (patch: 'TPatch) sqlProps =
Custom.nonQuery
@@ -726,11 +1181,11 @@ module Patch =
(addFieldParams fields [ jsonParam "@data" patch ])
sqlProps
- /// Patch documents using a JSON containment query in the WHERE clause (@>)
+ /// Patch documents using a JSON containment query in the WHERE clause (@>)
/// The table in which documents should be patched (may include schema)
/// The document to match the containment query
/// The partial document to patch the existing document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byContains tableName (criteria: 'TContains) (patch: 'TPatch) sqlProps =
Custom.nonQuery
@@ -738,11 +1193,11 @@ module Patch =
[ jsonParam "@data" patch; jsonParam "@criteria" criteria ]
sqlProps
- /// Patch documents using a JSON Path match query in the WHERE clause (@?)
+ /// Patch documents using a JSON Path match query in the WHERE clause (@?)
/// The table in which documents should be patched (may include schema)
/// The JSON Path expression to match
/// The partial document to patch the existing document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byJsonPath tableName jsonPath (patch: 'TPatch) sqlProps =
Custom.nonQuery
@@ -758,7 +1213,7 @@ module RemoveFields =
/// The table in which a document should be modified (may include schema)
/// The ID of the document to modify
/// One or more field names to remove from the document
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byId tableName (docId: 'TKey) fieldNames sqlProps =
Custom.nonQuery
@@ -769,7 +1224,7 @@ module RemoveFields =
/// Whether to match any or all of the field conditions
/// The field conditions to match
/// One or more field names to remove from the matching documents
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byFields tableName howMatched fields fieldNames sqlProps =
Custom.nonQuery
@@ -777,11 +1232,11 @@ module RemoveFields =
(addFieldParams fields [ fieldNameParams fieldNames ])
sqlProps
- /// Remove fields from documents via a JSON containment query (@>)
+ /// Remove fields from documents via a JSON containment query (@>)
/// The table in which documents should be modified (may include schema)
/// The document to match the containment query
/// One or more field names to remove from the matching documents
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byContains tableName (criteria: 'TContains) fieldNames sqlProps =
Custom.nonQuery
@@ -789,11 +1244,11 @@ module RemoveFields =
[ jsonParam "@criteria" criteria; fieldNameParams fieldNames ]
sqlProps
- /// Remove fields from documents via a JSON Path match query (@?)
+ /// Remove fields from documents via a JSON Path match query (@?)
/// The table in which documents should be modified (may include schema)
/// The JSON Path expression to match
/// One or more field names to remove from the matching documents
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byJsonPath tableName jsonPath fieldNames sqlProps =
Custom.nonQuery
@@ -808,33 +1263,33 @@ module Delete =
/// Delete a document by its ID
/// The table in which a document should be deleted (may include schema)
/// The ID of the document to delete
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byId tableName (docId: 'TKey) sqlProps =
Custom.nonQuery (Query.byId (Query.delete tableName) docId) [ idParam docId ] sqlProps
- /// Delete documents by matching a JSON field comparison query (->> =, etc.)
+ /// Delete documents by matching a JSON field comparison query (->> =, etc.)
/// The table in which documents should be deleted (may include schema)
/// Whether to match any or all of the field conditions
/// The field conditions to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byFields tableName howMatched fields sqlProps =
Custom.nonQuery
(Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) sqlProps
- /// Delete documents by matching a JSON contains query (@>)
+ /// Delete documents by matching a JSON contains query (@>)
/// The table in which documents should be deleted (may include schema)
/// The document to match the containment query
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byContains tableName (criteria: 'TCriteria) sqlProps =
Custom.nonQuery (Query.byContains (Query.delete tableName)) [ jsonParam "@criteria" criteria ] sqlProps
- /// Delete documents by matching a JSON Path match query (@?)
+ /// Delete documents by matching a JSON Path match query (@?)
/// The table in which documents should be deleted (may include schema)
/// The JSON Path expression to match
- /// The SqlProps to use to execute the query
+ /// The SqlProps to use to execute the query
[]
let byJsonPath tableName jsonPath sqlProps =
Custom.nonQuery (Query.byPathMatch (Query.delete tableName)) [ "@path", Sql.string jsonPath ] sqlProps