59.5.6 • Published 26 days ago

kmore v59.5.6

Weekly downloads
3
License
MIT
Repository
github
Last release
26 days ago

kmore

基于 Knex 的 SQL 查询生成器工厂, 提供高级 TypeScript 类型支持。

GitHub tag License npm.io ci codecov Conventional Commits lerna

特性

  • 类型安全的表对象操作
  • 类型安全的表连接操作
  • 类型安全的 IDE 编辑器自动完成
  • 一条查询链实现自动分页

安装

npm i kmore && npm i -D kmore-cli
// for Midway.js
npm i @mwcp/kmore && npm i -D kmore-cli

# Then add one of the following:
npm install pg
npm install pg-native
npm install mssql
npm install oracle
npm install sqlite3

pg-native-installation

基础应用

Build configuration:

Edit the package.json

{
  "script": {
    "build": "tsc -b && npm run db:gen",
    "db:gen": "kmore gen --path src/ test/",
    "db:gen-cjs": "kmore gen --path src/ test/ --format cjs"
  },
}

创建数据库连接

import { KnexConfig, kmoreFactory, genDbDict } from 'kmore'

// connection config
export const config: KnexConfig = {
  client: 'pg',
  connection: {
    host: 'localhost',
    user: 'postgres',
    password: 'foo',
    database: 'db_ci_test',
  },
}

// Define database model
export interface Db {
  tb_user: UserDo
  tb_user_ext: UserExtDo
}

export interface UserDo {
  uid: number
  name: string
  ctime: Date
}
export interface UserExtDo {
  uid: number
  age: number
  address: string
}  

const dict = genDbDict<Db>()
export const km = kmoreFactory({ config, dict })

建表

await km.dbh.schema
  .createTable('tb_user', (tb) => {
    tb.increments('uid')
    tb.string('name', 30)
    tb.timestamp('ctime', { useTz: false })
  })
  .createTable('tb_user_ext', (tb) => {
    tb.integer('uid')
    tb.foreign('uid')
      .references('tb_user.uid')
      .onDelete('CASCADE')
      .onUpdate('CASCADE')
    tb.integer('age')
    tb.string('address', 255)
  })
  .catch((err: Error) => {
    assert(false, err.message)
  })

插入数据

Snake style

// auto generated accessort tb_user() and tb_user_detail()
const { ref_tb_user, ref_tb_user_detail } = km.refTables

await ref_tb_user()
  .insert([
    { user_name: 'user1', ctime: new Date() }, // ms
    { user_name: 'user2', ctime: 'now()' }, // μs
  ])
  .then()

const affectedRows = await ref_tb_user_detail()
  .insert([
    { uid: 1, age: 10, user_address: 'address1' },
    { uid: 2, age: 10, user_address: 'address1' },
  ])
  .returning('*')
  .then()

Camel style

import { RecordCamelKeys } from '@waiting/shared-types'

// auto generated accessort tb_user() and tb_user_detail() 
const { ref_tb_user, ref_tb_user_detail } = km.camelTables

interface UserDO {
  user_name: string
  ctime: date | string
}
type UserDTO = RecordCamelKeys<UserDO>

const users: UserDTO[] = await ref_tb_user()
  .insert([
    { userName: 'user1', ctime: new Date() }, // ms
    { userName: 'user2', ctime: 'now()' }, // μs
  ])
  .returning('*')
  .then()

智能连表 (类型提示和自动完成)

const uid = 1

// tb_user JOIN tb_user_ext ON tb_user_ext.uid = tb_user.uid
const ret = await km.camelTables.ref_tb_user()
  .smartJoin(
    'tb_user_ext.uid',
    'tb_user.uid',
  )
  .select('*')
  .where({ uid }) // <-- 'uid' 可自动完成
  // .where('uid', uid)   <-- 'uid' 可自动完成
  // .where('tb_user_ext_uid', uid) <-- 'tb_user_ext_uid' 可自动完成
  // .where(km.dict.scoped.tb_user.uid, 1)
  .then(rows => rows[0])

assert(ret)
ret.uid
ret.tb_user_ext_uid   // <-- 重复字段名将会自动转换 为 "<表名>_<字段名>" 格式

More examples of join see joint-table

