Skip to content
This repository was archived by the owner on Jun 8, 2024. It is now read-only.

Beta 3 #3

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/BitBadger.Npgsql.Documents.Tests/CSharpTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,56 @@ let integrationTests =
Expect.hasCountOf docs 0u isTrue "There should have been no documents returned"
}
]
testList "FirstByContains" [
testTask "succeeds when a document is found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByContains<JsonDocument> (Db.tableName, {| Value = "again" |})
Expect.isNotNull doc "There should have been a document returned"
Expect.equal (doc :> JsonDocument).Id "two" "The incorrect document was returned"
}
testTask "succeeds when multiple documents are found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByContains<JsonDocument> (Db.tableName, {| Sub = {| Foo = "green" |} |})
Expect.isNotNull doc "There should have been a document returned"
Expect.contains [ "two"; "four" ] (doc :> JsonDocument).Id "An incorrect document was returned"
}
testTask "succeeds when a document is not found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByContains<JsonDocument> (Db.tableName, {| Value = "absent" |})
Expect.isNull doc "There should not have been a document returned"
}
]
testList "firstByJsonPath" [
testTask "succeeds when a document is found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByJsonPath<JsonDocument> (Db.tableName, """$.Value ? (@ == "FIRST!")""")
Expect.isNotNull doc "There should have been a document returned"
Expect.equal (doc :> JsonDocument).Id "one" "The incorrect document was returned"
}
testTask "succeeds when multiple documents are found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByJsonPath<JsonDocument> (Db.tableName, """$.Sub.Foo ? (@ == "green")""")
Expect.isNotNull doc "There should have been a document returned"
Expect.contains [ "two"; "four" ] (doc :> JsonDocument).Id "An incorrect document was returned"
}
testTask "succeeds when a document is not found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.FirstByJsonPath<JsonDocument> (Db.tableName, """$.Id ? (@ == "nope")""")
Expect.isNull doc "There should not have been a document returned"
}
]
]
testList "Document.Update" [
testList "Full" [
Expand Down
50 changes: 50 additions & 0 deletions src/BitBadger.Npgsql.Documents.Tests/FSharpTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,56 @@ let integrationTests =
Expect.isTrue (List.isEmpty docs) "There should have been no documents returned"
}
]
testList "firstByContains" [
testTask "succeeds when a document is found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByContains<JsonDocument> Db.tableName {| Value = "another" |}
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
}
testTask "succeeds when multiple documents are found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByContains<JsonDocument> Db.tableName {| Sub = {| Foo = "green" |} |}
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
}
testTask "succeeds when a document is not found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByContains<JsonDocument> Db.tableName {| Value = "absent" |}
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
}
]
testList "firstByJsonPath" [
testTask "succeeds when a document is found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByJsonPath<JsonDocument> Db.tableName """$.Value ? (@ == "FIRST!")"""
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
Expect.equal doc.Value.Id "one" "The incorrect document was returned"
}
testTask "succeeds when multiple documents are found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByJsonPath<JsonDocument> Db.tableName """$.Sub.Foo ? (@ == "green")"""
Expect.isTrue (Option.isSome doc) "There should have been a document returned"
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
}
testTask "succeeds when a document is not found" {
use db = Db.buildDatabase ()
do! loadDocs ()

let! doc = Find.firstByJsonPath<JsonDocument> Db.tableName """$.Id ? (@ == "nope")"""
Expect.isFalse (Option.isSome doc) "There should not have been a document returned"
}
]
]
testList "Update" [
testList "full" [
Expand Down
2 changes: 1 addition & 1 deletion src/BitBadger.Npgsql.Documents.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Expecto

let allTests = testList "Npgsql" [ FSharpTests.all; CSharpTests.all ]
let allTests = testList "BitBadger.Npgsql" [ FSharpTests.all; CSharpTests.all ]

[<EntryPoint>]
let main args = runTestsWithArgs defaultConfig args allTests
23 changes: 23 additions & 0 deletions src/BitBadger.Npgsql.Documents/Document.fs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ module WithProps =
return ResizeArray result
}

/// Execute a JSON containment query (@>), returning only the first result
let FirstByContains<'T when 'T : null> (tableName : string, criteria : obj, sqlProps : Sql.SqlProps)
: Task<'T> = backgroundTask {
let! result = FS.WithProps.Find.firstByContains tableName criteria sqlProps
return Option.toObj result
}

/// Execute a JSON Path match query (@?), returning only the first result
let FirstByJsonPath<'T when 'T : null> (tableName : string, jsonPath : string, sqlProps : Sql.SqlProps)
: Task<'T> = backgroundTask {
let! result = FS.WithProps.Find.firstByJsonPath tableName jsonPath sqlProps
return Option.toObj result
}

/// Commands to update documents
module Update =

Expand Down Expand Up @@ -228,9 +242,18 @@ module Find =
let ByContains<'T> (tableName : string, criteria : obj) =
WithProps.Find.ByContains<'T> (tableName, criteria, FS.fromDataSource ())

/// Execute a JSON Path match query (@?)
let ByJsonPath<'T> (tableName : string, jsonPath : string) =
WithProps.Find.ByJsonPath<'T> (tableName, jsonPath, FS.fromDataSource ())

/// Execute a JSON containment query (@>), returning only the first result
let FirstByContains<'T when 'T : null> (tableName : string, criteria : obj) =
WithProps.Find.FirstByContains<'T> (tableName, criteria, FS.fromDataSource ())

/// Execute a JSON Path match query (@?), returning only the first result
let FirstByJsonPath<'T when 'T : null> (tableName : string, jsonPath : string) =
WithProps.Find.FirstByJsonPath<'T> (tableName, jsonPath, FS.fromDataSource ())


/// Commands to update documents
module Update =
Expand Down
21 changes: 21 additions & 0 deletions src/BitBadger.Npgsql.FSharp.Documents/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ module WithProps =
|> Sql.query (Query.Find.byJsonPath tableName)
|> Sql.parameters [ "@path", Sql.string jsonPath ]
|> Sql.executeAsync fromData<'T>

/// Execute a JSON containment query (@>), returning only the first result
let firstByContains<'T> tableName (criteria : obj) sqlProps : Task<'T option> = backgroundTask {
let! results = byContains tableName criteria sqlProps
return List.tryHead results
}

/// Execute a JSON Path match query (@?), returning only the first result
let firstByJsonPath<'T> tableName jsonPath sqlProps : Task<'T option> = backgroundTask {
let! results = byJsonPath tableName jsonPath sqlProps
return List.tryHead results
}

/// Commands to update documents
[<RequireQualifiedAccess>]
Expand Down Expand Up @@ -490,8 +502,17 @@ module Find =
let byContains<'T> tableName criteria =
WithProps.Find.byContains<'T> tableName criteria (fromDataSource ())

/// Execute a JSON Path match query (@?)
let byJsonPath<'T> tableName jsonPath =
WithProps.Find.byJsonPath<'T> tableName jsonPath (fromDataSource ())

/// Execute a JSON containment query (@>), returning only the first result
let firstByContains<'T> tableName (criteria : obj) =
WithProps.Find.firstByContains<'T> tableName criteria (fromDataSource ())

/// Execute a JSON Path match query (@?), returning only the first result
let firstByJsonPath<'T> tableName jsonPath =
WithProps.Find.firstByJsonPath<'T> tableName jsonPath (fromDataSource ())


/// Commands to update documents
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>beta2</VersionSuffix>
<PackageReleaseNotes>Add Custom.scalar functions</PackageReleaseNotes>
<VersionSuffix>beta3</VersionSuffix>
<PackageReleaseNotes>Add Find.firstByContains/firstByJsonPath functions</PackageReleaseNotes>
<Authors>danieljsummers</Authors>
<Company>Bit Badger Solutions</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down