百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

bun-sqlgen

> 数据库
开源

为您的 Bun.sql 查询生成类型生成器

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

为您的 Bun.sql 查询生成类型生成器

bun-sqlgen

Types generator for your Bun.sql queries

You don't need an ORM to have type-safe SQL statements in your Bun application. @ilbertt/bun-sqlgen is a codegen tool that validates your queries against your schema and generates their result types. No running database is needed at codegen time, because your migrations run against an in-memory Wasm Postgres (PGlite).

Install

bash
bun add @ilbertt/bun-sqlgen

Quick start

  1. Your migrations are the source of truth for the schema — put them in any folder:

    sql
    -- db/migrations/0001_init.sql
    CREATE TABLE users (
      id           bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
      email        text NOT NULL,
      display_name text
    );
  2. Wrap your client with withTypes and tag each query with its name:

    typescript
    import { withTypes } from '@ilbertt/bun-sqlgen';
    import { SQL } from 'bun';
    
    const sql = withTypes(new SQL(Bun.env.DATABASE_URL!));
    
    export async function getUser(id: number) {
      const [user] = await sql.GetUser`
        SELECT id, email, display_name FROM users WHERE id = ${id}
      `;
      return user; // { id: string; email: string; display_name: string | null }
    }
  3. Generate the types:

    bash
    bun bun-sqlgen generate 'src/**/*.ts' --migrations db/migrations

    This writes src/queries.gen.ts — commit it. With it in place, user.whatever is a compile error and user.display_name.length is flagged as possibly-null, all by plain tsc.

    The same file also describes your schema — every table and view with its columns, index names and constraint names:

    typescript
    import type { DatabaseTables } from '@ilbertt/bun-sqlgen';
    
    type UserRow = DatabaseTables['users']['columns']; // { id: string; email: string; … }
    type UserIndex = DatabaseTables['users']['indexes']; // 'users_pkey' | 'users_email_idx'

Examples

Runnable projects live in the examples/ folder.

Contributing

Development setup and conventions are in CONTRIBUTING.md.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptbuncodegenjavascriptpostgresql

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库