Skip to content

Commit

Permalink
docs(plugin): add config notice for addSingleton function (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
rccoder authored and atian25 committed Apr 2, 2018
1 parent 1c74a84 commit 1611079
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/source/en/advanced/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Some plugins are made to introduce existing service into framework, like [egg-my
- Use different instances of the same service in one application(e.g:connect to two different MySQL Databases)
- Dynamically initialize connection after getting config from other service(gets MySQL server address from configuration center and then creates connection)

If each plugin makes their own implementation, all sorts of configs and initializations will be chaotic. So the framework supplies the `app.addSingleton(name, creator)` API to unify the creation of this kind of services.
If each plugin makes their own implementation, all sorts of configs and initializations will be chaotic. So the framework supplies the `app.addSingleton(name, creator)` API to unify the creation of this kind of services. Note that while using the `app.addSingleton(name, creator)` method, the configuration file must have the `client` or `clients` key configuration as the `config` to the `creator` function.

#### Writing Plugin

Expand Down Expand Up @@ -340,7 +340,7 @@ class PostController extends Controller {

##### Multiple Instances

1. Of course we need to configure MySQL in the config file, but different from single instance, we need to add `clients` in the config to declare the configuration of different instances. meanwhile, the `default` field can be used to configure the shared configuration in multiple instances(e.g. host and port).
1. Of course we need to configure MySQL in the config file, but different from single instance, we need to add `clients` in the config to declare the configuration of different instances. meanwhile, the `default` field can be used to configure the shared configuration in multiple instances(e.g. host and port). Note that in this case,should use `get` function to specify the corresponding instance(eg: use `app.mysql.get('db1').query()` instead of using `app.mysql.query()` directly to get a `undefined`).

```js
// config/config.default.js
Expand Down
4 changes: 2 additions & 2 deletions docs/source/zh-cn/advanced/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ $ npm test
- 在一个应用中同时使用同一个服务的不同实例(连接到两个不同的 MySQL 数据库)。
- 从其他服务获取配置后动态初始化连接(从配置中心获取到 MySQL 服务地址后再建立连接)。

如果让插件各自实现,可能会出现各种奇怪的配置方式和初始化方式,所以框架提供了 `app.addSingleton(name, creator)` 方法来统一这一类服务的创建。
如果让插件各自实现,可能会出现各种奇怪的配置方式和初始化方式,所以框架提供了 `app.addSingleton(name, creator)` 方法来统一这一类服务的创建。需要注意的是在使用 `app.addSingleton(name, creator)` 方法时,配置文件中一定要有 `client` 或者 `clients` 为 key 的配置作为传入 `creator` 函数 的 `config`

#### 插件写法

Expand Down Expand Up @@ -340,7 +340,7 @@ class PostController extends Controller {

##### 多实例

1. 同样需要在配置文件中声明 MySQL 的配置,不过和单实例时不同,配置项中需要有一个 `clients` 字段,分别申明不同实例的配置,同时可以通过 `default` 字段来配置多个实例中共享的配置(如 host 和 port)。
1. 同样需要在配置文件中声明 MySQL 的配置,不过和单实例时不同,配置项中需要有一个 `clients` 字段,分别申明不同实例的配置,同时可以通过 `default` 字段来配置多个实例中共享的配置(如 host 和 port)。需要注意的是在这种情况下要用 `get` 方法指定相应的实例。(例如:使用 `app.mysql.get('db1').query()`,而不是直接使用 `app.mysql.query()` 得到一个 `undefined`)。

```js
// config/config.default.js
Expand Down

0 comments on commit 1611079

Please sign in to comment.