namespace BitBadger.Documents.Postgres /// The type of index to generate for the document [] type DocumentIndex = /// A GIN index with standard operations (all operators supported) | Full /// A GIN index with JSONPath operations (optimized for @>, @?, @@ operators) | Optimized open Npgsql /// Configuration for document handling module Configuration = /// The data source to use for query execution let mutable private dataSourceValue : NpgsqlDataSource option = None /// Register a data source to use for query execution (disposes the current one if it exists) [] let useDataSource source = if Option.isSome dataSourceValue then dataSourceValue.Value.Dispose() dataSourceValue <- Some source /// Retrieve the currently configured data source [] let dataSource () = match dataSourceValue with | Some source -> source | None -> invalidOp "Please provide a data source before attempting data access" open Npgsql.FSharp /// Helper functions [] module private Helpers = /// Shorthand to retrieve the data source as SqlProps let internal fromDataSource () = Configuration.dataSource () |> Sql.fromDataSource /// Execute a task and ignore the result let internal ignoreTask<'T> (it : System.Threading.Tasks.Task<'T>) = backgroundTask { let! _ = it () } /// Create a number or string parameter, or use the given parameter derivation function if non-(numeric or string) let internal parameterFor<'T> (value: 'T) (catchAllFunc: 'T -> SqlValue) = match box value with | :? int8 as it -> Sql.int8 it | :? uint8 as it -> Sql.int8 (int8 it) | :? int16 as it -> Sql.int16 it | :? uint16 as it -> Sql.int16 (int16 it) | :? int as it -> Sql.int it | :? uint32 as it -> Sql.int (int it) | :? int64 as it -> Sql.int64 it | :? uint64 as it -> Sql.int64 (int64 it) | :? decimal as it -> Sql.decimal it | :? single as it -> Sql.double (double it) | :? double as it -> Sql.double it | :? string as it -> Sql.string it | _ -> catchAllFunc value open BitBadger.Documents /// Functions for creating parameters [] module Parameters = /// Create an ID parameter (name "@id") [] let idParam (key: 'TKey) = "@id", parameterFor key (fun it -> Sql.string (string it)) /// Create a parameter with a JSON value [] let jsonParam (name: string) (it: 'TJson) = name, Sql.jsonb (Configuration.serializer().Serialize it) /// Create JSON field parameters [] let addFieldParams fields parameters = let name = ParameterName() fields |> Seq.map (fun it -> seq { match it.Comparison with | Exists | NotExists -> () | Between (min, max) -> let p = name.Derive it.ParameterName yield ($"{p}min", parameterFor min (fun v -> Sql.parameter (NpgsqlParameter($"{p}min", v)))) yield ($"{p}max", parameterFor max (fun v -> Sql.parameter (NpgsqlParameter($"{p}max", v)))) | In values -> let p = name.Derive it.ParameterName yield! values |> Seq.mapi (fun idx v -> let paramName = $"{p}_{idx}" paramName, Sql.parameter (NpgsqlParameter(paramName, v))) | InArray (_, values) -> let p = name.Derive it.ParameterName yield (p, Sql.stringArray (values |> Seq.map string |> Array.ofSeq)) | Equal v | Greater v | GreaterOrEqual v | Less v | LessOrEqual v | NotEqual v -> let p = name.Derive it.ParameterName yield (p, parameterFor v (fun l -> Sql.parameter (NpgsqlParameter(p, l)))) }) |> Seq.collect id |> Seq.append parameters |> Seq.toList |> Seq.ofList /// Append JSON field name parameters for the given field names to the given parameters [] let fieldNameParams (fieldNames: string seq) = if Seq.length fieldNames = 1 then "@name", Sql.string (Seq.head fieldNames) else "@name", Sql.stringArray (Array.ofSeq fieldNames) /// An empty parameter sequence [] let noParams = Seq.empty /// Query construction functions [] module Query = /// Create a WHERE clause fragment to implement a comparison on fields in a JSON document [] let whereByFields (howMatched: FieldMatch) fields = let name = ParameterName() let isNumeric (it: obj) = match it with | :? int8 | :? uint8 | :? int16 | :? uint16 | :? int | :? uint32 | :? int64 | :? uint64 | :? decimal | :? single | :? double -> true | _ -> false fields |> Seq.map (fun it -> match it.Comparison with | Exists | NotExists -> $"{it.Path PostgreSQL AsSql} {it.Comparison.OpSql}" | InArray _ -> $"{it.Path PostgreSQL AsJson} {it.Comparison.OpSql} {name.Derive it.ParameterName}" | _ -> let p = name.Derive it.ParameterName let param, value = match it.Comparison with | Between (min, _) -> $"{p}min AND {p}max", min | In values -> let paramNames = values |> Seq.mapi (fun idx _ -> $"{p}_{idx}") |> String.concat ", " $"({paramNames})", defaultArg (Seq.tryHead values) (obj ()) | Equal v | Greater v | GreaterOrEqual v | Less v | LessOrEqual v | NotEqual v -> p, v | _ -> p, "" if isNumeric value then $"({it.Path PostgreSQL AsSql})::numeric {it.Comparison.OpSql} {param}" else $"{it.Path PostgreSQL AsSql} {it.Comparison.OpSql} {param}") |> String.concat $" {howMatched} " /// Create a WHERE clause fragment to implement an ID-based query [] let whereById<'TKey> (docId: 'TKey) = whereByFields Any [ { Field.Equal (Configuration.idField ()) docId with ParameterName = Some "@id" } ] /// Table and index definition queries module Definition = /// SQL statement to create a document table [] let ensureTable name = Query.Definition.ensureTableFor name "JSONB" /// SQL statement to create an index on JSON documents in the specified table [] let ensureDocumentIndex (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}_document ON {name} USING GIN (data{extraOps})" /// Create a WHERE clause fragment to implement a @> (JSON contains) condition [] let whereDataContains paramName = $"data @> %s{paramName}" /// Create a WHERE clause fragment to implement a @? (JSON Path match) condition [] let whereJsonPathMatches paramName = $"data @? %s{paramName}::jsonpath" /// Create an UPDATE statement to patch documents [] let patch tableName = $"UPDATE %s{tableName} SET data = data || @data" /// Create an UPDATE statement to remove fields from documents [] let removeFields tableName = $"UPDATE %s{tableName} SET data = data - @name" /// Create a query by a document's ID [] let byId<'TKey> statement (docId: 'TKey) = Query.statementWhere statement (whereById docId) /// Create a query on JSON fields [] let byFields statement howMatched fields = Query.statementWhere statement (whereByFields howMatched fields) /// Create a JSON containment query [] let byContains statement = Query.statementWhere statement (whereDataContains "@criteria") /// Create a JSON Path match query [] let byPathMatch statement = Query.statementWhere statement (whereJsonPathMatches "@path") /// Functions for dealing with results [] module Results = /// Create a domain item from a document, specifying the field in which the document is found [] let fromDocument<'T> field (row: RowReader) : 'T = Configuration.serializer().Deserialize<'T>(row.string field) /// Create a domain item from a document [] let fromData<'T> row : 'T = 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 module WithProps = module FSharpList = Microsoft.FSharp.Collections.List /// Commands to execute custom SQL queries [] module Custom = /// Execute a query that returns a list of results [] let list<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) sqlProps = Sql.query query sqlProps |> Sql.parameters (List.ofSeq parameters) |> Sql.executeAsync mapFunc /// Execute a query that returns a list of results 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 one or no results; returns None if not found [] let single<'TDoc> query parameters mapFunc sqlProps = backgroundTask { let! results = list<'TDoc> 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<'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 no results [] let nonQuery query parameters sqlProps = Sql.query query sqlProps |> Sql.parameters (FSharpList.ofSeq parameters) |> Sql.executeNonQueryAsync |> ignoreTask /// Execute a query that returns a scalar value [] let scalar<'T when 'T: struct> query parameters (mapFunc: RowReader -> 'T) sqlProps = Sql.query query sqlProps |> Sql.parameters (FSharpList.ofSeq parameters) |> Sql.executeRowAsync mapFunc /// Execute a query that returns a scalar value let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func, sqlProps) = scalar<'T> query parameters mapFunc.Invoke sqlProps /// Table and index definition commands module Definition = /// Create a document table [] let ensureTable name sqlProps = backgroundTask { do! Custom.nonQuery (Query.Definition.ensureTable name) [] sqlProps do! Custom.nonQuery (Query.Definition.ensureKey name PostgreSQL) [] sqlProps } /// Create an index on documents in the specified table [] let ensureDocumentIndex name idxType sqlProps = Custom.nonQuery (Query.Definition.ensureDocumentIndex name idxType) [] sqlProps /// Create an index on field(s) within documents in the specified table [] let ensureFieldIndex tableName indexName fields sqlProps = Custom.nonQuery (Query.Definition.ensureIndexOn tableName indexName fields PostgreSQL) [] sqlProps /// Commands to add documents [] module Document = /// Insert a new document [] let insert<'TDoc> tableName (document: 'TDoc) sqlProps = let query = match Configuration.autoIdStrategy () with | Disabled -> Query.insert tableName | strategy -> let idField = Configuration.idField () let dataParam = if AutoId.NeedsAutoId strategy document idField then match strategy with | Number -> $"' || (SELECT COALESCE(MAX((data->>'{idField}')::numeric), 0) + 1 FROM {tableName}) || '" | Guid -> $"\"{AutoId.GenerateGuid()}\"" | RandomString -> $"\"{AutoId.GenerateRandomString(Configuration.idStringLength ())}\"" | Disabled -> "@data" |> function it -> $"""@data::jsonb || ('{{"{idField}":{it}}}')::jsonb""" else "@data" (Query.insert tableName).Replace("@data", dataParam) Custom.nonQuery query [ jsonParam "@data" document ] sqlProps /// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert") [] let save<'TDoc> tableName (document: 'TDoc) sqlProps = Custom.nonQuery (Query.save tableName) [ jsonParam "@data" document ] sqlProps /// Commands to count documents [] module Count = /// Count all documents in a table [] let all tableName sqlProps = Custom.scalar (Query.count tableName) [] toCount sqlProps /// Count matching documents using JSON field comparisons (->> =) [] 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 (@>) [] 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 (@?) [] let byJsonPath tableName jsonPath sqlProps = Custom.scalar (Query.byPathMatch (Query.count tableName)) [ "@path", Sql.string jsonPath ] toCount sqlProps /// Commands to determine if documents exist [] module Exists = /// Determine if a document exists for the given ID [] 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 (->> =) [] let byFields tableName howMatched fields sqlProps = Custom.scalar (Query.exists tableName (Query.whereByFields howMatched fields)) (addFieldParams fields []) toExists sqlProps /// Determine if a document exists using a JSON containment query (@>) [] let byContains tableName (criteria: 'TContains) sqlProps = Custom.scalar (Query.exists tableName (Query.whereDataContains "@criteria")) [ jsonParam "@criteria" criteria ] toExists sqlProps /// Determine if a document exists using a JSON Path match query (@?) [] let byJsonPath tableName jsonPath sqlProps = Custom.scalar (Query.exists tableName (Query.whereJsonPathMatches "@path")) [ "@path", Sql.string jsonPath ] toExists sqlProps /// Commands to determine if documents exist [] module Find = /// Retrieve all documents in the given table [] let all<'TDoc> tableName sqlProps = Custom.list<'TDoc> (Query.find tableName) [] fromData<'TDoc> sqlProps /// Retrieve all documents in the given table let All<'TDoc>(tableName, sqlProps) = Custom.List<'TDoc>(Query.find tableName, [], fromData<'TDoc>, sqlProps) /// Retrieve all documents in the given table ordered by the given fields in the document [] let allOrdered<'TDoc> tableName orderFields sqlProps = Custom.list<'TDoc> (Query.find tableName + Query.orderBy orderFields PostgreSQL) [] fromData<'TDoc> sqlProps /// Retrieve all documents in the given table ordered by the given fields in the document let AllOrdered<'TDoc>(tableName, orderFields, sqlProps) = Custom.List<'TDoc>( Query.find tableName + Query.orderBy orderFields PostgreSQL, [], fromData<'TDoc>, sqlProps) /// Retrieve a document by its ID (returns None if not found) [] let byId<'TKey, 'TDoc> tableName (docId: 'TKey) sqlProps = Custom.single (Query.byId (Query.find tableName) docId) [ idParam docId ] fromData<'TDoc> sqlProps /// Retrieve a document by its ID (returns null if not found) 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 (->> =) [] let byFields<'TDoc> tableName howMatched fields sqlProps = Custom.list<'TDoc> (Query.byFields (Query.find tableName) howMatched fields) (addFieldParams fields []) fromData<'TDoc> sqlProps /// Retrieve documents matching JSON field comparisons (->> =) let ByFields<'TDoc>(tableName, howMatched, fields, sqlProps) = Custom.List<'TDoc>( Query.byFields (Query.find tableName) howMatched fields, addFieldParams fields [], fromData<'TDoc>, sqlProps) /// Retrieve documents matching JSON field comparisons (->> =) ordered by the given fields in the document [] let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps = Custom.list<'TDoc> (Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL) (addFieldParams queryFields []) fromData<'TDoc> sqlProps /// Retrieve documents matching JSON field comparisons (->> =) ordered by the given fields in the document let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, sqlProps) = Custom.List<'TDoc>( Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields PostgreSQL, addFieldParams queryFields [], fromData<'TDoc>, sqlProps) /// Retrieve documents matching a JSON 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 (@>) 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 (@>) ordered by the given fields in the document [] let byContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps = Custom.list<'TDoc> (Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL) [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document let ByContainsOrdered<'TDoc>(tableName, criteria: obj, orderFields, sqlProps) = Custom.List<'TDoc>( Query.byContains (Query.find tableName) + Query.orderBy orderFields PostgreSQL, [ jsonParam "@criteria" criteria ], fromData<'TDoc>, sqlProps) /// Retrieve documents matching a JSON Path match query (@?) [] 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 (@?) 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 (@?) ordered by the given fields in the document [] let byJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps = Custom.list<'TDoc> (Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL) [ "@path", Sql.string jsonPath ] fromData<'TDoc> sqlProps /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document let ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, sqlProps) = Custom.List<'TDoc>( Query.byPathMatch (Query.find tableName) + Query.orderBy orderFields PostgreSQL, [ "@path", Sql.string jsonPath ], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching JSON field comparisons (->> =); returns None if not found [] let firstByFields<'TDoc> tableName howMatched fields sqlProps = Custom.single<'TDoc> $"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1" (addFieldParams fields []) fromData<'TDoc> sqlProps /// Retrieve the first document matching JSON field comparisons (->> =); returns 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", addFieldParams fields [], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching JSON field comparisons (->> =) ordered by the given fields in the /// document; returns None if not found [] let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields sqlProps = Custom.single<'TDoc> $"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1" (addFieldParams queryFields []) fromData<'TDoc> sqlProps /// Retrieve the first document matching JSON field comparisons (->> =) ordered by the given fields in the /// document; returns null if not found let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>( tableName, howMatched, queryFields, orderFields, sqlProps) = Custom.Single<'TDoc>( $"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields PostgreSQL} LIMIT 1", addFieldParams queryFields [], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found [] let firstByContains<'TDoc> tableName (criteria: obj) sqlProps = Custom.single<'TDoc> $"{Query.byContains (Query.find 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 and 'TDoc: not struct>(tableName, criteria: obj, sqlProps) = Custom.Single<'TDoc>( $"{Query.byContains (Query.find tableName)} LIMIT 1", [ jsonParam "@criteria" criteria ], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the /// document; returns None if not found [] let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields sqlProps = Custom.single<'TDoc> $"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1" [ jsonParam "@criteria" criteria ] fromData<'TDoc> sqlProps /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the /// document; returns null if not found let FirstByContainsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>( tableName, criteria: obj, orderFields, sqlProps) = Custom.Single<'TDoc>( $"{Query.byContains (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1", [ jsonParam "@criteria" criteria ], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching a JSON Path match query (@?); returns None if not found [] let firstByJsonPath<'TDoc> tableName jsonPath sqlProps = Custom.single<'TDoc> $"{Query.byPathMatch (Query.find 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 and 'TDoc: not struct>(tableName, jsonPath, sqlProps) = Custom.Single<'TDoc>( $"{Query.byPathMatch (Query.find tableName)} LIMIT 1", [ "@path", Sql.string jsonPath ], fromData<'TDoc>, sqlProps) /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the /// document; returns None if not found [] let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields sqlProps = Custom.single<'TDoc> $"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1" [ "@path", Sql.string jsonPath ] fromData<'TDoc> sqlProps /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the /// document; returns null if not found let FirstByJsonPathOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>( tableName, jsonPath, orderFields, sqlProps) = Custom.Single<'TDoc>( $"{Query.byPathMatch (Query.find tableName)}{Query.orderBy orderFields PostgreSQL} LIMIT 1", [ "@path", Sql.string jsonPath ], fromData<'TDoc>, sqlProps) /// Commands to update documents [] module Update = /// Update an entire document by its ID [] let byId tableName (docId: 'TKey) (document: 'TDoc) sqlProps = Custom.nonQuery (Query.byId (Query.update tableName) docId) [ idParam docId; jsonParam "@data" document ] sqlProps /// Update an entire document by its ID, using the provided function to obtain the ID from the document [] let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) sqlProps = byId tableName (idFunc document) document sqlProps /// Update an entire document by its ID, using the provided function to obtain the ID from the document let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, sqlProps) = byFunc tableName idFunc.Invoke document sqlProps /// Commands to patch (partially update) documents [] module Patch = /// Patch a document by its ID [] 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 (->> =) [] let byFields tableName howMatched fields (patch: 'TPatch) sqlProps = Custom.nonQuery (Query.byFields (Query.patch tableName) howMatched fields) (addFieldParams fields [ jsonParam "@data" patch ]) sqlProps /// Patch documents using a JSON containment query in the WHERE clause (@>) [] let byContains tableName (criteria: 'TContains) (patch: 'TPatch) sqlProps = Custom.nonQuery (Query.byContains (Query.patch tableName)) [ jsonParam "@data" patch; jsonParam "@criteria" criteria ] sqlProps /// Patch documents using a JSON Path match query in the WHERE clause (@?) [] let byJsonPath tableName jsonPath (patch: 'TPatch) sqlProps = Custom.nonQuery (Query.byPathMatch (Query.patch tableName)) [ jsonParam "@data" patch; "@path", Sql.string jsonPath ] sqlProps /// Commands to remove fields from documents [] module RemoveFields = /// Remove fields from a document by the document's ID [] let byId tableName (docId: 'TKey) fieldNames sqlProps = Custom.nonQuery (Query.byId (Query.removeFields tableName) docId) [ idParam docId; fieldNameParams fieldNames ] sqlProps /// Remove fields from documents via a comparison on JSON fields in the document [] let byFields tableName howMatched fields fieldNames sqlProps = Custom.nonQuery (Query.byFields (Query.removeFields tableName) howMatched fields) (addFieldParams fields [ fieldNameParams fieldNames ]) sqlProps /// Remove fields from documents via a JSON containment query (@>) [] let byContains tableName (criteria: 'TContains) fieldNames sqlProps = Custom.nonQuery (Query.byContains (Query.removeFields tableName)) [ jsonParam "@criteria" criteria; fieldNameParams fieldNames ] sqlProps /// Remove fields from documents via a JSON Path match query (@?) [] let byJsonPath tableName jsonPath fieldNames sqlProps = Custom.nonQuery (Query.byPathMatch (Query.removeFields tableName)) [ "@path", Sql.string jsonPath; fieldNameParams fieldNames ] sqlProps /// Commands to delete documents [] module Delete = /// Delete a document by its ID [] 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 (->> =) [] 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 (@>) [] 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 (@?) [] let byJsonPath tableName path sqlProps = Custom.nonQuery (Query.byPathMatch (Query.delete tableName)) [ "@path", Sql.string path ] sqlProps /// Commands to execute custom SQL queries [] module Custom = /// Execute a query that returns a list of results [] let list<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) = WithProps.Custom.list<'TDoc> query parameters mapFunc (fromDataSource ()) /// Execute a query that returns a list of results let List<'TDoc>(query, parameters, mapFunc: System.Func) = WithProps.Custom.List<'TDoc>(query, parameters, mapFunc, fromDataSource ()) /// Execute a query that returns one or no results; returns None if not found [] let single<'TDoc> query parameters (mapFunc: RowReader -> 'TDoc) = WithProps.Custom.single<'TDoc> query parameters mapFunc (fromDataSource ()) /// Execute a query that returns one or no results; returns null if not found let Single<'TDoc when 'TDoc: null and 'TDoc: not struct>( query, parameters, mapFunc: System.Func) = WithProps.Custom.Single<'TDoc>(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 ()) /// Execute a query that returns a scalar value let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func) = WithProps.Custom.Scalar<'T>(query, parameters, mapFunc, fromDataSource ()) /// Table and index definition commands [] module Definition = /// Create a document table [] let ensureTable name = WithProps.Definition.ensureTable name (fromDataSource ()) /// Create an index on documents in the specified table [] let ensureDocumentIndex name idxType = WithProps.Definition.ensureDocumentIndex name idxType (fromDataSource ()) /// Create an index on field(s) within documents in the specified table [] let ensureFieldIndex tableName indexName fields = WithProps.Definition.ensureFieldIndex tableName indexName fields (fromDataSource ()) /// Document writing functions [] module Document = /// Insert a new document [] 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") [] let save<'TDoc> tableName (document: 'TDoc) = WithProps.Document.save<'TDoc> tableName document (fromDataSource ()) /// Queries to count documents [] module Count = /// Count all documents in a table [] let all tableName = WithProps.Count.all tableName (fromDataSource ()) /// Count matching documents using a JSON field comparison query (->> =) [] let byFields tableName howMatched fields = WithProps.Count.byFields tableName howMatched fields (fromDataSource ()) /// Count matching documents using a JSON containment query (@>) [] let byContains tableName criteria = WithProps.Count.byContains tableName criteria (fromDataSource ()) /// Count matching documents using a JSON Path match query (@?) [] let byJsonPath tableName jsonPath = WithProps.Count.byJsonPath tableName jsonPath (fromDataSource ()) /// Queries to determine if documents exist [] module Exists = /// Determine if a document exists for the given ID [] let byId tableName docId = WithProps.Exists.byId tableName docId (fromDataSource ()) /// Determine if documents exist using a JSON field comparison query (->> =) [] let byFields tableName howMatched fields = WithProps.Exists.byFields tableName howMatched fields (fromDataSource ()) /// Determine if documents exist using a JSON containment query (@>) [] let byContains tableName criteria = WithProps.Exists.byContains tableName criteria (fromDataSource ()) /// Determine if documents exist using a JSON Path match query (@?) [] let byJsonPath tableName jsonPath = WithProps.Exists.byJsonPath tableName jsonPath (fromDataSource ()) /// Commands to retrieve documents [] module Find = /// Retrieve all documents in the given table [] let all<'TDoc> tableName = WithProps.Find.all<'TDoc> tableName (fromDataSource ()) /// Retrieve all documents in the given table let All<'TDoc> tableName = WithProps.Find.All<'TDoc>(tableName, fromDataSource ()) /// Retrieve all documents in the given table ordered by the given fields in the document [] let allOrdered<'TDoc> tableName orderFields = WithProps.Find.allOrdered<'TDoc> tableName orderFields (fromDataSource ()) /// Retrieve all documents in the given table ordered by the given fields in the document let AllOrdered<'TDoc> tableName orderFields = WithProps.Find.AllOrdered<'TDoc>(tableName, orderFields, fromDataSource ()) /// Retrieve a document by its ID; returns None if not found [] let byId<'TKey, 'TDoc> tableName docId = WithProps.Find.byId<'TKey, 'TDoc> tableName docId (fromDataSource ()) /// Retrieve a document by its ID; returns null if not found let ById<'TKey, 'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, docId: 'TKey) = WithProps.Find.ById<'TKey, 'TDoc>(tableName, docId, fromDataSource ()) /// Retrieve documents matching a JSON field comparison query (->> =) [] let byFields<'TDoc> tableName howMatched fields = WithProps.Find.byFields<'TDoc> tableName howMatched fields (fromDataSource ()) /// Retrieve documents matching a JSON field comparison query (->> =) let ByFields<'TDoc>(tableName, howMatched, fields) = WithProps.Find.ByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ()) /// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the document [] let byFieldsOrdered<'TDoc> tableName howMatched queryField orderFields = WithProps.Find.byFieldsOrdered<'TDoc> tableName howMatched queryField orderFields (fromDataSource ()) /// Retrieve documents matching a JSON field comparison query (->> =) ordered by the given fields in the document let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields) = WithProps.Find.ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, fromDataSource ()) /// Retrieve documents matching a JSON containment query (@>) [] let byContains<'TDoc> tableName (criteria: obj) = WithProps.Find.byContains<'TDoc> tableName criteria (fromDataSource ()) /// Retrieve documents matching a JSON containment query (@>) let ByContains<'TDoc>(tableName, criteria: obj) = WithProps.Find.ByContains<'TDoc>(tableName, criteria, fromDataSource ()) /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document [] let byContainsOrdered<'TDoc> tableName (criteria: obj) orderFields = WithProps.Find.byContainsOrdered<'TDoc> tableName criteria orderFields (fromDataSource ()) /// Retrieve documents matching a JSON containment query (@>) ordered by the given fields in the document let ByContainsOrdered<'TDoc>(tableName, criteria: obj, orderFields) = WithProps.Find.ByContainsOrdered<'TDoc>(tableName, criteria, orderFields, fromDataSource ()) /// Retrieve documents matching a JSON Path match query (@?) [] 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 documents matching a JSON Path match query (@?) ordered by the given fields in the document [] let byJsonPathOrdered<'TDoc> tableName jsonPath orderFields = WithProps.Find.byJsonPathOrdered<'TDoc> tableName jsonPath orderFields (fromDataSource ()) /// Retrieve documents matching a JSON Path match query (@?) ordered by the given fields in the document let ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields) = WithProps.Find.ByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, fromDataSource ()) /// Retrieve the first document matching a JSON field comparison query (->> =); returns None if not found [] let firstByFields<'TDoc> tableName howMatched fields = WithProps.Find.firstByFields<'TDoc> tableName howMatched fields (fromDataSource ()) /// Retrieve the first document matching a JSON field comparison query (->> =); returns null if not found let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields) = WithProps.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, fromDataSource ()) /// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in the /// document; returns None if not found [] let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields = WithProps.Find.firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields (fromDataSource ()) /// Retrieve the first document matching a JSON field comparison query (->> =) ordered by the given fields in the /// document; returns null if not found let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>( tableName, howMatched, queryFields, orderFields) = WithProps.Find.FirstByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, fromDataSource ()) /// Retrieve the first document matching a JSON containment query (@>); returns None if not found [] 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 and 'TDoc: not struct>(tableName, criteria: obj) = WithProps.Find.FirstByContains<'TDoc>(tableName, criteria, fromDataSource ()) /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the document; /// returns None if not found [] let firstByContainsOrdered<'TDoc> tableName (criteria: obj) orderFields = WithProps.Find.firstByContainsOrdered<'TDoc> tableName criteria orderFields (fromDataSource ()) /// Retrieve the first document matching a JSON containment query (@>) ordered by the given fields in the document; /// returns null if not found let FirstByContainsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, criteria: obj, orderFields) = WithProps.Find.FirstByContainsOrdered<'TDoc>(tableName, criteria, orderFields, fromDataSource ()) /// Retrieve the first document matching a JSON Path match query (@?); returns None if not found [] 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 and 'TDoc: not struct>(tableName, jsonPath) = WithProps.Find.FirstByJsonPath<'TDoc>(tableName, jsonPath, fromDataSource ()) /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the document; /// returns None if not found [] let firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields = WithProps.Find.firstByJsonPathOrdered<'TDoc> tableName jsonPath orderFields (fromDataSource ()) /// Retrieve the first document matching a JSON Path match query (@?) ordered by the given fields in the document; /// returns null if not found let FirstByJsonPathOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, jsonPath, orderFields) = WithProps.Find.FirstByJsonPathOrdered<'TDoc>(tableName, jsonPath, orderFields, fromDataSource ()) /// Commands to update documents [] module Update = /// Update an entire document by its ID [] let byId tableName (docId: 'TKey) (document: 'TDoc) = WithProps.Update.byId tableName docId document (fromDataSource ()) /// Update an entire document by its ID, using the provided function to obtain the ID from the document [] let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) = WithProps.Update.byFunc tableName idFunc document (fromDataSource ()) /// Update an entire document by its ID, using the provided function to obtain the ID from the document let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc) = WithProps.Update.ByFunc(tableName, idFunc, document, fromDataSource ()) /// Commands to patch (partially update) documents [] module Patch = /// Patch a document by its ID [] let byId tableName (docId: 'TKey) (patch: 'TPatch) = WithProps.Patch.byId tableName docId patch (fromDataSource ()) /// Patch documents using a JSON field comparison query in the WHERE clause (->> =) [] let byFields tableName howMatched fields (patch: 'TPatch) = WithProps.Patch.byFields tableName howMatched fields patch (fromDataSource ()) /// Patch documents using a JSON containment query in the WHERE clause (@>) [] let byContains tableName (criteria: 'TCriteria) (patch: 'TPatch) = WithProps.Patch.byContains tableName criteria patch (fromDataSource ()) /// Patch documents using a JSON Path match query in the WHERE clause (@?) [] let byJsonPath tableName jsonPath (patch: 'TPatch) = WithProps.Patch.byJsonPath tableName jsonPath patch (fromDataSource ()) /// Commands to remove fields from documents [] module RemoveFields = /// Remove fields from a document by the document's ID [] let byId tableName (docId: 'TKey) fieldNames = WithProps.RemoveFields.byId tableName docId fieldNames (fromDataSource ()) /// Remove fields from documents via a comparison on JSON fields in the document [] let byFields tableName howMatched fields fieldNames = WithProps.RemoveFields.byFields tableName howMatched fields fieldNames (fromDataSource ()) /// Remove fields from documents via a JSON containment query (@>) [] let byContains tableName (criteria: 'TContains) fieldNames = WithProps.RemoveFields.byContains tableName criteria fieldNames (fromDataSource ()) /// Remove fields from documents via a JSON Path match query (@?) [] let byJsonPath tableName jsonPath fieldNames = WithProps.RemoveFields.byJsonPath tableName jsonPath fieldNames (fromDataSource ()) /// Commands to delete documents [] module Delete = /// Delete a document by its ID [] let byId tableName (docId: 'TKey) = WithProps.Delete.byId tableName docId (fromDataSource ()) /// Delete documents by matching a JSON field comparison query (->> =) [] let byFields tableName howMatched fields = WithProps.Delete.byFields tableName howMatched fields (fromDataSource ()) /// Delete documents by matching a JSON containment query (@>) [] let byContains tableName (criteria: 'TContains) = WithProps.Delete.byContains tableName criteria (fromDataSource ()) /// Delete documents by matching a JSON Path match query (@?) [] let byJsonPath tableName path = WithProps.Delete.byJsonPath tableName path (fromDataSource ())