问题描述
你好,我有一个关于单针筒和性能的问题, 我启动了mikro orm并进行表达时遵循以下逻辑,但是我有以下问题: 将我的orm / http服务器保留为单例是一个会影响性能的选项,这是不正确的吗?
引导应用程序:
export interface BootStrapContainer {
HttpServer: IHttpServer;
DatabaSEOrm:IDatabaSEORM
}
@singleton()
export class BootstrapApplication {
constructor(@inject(ExpressServer) private HttpServer: IHttpServer,@inject(MikroOrmClient) private DatabaSEOrm: IDatabaSEORM) {
}
public start = async () => {
console.log(chalk.yellow(`Starting Http Server`))
await this.HttpServer.start();
console.log(chalk.yellow(`Http Server started successfully`))
console.log(chalk.yellow(`Starting Database`))
await this.DatabaSEOrm.start();
console.log(chalk.yellow(`Database started successfully`))
};
}
orm:
@singleton()
export class MikroOrmClient implements IDatabaSEORM{
private connection: MikroORM<IDatabaseDriver<Connection>>;
constructor(@inject("OrmConfig") private options: Options){
}
public getConnection = ():MikroORM<IDatabaseDriver<Connection>> => {
return this.connection;
};
public close(): void {
if(!this.connection) return
this.connection.close()
}
public getEntityManager() {
return this.connection.em.fork()
}
public start = async () => {
try {
this.connection = await MikroORM.init(this.options)
const migrator = await this.connection.getMigrator();
const migrations = await migrator.getPendingMigrations();
if (!(migrations && migrations.length > 0)) return;
await migrator.up();
} catch (error) {
process.stdout.write(
chalk.redBright(`${(error as Error).message}\n`)
);
}
};
}
httpServer:
@singleton()
export class ExpressServer implements IHttpServer {
private server: express.Application;
constructor() {
this.server = express();
}
private initializeRouter = () => {
this.server.get('/favico.ico',(req,res) => {
res.sendStatus(404);
});
this.server.get('/',res) => {
res.send('Welcome');
});
};
private initializeErrorHandling() {
this.server.use(ErrorMiddleware);
}
private initializeMiddlewares = () => {
this.server.use(cors());
this.server.use(express.json());
this.server.use(bodyParser.json());
this.server.use(cookieParser());
};
public getServer = () => {
return this.server;
};
public start = async () => {
this.initializeMiddlewares();
this.initializeRouter();
this.initializeErrorHandling();
this.server.listen(3000,() => {
console.log('this server is ready on port 3000');
});
};
public stop = (): void => {
throw new Error('Method not implemented.');
};
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)