https://github.com/waitingsong/egg-kmore
https://github.com/waitingsong/egg-kmore
egg egg-plugin eggplugin knex midway mssql oracle postgresql sqlite3 typescript
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/waitingsong/egg-kmore
- Owner: waitingsong
- License: mit
- Created: 2019-08-17T09:37:05.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-21T07:15:19.000Z (almost 6 years ago)
- Last Synced: 2025-01-19T12:27:34.391Z (5 months ago)
- Topics: egg, egg-plugin, eggplugin, knex, midway, mssql, oracle, postgresql, sqlite3, typescript
- Language: JavaScript
- Size: 360 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# [egg-kmore](https://waitingsong.github.io/egg-kmore/)
[kmore](https://www.npmjs.com/package/kmore) for midway framework.
[](https://www.npmjs.com/package/egg-kmore)
[](https://opensource.org/licenses/MIT)

[](https://conventionalcommits.org)## Installation
```sh
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`:
```ts
import { EggPlugin } from 'midway'export default {
kmore: {
enable: true,
package: 'egg-kmore',
},
} as EggPlugin
```### Add Configurations
```ts
/* 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(),
}// 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
```ts
/* 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,
) { }/**
* Read user info
*/
public async getUser(options: GetUserOpts): Promise {
const { rb, tables: t } = this.dbconst 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 {
const { rb } = this.dbconst 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
```sh
cd ${app_root}
kmore gen --path ./src// then you could build the project
npm run build
```## License
[MIT](LICENSE)### Languages
- [English](README.md)
- [中文](README.zh-CN.md)