Whenever you begin a brand new mission in Go, you most likely at all times ask your self concerning the information entry layer. Use the generic database/sql
interface and write queries by hand or use an ORM package deal, or instruments to assist generate queries? Which database driver to make use of? What packages to make use of to make your workflow quick and easy? On this put up we’ll attempt to evaluation the very best out there drivers and ORM packages for the PostgreSQL database. Sadly, there are a number of them, and they’re all ok, so the selection won’t be straightforward, and you need to resolve what’s a very powerful for you 😉.
Drivers
1. pgx
Really useful driver for a brand new mission, actively maintained and developed. Low-level, quick, and performant. It consists of two elements: driver and toolkit. The motive force is appropriate with the database/sql
interface, whereas the toolkit incorporates PostgreSQL-specific options and functionalities that make your work simpler, akin to mapping between PostgreSQL and Go.
Github: https://github.com/jackc/pgx
Package deal documentation: pgx
2. pq
Traditionally the most well-liked PostgreSQL driver for Go. Nicely examined and utilized in many present tasks. Presently solely within the upkeep mode. Because the authors themselves say:
“For customers that require new options or dependable decision of reported bugs, we suggest utilizing pgx which is underneath lively growth.”
– pq maintainers
Nonetheless, in case you are searching for a battle-tested and secure driver for Go, the pq
is a superb alternative.
Github: https://github.com/lib/pq
Package deal documentation: pg
ORMs
1. GORM
One of the widespread ORM packages in Go, whose the primary aim is to be developer-friendly. Formally helps PostgreSQL, MySQL, SQLite, and MSSQL. It’s actively developed, has nice documentation, and plenty of options, like:
- Struct – Desk mapping assist
- Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance)
- Hooks (Earlier than/After Create/Save/Replace/Delete/Discover)
- Keen loading with
Preload
,Joins
- Transactions, nested transactions, Save Level, RollbackTo to Saved Level
- Context, ready assertion mode, DryRun mode
- Batch Insert, FindInBatches, Discover/Create with map, CRUD with SQL Expr and Context Valuer
- SQL Builder, Upsert, Locking, Optimizer/Index/Remark Hints, Named Argument, SubQuery
- Composite Major Key, Indexes, Constraints
- Auto migrations
- and plenty of extra
Web site: https://gorm.io
Github: https://github.com/go-gorm/gorm
Package deal documentation: GORM
2. Bun
Bun
is a SQL-first ORM that gives an ORM-like expertise utilizing good outdated SQL. It helps PostgreSQL, MySQL, MariaDB, MSSQL, and SQLite. Nice documentation and important options, helpful in on a regular basis work are its robust factors:
- Bulk inserts
- Bulk updates
- Bulk deletes
- Fixtures – load preliminary information right into a database for testing or demonstration functions utilizing YAML recordsdata
- Migrations
- Tender deletes
Web site: https://bun.uptrace.dev
Github: https://github.com/uptrace/bun
Package deal documentation: Bun
3. ent
An entity framework for Go. It’s the ORM developed by Fb specializing in graph-based information fashions. The schema is outlined as a graph construction in Go code, from which the mannequin and operations are generated utilizing code technology. It helps PostgreSQL, MySQL, MariaDB, TiDB, SQLite, and Gremlin.
Web site: https://entgo.io
Github: https://github.com/ent/ent
Package deal documentation: ent
4. higher/db
An information entry layer made for productiveness. API is appropriate with SQL and NoSQL databases like PostgreSQL, MySQL, MSSQL, CockroachDB, MongoDB, QL, and SQLite. It offers an agnostic API centered on working with collections of things, a SQL builder for extra direct entry to the database, and an ORM-like layer for mapping between a struct and a database desk and dealing with the database in a extra ORM-like type. Some options:
- Struct – Desk Mapping Help
- Search and delimitation of end result units
- Restrict-offset pagination (web page numbers)
- Cursor-based pagination (earlier and subsequent)
- Transactions assist
Web site: https://higher.io
Github: https://github.com/higher/db
Package deal documentation: higher/db
5. XORM
Easy and highly effective GORM various supporting PostgreSQL, MySQL, SQLite, MSSQL, MariaDB, TiDB, CockroachDB, and Oracle. A number of the options:
- Struct – Desk mapping assist
- Transactions assist
- Synchronization of database schema
- Question cache
- Database Reverse – software that generates code from the database schema
- Easy cascade loading
- Optimistic Locking assist
- SQL Builder
- and plenty of extra
Web site: https://xorm.io/
Gitea: https://gitea.com/xorm/xorm
Package deal documentation: XORM
6. POP
It’s a part of the Buffalo Go net framework, deeply built-in, and really useful when utilizing Buffalo. However can be used as a stand-alone information entry layer. POP follows conventions influenced by the ActiveRecord Ruby gem making it straightforward to do CRUD operations with primary ORM performance, run migrations, and construct/execute queries. It helps PostgreSQL, CockroachDB, MySQL, and SQLite databases.
Web site: https://gobuffalo.io/documentation/database/pop
Github: https://github.com/gobuffalo/pop
Package deal documentation: POP
7. REL
REL is a contemporary ORM-ish information entry layer for layered structure. It’s constructed with testability in thoughts and comes with its customized check library. It helps PostgreSQL, MySQL, MSSQL, and SQLite. Options:
- Testable repository
- Nested transactions
- Question builder
- Keen loading assist
- Composite Major Key
- Tender deletes
- Pagination
- Schema Migration
Web site: https://go-rel.github.io
Github: https://github.com/go-rel/rel
Package deal documentation: REL
8. Beego ORM
This ORM is a part of the Beego net framework. Closely influenced by Django ORM and SQLAlchemy. It really works with PostgreSQL, MySQL, and SQLite databases. The principle options are: straightforward to make use of CRUD operations, Struct – Desk mapping, uncooked SQL assist and question builder, auto joins, transactions assist.
Web site: https://beego.vip/docs/mvc/mannequin/overview.md
Github: https://github.com/beego/beego/tree/grasp/consumer/orm
Package deal documentation: Beego ORM