gormcase

package module
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 3 Imported by: 0

README

🧳 Gorm Case Insensitivity Plugin

Go package

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.

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:

SELECT * FROM employees WHERE UPPER(name) = UPPER('jessica');

⬇️ Installation

go get github.com/survivorbat/gorm-case

📋 Usage

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.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) gorm.Plugin

New creates a new instance of the plugin that can be registered in gorm.

Example
db, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})

_ = db.Use(New())
_ = db.Use(New(TaggedOnly()))
_ = db.Use(New(SettingOnly()))
Output:

Types

type Option

type Option func(gormCase *gormCase)

Option can be given to the New() method to tweak its behaviour

func SettingOnly

func SettingOnly() Option

SettingOnly makes it so that only queries with the setting 'gormcase' set to true are made case-insensitive. This can be configured using db.Set("gormcase", true) on the query.

func TaggedOnly

func TaggedOnly() Option

TaggedOnly makes it so that only fields with the tag `gormcase` can be turned into case-insensitive queries, useful if you don't want every field to be checked case-insensitively.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL