From 2e31e11cab53db6a843330d4f6516acb3e40da5d Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:07:19 +0800 Subject: [PATCH] docs: elaborate on midway leoric component usage (#428) --- docs/assets/css/style.scss | 1 - docs/setup/midway.md | 64 ++++++++++++++++++++++++++++++++++++++ docs/zh/setup/midway.md | 62 ++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) diff --git a/docs/assets/css/style.scss b/docs/assets/css/style.scss index 3192daca..18d6eab5 100644 --- a/docs/assets/css/style.scss +++ b/docs/assets/css/style.scss @@ -268,7 +268,6 @@ body { right: calc(50% + (var(--container-width) / 2) + var(--padding-base)); top: 26px; bottom: 24px; - width: calc(50% - (var(--container-width) / 2) + var(--padding-base)); background: none; border-bottom: none; overflow-y: auto; diff --git a/docs/setup/midway.md b/docs/setup/midway.md index 7208b36c..872bd0de 100644 --- a/docs/setup/midway.md +++ b/docs/setup/midway.md @@ -11,6 +11,8 @@ title: Setup with Midway ## Usage +Firstly, activate the leoric component in src/configuration.ts + ```ts // src/configuration.ts import { Configuration, ILifeCycle } from '@midwayjs/core'; @@ -25,6 +27,8 @@ import * as leoric from '@midwayjs/leoric'; export class ContainerLifeCycle implements ILifeCycle {} ``` +Secondly, supply database configurations in src/config/config.default.ts + ```ts // src/config/config.default.ts export default () => { @@ -45,6 +49,8 @@ export default () => { } ``` +Lastly, models from the configured directory should be available with `@InjectModel()`: + ```ts // src/controller/user.ts import { Controller } from '@midwayjs/core'; @@ -104,3 +110,61 @@ export class UserService { ``` If multiple datasources were configured, pass the name of the data source to `@InjectDataSource(name)` for the corresponding one. + +## Multiple Data Sources + +The way to configure multiple data sources in midway with leoric should not be very different from in midway with other ORM components. Here is one example of utilizing two sqlite databases in midway. + +```ts +// src/config/config.default.ts +export default () => { + return { + leoric: { + dataSource: { + main: { + dialect: 'sqlite', + database: path.join(__dirname, '../../', 'database.sqlite'), + models: [ + 'models/*{.ts,.js}' + ] + }, + backup: { + dialect: 'sqlite', + database: path.join(__dirname, '../../', 'backup.sqlite'), + models: [ + 'backup/models/*{.ts,.js}' + ] + }, + }, + defaultDataSourceName: 'main', + }, + }; +} +``` + +By specifing the `dataSource` parameter, the related models can be injected accordingly. If `dataSource` isn't specified, the one set with `defaultDataSourceName` will be used. + +For example, the backup models or the backup data source itself can be injected with `@InjectModel(BoneLike, 'backup')` or `@InjectDataSource('backup')`: + + +```ts +// src/controller/user.ts +import { Controller, Get } from '@midwayjs/core'; +import Realm, { InjectDataSource, InjectModel } from '@midwayjs/leoric'; +import User from '../model/user'; + +@Controller('/api/users') +export class UserController { + @InjectModel(User, 'backup') + User: typeof User; + + @InjectDataSource('backup') + backupRealm: Realm + + @Get('') + async index() { + const users = await this.User.find(); + return users.toJSON(); + } +} +``` \ No newline at end of file diff --git a/docs/zh/setup/midway.md b/docs/zh/setup/midway.md index bfc137bd..7b5a6120 100644 --- a/docs/zh/setup/midway.md +++ b/docs/zh/setup/midway.md @@ -11,6 +11,8 @@ title: 在 Midway 应用中使用 ## 使用指南 +首先在 src/configuration.ts 启用 leoric 组件: + ```ts // src/configuration.ts import { Configuration, ILifeCycle } from '@midwayjs/core'; @@ -25,6 +27,8 @@ import * as leoric from '@midwayjs/leoric'; export class ContainerLifeCycle implements ILifeCycle {} ``` +然后在配置文件(例如 src/config/config.default.ts)增加对应的数据源配置,下面这个例子配置了一个默认的数据源,使用 sqlite 数据库,在所有目录的 model 子目录中查找并加载数据模型定义: + ```ts // src/config/config.default.ts export default () => { @@ -45,6 +49,8 @@ export default () => { } ``` +然后就可以在 controller 或者 service 中按需使用数据模型,使用 `@InjectModel()` 装饰器注入对应的数据模型即可: + ```ts // src/controller/user.ts import { Controller } from '@midwayjs/core'; @@ -104,3 +110,59 @@ export class UserService { ``` 如果配置有多个实例,可以给装饰器传数据源名称来获得对应的数据源。 + +## 多数据源配置 + +midway 给数据模型组件提供基础的多数据源配置规则,无论是使用 leoric 组件还是其他 ORM 库,使用方式大致是相同的,下面仍然以 leoric 组件为例: + +```ts +// src/config/config.default.ts +export default () => { + return { + leoric: { + dataSource: { + main: { + dialect: 'sqlite', + database: path.join(__dirname, '../../', 'database.sqlite'), + models: [ + 'models/*{.ts,.js}' + ] + }, + backup: { + dialect: 'sqlite', + database: path.join(__dirname, '../../', 'backup.sqlite'), + models: [ + 'backup/models/*{.ts,.js}' + ] + }, + }, + // 如果想要在 @InjectModel() 时省略数据源名称,那就需要在这里指定缺省值 + defaultDataSourceName: 'main', + }, + }; +} +``` + +然后在使用的时候需要传入对应的数据源名称,如果省略,则使用 `defaultDataSourceName` 配置项所指定的数据源: + +```ts +// src/controller/user.ts +import { Controller, Get } from '@midwayjs/core'; +import Realm, { InjectDataSource, InjectModel } from '@midwayjs/leoric'; +import User from '../model/user'; + +@Controller('/api/users') +export class UserController { + @InjectModel(User, 'backup') + User: typeof User; + + @InjectDataSource('backup') + backupRealm: Realm + + @Get('') + async index() { + const users = await this.User.find(); + return users.toJSON(); + } +} +``` \ No newline at end of file