1.0.26 • Published 2 years ago
laf-db-query-wrapper v1.0.26
简介
对 laf-client-sdk
/ database-ql 数据库操作语法包装, 借鉴 MyBatis-Plus 的链式查询语法, 优化单表 CRUD 操作, 避免频繁的判断响应状态, 构建
where 参数; 同时提供完整 model
类型提示;
开始
安装
npm install laf-db-query-wrapper -S
配置
在项目目录下任意位置:
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
import {LafClient} from 'laf-db-query-wrapper'
export class AccountService {
private readonly client = new LafClient<Account>(Account.TABLE_NAME)
private async test() {
// 新增
const id: string | number = await this.client.insert({})
// 查
const account: Account | null = await this.client.selectById('<ID>')
// 更新
const updateOk: boolean = await this.client.updateById('<ID>', {}, '_id')
// 删除
const deleteOk: boolean = await this.client.deleteById('<ID>')
}
}
分页查询, 列表查询, 计数, 等复杂查询: QueryChainWrapper
和 UpdateChainWrapper
详细的操作见类型提示和方法文档注释
import {QueryChainWrapper} from 'laf-db-query-wrapper'
import {UpdateChainWrapper} from 'laf-db-query-wrapper'
// 直接 new 然后使用链式操作
new QueryChainWrapper('<TableName>')
new UpdateChainWrapper('<TableName>')
// 或者使用上例中的 `LafClient` 本质上就是在方法内 new 对象, 只是复用了 LafClient 的 tableName
this.client.queryWrapper()
this.client.updateWrapper()
/* 数据库表模型定义 */
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>(Account.TABLE_NAME)
.eq('name', '')
.neq('level', 123)
.page(new Page<Account>(1, 20))
.then((page: Page<Account>) => {
page.list.forEach(i => {
})
})
// 关联查询
const withResource = new QueryChainWrapper<Resource>(Resource.TABLE_NAME)
.eq('type', 1)
.show('path')
.orderByDesc('createTime')
.getWithArg<Account>('id', 'avatarId', 'avatar')
new QueryChainWrapper<Account>(Account.TABLE_NAME)
.withOne(withResource)
.list(1000)
.then(list => {
console.debug(list)
})
仓库地址
1.0.26
2 years ago
1.0.25
2 years ago
1.0.24
2 years ago
1.0.22
2 years ago
1.0.23
2 years ago
1.0.21
3 years ago
1.0.20
3 years ago
1.0.19
3 years ago
1.0.18
3 years ago
1.0.17
3 years ago
1.0.16
3 years ago
1.0.15
3 years ago
1.0.14
3 years ago
1.0.13
3 years ago
1.0.12
3 years ago
1.0.11
3 years ago
1.0.10
3 years ago
1.0.9
3 years ago
1.0.8
3 years ago
1.0.7
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago