https://github.com/survivorbat/gorm-case
I wanted to have some case-insensitive where-queries and I often use maps for filtering, so I made this plugin to convert queries into that.
https://github.com/survivorbat/gorm-case
case-insensitive database go go-orm golang gorm
Last synced: 12 months ago
JSON representation
I wanted to have some case-insensitive where-queries and I often use maps for filtering, so I made this plugin to convert queries into that.
- Host: GitHub
- URL: https://github.com/survivorbat/gorm-case
- Owner: survivorbat
- License: mit
- Created: 2023-07-04T10:06:24.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-02T17:04:05.000Z (over 2 years ago)
- Last Synced: 2024-12-31T00:27:49.057Z (about 1 year ago)
- Topics: case-insensitive, database, go, go-orm, golang, gorm
- Language: Go
- Homepage: https://pkg.go.dev/github.com/survivorbat/gorm-case
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🧳 Gorm Case Insensitivity Plugin
[](https://github.com/survivorbat/gorm-case/actions/workflows/test.yaml)
I wanted to have some case-insensitive where-queries and I often use maps for filtering, so I made this plugin to convert
queries into that.
By default, all queries are turned into case-insensitive queries, if you don't want this,
you have 2 options:
- `TaggedOnly()`: Will only change queries on fields that have the `gormcase:"true"` tag
- `SettingOnly()`: Will only change queries on `*gorm.DB` objects that have `.Set("gormcase", true)` set.
If you want a particular query to not be case-insensitive, use `.Set("gormcase", false)`. This works
regardless of configuration.
```go
package main
import (
gormcase "github.com/survivorbat/gorm-case"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Employee is the example for normal usage
type Employee struct {
Name string
}
// RestrictedEmployee is the example for gormcase.TaggedOnly()
type RestrictedEmployee struct {
// Will be case-insensitive in queries
Name string `gormcase:"true"`
// Will not be case-insensitive in queries
Job string
}
func main() {
// Normal usage
db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
db.Create(Employee{Name: "jEsSiCa"})
filters := map[string]any{
"name": "jessica",
}
db.Use(gormcase.New())
db.Model(&Employee{}).Where(filters)
// Only uses case-insensitive-queries for tagged fields
db, _ = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
db.Create(Employee{Name: "jEsSiCa", Job: "dEvElOpEr"})
filters := map[string]any{
"name": "jessica",
"job": "dEvElOpEr",
}
db.Use(gormcase.New(gormcase.TaggedOnly()))
db.Model(&RestrictedEmployee{}).Where(filters)
}
```
Is automatically turned into a query that looks like this:
```sql
SELECT * FROM employees WHERE UPPER(name) = UPPER('jessica');
```
## 💡 Related Libraries
- [deepgorm](https://github.com/survivorbat/gorm-deep-filtering) turns nested maps in WHERE-calls into subqueries
- [gormqonvert](https://github.com/survivorbat/gorm-query-convert) turns WHERE-calls into different queries if certain tokens were found
- [gormlike](https://github.com/survivorbat/gorm-like) turns WHERE-calls into LIkE queries if certain tokens were found
- [gormtestutil](https://github.com/ing-bank/gormtestutil) provides easy utility methods for unit-testing with gorm
## ⬇️ Installation
`go get github.com/survivorbat/gorm-case`
## 📋 Usage
```go
package main
import (
"github.com/survivorbat/gorm-case"
)
func main() {
db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
db.Use(gormcase.New())
}
```
## 🔭 Plans
Not much here.