2022-10-31 11:52:17 +08:00
# Application
2022-12-25 12:29:19 +08:00
## Overview
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
### Web Service
NocoBase Application is a web framework implemented based on [Koa ](https://koajs.com/ ), compatible with Koa API.
2022-11-13 23:00:59 +08:00
```javascript
// index.js
const { Application } = require('@nocobase/server');
2022-12-25 12:29:19 +08:00
// Create App instance and configure the database
2022-11-13 23:00:59 +08:00
const app = new Application({
database: {
dialect: 'sqlite',
storage: ':memory:',
}
});
2022-12-25 12:29:19 +08:00
// Register middleware, response to requests
2022-11-13 23:00:59 +08:00
app.use(async ctx => {
ctx.body = 'Hello World';
});
2022-12-25 12:29:19 +08:00
// Run in the CLI mode
2022-11-13 23:00:59 +08:00
app.runAsCLI();
```
2022-12-25 12:29:19 +08:00
After running `node index.js start` in CLI to start service, use `curl` to request service.
2022-11-13 23:00:59 +08:00
```bash
$> curl localhost:3000
Hello World
```
2022-12-25 12:29:19 +08:00
### CLI Tool
NocoBase Application has a built-in `cli commander` , which can be run as CLI tool.
2022-11-13 23:00:59 +08:00
```javascript
// cmd.js
const {Application} = require('@nocobase/server');
const app = new Application({
database: {
dialect: 'sqlite',
storage: ':memory:',
}
});
app.cli.command('hello').action(async () => {
console.log("hello world")
});
app.runAsCLI()
```
2022-12-25 12:29:19 +08:00
Run in CLI:
2022-11-13 23:00:59 +08:00
```bash
$> node cmd.js hello
hello world
```
2022-12-25 12:29:19 +08:00
### Inject Plugin
2022-11-13 23:00:59 +08:00
2022-12-25 19:17:20 +08:00
NocoBase Application is designed as a highly extensible framework, plugins can be written and injected to the applicationto to extend its functionality. For example, the above-mentioned web service can be replaced with a plugin.
2022-11-13 23:00:59 +08:00
```javascript
const { Application, Plugin } = require('@nocobase/server');
2022-12-25 12:29:19 +08:00
// Write plugin by inheriting the Plugin class
2022-11-13 23:00:59 +08:00
class HelloWordPlugin extends Plugin {
load() {
this.app.use(async (ctx, next) => {
ctx.body = "Hello World";
})
}
}
const app = new Application({
database: {
dialect: 'sqlite',
storage: ':memory:',
}
});
2022-12-25 12:29:19 +08:00
// Inject plugin
2022-11-13 23:00:59 +08:00
app.plugin(HelloWordPlugin, { name: 'hello-world-plugin'} );
app.runAsCLI()
```
2022-12-25 12:29:19 +08:00
### More Examples
Please refer to the detailed guides of [plugin development ](./plugin.md ). Read more [examples ](https://github.com/nocobase/nocobase/blob/main/examples/index.md ) of the Application class.
## Life Cycle
2022-11-13 23:00:59 +08:00
2022-12-25 19:17:20 +08:00
Application has three life cycle stages depends on the running mode.
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
### Install
2022-11-13 23:00:59 +08:00
2022-12-25 19:17:20 +08:00
Use the `install` command in `cli` to invoke the installation. Generally, if needs to write new tables or data to the database before using the plugin, you have to do it during installation. Installation is also required when using NocoBase for the first time.
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
* Call the `load` method to load registered plugins.
* Trigger the `beforeInstall` event.
* Call the `db.sync` method to synchronize database.
* Call the `pm.install` method to execute the `install` methods of registered plugins.
* Write the version of `nocobase` .
* Trigger the `afterInstall` event.
* Call the `stop` method to end installation.
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
### Start
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
Use the `start` command in `cli` to start NocoBase Web service.
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
* Call the `load` method to load registered plugins.
* Call the `start` medthod:
* Trigger the `beforeStart` event
* Start port listening
* Trigger the `afterStart` event
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
### Upgrade
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
Use the `upgrade` command in `cli` to upgrade NocoBase Web service when needed.
2022-11-13 23:00:59 +08:00
2022-12-25 12:29:19 +08:00
* Call the `load` method to load registered plugins.
* Trigger the `beforeUpgrade` event.
* Call the `db.migrator.up` method to migrate database.
* Call the `db.sync` method to synchronize database.
* Call the `version.update` method to update the version of `nocobase` .
* Trigger the `afterUpgrade` event.
* Call the `stop` medthod to end upgrade.
2022-10-31 11:52:17 +08:00
2022-12-25 18:15:58 +08:00
## Constructor
2022-10-31 11:52:17 +08:00
### `constructor()`
2022-12-25 19:10:38 +08:00
Create an application instance.
2022-10-31 11:52:17 +08:00
2022-12-25 18:15:58 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `constructor(options: ApplicationOptions)`
2022-12-25 18:15:58 +08:00
**Parameters**
2022-10-31 11:52:17 +08:00
2022-12-25 18:15:58 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2022-12-25 18:15:58 +08:00
| `options.database` | `IDatabaseOptions` or `Database` | `{}` | Database configuration |
| `options.resourcer` | `ResourcerOptions` | `{}` | Resource route configuration |
| `options.logger` | `AppLoggerOptions` | `{}` | Log |
| `options.cors` | [`CorsOptions` ](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/koa__cors/index.d.ts#L24 ) | `{}` | Cross-domain configuration, refer to [@koa/cors ](https://npmjs.com/package/@koa/cors ) |
2022-12-25 19:17:20 +08:00
| `options.dataWrapping` | `boolean` | `true` | Whether or not to wrap the response data, `true` will wrap the usual `ctx.body` into a `{ data, meta }` structure |
2022-12-25 18:15:58 +08:00
| `options.registerActions` | `boolean` | `true` | Whether or not to register the default [actions ](# ) |
| `options.i18n` | `I18nOptions` | `{}` | Internationalization configuration, refer to [i18next ](https://www.i18next.com/overview/api ) |
| `options.plugins` | `PluginConfiguration[]` | `[]` | Configuration of the plugins enabled by default |
2022-10-31 11:52:17 +08:00
Type
```ts
interface ApplicationOptions {
}
```
2022-12-25 18:15:58 +08:00
## Instance Members
2022-10-31 11:52:17 +08:00
### `cli`
2022-12-25 18:15:58 +08:00
CLI tool instance, refer to the npm package [Commander ](https://www.npmjs.com/package/commander )。
2022-10-31 11:52:17 +08:00
### `db`
2022-12-25 18:15:58 +08:00
Database instance, refer to [Database ](/api/database ) for the related API.
2022-10-31 11:52:17 +08:00
### `resourcer`
2022-12-25 18:15:58 +08:00
Resource route management instance created automatically during app initialization, refer to [Resourcer ](/api/resourcer ) for the related API.
2022-10-31 11:52:17 +08:00
2022-12-25 18:15:58 +08:00
### `acl`
2022-10-31 11:52:17 +08:00
2022-12-25 18:15:58 +08:00
ACL instance, refer to [ACL ](/api/acl ) for the related API.
2022-11-13 23:00:59 +08:00
### `logger`
2022-12-25 18:15:58 +08:00
Winston instance, refer to [Winston ](https://github.com/winstonjs/winston#table-of-contents ) for the related API.
2022-11-13 23:00:59 +08:00
2022-10-31 11:52:17 +08:00
### `i18n`
2022-12-25 18:15:58 +08:00
I18next instance, refer to [I18next ](https://www.i18next.com/overview/api ) for the related API.
2022-10-31 11:52:17 +08:00
### `pm`
2022-12-25 18:15:58 +08:00
Plugin manager instance, refer to [PluginManager ](./plugin-manager ) for the related API.
2022-10-31 11:52:17 +08:00
### `version`
2022-12-25 18:15:58 +08:00
App version instance, refer to [ApplicationVersion ](./application-version ) for the related API.
2022-10-31 11:52:17 +08:00
### `middleware`
2022-12-25 18:15:58 +08:00
Built-in middleware includes:
2022-10-31 11:52:17 +08:00
2022-11-13 23:00:59 +08:00
- logger
2022-10-31 11:52:17 +08:00
- i18next
- bodyParser
- cors
- dataWrapping
2022-11-13 23:00:59 +08:00
- db2resource
2022-10-31 11:52:17 +08:00
- restApiMiddleware
### `context`
2022-12-25 18:15:58 +08:00
Context inherited from koa, accessible via `app.context` , is used to inject context-accessible content to each request. Refer to [Koa Context ](https://koajs.com/#app-context ).
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
NocoBase injects the following members to context by default, which can be used directly in the request handler function:
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
| Variable Name | Type | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- |
2022-12-25 19:10:38 +08:00
| `ctx.app` | `Application` | Application instance |
| `ctx.db` | `Database` | Database instance |
| `ctx.resourcer` | `Resourcer` | Resource route manager instance |
| `ctx.action` | `Action` | Resource action related object instance |
| `ctx.logger` | `Winston` | Log instance |
| `ctx.i18n` | `I18n` | Internationlization instance |
| `ctx.t` | `i18n.t` | Shortcut of internationalized translation function |
| `ctx.getBearerToken()` | `Function` | Get the bearer token in the header of request |
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
## Instance Methods
2022-10-31 11:52:17 +08:00
### `use()`
2022-12-25 19:10:38 +08:00
Register middleware, compatible with all [Koa plugins ](https://www.npmjs.com/search?q=koa ).
2022-10-31 11:52:17 +08:00
### `on()`
2022-12-25 19:10:38 +08:00
Subscribe to application-level events, mainly are related to life cycle. It is equivalent to `eventEmitter.on()` . Refer to [events ](#events ) for all subscribable events.
2022-10-31 11:52:17 +08:00
### `command()`
2022-12-25 19:10:38 +08:00
Customize command.
2022-10-31 11:52:17 +08:00
### `findCommand()`
2022-12-25 19:10:38 +08:00
Find defined command.
2022-10-31 11:52:17 +08:00
### `runAsCLI()`
2022-12-25 19:10:38 +08:00
Run as CLI.
2022-10-31 11:52:17 +08:00
### `load()`
2022-12-25 19:10:38 +08:00
Load application configuration.
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async load(): Promise<void>`
### `reload()`
2022-12-25 19:10:38 +08:00
Reload application configuration.
2022-10-31 11:52:17 +08:00
### `install()`
2022-12-25 19:10:38 +08:00
Initialize the installation of the application, meanwhile, install the plugin.
2022-10-31 11:52:17 +08:00
### `upgrade()`
2022-12-25 19:10:38 +08:00
Upgrade application, meanwhile, upgrade plugin.
2022-10-31 11:52:17 +08:00
### `start()`
2022-12-25 19:10:38 +08:00
Start application, listening will also be started if the listening port is configured, then the application can accept HTTP requests.
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `async start(options: StartOptions): Promise<void>`
2022-12-25 19:10:38 +08:00
**Parameters**
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2022-12-25 19:10:38 +08:00
| `options.listen?` | `ListenOptions` | `{}` | HTTP Listening parameters object |
| `options.listen.port?` | `number` | 13000 | Port |
| `options.listen.host?` | `string` | `'localhost'` | Domain name |
2022-10-31 11:52:17 +08:00
### `stop()`
2022-12-25 19:10:38 +08:00
Stop application. This method will deconnect database, close HTTP port, but will not delete data.
2022-10-31 11:52:17 +08:00
### `destroy()`
2022-12-25 19:10:38 +08:00
Delete application. This methos will delete the corresponding database of application.
2022-10-31 11:52:17 +08:00
2022-12-25 19:10:38 +08:00
## Events
2022-10-31 11:52:17 +08:00
### `'beforeLoad'` / `'afterLoad'`
### `'beforeInstall'` / `'afterInstall'`
### `'beforeUpgrade'` / `'afterUpgrade'`
### `'beforeStart'` / `'afterStart'`
### `'beforeStop'` / `'afterStop'`
### `'beforeDestroy'` / `'afterDestroy'`