Add Json to Postgres WithProps

This commit is contained in:
Daniel J. Summers 2025-04-04 20:25:25 -04:00
parent 5580284910
commit f7ab1f1445
2 changed files with 648 additions and 132 deletions

View File

@ -1,5 +1,9 @@
namespace BitBadger.Documents.Postgres
open System.IO
open System.Text
open Npgsql.FSharp
/// <summary>The type of index to generate for the document</summary>
[<Struct>]
type DocumentIndex =
@ -302,3 +306,60 @@ module Results =
[<CompiledName "ToExists">]
let toExists (row: RowReader) =
row.bool "it"
/// <summary>Extract a JSON document, specifying the field in which the document is found</summary>
/// <param name="field">The field name containing the JSON document</param>
/// <param name="row">A row reader set to the row with the document to be extracted</param>
/// <returns>The JSON from the given field (an empty object if no field exists)</returns>
[<CompiledName "JsonFromDocument">]
let jsonFromDocument field (row: RowReader) =
row.stringOrNone field |> Option.defaultValue "{}"
/// <summary>Extract a JSON document</summary>
/// <param name="row">A row reader set to the row with the document to be extracted</param>
/// <returns>The JSON from the row (an empty object if no field exists)</returns>
[<CompiledName "JsonFromData">]
let jsonFromData row =
jsonFromDocument "data" row
/// <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>
/// <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 output = StringBuilder("[")
sqlProps
|> Sql.iter (fun it ->
if output.Length > 2 then ignore (output.Append ",")
mapFunc it |> output.Append |> ignore)
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>
/// <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>
let ToJsonArray(mapFunc: System.Func<RowReader, string>, sqlProps) =
toJsonArray mapFunc.Invoke sqlProps
/// <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>
/// <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 mutable isFirst = true
sqlProps
|> Sql.iter (fun it ->
if isFirst then isFirst <- false else writer.Write ","
mapFunc it |> writer.Write)
writer.Write "]"
/// <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>
/// <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>
let WriteJsonArray(writer, mapFunc: System.Func<RowReader, string>, sqlProps) =
writeJsonArray writer mapFunc.Invoke sqlProps

File diff suppressed because it is too large Load Diff