Skip to content

Latest commit

History

202 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

querybm

CIcodecovGo Report CardGo project versionEnabled LintersDocumentationGitHub go.mod Go versionGitHub License

A simple SQL query builder and mapper. querybm works well with models generated by sqlc. But it works with your models without sqlc.

FEATURES

  • No dependencies
  • Build SELECT statement dynamically with struct type
  • Mapping with generics instead of reflection
  • 100% test coverage

See godoc for more details.

INSTALL

go get -u github.com/tecowl/querybm

USAGE

// You can use model generated by sqlc instead of defining your owntypeAuthorstruct {
AuthorIDint32Namestring
}
typeConditionstruct {
Namestring
}
func (c*Condition) Build(s*querybm.Statement) {
ifc.Name!="" {
s.Where.Add(Field("name", LikeContains(c.Name)))
}
}
funcNew(db*sql.DB, condition*Condition) *querybm.Query[Author] {
returnquerybm.New(
db,
"authors",
querybm.NewFields(
[]string{"author_id", "name"},
func(rows querybm.Scanner, author*models.Author) error {
returnrows.Scan(&author.AuthorID, &author.Name)
},
),
condition,
querybm.NewSortItem("name", false),
querybm.NewLimitOffset(100, 0),
)
}
funcmain() {
db, _:=sql.Open("..", "...")
deferdb.Close()
condition:=&Condition{Name: "John"}
q:=New(db, condition)
cnt, _:=q.Count(ctx)
list, _:=q.List(ctx)
}

EXAMPLES

TESTS

LICENSE

MIT

About

A simple SQL query builder and mapper

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages