22.6.0 • Published 3 years ago

egg-kmore v22.6.0

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

egg-kmore

kmore for midway framework.

Version License npm.io Conventional Commits

Installation

npm install egg-kmore knex

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

Configuration

Enable Plugin

Edit ${app_root}/src/config/plugin.ts:

import { EggPlugin } from 'midway'

export default {
  kmore: {
    enable: true,
    package: 'egg-kmore',
  },
} as EggPlugin

Add Configurations

/* location: ${app_root}/src/config/config.${env}.ts */

import { EggKmoreConfig, genTbListFromType, ClientOpts } from 'egg-kmore'
import { TbListModel } from '../app/user/user.model'

const master: ClientOpts = {
  knexConfig: {
    client: 'pg',
    connection: {
      host: 'localhost',
      user: 'postgres',
      password: '',
      database: 'db_ci_test',
    },
    acquireConnectionTimeout: 10000,
  },
  tables: genTbListFromType<TbListModel>(),
}

// app: default true
export const kmore: EggKmoreConfig = {
  client: master,
}


/* location: ../app/user/user.model.ts */
export interface TbListModel {
  tb_user: User
  tb_user_detail: UserDetail
}

/**
 * user info
 */
export interface UserInfo extends User, UserDetail {
}

export interface User {
  uid: number
  user_name: string
}
export interface UserDetail {
  uid: number
  phone: string
  email: string
}

export interface GetUserOpts {
  uid: number
}

Usage

/* location: ../app/user/user.service.ts */

import { provide, plugin } from 'midway'
import { DbModel } from 'egg-kmore'
import { GetUserOpts, UserInfo, User, TbListModel } from './user.model'

@provide()
export class UserService {

  constructor(
    @plugin('kmore') private readonly db: DbModel<TbListModel>,
  ) { }

  /**
   * Read user info
   */
  public async getUser(options: GetUserOpts): Promise<UserInfo> {
    const { rb, tables: t } = this.db

    const row: UserInfo = await rb.tb_user()
      .innerJoin(
        t.tb_user_detail,
        `${t.tb_user}.uid`,
        `${t.tb_user_detail}.uid`,
      )
      .select('*')
      .where(`${t.tb_user}.uid`, options.uid)
      .then(rows => rows[0])

    return row
  }

  /**
   * Read user_name
   */
  public async getUserName(options: GetUserOpts): Promise<User['user_name']> {
    const { rb } = this.db

    const name = await rb.tb_user()
      .select('user_name')
      .where('uid', options.uid)
      .then(rows => rows[0] ? rows[0].user_name : '')

    return name
  }

}

Generating source files pre build

The files generated automatically under typescript environment for debug or test

cd ${app_root}
kmore gen --path ./src

// then you could build the project
npm run build

License

MIT

Languages

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

22.0.1

3 years ago

22.0.0

3 years ago

21.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

14.0.0

3 years ago

13.0.0

3 years ago

17.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

6.1.0

3 years ago

7.1.0

3 years ago

6.2.1

3 years ago

6.2.0

3 years ago

6.2.2

3 years ago

7.0.0

3 years ago

6.0.0

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

3.3.0

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.1

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.16.4

4 years ago

0.17.0

4 years ago

0.16.3

4 years ago

0.16.0

4 years ago

0.16.1

4 years ago

0.16.2

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.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago