-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from networkteam/jsonb_build_object
added jsonb_build_object
- Loading branch information
Showing
3 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package builder_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/networkteam/qrb" | ||
"github.com/networkteam/qrb/builder" | ||
"github.com/networkteam/qrb/fn" | ||
"github.com/networkteam/qrb/internal/testhelper" | ||
) | ||
|
||
func TestJsonbQuery(t *testing.T) { | ||
t.Run("select json object", func(t *testing.T) { | ||
b := qrb.SelectJson( | ||
fn.JsonbBuildObject(). | ||
Prop("id", qrb.N("authors.author_id")). | ||
Prop("name", qrb.N("authors.name")), | ||
). | ||
From(qrb.N("authors")). | ||
Where(qrb.N("authors.author_id").Eq(qrb.Arg(123))) | ||
|
||
testhelper.AssertSQLWriterEquals( | ||
t, | ||
"SELECT jsonb_build_object('id',authors.author_id,'name',authors.name) FROM authors WHERE authors.author_id = $1", | ||
[]any{123}, | ||
b, | ||
) | ||
|
||
// We can now modify an existing JSON selection! | ||
// Each SelectBuilder acts as a kind of query blueprint that can be used to modify later. | ||
withPostCount := b.ApplySelectJson(func(obj builder.JsonBuildObjectBuilder) builder.JsonBuildObjectBuilder { | ||
return obj.Prop("postCount", fn.Count(qrb.N("posts"))) | ||
}) | ||
|
||
testhelper.AssertSQLWriterEquals( | ||
t, | ||
"SELECT jsonb_build_object('id',authors.author_id,'name',authors.name,'postCount',count(posts)) FROM authors WHERE authors.author_id = $1", | ||
[]any{123}, | ||
withPostCount, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters