Skip to content

Repository files navigation

@nelts/typeorm

基于ts:typeorm的nelts插件。

Usage

npm i @nelts/typeorm

Example

我们需要在package.json上写入依赖:

{
"plugin": {
"@nelts/typeorm": {
"enable": true,
"runAt": [
"master",
"worker",
"agent"
]
}
}
}

我们需要定义一些全局type或者interface类型[src/index.ts]

import{Connection}from'typeorm';import{LocalAgentFactoryasOrmLocalAgentFactory,LocalMasterFactoryasOrmLocalMasterFactory,LocalAgentPluginasOrmLocalAgentPlugin,LocalMasterPluginasOrmLocalMasterPlugin,LocalWorkerContextasOrmLocalWorkerContext,LocalWorkerFactoryasOrmLocalWorkerFactory,LocalWorkerPluginasOrmLocalWorkerPlugin,}from'@nelts/typeorm';importPackagefrom'./modal/package';// worker interfacesexportinterfaceLocalWorkerContextextendsOrmLocalWorkerContext{conn: Connection;}exportinterfaceLocalWorkerPluginextendsOrmLocalWorkerPlugin{getConnection(id: string): Connection;}exportinterfaceLocalWorkerFactoryextendsOrmLocalWorkerFactory{}// agent interfacesexportinterfaceLocalAgentPluginextendsOrmLocalAgentPlugin{}exportinterfaceLocalAgentFactoryextendsOrmLocalAgentFactory{conn: Connection,}// master interfacesexportinterfaceLocalMasterFactoryextendsOrmLocalMasterFactory{}exportinterfaceLocalMasterPluginextendsOrmLocalMasterPlugin{}exportconstModals=[Package];

在master进程上,需要预设置数据库信息用来初始化数据库 [src/master.ts]

import{LocalMasterPlugin,Modals}from'./index';exportdefault(plu: LocalMasterPlugin)=>{plu.on('props',asyncconfigs=>{constormComponent=plu.getComponent<LocalMasterPlugin>('@nelts/typeorm');ormComponent.typeorm.preset(configs.mysql,Modals);});}

在agent进程上,我们需要设置conn对象获取方式

import{LocalAgentPlugin,Modals,LocalAgentFactory}from'./index';exportdefault(plu: LocalAgentPlugin)=>{letid: string;plu.on('props',asyncconfigs=>{constormComponent=plu.getComponent<LocalAgentPlugin>('@nelts/typeorm');id=ormComponent.typeorm.preset(configs.mysql,Modals);});plu.on('ServerStarted',async()=>{constapp=plu.appasLocalAgentFactory;constormComponent=plu.getComponent<LocalAgentPlugin>('@nelts/typeorm');if(!id)thrownewError('you cannot use getConnection method before recevie configs');app.conn=ormComponent.typeorm.get(id);})}

在worker进程上,我们需要对Context对象进程设置 [src/worker.ts]

import{LocalWorkerPlugin,Modals,LocalWorkerContext}from'./index';exportdefault(plu: LocalWorkerPlugin)=>{letid: string;plu.on('props',asyncconfigs=>{constormComponent=plu.getComponent<LocalWorkerPlugin>('@nelts/typeorm');id=ormComponent.typeorm.preset(configs.mysql,Modals);});plu.app.on('ContextStart',async(ctx: LocalWorkerContext)=>ctx.conn=plu.getConnection(id));plu.getConnection=(id: string)=>{if(!id)thrownewError('you cannot use getConnection method before recevie configs');constormComponent=plu.getComponent<LocalWorkerPlugin>('@nelts/typeorm');returnormComponent.typeorm.get(id);}}

至此,全部设置完成。当服务启动后,首先会在master进程上初始化数据库,然后在agent和worker进程上获取到对象的conn对象。我们可以在Controller层上这样使用

import{Controller,Prefix,Get}from'@nelts/http';import{LocalWorkerContext,LocalWorkerPlugin}from'../index';importPackagefrom'../modal/package';
@Prefix()exportdefaultclassIndexControllerextendsController<LocalWorkerPlugin>{
@Get()asyncHome(ctx: LocalWorkerContext){constpackageRepo=ctx.conn.getRepository(Package);ctx.body=awaitpackageRepo.find();}}

在agent进程上可以这样使用:

import{AgentComponent,AgentComponentImplements,Ipc,Auto}from'@nelts/agent';
@AutoexportdefaultclassIndexAgentextendsAgentComponentimplementsAgentComponentImplements{
@Ipc(true)asynctest(){constpackageRepo=this.app.conn.getRepository(Package);returnawaitpackageRepo.find();}}

License

MIT

Copyright (c) 2019-present, yunjie (Evio) shen

About

typeorm for nelts

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages