Skip to content

feat: [#708] facades.DB() Support JSON Where Clauses#1082

Merged
almas-x merged 2 commits intomasterfrom
almas/#708
Jun 20, 2025
Merged

feat: [#708] facades.DB() Support JSON Where Clauses#1082
almas-x merged 2 commits intomasterfrom
almas/#708

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Jun 17, 2025

📑 Description

Closes goravel/goravel#708

Support to query a JSON column, use the -> operator:

facades.DB().Table("users").Where("preferences->dining->meal", "salad").First(&user)
facades.DB().Table("users").Where("options->languages[0]", "en").First(&user)

facades.DB().Table("users").WhereJsonContainsKey("contacts->personal->email").First(&user)
facades.DB().Table("users").WhereJsonDoesntContainKey("contacts->personal->email").First(&user)

facades.DB().Table("users").WhereJsonContains("options->languages", "en").First(&user)
facades.DB().Table("users").WhereJsonContains("options->languages", []string{"en", "de"}).First(&user)
facades.DB().Table("users").WhereJsonDoesntContain("options->languages", "en").First(&user)
facades.DB().Table("users").WhereJsonDoesntContain("options->languages", []string{"en", "de"}).First(&user)

facades.DB().Table("users").WhereJsonLength('options->languages', 1).First(&user)
facades.DB().Table("users").WhereJsonLength('options->languages > ?', 1).First(&user)

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings June 17, 2025 03:21
@almas-x almas-x requested a review from a team as a code owner June 17, 2025 03:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for JSON where clauses in DB queries by allowing the use of the "->" operator and introducing related JSON handling functions. Key changes include:

  • New JSON query test cases and table setup in tests/db_test.go.
  • Updates to query building functions in the database/gorm and database/db packages to support JSON operators.
  • Consistent updates to contracts and driver interfaces to include new JSON where clause operations.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/go.mod Updates dependency version for samber/lo library
tests/db_test.go Adds tests for various JSON query clauses and new table creation
database/gorm/query.go Refactors query building to use exported fields and new JSON types
database/gorm/conditions.go Updates conditions struct to reference contractsdriver types
database/db/query.go Extends query interface with new JSON query methods
database/db/builder.go Introduces NameMapper for consistent field-to-column mapping
contracts/database/driver/conditions.go Defines new WhereType constants and updates the Where struct
contracts/database/db/db.go Updates DB Query interface to include JSON where clause operations
Comments suppressed due to low confidence (3)

tests/db_test.go:1427

  • [nitpick] Consider extracting lengthy inline JSON literals into helper constants or separate functions to improve readability and maintainability.
Data: `{\n	"string": "first",\n	"int": 123,\n	"float": 123.456,\n	"bool": true,\n	"array": ["abc", "def", "ghi"],\n	 ...`

contracts/database/db/db.go:114

  • [nitpick] Consider adding inline documentation comments to the new JSON query interface methods to clarify their purpose and usage for API consumers.
OrWhereJsonContains(column string, value any) Query

database/db/builder.go:30

  • [nitpick] Verify that the NameMapper function covers all necessary edge cases for converting struct field names to snake case, especially for fields beyond 'ID'.
dbx.MapperFunc(NameMapper)

@codecov
Copy link

codecov bot commented Jun 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.13%. Comparing base (d5bbc40) to head (cc129ef).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1082      +/-   ##
==========================================
+ Coverage   71.03%   71.13%   +0.10%     
==========================================
  Files         181      181              
  Lines       12710    12720      +10     
==========================================
+ Hits         9028     9048      +20     
+ Misses       3312     3305       -7     
+ Partials      370      367       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: a724b55 Previous: d5bbc40 Ratio
BenchmarkFile_ReadWrite 346871 ns/op 6185 B/op 97 allocs/op 204521 ns/op 6185 B/op 97 allocs/op 1.70
BenchmarkFile_ReadWrite - ns/op 346871 ns/op 204521 ns/op 1.70

This comment was automatically generated by workflow using github-action-benchmark.

@almas-x almas-x force-pushed the almas/#708 branch 3 times, most recently from d2b7ecb to 426e6f3 Compare June 17, 2025 05:39
@almas-x
Copy link
Contributor Author

almas-x commented Jun 20, 2025

Hi~ @hwbrzzl ,Is there anything else I need to do for this PR?

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Jun 20, 2025

So sorry, missed this PR, will check it.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing 👍

withTrashed bool
}

type Having struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

@almas-x almas-x requested a review from hwbrzzl June 20, 2025 08:42
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@almas-x almas-x merged commit e655223 into master Jun 20, 2025
14 checks passed
@almas-x almas-x deleted the almas/#708 branch June 20, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

facades.DB() Support JSON Where Clauses

3 participants