Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 1.36 KB

README.md

File metadata and controls

42 lines (36 loc) · 1.36 KB

sqld

A Go library to build and manage dynamic queries. No external dependencies. Slightly inspired by Drizzle ORM

Scope of the project

The scope of sqld is to provide an easy way to organize your dynamic queries in re-usable components, not to validate said queries. I suggest to use other tools like SQLParser and a lot of e2e tests!

Usage

query := sqld.New(
	sqld.Select(
		sqld.Columns(
			"name",
			"pizzas",
		),
	),
	sqld.From(sqld.Just("Table")),
	sqld.Where(
		sqld.And( 
			sqld.IfNotNil(filters.Name,
				sqld.Eq("name", filters.Name),
			),
			sqld.IfNotEmpty(filters.Pizzas,
				sqld.In("pizzas", filters.Pizzas),
			),
		),
	),
	sqld.OrderBy(sqld.Desc(filters.OrderBy)),
)

s, args, err := query()

Glossary

  • Operators: callbacks that have this signature, used to build various parts of the query
  • Statements: major "blocks" of the query, like a whole WHERE statement with all its conditions
  • Conditionals: functions that return different operators depending on the inputs (usually boolean checks). Check this file

Customize

You can provide your own operators: every function (anonymous or not) that has this signature can be used by the library.

Check here for the built-in errors.