自动分页

  • RawType:

    const options: Partial<PagingOptions> = {
      page: 2,      // default 1
      pageSize: 20, // default 10
    }
    const users = await tables.ref_tb_user().autoPaging(options)
    assert(Array.isArray(users))
    assert(users.length)
    
    // 不可枚举分页属性
    const { 
      total,    // 总记录数
      page,     // 当前页号,起始 1
      pageSize, // 每页记录数
    } = users
    const [ user ] = users
  • WrapType:

    const options: Partial<PagingOptions> = {
      page: 2,      // default 1
      pageSize: 20, // default 10
    }
    const users = await tables.ref_tb_user().autoPaging(options, true)
    assert(! Array.isArray(users))
    assert(Array.isArray(users.rows))
    assert(users.rows.length)
    
    // 可枚举分页属性
    const { 
      total,    // 总记录数
      page,     // 当前页号,起始 1
      pageSize, // 每页记录数
      rows,     // 查询结果数据
    } = users
    const [ user ] = users.rows

More examples of auto paging see auto-paing

使用 knex

// drop table
await km.dbh.raw(`DROP TABLE IF EXISTS "${tb}" CASCADE;`).then()

// disconnect
await km.dbh.destroy()

Midway.js component

Config

// file: src/config/config.{prod | local | unittest}.ts

import { genDbDict } from 'kmore-types'
import { KmoreSourceConfig } from '@mwcp/kmore'
import { TbAppDO, TbMemberDO } from '../do/database.do.js'

export interface Db {
  tb_app: TbAppDO
  tb_user: TbMemberDO
}

export const dbDict = genDbDict<Db>()

const master: DbConfig<Db, Context> = {
  config: {
    client: 'pg',
    connection: {
      host: 'localhost',
      port: 5432,
      database: 'db_test',
      user: 'postgres',
      password: 'password',
    },
  },
  dict: dbDict,
  sampleThrottleMs: 500,
  enableTracing: true, // jaeger tracer
}
export const kmoreConfig: KmoreSourceConfig = {
  dataSource: {
    master,
    // slave,
  },
}  

Usage

import { Init, Inject } from '@midwayjs/decorator'

@Provide()
export class UserRepo {

  @Inject() dbManager: DbManager<'master' | 'slave', Db>

  protected db: Kmore<Db>

  @Init()
  async init(): Promise<void> {
    this.db = this.dbManager.getDataSource('master')
  }

  async getUser(uid: number): Promise<UserDTO | undefined> {
    const { ref_tb_user } = this.db.camelTables
    const user = await ref_tb_user()
      .where({ uid })
      .then(rows => rows[0])
    return user
  }
}

Demo

Packages

kmore is comprised of many specialized packages. This repository contains all these packages. Below you will find a summary of each package.

PackageVersion
kmorekmore-svg
kmore-typestypes-svg
kmore-clicli-svg
@mwcp/kmoremw-svg

License

MIT

Languages

59.5.6

26 days ago

59.5.5

29 days ago

59.5.4

30 days ago

59.5.3

1 month ago

59.5.2

2 months ago

59.4.0

2 months ago

59.4.1

2 months ago

59.3.0

2 months ago

59.2.1

3 months ago

59.2.0

3 months ago

59.1.0

3 months ago

59.0.0

3 months ago

58.0.0

8 months ago

58.0.1

8 months ago

58.0.2

8 months ago

57.0.1

8 months ago

57.0.0

8 months ago

56.0.2

9 months ago

55.6.0

9 months ago

55.5.3

10 months ago

55.5.2

11 months ago

56.0.0

9 months ago

56.0.1

9 months ago

55.3.3

12 months ago

55.2.7

1 year ago

53.2.2

1 year ago

53.2.3

1 year ago

53.2.1

1 year ago

54.0.0

1 year ago

53.1.1

1 year ago

55.0.0

1 year ago

55.2.3

1 year ago

55.2.0

1 year ago

55.2.1

1 year ago

55.1.1

1 year ago

55.1.2

1 year ago

53.0.0

1 year ago

53.0.1

1 year ago

51.3.3

1 year ago

47.0.0

2 years ago

51.3.4

1 year ago

51.3.1

1 year ago

51.3.2

1 year ago

51.3.0

1 year ago

47.0.2

2 years ago

47.0.1

2 years ago

52.1.0

1 year ago

43.0.0

2 years ago

46.1.0

2 years ago

45.1.0

2 years ago

46.0.1

2 years ago

46.0.0

2 years ago

37.0.0

2 years ago

37.0.2

2 years ago

46.0.2

2 years ago

45.2.0

2 years ago

50.0.0

1 year ago

42.0.0

2 years ago

49.0.2

1 year ago

49.0.1

1 year ago

49.0.0

1 year ago

49.0.3

1 year ago

40.0.0

2 years ago

48.2.0

2 years ago

38.0.0

2 years ago

45.0.0

2 years ago

48.1.1

2 years ago

51.0.0

1 year ago

50.2.0

1 year ago

41.0.0

2 years ago

48.0.1

2 years ago

48.0.0

2 years ago

52.0.0

1 year ago

52.0.3

1 year ago

51.1.3

1 year ago

51.1.1

1 year ago

51.1.0

1 year ago

40.1.0

2 years ago

39.0.2

2 years ago

39.0.1

2 years ago

44.0.0

2 years ago

39.0.0

2 years ago

51.2.2

1 year ago

51.2.3

1 year ago

51.2.0

1 year ago

36.0.3

2 years ago

36.0.4

2 years ago

36.0.2

2 years ago

36.0.0

2 years ago

36.2.1

2 years ago

36.2.0

2 years ago

34.3.0

2 years ago

34.3.1

2 years ago

33.1.0

2 years ago

33.1.1

2 years ago

28.2.0

2 years ago

28.2.1

2 years ago

31.0.0

2 years ago

27.0.0

2 years ago

34.2.0

2 years ago

33.0.0

2 years ago

33.0.2

2 years ago

28.1.0

2 years ago

31.3.0

2 years ago

35.0.0

2 years ago

35.0.1

2 years ago

33.3.0

2 years ago

29.0.1

2 years ago

29.0.2

2 years ago

29.0.0

2 years ago

32.0.0

2 years ago

32.0.1

2 years ago

28.0.0

2 years ago

28.0.1

2 years ago

28.0.2

2 years ago

26.0.0

2 years ago

30.0.0

2 years ago

34.0.0

2 years ago

33.2.0

2 years ago

25.1.6

2 years ago

25.1.0

2 years ago

25.0.1

2 years ago

25.0.0

2 years ago

23.0.0

2 years ago

23.0.2

2 years ago

24.0.2

2 years ago

24.0.1

2 years ago

24.0.0

2 years ago

22.6.1

3 years ago

22.6.0

3 years ago

22.4.0

3 years ago

22.5.0

3 years ago

22.2.0

3 years ago

22.0.2

3 years ago

21.0.0

3 years ago

22.0.1

3 years ago

22.0.0

3 years ago

19.0.0

3 years ago

18.0.0

3 years ago

20.0.1

3 years ago

20.0.0

3 years ago

17.2.0

3 years ago

12.0.1

3 years ago

15.0.0

3 years ago

13.0.0

3 years ago

17.0.0

3 years ago

14.0.0

3 years ago

16.0.0

3 years ago

12.0.0

3 years ago

10.4.0

3 years ago

11.0.0

3 years ago

10.2.0

3 years ago

10.1.0

3 years ago

10.1.1

3 years ago

10.3.0

3 years ago

10.0.0

3 years ago

7.6.1

3 years ago

7.7.1

3 years ago

7.7.0

3 years ago

7.7.2

3 years ago

8.0.0

3 years ago

9.0.0

3 years ago

7.3.1

3 years ago

7.6.0

3 years ago

7.4.1

3 years ago

7.4.0

3 years ago

7.5.0

3 years ago

7.3.0

3 years ago

7.2.0

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.1.0

3 years ago

6.0.0

3 years ago

6.2.1

3 years ago

6.2.0

3 years ago

6.2.2

3 years ago

5.7.5

3 years ago

5.7.4

3 years ago

5.7.3

3 years ago

5.5.0

3 years ago

5.3.1

3 years ago

5.3.0

3 years ago

5.1.0

3 years ago

5.6.0

3 years ago

5.4.0

3 years ago

5.2.0

3 years ago

5.7.2

3 years ago

5.7.1

3 years ago

5.7.0

3 years ago

5.0.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.0.3

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

4 years ago

1.1.1

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

0.21.0

4 years ago

0.20.0

4 years ago

0.19.0

4 years ago

0.18.0

4 years ago

0.17.1

4 years ago

0.17.0

4 years ago

0.16.4

4 years ago

0.16.3

4 years ago

0.16.1

4 years ago

0.16.2

4 years ago

0.16.0

4 years ago

0.15.0

4 years ago

0.14.0

4 years ago

0.13.1

4 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago