2022-08-15 10:35:05 +00:00
|
|
|
# SQL-first Golang ORM for PostgreSQL, MySQL, MSSQL, and SQLite
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
[![build workflow](https://github.com/uptrace/bun/actions/workflows/build.yml/badge.svg)](https://github.com/uptrace/bun/actions)
|
|
|
|
[![PkgGoDev](https://pkg.go.dev/badge/github.com/uptrace/bun)](https://pkg.go.dev/github.com/uptrace/bun)
|
|
|
|
[![Documentation](https://img.shields.io/badge/bun-documentation-informational)](https://bun.uptrace.dev/)
|
2022-04-24 10:26:22 +00:00
|
|
|
[![Chat](https://discordapp.com/api/guilds/752070105847955518/widget.png)](https://discord.gg/rWtp5Aj)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
> Bun is brought to you by :star: [**uptrace/uptrace**](https://github.com/uptrace/uptrace). Uptrace
|
2022-11-28 10:19:39 +00:00
|
|
|
> is an open-source APM tool that supports distributed tracing, metrics, and logs. You can use it to
|
|
|
|
> monitor applications and set up automatic alerts to receive notifications via email, Slack,
|
|
|
|
> Telegram, and others.
|
|
|
|
>
|
|
|
|
> See [OpenTelemetry](example/opentelemetry) example which demonstrates how you can use Uptrace to
|
|
|
|
> monitor Bun.
|
2021-09-08 19:05:26 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
## Features
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
- Works with [PostgreSQL](https://bun.uptrace.dev/guide/drivers.html#postgresql),
|
2021-09-08 19:05:26 +00:00
|
|
|
[MySQL](https://bun.uptrace.dev/guide/drivers.html#mysql) (including MariaDB),
|
2022-04-24 10:26:22 +00:00
|
|
|
[MSSQL](https://bun.uptrace.dev/guide/drivers.html#mssql),
|
2021-08-25 13:34:33 +00:00
|
|
|
[SQLite](https://bun.uptrace.dev/guide/drivers.html#sqlite).
|
2022-04-24 10:26:22 +00:00
|
|
|
- [ORM-like](/example/basic/) experience using good old SQL. Bun supports structs, map, scalars, and
|
|
|
|
slices of map/structs/scalars.
|
2021-11-27 14:26:58 +00:00
|
|
|
- [Bulk inserts](https://bun.uptrace.dev/guide/query-insert.html).
|
|
|
|
- [Bulk updates](https://bun.uptrace.dev/guide/query-update.html) using common table expressions.
|
|
|
|
- [Bulk deletes](https://bun.uptrace.dev/guide/query-delete.html).
|
2021-08-25 13:34:33 +00:00
|
|
|
- [Fixtures](https://bun.uptrace.dev/guide/fixtures.html).
|
|
|
|
- [Migrations](https://bun.uptrace.dev/guide/migrations.html).
|
|
|
|
- [Soft deletes](https://bun.uptrace.dev/guide/soft-deletes.html).
|
|
|
|
|
2023-01-22 11:26:47 +00:00
|
|
|
### Resources
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
- [**Get started**](https://bun.uptrace.dev/guide/golang-orm.html)
|
2021-08-25 13:34:33 +00:00
|
|
|
- [Examples](https://github.com/uptrace/bun/tree/master/example)
|
2021-12-12 14:47:51 +00:00
|
|
|
- [Discussions](https://github.com/uptrace/bun/discussions)
|
2022-04-24 10:26:22 +00:00
|
|
|
- [Chat](https://discord.gg/rWtp5Aj)
|
2021-08-25 13:34:33 +00:00
|
|
|
- [Reference](https://pkg.go.dev/github.com/uptrace/bun)
|
|
|
|
- [Starter kit](https://github.com/go-bun/bun-starter-kit)
|
2021-09-29 13:09:45 +00:00
|
|
|
|
2023-01-22 11:26:47 +00:00
|
|
|
### Tutorials
|
|
|
|
|
|
|
|
Wrote a tutorial for Bun? Create a PR to add here and on [Bun](https://bun.uptrace.dev/) site.
|
|
|
|
|
|
|
|
### Featured projects using Bun
|
2021-09-29 13:09:45 +00:00
|
|
|
|
2022-09-28 17:30:40 +00:00
|
|
|
- [uptrace](https://github.com/uptrace/uptrace) - Distributed tracing and metrics.
|
|
|
|
- [paralus](https://github.com/paralus/paralus) - All-in-one Kubernetes access manager.
|
|
|
|
- [inovex/scrumlr.io](https://github.com/inovex/scrumlr.io) - Webapp for collaborative online
|
|
|
|
retrospectives.
|
2021-09-29 13:09:45 +00:00
|
|
|
- [gotosocial](https://github.com/superseriousbusiness/gotosocial) - Golang fediverse server.
|
2022-09-28 17:30:40 +00:00
|
|
|
- [lorawan-stack](https://github.com/TheThingsNetwork/lorawan-stack) - The Things Stack, an Open
|
|
|
|
Source LoRaWAN Network Server.
|
|
|
|
- [anti-phishing-bot](https://github.com/Benricheson101/anti-phishing-bot) - Discord bot for
|
|
|
|
deleting Steam/Discord phishing links.
|
2022-04-24 10:26:22 +00:00
|
|
|
- [emerald-web3-gateway](https://github.com/oasisprotocol/emerald-web3-gateway) - Web3 Gateway for
|
|
|
|
the Oasis Emerald paratime.
|
|
|
|
- [lndhub.go](https://github.com/getAlby/lndhub.go) - accounting wrapper for the Lightning Network.
|
2022-09-28 17:30:40 +00:00
|
|
|
- [penguin-statistics](https://github.com/penguin-statistics/backend-next) - Penguin Statistics v3
|
|
|
|
Backend.
|
|
|
|
- And
|
|
|
|
[hundreds more](https://github.com/uptrace/bun/network/dependents?package_id=UGFja2FnZS0yMjkxOTc4OTA4).
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
## Why another database client?
|
|
|
|
|
|
|
|
So you can elegantly write complex queries:
|
|
|
|
|
|
|
|
```go
|
|
|
|
regionalSales := db.NewSelect().
|
|
|
|
ColumnExpr("region").
|
|
|
|
ColumnExpr("SUM(amount) AS total_sales").
|
|
|
|
TableExpr("orders").
|
|
|
|
GroupExpr("region")
|
|
|
|
|
|
|
|
topRegions := db.NewSelect().
|
|
|
|
ColumnExpr("region").
|
|
|
|
TableExpr("regional_sales").
|
|
|
|
Where("total_sales > (SELECT SUM(total_sales) / 10 FROM regional_sales)")
|
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
var items []map[string]interface{}
|
2021-11-13 11:29:08 +00:00
|
|
|
err := db.NewSelect().
|
|
|
|
With("regional_sales", regionalSales).
|
|
|
|
With("top_regions", topRegions).
|
|
|
|
ColumnExpr("region").
|
|
|
|
ColumnExpr("product").
|
|
|
|
ColumnExpr("SUM(quantity) AS product_units").
|
|
|
|
ColumnExpr("SUM(amount) AS product_sales").
|
|
|
|
TableExpr("orders").
|
|
|
|
Where("region IN (SELECT region FROM top_regions)").
|
|
|
|
GroupExpr("region").
|
|
|
|
GroupExpr("product").
|
2021-11-27 14:26:58 +00:00
|
|
|
Scan(ctx, &items)
|
2021-11-13 11:29:08 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```sql
|
|
|
|
WITH regional_sales AS (
|
|
|
|
SELECT region, SUM(amount) AS total_sales
|
|
|
|
FROM orders
|
|
|
|
GROUP BY region
|
|
|
|
), top_regions AS (
|
|
|
|
SELECT region
|
|
|
|
FROM regional_sales
|
|
|
|
WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
|
|
|
|
)
|
|
|
|
SELECT region,
|
|
|
|
product,
|
|
|
|
SUM(quantity) AS product_units,
|
|
|
|
SUM(amount) AS product_sales
|
|
|
|
FROM orders
|
|
|
|
WHERE region IN (SELECT region FROM top_regions)
|
|
|
|
GROUP BY region, product
|
|
|
|
```
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
And scan results into scalars, structs, maps, slices of structs/maps/scalars:
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
```go
|
2022-04-24 10:26:22 +00:00
|
|
|
users := make([]User, 0)
|
|
|
|
if err := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user1 := new(User)
|
|
|
|
if err := db.NewSelect().Model(user1).Where("id = ?", 1).Scan(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
```
|
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
See [**Getting started**](https://bun.uptrace.dev/guide/golang-orm.html) guide and check
|
2021-12-12 14:47:51 +00:00
|
|
|
[examples](example).
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-08-15 10:35:05 +00:00
|
|
|
## See also
|
|
|
|
|
|
|
|
- [Golang HTTP router](https://github.com/uptrace/bunrouter)
|
|
|
|
- [Golang ClickHouse ORM](https://github.com/uptrace/go-clickhouse)
|
|
|
|
- [Golang msgpack](https://github.com/vmihailenco/msgpack)
|
|
|
|
|
2023-01-22 11:26:47 +00:00
|
|
|
## Contributing
|
|
|
|
|
|
|
|
See [CONTRIBUTING.md](CONTRIBUTING.md) for some hints.
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2023-01-22 11:26:47 +00:00
|
|
|
And thanks to all the people who already contributed!
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
<a href="https://github.com/uptrace/bun/graphs/contributors">
|
|
|
|
<img src="https://contributors-img.web.app/image?repo=uptrace/bun" />
|
|
|
|
</a>
|