We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
类似 oss / mysql 等都可能存在需要创建多个实例的需求,在插件里面现在有两种实现方式:
this.mysql.db1
对外的插件最好有一个统一的形式。特别是 aliyun-egg 估计会遇到挺多类似的问题
The text was updated successfully, but these errors were encountered:
每个 client 的实例化方案都不一样,如何通过一个 API 搞定呢
Sorry, something went wrong.
提供一个 Singleton 类,所有的类似 oss / rds / mongo 这样的插件都继承于 Singleton。
Singleton
class Sigleton { constructor(opts) { this.clients = new Map(); if (opts.clients) { for (const id in opts.clients) { this.set(id, this.create(opts.clients[id])); } return; } } get(id) { return this.clients.get(id); } set(id, client) { return this.clients.set(id, client); } create(config) { // 子类需要实现 create 方法 } } // lib/oss.js const oss = require('ali-oss'); const Sigleton = require('egg-sigleton').; class Oss extends Sigleton { constructor(opts) { super(opts); } create(config) { return oss(config); } } // app.js const oss = require('./lib/oss'); module.exports = function (app) { app.oss = new Oss(app.config.oss); app.oss.set('cdn', app.oss.create(app.config.cdnOss)); // or app.cdnOss = app.oss.create(app.config.cdnOss); } // services 中 get cdnOss() { return this.app.oss.get('cdnOss'); } * upload(name, file) { yield this.cdnOss.upload(name, file); }
不过如果只有一个实例也要遵循这个方式...
feat: mv examples from egg (eggjs#3)
c291ad3
dead-horse
No branches or pull requests
类似 oss / mysql 等都可能存在需要创建多个实例的需求,在插件里面现在有两种实现方式:
this.mysql.db1
来获取对外的插件最好有一个统一的形式。特别是 aliyun-egg 估计会遇到挺多类似的问题
The text was updated successfully, but these errors were encountered: