@bob-kit/client

中文文档
0.0.34发布于 2026/05/01ISCTypeScriptESM + CJS近 30 天下载 7250

Disclaimer:
Bob is under development! Use at your own risk.

原始使用文档
文档目录13 节
  1. What is Bob?
  2. Installation
  3. Environment requirements
  4. Concepts: Driver and cache
  5. Quick start (Node + SQLite)
  6. Advanced examples
  7. Parameters
  8. API
  9. default export function bob(driver: Driver)
  10. bobQuery({ driver, input, id? })
  11. @bob-kit/client/cache
  12. Errors
  13. Important notes

@bob-kit/client

Disclaimer:
Bob is under development! Use at your own risk.

Official Bob client for consuming the bob DSL from JavaScript/TypeScript.

This package loads Bob’s runtime (WASM) and exposes a small API to:

  • Transpile your bob input into SQL for a given engine (SQLite, MariaDB/MySQL, PostgreSQL).
  • Automatically execute the generated SQL using a Driver you provide.
  • Optionally cache transpilation results.

What is Bob?

Bob is a lightweight, declarative transpiler that lets you define database schemas and queries using a simple, human‑readable DSL called bob.
Bob then converts those definitions into SQL for multiple engines such as SQLite, MariaDB/MySQL, and PostgreSQL.

Instead of writing verbose SQL by hand, you describe tables, relationships, and queries concisely, and Bob generates the corresponding SQL.

Installation

With pnpm:

pnpm add @bob-kit/client

With npm:

npm i @bob-kit/client

With yarn:

yarn add @bob-kit/client

Environment requirements

Right now, @bob-kit/client loads the WASM using Node APIs (fs, vm).
That means it’s currently Node.js-oriented (not browser) as implemented today.

Concepts: Driver and cache

The client needs a Driver (from @bob-kit/types) to execute the generated SQL:

  • driver: target engine ("sqlite" | "mariadb" | "mysql" | "postgres").
  • querier(query, ...params): async function that executes SQL (and returns rows for SELECT).
  • cache: optional implementation to cache transpilation results (memory, redis, etc.).

This package also exports a ready-to-use in-memory cache: MemoryCache.

Quick start (Node + SQLite)

Complete example using @bob-kit/sqlite as the querier:

import bob from "@bob-kit/client";
import { MemoryCache } from "@bob-kit/client/cache";
import { sqlite } from "@bob-kit/sqlite";

const querier = sqlite(":memory:");

const client = bob({
  driver: "sqlite",
  querier,
  cache: new MemoryCache(),
});

// 1) Define multiple tables in a single query
await client.query`
  table Profile {
    id id
    avatar string
  }

  table Users {
    id id
    name string
    lastName string
    Profile
  }
`;

// 2) Insert one entity
await client.query`
  new Profile {
    avatar "bear"
  }
`;

// 3) Bulk insert with multiple rows and columns
await client.query`
  new Users name          lastName      Profile.id {
            "John"        "Doe"         1
            "Mary"        "Jane"        3
            "Robert"      "Smith"       3
  }
`;

// 4) Create reusable query functions with types
const getUserByName = (name: string) => client.query`
  get Users {
    name
    profile_id

    -> Profile {
        avatar
    }

    desc Profile.id
  }
`;

const users = await getUserByName("John");

Advanced examples

Joins and relationships

// Join with related table
const usersWithProfiles = await client.query`
  get Users {
    id
    name
    lastName

    -> Profile {
      avatar
    }
  }
`;

Filtering and sorting

const filteredUsers = await client.query`
  get Users {
    id
    name
    if name = "John" || "Mary"
  }
`;

const sortedUsers = await client.query`
  get Users {
    id
    name
    desc id
  }
`;

Parameters

The API accepts parameters as arguments to query(...). Internally, the template literal is converted to a string using ? as the placeholder.

const byEmail = await client.query`
  get Users {
    id
    name
    email
    if email == ?
  }
`("[email protected]");

API

default export function bob(driver: Driver)

Creates a client instance.

Returns an object with:

  • query<A>(input, ...params): Promise<A[]>: executes a bob input. You can destructure it for convenience: const { query: b } = bob(...).
  • cache(id)(input, ...params): Promise<A[]>: same as query, but caches the transpilation result under a fixed id.
  • factory.<id>(input, ...params): Promise<A[]>: shortcut for cache("<id>")(...).

bobQuery({ driver, input, id? })

Low-level function that only performs transpilation (and applies cache if present). You usually won’t need it.

@bob-kit/client/cache

Includes:

  • MemoryCache: in-memory (Map) cache for BobQueryResult.

Errors

When Bob returns a parsing/validation/transpilation error, the client throws BobError with:

  • name: a value from BobErrors (from @bob-kit/types/errors)
  • message: error details

Important notes

  • Multiple actions: if your input generates more than 1 SQL action, the client throws (MultipleActions).
    The current flow assumes 0 or 1 action per execution.
  • Table execution: if the output includes tables, all of them are executed (in order) before the action/query.

版本与发布记录

@bob-kit/client 共发布 35 个版本,首次发布于 2025/11/17;过去 12 个月发布了 35 个版本。

版本发布日期距今
0.0.34latest2026/05/016 个月前
0.0.332026/02/228 个月前
0.0.322026/01/249 个月前
0.0.312026/01/249 个月前
0.0.302026/01/229 个月前
0.0.292026/01/149 个月前

关于 @bob-kit/client 的常见问题

@bob-kit/client 是什么?

Disclaimer: Bob is under development! Use at your own risk.

如何安装 @bob-kit/client?

在项目目录运行 npm install @bob-kit/client,或使用 pnpm add / yarn add / bun add。

@bob-kit/client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bob-kit/client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bob-kit/client 可以商用吗?

@bob-kit/client 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bob-kit/client 的下载量有多少?

根据 npm 官方下载统计,@bob-kit/client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 7,250 次,日均约 242 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bob-kit/client 还在维护吗?

最新版本 0.0.34 发布于 2026/05/01(6 个月前);至今共发布 35 个版本,其中过去 12 个月发布了 35 个;首次发布于 2025/11/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。