Skip to content

feat: [#705] facades.Orm().Query() support JSON where clauses#1074

Merged
almas-x merged 1 commit intomasterfrom
almas/#705
Jun 13, 2025
Merged

feat: [#705] facades.Orm().Query() support JSON where clauses#1074
almas-x merged 1 commit intomasterfrom
almas/#705

Conversation

@almas-x
Copy link
Contributor

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

📑 Description

Closes goravel/goravel#705

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

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

facades.Orm().Query().WhereJsonContainsKey("contacts->personal->email").First(&user)
facades.Orm().Query().WhereJsonDoesntContainKey("contacts->personal->email").First(&user)

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

facades.Orm().Query().WhereJsonLength('options->languages', 1).First(&user)
facades.Orm().Query().WhereJsonLength('options->languages > ?', 1).First(&user)

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings June 12, 2025 02:25
@almas-x almas-x requested a review from a team as a code owner June 12, 2025 02:25
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: aae38ba Previous: b2b1821 Ratio
BenchmarkFile_ReadWrite 360958 ns/op 6186 B/op 97 allocs/op 217821 ns/op 6185 B/op 97 allocs/op 1.66
BenchmarkFile_ReadWrite - ns/op 360958 ns/op 217821 ns/op 1.66

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

@codecov
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.03%. Comparing base (b2b1821) to head (3eabf68).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1074      +/-   ##
==========================================
+ Coverage   70.87%   71.03%   +0.15%     
==========================================
  Files         180      181       +1     
  Lines       12644    12710      +66     
==========================================
+ Hits         8962     9028      +66     
  Misses       3312     3312              
  Partials      370      370              

☔ 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
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-based WHERE clauses in the ORM, enabling -> operators and JSON-specific query methods.

  • Introduces new WhereJson*/OrWhereJson* methods and integrates them into Query.buildWhere.
  • Extends the Wrap utility with JSON path parsing (JsonFieldAndPath, JsonPath, JsonPathAttributes) and value wrapping customization.
  • Updates driver contracts/mocks to include a new JsonGrammar interface, adds a new error type, and augments test coverage in wrap_test.go.

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
database/gorm/query.go Added JSON where support and placeholder builder; factored addWhere.
database/schema/wrap.go Implemented JSON path parsing and configurable value wrapper.
database/schema/wrap_test.go Added tests for JSON field/path handling and value-wrapper toggles.
errors/list.go Declared a new error for invalid JSON bindings.
contracts/database/driver/grammar.go Extended Grammar contract with JsonGrammar.
mocks/database/driver/JsonGrammar.go & Grammar.go Generated mocks for the new JSON grammar methods.
Comments suppressed due to low confidence (6)

database/gorm/query.go:943

  • Consider adding unit tests for the new WhereJson* and OrWhereJson* methods to validate that SQL is generated as expected for various JSON path scenarios.
func (r *Query) WhereJsonContains(column string, value any) contractsorm.Query {

database/gorm/query.go:1377

  • The reflect.ValueOf call requires importing the reflect package; add import "reflect" at the top of this file.
if val := reflect.ValueOf(args[0]); val.Kind() == reflect.Slice || val.Kind() == reflect.Array {

database/schema/wrap.go:95

  • Default quoting in JsonPathAttributes uses r.Quote (double quotes), but the tests expect single-quoted values ('column'). Adjust the default to wrap with single quotes or update the tests to match double-quote behavior.
return r.Quote(v)

database/schema/wrap_test.go:68

  • Typo in helper name: spiltPath should be splitPath. Also remove the empty if block inside this function.
spiltPath := func(v string) []string {

database/schema/wrap.go:63

  • [nitpick] Regexps are being compiled on every call. Consider precompiling these patterns to package-level var so they aren’t recompiled repeatedly.
value = regexp.MustCompile(`(\\+)?'`).ReplaceAllString(value, "''")

contracts/database/driver/grammar.go:166

  • You’ve extended the Grammar contract with JsonGrammar, but the default driver grammar implementations (e.g., MySQL, Postgres) must also implement these new methods (CompileJsonContains, CompileJsonContainsKey, etc.). Add their implementations or tests will fail to compile.
type JsonGrammar interface {

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Jun 12, 2025

Thanks, checking...

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 PR 👍 Only left one question

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.

Great 👍

@almas-x
Copy link
Contributor Author

almas-x commented Jun 13, 2025

I will submit a separate PR to fix and add the external tests after the database driver repository PR is merged.

@almas-x almas-x merged commit be5ad87 into master Jun 13, 2025
11 of 14 checks passed
@almas-x almas-x deleted the almas/#705 branch June 13, 2025 04:19
almas-x added a commit that referenced this pull request Jun 13, 2025
almas-x added a commit that referenced this pull request Jun 13, 2025
almas-x added a commit that referenced this pull request Jun 13, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
almas-x added a commit that referenced this pull request Jun 14, 2025
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.Orm().Query() Support JSON Where Clauses

3 participants