Skip to content

Commit

Permalink
docs: elaborate on midway leoric component usage (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Nov 20, 2024
1 parent ffc258b commit 2e31e11
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
1 change: 0 additions & 1 deletion docs/assets/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 64 additions & 0 deletions docs/setup/midway.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 () => {
Expand All @@ -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';
Expand Down Expand Up @@ -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();
}
}
```
62 changes: 62 additions & 0 deletions docs/zh/setup/midway.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ title: 在 Midway 应用中使用

## 使用指南

首先在 src/configuration.ts 启用 leoric 组件:

```ts
// src/configuration.ts
import { Configuration, ILifeCycle } from '@midwayjs/core';
Expand All @@ -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 () => {
Expand All @@ -45,6 +49,8 @@ export default () => {
}
```

然后就可以在 controller 或者 service 中按需使用数据模型,使用 `@InjectModel()` 装饰器注入对应的数据模型即可:

```ts
// src/controller/user.ts
import { Controller } from '@midwayjs/core';
Expand Down Expand Up @@ -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();
}
}
```

0 comments on commit 2e31e11

Please sign in to comment.