# laf-db-query-wrapper **Repository Path**: syrm/laf-db-query-wrapper ## Basic Information - **Project Name**: laf-db-query-wrapper - **Description**: `laf-client-sdk ` 查询语法包装 - **Primary Language**: TypeScript - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-01-11 - **Last Updated**: 2023-03-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简介 对 [laf-client-sdk](https://www.npmjs.com/package/laf-client-sdk) / [database-ql](https://www.npmjs.com/package/database-ql) 数据库操作语法包装, 借鉴 [MyBatis-Plus](https://baomidou.com/) 的链式查询语法, 优化单表 CRUD 操作, 避免频繁的判断响应状态, 构建 where 参数; 同时提供完整 `model` 类型提示; # 开始 ## 安装 ```npm npm install laf-db-query-wrapper -S ``` ## 配置 在项目目录下任意位置: ```typescript import {LafWrapperConfig} from 'laf-db-query-wrapper' // 你的 laf-client-sdk cloud 对象 import {cloud} from '@/config/LafClientSdkConfig' // 最小配置 LafWrapperConfig.cloud = () => cloud // 自定义日志实现 LafWrapperConfig.LoggerFactory = myLoggerFactory // 设置日志等级, LoggerLevel.ALL 是默认值 LafWrapperConfig.LoggerFactory.enableLevel = LoggerLevel.ALL ``` ### 使用 #### 单表, 单记录(文档) 增删改查操作: `LafClient` ```typescript import {LafClient} from 'laf-db-query-wrapper' export class AccountService { private readonly client = new LafClient(Account.TABLE_NAME) private async test() { // 新增 const id: string | number = await this.client.insert({}) // 查 const account: Account | null = await this.client.selectById('') // 更新 const updateOk: boolean = await this.client.updateById('', {}, '_id') // 删除 const deleteOk: boolean = await this.client.deleteById('') } } ``` #### 分页查询, 列表查询, 计数, 等复杂查询: `QueryChainWrapper` 和 `UpdateChainWrapper` 详细的操作见类型提示和方法文档注释 ```typescript import {QueryChainWrapper} from 'laf-db-query-wrapper' import {UpdateChainWrapper} from 'laf-db-query-wrapper' // 直接 new 然后使用链式操作 new QueryChainWrapper('') new UpdateChainWrapper('') // 或者使用上例中的 `LafClient` 本质上就是在方法内 new 对象, 只是复用了 LafClient 的 tableName this.client.queryWrapper() this.client.updateWrapper() ``` ```typescript /* 数据库表模型定义 */ class Account { public static readonly TABLE_NAME: string = 'sys_account' id: number name: string password: string level: number avatarId: number } class Resource { public static readonly TABLE_NAME = 'sys_resource' id: number path: string type: 1 | 2 | 3 createTime: number } // 分页查询 new QueryChainWrapper(Account.TABLE_NAME) .eq('name', '') .neq('level', 123) .page(new Page(1, 20)) .then((page: Page) => { page.list.forEach(i => { }) }) // 关联查询 const withResource = new QueryChainWrapper(Resource.TABLE_NAME) .eq('type', 1) .show('path') .orderByDesc('createTime') .getWithArg('id', 'avatarId', 'avatar') new QueryChainWrapper(Account.TABLE_NAME) .withOne(withResource) .list(1000) .then(list => { console.debug(list) }) ``` # 仓库地址