gormqonvert

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 4 Imported by: 1

README

⚙️ Gorm Query Convert

Go package

Laziness rules, why write GORM queries if you can simply add prefixes to a map[string]any's values and automatically convert queries to use different operators. All prefix characters can be custom-defined and are only enabled if you define them. The currently supported queries are:

  • WHERE x != y
  • WHERE x >= y
  • WHERE x > y
  • WHERE x <= y
  • WHERE x < y
  • WHERE x LIKE y
  • WHERE x NOT LIKE y

By default, all queries are converted, if you want it to be more specific use:

  • SettingOnly(): Will only change queries on *gorm.DB objects that have .Set("gormqonvert", true) set.

If you want a particular query to not be converted, use .Set("gormqonvert", false). This works regardless of configuration.

⬇️ Installation

go get github.com/survivorbat/gorm-query-convert

📋 Usage

package main

import (
    "github.com/survivorbat/gorm-query-convert"
)

func main() {
	db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	config := gormqonvert.CharacterConfig{
		GreaterThanPrefix:      ">",
		GreaterOrEqualToPrefix: ">=",
		LessThanPrefix:         "<",
		LessOrEqualToPrefix:    "<=",
		NotEqualToPrefix:       "!=",
		LikePrefix:             "~",
		NotLikePrefix:          "!~",
    }
	db.Use(gormqonvert.New(config))
}

🔭 Plans

Not much here.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config CharacterConfig, 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{})

config := CharacterConfig{
	GreaterThanPrefix:      ">",
	GreaterOrEqualToPrefix: ">=",
	LessThanPrefix:         "<",
	LessOrEqualToPrefix:    "<=",
	NotEqualToPrefix:       "!=",
	LikePrefix:             "~",
	NotLikePrefix:          "!~",
}

_ = db.Use(New(config))

_ = db.Use(New(config, SettingOnly()))
Output:

Types

type CharacterConfig

type CharacterConfig struct {
	GreaterThanPrefix      string
	GreaterOrEqualToPrefix string
	LessThanPrefix         string
	LessOrEqualToPrefix    string
	NotEqualToPrefix       string
	LikePrefix             string
	NotLikePrefix          string
}

CharacterConfig must be provided to indicate when a field must be changed, these should all be prefixes

type Option

type Option func(like *gormQonvert)

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 'gormQonvert' set to true can be turned into LIKE queries. This can be configured using db.Set("gormQonvert", true) on the query.

Jump to

Keyboard shortcuts

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