Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
MIT License
Maintenance release with dependency upgrades.
Published by xiam about 2 years ago
This release includes memory and speed optimizations.
Published by xiam about 2 years ago
This release includes memory and speed optimizations.
Published by xiam over 2 years ago
Published by xiam over 2 years ago
Published by xiam over 2 years ago
Published by xiam almost 3 years ago
The github.com/lib/pq
package that we used as driver for PostgreSQL and CockroachDB went under maintenance mode, the package maintainers recommended users to switch to github.com/jackc/pgx
. We followed that recommendation, and this release uses github.com/jackc/pgx
as driver for both PostgreSQL and CockroachDB.
For some codebases, the update will be completely transparent, but others cases might require slight modifications to their code, specially if they're using custom field types. If you're upgrading to v4.5.0
make sure you're either using the special types with driver.Value
and sql.Scanner
methods provided by the github.com/upper/db/v4/adapter/postgresql
package, or provide your own ones.
If the change is too complicated, you'll be able to use github.com/lib/pq
for a while by including the pgx
build constraint along the other build tags. See:
go build -tags=pgx ...
however, this is a temporary solution, support for the github.com/lib/pq
driver is going to be definitely removed on February 28th 2022.
If you upgraded to v4.5.0
and your code doesn't compile anymore or if it throws runtime errors around conversions between Go types and database types you'll have to tweak it a bit. Basically, you'll have to make sure you're providing scanners and values for all your types. Basic Go types, like int
, string
, bool
or even a slice of basic types won't require any special handling, but some other types like *[]int
or []uint
will do.
For instance, if your struct looks like this:
type Foo struct {
ID uint64 `db:"id"`
Names *[]string `db:"names"`
}
you'll have to convert it into:
import (
"github.com/upper/db/v4/adapter/postgresql"
)
type Foo struct {
ID uint64 `db:"id"` // no need to be updated
Names *postgresql.StringArray `db:"names"` // changed
}
The table below has some of the most common equivalences between old types and new types:
instead of | use |
---|---|
*[]string |
*postgresql.StringArray |
*[]int64 |
*postgresql.Int64Array |
*[]float64 |
*postgresql.Float64Array |
*[]float32 |
*postgresql.Float32Array |
*[]bool |
*postgresql.BoolArray |
*map[string]interface{} |
*postgresql.JSONBMap |
*[]interface{} |
*postgresql.JSONBArray |
If you're using custom types that are not provided in the table above, like map[string]bool
, you'll have to provide your own driver.Value
and sql.Scanner
methods.
If the destination type is JSONB
you can use postgresql.JSONBValue
and postgresql.ScanJSONB
to make those conversions easier, see the example below:
package foo
import (
"github.com/upper/db/v4/adapter/postgresql"
)
type CustomJSONB map[string]bool
func (c CustomJSONB) Value() (driver.Value, error) {
return postgresql.JSONBValue(c)
}
func (c *CustomJSONB) Scan(src interface{}) error {
return postgresql.ScanJSONB(c, src)
}
You can also use the *postgresql.JSONBConverter
type to add automatic conversions to some types, like this:
package foo
import (
"github.com/upper/db/v4/adapter/postgresql"
)
type CustomJSONB struct {
N string `json:"name"`
V float64 `json:"value"`
*postgresql.JSONBConverter
}
type CustomJSONBObjectArray []postgresql.CustomJSONB
func (CustomJSONBObjectArray) ConvertValue(in interface{}) interface {
sql.Scanner
driver.Valuer
} {
return &postgresql.JSONB{in}
}
type CustomJSONBObjectMap map[string]CustomJSONB
func (c CustomJSONBObjectMap) Value() (driver.Value, error) {
return postgresql.JSONBValue(c)
}
func (c *CustomJSONBObjectMap) Scan(src interface{}) error {
return postgresql.ScanJSONB(c, src)
}
Thanks for using db/v4!
Special thanks to @pkieltyka and Horizon Blockchain Games for their continuos support over the years! Thank you very much
Full Changelog: https://github.com/upper/db/compare/v4.2.1...v4.5.0
Published by xiam almost 3 years ago
Published by xiam almost 3 years ago
Published by xiam over 3 years ago
Added db.AnyOf
and db.NotAnyOf
, which work like db.In
and db.NotIn
, except they accept a slice of any type.
Published by xiam over 3 years ago
The most relevant change for this release is the behaviour of Save()
: it will attempt to update a record if and only if the record exists, otherwise it will attempt to create a record.
Published by xiam about 4 years ago
See: What's new in v4
Published by xiam over 4 years ago
Published by xiam over 4 years ago
Published by xiam over 4 years ago
Published by xiam about 5 years ago
Published by xiam about 5 years ago
Published by xiam almost 6 years ago
Fixes problems with MSSQL adapter and LastInsertId.
Published by VojtechVitek almost 6 years ago
Fixes #474, an error in fetchRows()
was ignored
Published by xiam about 6 years ago
Fixes a problem with InsertReturning()
and tables with varchar/string primary keys.