gormlike

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: MIT Imports: 4 Imported by: 0

README

👍 Gorm Auto Like Plugin

Go package

I wanted to provide a map to a WHERE query and automatically turn it into a LIKE query if wildcards were present, this plugin does just that. If you want to convert queries based on prefix, you should have a look at gorm-query-convert.

By default, all queries are turned into like-queries if either a % or a given character is found, if you don't want this, you have 2 options:

  • TaggedOnly(): Will only change queries on fields that have the gormlike:"true" tag
  • SettingOnly(): Will only change queries on *gorm.DB objects that have .Set("gormlike", true) set.

If you want a particular query or field to not be like-able, use .Set("gormlike", false) or gormlike:"false" respectively. These work regardless of configuration.

  • deepgorm turns nested maps in WHERE-calls into subqueries
  • gormqonvert turns WHERE-calls into different queries if certain tokens were found
  • gormcase adds case insensitivity to WHERE queries
  • gormtestutil provides easy utility methods for unit-testing with gorm

⬇️ Installation

go get github.com/survivorbat/gorm-like

📋 Usage

package main

import (
    "github.com/survivorbat/gorm-like"
)

func main() {
	db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	db.Use(gormlike.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. Without any settings, all queries will be LIKE-d.

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

_ = db.Use(New())

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

Types

type Option

type Option func(like *gormLike)

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

func SettingOnly added in v0.0.3

func SettingOnly() Option

SettingOnly makes it so that only queries with the setting 'gormlike' set to true can be turned into LIKE queries. This can be configured using db.Set("gormlike", true) on the query.

func TaggedOnly added in v0.0.1

func TaggedOnly() Option

TaggedOnly makes it so that only fields with the tag `gormlike` can be turned into LIKE queries, useful if you don't want every field to be LIKE-able.

func WithCharacter

func WithCharacter(character string) Option

WithCharacter allows you to specify a replacement character for the % in the LIKE queries

Jump to

Keyboard shortcuts

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