# lite-ibatis **Repository Path**: kiduc/lite-ibatis ## Basic Information - **Project Name**: lite-ibatis - **Description**: 轻量sql构建工具思路模仿ibatis,格式模仿pug - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-09 - **Last Updated**: 2026-04-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # lite-ibatis 轻量级 SQL 模板引擎,思路模仿 ibatis,格式模仿 pug,使用缩进式语法。 ## 特性 - **缩进式语法** - 类似 Pug 的简洁语法 - **条件构建** - `#if` 动态生成 SQL 片段 - **参数替换** - `#{param}` 预编译占位符,`${param}` 直接插值 - **编译执行** - 模板编译为 JS 函数,高效复用 - **可扩展** - 支持自定义语法插件 ## 安装 ```bash npm install lite-ibatis ``` ## 快速开始 ```typescript import { LiteIbatis } from 'lite-ibatis'; const lite = new LiteIbatis(); // 1. 解析模板 const tokens = lite.parse(`$getUser: select * from users #if name where name = #{name} #if age and age = #{age}`); // 2. 编译为可执行函数 const run = lite.compile(tokens); // 3. 执行并获取 SQL 和参数 const [sql, params] = run('getUser', { name: '张三', age: 25 }); // sql: select * from users\nwhere name = ?\nand age = ? // params: ['张三', 25] ``` ## 语法 | 语法 | 说明 | 示例 | |------|------|------| | `$id:` | 定义 SQL 块 | `$getUser:` | | `#{param}` | 预编译参数(生成 `?`) | `where id = #{id}` | | `${param}` | 字符串插值 | `select * from ${table}` | | `#if cond` | 条件块 | `#if name\n where name = #{name}` | | `// comment` | 单行注释 | `// 这是注释` | | `//.` | 多行注释 | `//.\n 多行\n 注释` | | `- code` | 单行脚本 | `- var x = 1` | | `-.` | 多行脚本 | `-. \n var x = 1` | ## API ### `new LiteIbatis(options?)` 创建实例。 ### `lite.parse(source: string): Token[]` 解析模板字符串为 Token 树。 ### `lite.compile(tokens: Token[]): Compiled` 编译 Token 树为可执行函数,返回 `(id: string, data?: any) => [sql: string, params: any[]]`。 ### `lite.syntax` 获取/设置自定义语法插件。 ## 文档 详细文档请查看 [doc/](doc/) 目录。 ## 许可证 MIT