egg-orm-ts
v0.2.12
Published
Simple orm with Typeorm, for egg plugin.
Downloads
35
Maintainers
Readme
egg-orm-ts
Install
$ npm i egg-orm-ts --save
Usage
// {app_root}/config/plugin.js
exports.ormTs = {
enable: true,
package: 'egg-orm-ts',
};
Configuration
Database
see config/config.default.ts for more detail.
// {app_root}/config/config.default.ts
exports.typeorm = {
// For DataBase (Recommended offline profile.)
...
};
Example
- Controller
// {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
// {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
and sometimes with typeorm-encrypted.
// {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
// desensitize ...
const { desensitize } = require('egg-orm-ts');
// 123456 => 12**56
console.log(desensitize(123456, 2, 2));
Reference
see Typeorm for more detail.
see egg-typeorm for more detail.
Example
Questions & Suggestions
Please open an issue here.