@bob- kit/ 客户端
免责声明:
鲍勃正在开发中! 自己冒险使用.
公务 鲍勃 消费者客户端 bob DSL 从 Java 脚本/ Type 脚本。 。 。 .
这个包装入了Bob的运行时间(WASM),并暴露出一个小API:
- 翻译你的
bob为给定的引擎输入SQL(SQLite,MariaDB/MySQL,PostgreSQL). - 使用一个
Driver你提供. - 可选缓存转录结果.
鲍勃是什么?
鲍勃 是一个轻量级、 声明式的转录器, 可以定义数据库 计划 财务报告和审定财务报表 查询 使用一个简单的,人类可读的DSL bob。 。 。 .
Bob然后将这些定义转换成SQL,用于多个引擎,例如: SQL 点, (中文(简体) ), 马里亚DB/ MySQL,以及 邮递SQL。 。 。 .
与其用手写动词SQL,不如简洁地描述表格,关系,以及查询,而Bob生成相应的SQL.
安装
与 pnpm 连接 :
pnpm add @bob-kit/client
以 npm 为单位 :
npm i @bob-kit/client
用纱线:
yarn add @bob-kit/client
环境要求
现在, @bob-kit/client 使用节点API加载WASM(fs, (中文(简体) ), vm) (中文(简体) ).
也就是说现在 面向节点.js (不是浏览器)今天执行.
概念:驱动和缓存
客户需要一个 Driver (从) @bob-kit/types执行生成的 SQL :
- 驱动程序:目标引擎(
"sqlite" | "mariadb" | "mysql" | "postgres") (中文(简体) ). - 等离子( 等离子): 执行 SQL 的async 函数(并返回行)
SELECT) (中文(简体) ). - 缓存: 缓存转录结果的可选执行(memory, redis等).
此套件还导出一个现成的内存: MemoryCache。 。 。 .
快速启动( 节点 + SQLite)
使用完整示例 @bob-kit/sqlite 作为 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");
高级实例
结合和关系
// Join with related table
const usersWithProfiles = await client.query`
get Users {
id
name
lastName
-> Profile {
avatar
}
}
`;
过滤和排序
const filteredUsers = await client.query`
get Users {
id
name
if name = "John" || "Mary"
}
`;
const sortedUsers = await client.query`
get Users {
id
name
desc id
}
`;
参数
API 接受参数作为参数 query(...)。在内部,模板文字将使用 ? 作为占位符.
const byEmail = await client.query`
get Users {
id
name
email
if email == ?
}
`("[email protected]");
API 密码
default export function bob(driver: Driver)
创建客户端实例.
以 :
query<A>(input, ...params): Promise<A[]>: 执行 abob输入。 为了方便,你可以调整结构:const { query: b } = bob(...)。 。 。 .cache(id)(input, ...params): Promise<A[]>: 与query,但缓存转换结果在一个固定id。 。 。 .factory.<id>(input, ...params): Promise<A[]>: 快捷键cache("<id>")(...)。 。 。 .
bobQuery({ driver, input, id? })
低级函数,只执行转录(如果存在则应用缓存). 你通常不需要它.
@bob-kit/client/cache
包括:
MemoryCache: 内嵌 (映射)缓存BobQueryResult。 。 。 .
错误
当Bob返回解析/验证/递译错误时,客户端会丢弃 BobError 改为:
name: 一个值来自BobErrors(从)@bob-kit/types/errors(中文(简体) )message: 错误细节
重要说明
- 多重行动:如果您的输入生成超过1个 SQL 动作,客户端会丢出(
MultipleActions) (中文(简体) ).
当前流量假定 0或1个动作 每次执行. - 表格执行: 如果输出包含
tables,它们全部在动作/静音前被执行(顺序).