laf-db-query-wrapper
TypeScript icon, indicating that this package has built-in type declarations

1.0.26 • Public • Published

简介

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>')

    }
}

分页查询, 列表查询, 计数, 等复杂查询: QueryChainWrapperUpdateChainWrapper

详细的操作见类型提示和方法文档注释

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)
    })

仓库地址

https://gitee.com/song-yan-ru-mo_admin/laf-db-query-wrapper

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.261latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.261
1.0.251
1.0.241
1.0.231
1.0.221
1.0.211
1.0.201
1.0.191
1.0.182
1.0.171
1.0.161
1.0.151
1.0.141
1.0.131
1.0.121
1.0.111
1.0.101
1.0.91
1.0.81
1.0.71
1.0.61
1.0.51
1.0.41
1.0.31
1.0.21
1.0.11
1.0.01

Package Sidebar

Install

npm i laf-db-query-wrapper

Weekly Downloads

28

Version

1.0.26

License

Apache License 2.0

Unpacked Size

358 kB

Total Files

155

Last publish

Collaborators

  • songyanrumo