UNPKG

egg-orm-ts

Version:

Simple orm with Typeorm, for egg plugin.

190 lines (148 loc) 5.13 kB
# egg-orm-ts [![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] [npm-image]: https://img.shields.io/npm/v/egg-orm-ts.svg?style=flat-square [npm-url]: https://npmjs.org/package/egg-orm-ts [travis-image]: https://img.shields.io/travis/eggjs/egg-orm-ts.svg?style=flat-square [travis-url]: https://travis-ci.org/eggjs/egg-orm-ts [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-orm-ts.svg?style=flat-square [codecov-url]: https://codecov.io/github/eggjs/egg-orm-ts?branch=master [david-image]: https://img.shields.io/david/eggjs/egg-orm-ts.svg?style=flat-square [david-url]: https://david-dm.org/eggjs/egg-orm-ts [snyk-image]: https://snyk.io/test/npm/egg-orm-ts/badge.svg?style=flat-square [snyk-url]: https://snyk.io/test/npm/egg-orm-ts [download-image]: https://img.shields.io/npm/dm/egg-orm-ts.svg?style=flat-square [download-url]: https://npmjs.org/package/egg-orm-ts <!-- Description here. --> ## Install ```bash $ npm i egg-orm-ts --save ``` ## Usage ```js // {app_root}/config/plugin.js exports.ormTs = { enable: true, package: 'egg-orm-ts', }; ``` ## Configuration ### **Database** 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.) ... }; ``` ## Example - 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)); ``` ### **Reference** see [Typeorm](https://github.com/typeorm/typeorm) for more detail. see [egg-typeorm](https://github.com/forsigner/egg-typeorm) for more detail. ## Example <!-- example here --> ## Questions & Suggestions Please open an issue [here](https://github.com/senique/egg-orm-ts/issues). ## License [MIT](LICENSE)