先看最小代码:
bash
import { sql, RedisClient } from "bun";
// PG,自带
const users = await sql`SELECT * FROM users WHERE id = ${1}`;
// Redis,自带
const redis = new RedisClient("redis://localhost:6379");
await redis.set("key", "value");
// S3,自带
const file = Bun.s3.file("my-bucket", "hello.txt");
await file.write("hello world");
数据库、缓存、对象存储,一把梭。
不用 npm install。不用 go get。不用 pip install。
Bun 出生就带。
痛点:每个语言都要装包
写后端,连数据库是基本操作。
但每个语言,都得装一遍。
Java:
bash
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
bash
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(url, user, pwd);
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users");
ResultSet rs = ps.executeQuery();
连接还得手动关。忘关就泄漏。
Go:
bash
import (
"context"
"github.com/jackc/pgx/v5"
)
func main() {
db, _ := pgx.Connect(context.Background(), "postgres://...")
}
go get 拉包,版本对不对靠运气。
Python:
bash
import psycopg2
conn = psycopg2.connect(host="localhost", dbname="mydb", user="u", password="p")
cur = conn.cursor()
cur.execute("SELECT * FROM users")
pip install 装一堆。虚拟环境一开,版本锁死。
AI 写代码,生成完还得提醒你"记得装包"。
Bun 不需要。生下来就有。
Bun 还内置了啥
-
• Bun.file:文件读写
-
• Bun.password:密码哈希
-
• Bun.hash:哈希算法
-
• Bun.serve:HTTP 服务
-
• Bun.sql:PostgreSQL 驱动
-
• Redis:Redis 客户端
-
• Bun.s3:对象存储
-
• Bun.build:打包器
-
• bun:test:测试框架
-
• bun install:包管理
-
• bunx:npx 替代
一把梭,啥都带。
为什么 Bun 能集成?
1. 没历史包袱
Bun 2022 年才出。Java 都 30 年了,标准库动一行,几千万项目可能挂。
Go 1.0 后承诺向后兼容,加东西格外谨慎。
Bun 一身轻,想加啥加啥。
2. 用 Zig 手写协议
PG 协议、Redis 协议、HTTP/1.1、HTTP/2、TLS。
Bun 团队用 Zig 全部手写。
不是套壳 libpq。不是套壳 hiredis。
是从 wire format 一行一行抠出来的。
性能比 C 还快。
3. 商业公司在背后推
Jarred Sumner 拿了融资。专门养一队人维护这些驱动。
Go 和 Python 的 PG 驱动,基本靠社区志愿者。
4. 没到 1.0
Bun 1.0 还没发。API 改就改了。
Java、Go 改个函数签名,社区能吵一年。
为什么其他语言不集成?
1. 标准库哲学不同
Go 哲学是"小而精"。goroutine 进标准库,数据库驱动一律外部。
Python 哲学是"自带电池",但电池也就那几个。asyncio、json、http 是核心。
Java 标准库只规定 JDBC 接口,不规定具体驱动。商业数据库太多,Sun 也管不过来。
2. 协议碎片化
PG、MySQL、Redis、MongoDB、ClickHouse、Elasticsearch。
每个协议都不一样,每年还在演进。
今天集成 PG,明天 MySQL 8.4 协议更新,又得跟着改。
谁来长期维护?
3. 许可证冲突
PG 驱动 BSD。Redis 驱动 MIT。MySQL 驱动 GPL。
打进标准库,许可证一污染,整个语言生态跟着遭殃。
4. ABI/API 稳定性
Java 改个函数签名,几十万 JAR 可能挂。
Go 1.x 承诺兼容,标准库加东西就畏手畏脚。
5. 维护意愿
数据库协议每年更新。PG 每年发版,MySQL 8 改了一堆,Redis 6 跟 7 协议都有差异。
谁愿意在标准库长期背这活儿?
Bun 不怕。没承诺 ABI 稳定,改就改了。
对开发有啥影响?
AI 写代码更顺
AI 生成 Bun 代码,直接能跑。生成 Java 代码,AI 还得提醒你"加依赖"。
bash
import { sql, RedisClient } from "bun";
const redis = new RedisClient("redis://localhost:6379");
// 一段完整:HTTP + PG + Redis
Bun.serve({
port: 3000,
async fetch(req) {
const id = new URL(req.url).searchParams.get("id");
if (!id) return new Response("id required", { status: 400 });
// 查缓存
const cached = await redis.get(`user:${id}`);
if (cached) {
return Response.json({ source: "cache", data: JSON.parse(cached) });
}
// 查库
const user = await sql`SELECT * FROM users WHERE id = ${id}`;
if (user.length === 0) {
return new Response("not found", { status: 404 });
}
// 写回缓存
await redis.set(`user:${id}`, JSON.stringify(user[0]), "EX", 10);
return Response.json({ source: "db", data: user[0] });
}
});
HTTP、PG、Redis,一把梭。
换 Node.js,得装 pg、ioredis。然后 npm install。然后祈祷版本对。
新项目可以试 Bun
小项目、脚本、工具、内部系统,Bun 一把梭。性能比 Node.js 快,内置工具多。
老项目别瞎迁移
Bun 还在 1.0 之前,生态没 Node.js 全。生产大项目,Node.js 22 LTS 稳得多。
以后新语言可能都这么干
Deno 内置了 KV,虽然不是 PG 但思路一样。
Bun 把 PG、Redis、SQLite 都内置了。
以后新语言出生,大概率都带数据库驱动。
老牌语言改不动,只能让社区卷。
写在最后
Bun 集成数据库驱动,不是技术难题。
是"利益"、"哲学"、"包袱"的问题。
-
• Go 哲学"小而精",不会改。
-
• Java 包袱重,改一行挂一片。
-
• Python 靠社区,统一不了。
-
• Bun 出生晚,没包袱,有钱,有人,敢改。
写代码的,跟着用就行。
哪个顺手用哪个。
写小工具用 Bun。写大项目用 Node.js。写底层用 Go。写胶水用 Python。
别死磕一门。能解决问题就行。