egg-orm-ts
Version:
Simple orm with Typeorm, for egg plugin.
190 lines (148 loc) • 5.13 kB
Markdown
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[]: https://img.shields.io/npm/v/egg-orm-ts.svg?style=flat-square
[]: https://npmjs.org/package/egg-orm-ts
[]: https://img.shields.io/travis/eggjs/egg-orm-ts.svg?style=flat-square
[]: https://travis-ci.org/eggjs/egg-orm-ts
[]: https://img.shields.io/codecov/c/github/eggjs/egg-orm-ts.svg?style=flat-square
[]: https://codecov.io/github/eggjs/egg-orm-ts?branch=master
[]: https://img.shields.io/david/eggjs/egg-orm-ts.svg?style=flat-square
[]: https://david-dm.org/eggjs/egg-orm-ts
[]: https://snyk.io/test/npm/egg-orm-ts/badge.svg?style=flat-square
[]: https://snyk.io/test/npm/egg-orm-ts
[]: https://img.shields.io/npm/dm/egg-orm-ts.svg?style=flat-square
[]: https://npmjs.org/package/egg-orm-ts
<!--
Description here.
-->
```bash
$ npm i egg-orm-ts --save
```
```js
// {app_root}/config/plugin.js
exports.ormTs = {
enable: true,
package: 'egg-orm-ts',
};
```
see [config/config.default.ts](config/config.default.ts) for more detail.
```js
// {app_root}/config/config.default.ts
exports.typeorm = {
// For DataBase (Recommended offline profile.)
...
};
```
- Controller
```js
// {app_root}/app/controller/demo.ts
const { BaseController } = require('egg-orm-ts');
const Joi = require('joi');
export default class DemoController extends BaseController {
async demo() {
// Use service with the same name ('{app_root}/app/service/demo.ts')
const { ctx, callService } = this;
// use function with the same name ('demo')
const result1 = await callService(ctx.request.body);
// use function with the same name ('demo') and with paramsSchema.
const result2 = await callService(
ctx.request.body,
Joi.object({
name: Joi.string().required(),
}).required()
);
// use another function use third param ('methodName') and with paramsSchema (Or just set 'null').
const result3 = await callService(
ctx.request.body,
// Joi.object({
// name: Joi.string().required(),
// }).required(),
null,
'demo2'
);
// Unified Response
ctx.body = this.success(result);
}
}
```
- Service
```js
// {app_root}/app/service/demo.ts
const { BaseService } = require('egg-orm-ts');
export default class DemoService extends BaseService<Demo> {
/**
* description
* @param {object} params params
* @return {object} obj
*/
async demo(params) {
// Use default method of BaseService or Other method of Typeorm.
// return await this.save({ name: '名称' + Math.floor(Math.random() * 1000), type: String(Math.floor(Math.random() * 10) % 3) });
// const params = undefined;
// const params = {};
// const params = { id: 1 };
// const params = { id: [ 1, 2 ] };
// const params = { order: { id: 'desc' } };
// const params = { id: [ 1, 2 ], order: { id: 'desc' } };
// const params = { select: [ 'id' , 'name' ], id: [ 1, 2 ], order: { id: 'desc' } };
// const params = { where: { id: [ 1, 2 ] }, order: { id: 'desc' } };
// const params = { select: [ 'id' , 'name' ], where: { id: [ 1, 2 ] }, order: { id: 'desc' } };
// const params = { select: [ 'id' , 'name' ], where: { id: [ 1, 2 ] }, sort: { id: 'desc' } };
// const params = { name: '名称569',
// id: {
// opr: 'in',
// // opr: '<>',
// val: [ 1, 2 ],
// },
// order: {
// id: 'desc',
// }
// };
// return await this.getList(params);
// return await this.getPage(params);
}
}
```
- Entity
> Use [typeorm-model-generator](https://github.com/Kononnable/typeorm-model-generator)
and sometimes with [typeorm-encrypted](https://github.com/generalpiston/typeorm-encrypted).
```js
// {app_root}/app/entity/Demo.ts
import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
@Index("pk_home_id", ["id"], { unique: true })
@Entity("home", { schema: "public" })
export class Home {
@PrimaryGeneratedColumn({ type: "integer", name: "id" })
id: number;
@Column("character varying", { name: "name", nullable: true, length: 100 })
name: string | null;
@Column("character varying", { name: "type", nullable: true })
type: string | null;
}
```
- Util Functions
```js
// desensitize ...
const { desensitize } = require('egg-orm-ts');
// 123456 => 12**56
console.log(desensitize(123456, 2, 2));
```
see [Typeorm](https://github.com/typeorm/typeorm) for more detail.
see [egg-typeorm](https://github.com/forsigner/egg-typeorm) for more detail.
<!-- example here -->
Please open an issue [here](https://github.com/senique/egg-orm-ts/issues).
[](LICENSE)