Make Json calls and stream writes async

This commit is contained in:
2025-04-05 19:33:47 -04:00
parent 74961c4ba7
commit 120a59ff7f
4 changed files with 203 additions and 155 deletions

View File

@@ -326,13 +326,14 @@ module Results =
/// <param name="sqlProps">The query from which JSON should be extracted</param>
/// <returns>A JSON array as a string; no results will produce an empty array (<c>"[]"</c>)</returns>
[<CompiledName "FSharpToJsonArray">]
let toJsonArray (mapFunc: RowReader -> string) sqlProps =
let toJsonArray (mapFunc: RowReader -> string) sqlProps = backgroundTask {
let output = StringBuilder("[")
sqlProps
|> Sql.iter (fun it ->
if output.Length > 2 then ignore (output.Append ",")
mapFunc it |> output.Append |> ignore)
output.Append("]").ToString()
do! sqlProps
|> Sql.iterAsync (fun it ->
if output.Length > 2 then ignore (output.Append ",")
mapFunc it |> output.Append |> ignore)
return output.Append("]").ToString()
}
/// <summary>Create a JSON array of items for the results of a query</summary>
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
@@ -346,14 +347,15 @@ module Results =
/// <param name="mapFunc">The mapping function to extract JSON from the query's results</param>
/// <param name="sqlProps">The query from which JSON should be extracted</param>
[<CompiledName "FSharpWriteJsonArray">]
let writeJsonArray (writer: StreamWriter) (mapFunc: RowReader -> string) sqlProps =
writer.Write "["
let writeJsonArray (writer: StreamWriter) (mapFunc: RowReader -> string) sqlProps = backgroundTask {
do! writer.WriteAsync "["
let mutable isFirst = true
sqlProps
|> Sql.iter (fun it ->
if isFirst then isFirst <- false else writer.Write ","
mapFunc it |> writer.Write)
writer.Write "]"
do! sqlProps
|> Sql.iterAsync (fun it ->
if isFirst then isFirst <- false else writer.Write ","
writer.WriteAsync(mapFunc it).ConfigureAwait(false).GetAwaiter().GetResult())
do! writer.WriteAsync "]"
}
/// <summary>Write a JSON array of items for the results of a query to the given <c>StreamWriter</c></summary>
/// <param name="writer">The StreamWriter to which results should be written</param>

View File

@@ -107,9 +107,10 @@ module Custom =
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
/// <returns>The JSON document with the first matching result, or an empty document if not found</returns>
[<CompiledName "FSharpJsonSingle">]
let jsonSingle query parameters mapFunc sqlProps =
let results = jsonArray $"%s{query} LIMIT 1" parameters mapFunc sqlProps
if results = "[]" then "{}" else results[1..results.Length - 2]
let jsonSingle query parameters mapFunc sqlProps = backgroundTask {
let! results = jsonArray $"%s{query} LIMIT 1" parameters mapFunc sqlProps
return if results = "[]" then "{}" else results[1..results.Length - 2]
}
/// <summary>Execute a query that returns one or no JSON documents</summary>
/// <param name="query">The query to retrieve the results</param>
@@ -972,8 +973,10 @@ module Json =
/// <param name="fields">The field conditions to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFields">]
let writeFirstByFields tableName (writer: StreamWriter) howMatched fields sqlProps =
firstByFields tableName howMatched fields sqlProps |> writer.Write
let writeFirstByFields tableName (writer: StreamWriter) howMatched fields sqlProps = backgroundTask {
let! json = firstByFields tableName howMatched fields sqlProps
do! writer.WriteAsync json
}
/// <summary>
/// Retrieve the first JSON document matching JSON field comparisons (<c>-&gt;&gt; =</c>, etc.) ordered by the given
@@ -1005,7 +1008,10 @@ module Json =
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByFieldsOrdered">]
let writeFirstByFieldsOrdered tableName (writer: StreamWriter) howMatched queryFields orderFields sqlProps =
firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps |> writer.Write
backgroundTask {
let! json = firstByFieldsOrdered tableName howMatched queryFields orderFields sqlProps
do! writer.WriteAsync json
}
/// <summary>Retrieve the first JSON document matching a JSON containment query (<c>@&gt;</c>)</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
@@ -1025,8 +1031,10 @@ module Json =
/// <param name="criteria">The document to match with the containment query</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByContains">]
let writeFirstByContains tableName (writer: StreamWriter) (criteria: obj) sqlProps =
firstByContains tableName criteria sqlProps |> writer.Write
let writeFirstByContains tableName (writer: StreamWriter) (criteria: obj) sqlProps = backgroundTask {
let! json = firstByContains tableName criteria sqlProps
do! writer.WriteAsync json
}
/// <summary>
/// Retrieve the first JSON document matching a JSON containment query (<c>@&gt;</c>) ordered by the given fields in
@@ -1056,7 +1064,10 @@ module Json =
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByContainsOrdered">]
let writeFirstByContainsOrdered tableName (writer: StreamWriter) (criteria: obj) orderFields sqlProps =
firstByContainsOrdered tableName criteria orderFields sqlProps |> writer.Write
backgroundTask {
let! json = firstByContainsOrdered tableName criteria orderFields sqlProps
do! writer.WriteAsync json
}
/// <summary>Retrieve the first JSON document matching a JSON Path match query (<c>@?</c>)</summary>
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
@@ -1076,8 +1087,10 @@ module Json =
/// <param name="jsonPath">The JSON Path expression to match</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByJsonPath">]
let writeFirstByJsonPath tableName (writer: StreamWriter) jsonPath sqlProps =
firstByJsonPath tableName jsonPath sqlProps |> writer.Write
let writeFirstByJsonPath tableName (writer: StreamWriter) jsonPath sqlProps = backgroundTask {
let! json = firstByJsonPath tableName jsonPath sqlProps
do! writer.WriteAsync json
}
/// <summary>
/// Retrieve the first JSON document matching a JSON Path match query (<c>@?</c>) ordered by the given fields in the
@@ -1106,8 +1119,10 @@ module Json =
/// <param name="orderFields">Fields by which the results should be ordered</param>
/// <param name="sqlProps">The <c>SqlProps</c> to use to execute the query</param>
[<CompiledName "WriteFirstByJsonPathOrdered">]
let writeFirstByJsonPathOrdered tableName (writer: StreamWriter) jsonPath orderFields sqlProps =
firstByJsonPathOrdered tableName jsonPath orderFields sqlProps |> writer.Write
let writeFirstByJsonPathOrdered tableName (writer: StreamWriter) jsonPath orderFields sqlProps = backgroundTask {
let! json = firstByJsonPathOrdered tableName jsonPath orderFields sqlProps
do! writer.WriteAsync json
}
/// <summary>Commands to update documents</summary>
[<RequireQualifiedAccess>]