sqlgen is not an ORM, it's a code generator instead. It parse the go struct and generate the necessary struct methods for you.
- sqlgen is based on a Code first approach β You don't require to write SQL first, but Go code instead.
- sqlgen enables Codegen β We generate the boring bits, so you can focus on building your app quickly.
- sqlgen prioritizes Performance β Most of the things will define in compile time instead of runtime.
- sqlgen embrace Generics β We use generics to eliminate runtime reflection costs and reduce memory allocation.
- sqlgen eliminates Side Effects - You will get expected results instead of side effects when mutate your models.
Driver | Support |
---|---|
mysql |
β |
postgres |
β |
sqlite |
β |
-
Install sqlgen.
go install github.com/si3nloong/sqlgen/cmd/sqlgen@main
-
Define your model struct.
package model import "time" type LongText string type User struct { ID int64 `sql:",auto_increment"` Name string Age uint8 Address LongText Created time.Time }
-
Generate the output files.
# sqlgen generate <source_file> sqlgen generate model/user.go
-
Generated code will be as follow.
// Code generated by sqlgen. DO NOT EDIT. package model import ( "database/sql/driver" "time" "github.com/si3nloong/sqlgen/sequel/types" ) func (User) TableName() string { return "`user`" } func (User) Columns() []string { return []string{"`id`", "`name`", "`age`", "`address`", "`created`"} } func (v User) IsAutoIncr() {} func (v User) PK() (columnName string, pos int, value driver.Value) { return "`id`", 0, int64(v.ID) } func (v User) Values() []any { return []any{int64(v.ID), string(v.Name), int64(v.Age), string(v.Address), time.Time(v.Created)} } func (v *User) Addrs() []any { return []any{types.Integer(&v.ID), types.String(&v.Name), types.Integer(&v.Age), types.String(&v.Address), (*time.Time)(&v.Created)} }
More help to get started:
- Getting started tutorial - a comprehensive guide to help you get started
- CLI guide - the CLI commands.
- FAQ - frequent ask questions.
- Configuration file - configure code generation.
As you can see, sqlgen is perform as close as Raw query, and having low memory allocations.
If you think you've found a bug, or something isn't behaving the way you think it should, please raise an issue on GitHub.
We welcome contributions, Read our Contribution Guidelines to learn more about contributing to sqlgen
Thanks to these awesome companies for their support of Open Source developers β€
Copyright (c) 2023-present, SianLoong Lee