Skip to content

Commit

Permalink
docs: update some data source tip
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Sep 15, 2024
1 parent f4086c0 commit bd50df7
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 105 deletions.
52 changes: 47 additions & 5 deletions site/docs/extensions/mikro.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class MainConfiguration {
```



## 基础使用

和其他 orm 框架类似,都是分为几个步骤:
Expand All @@ -112,6 +113,29 @@ export class MainConfiguration {



### 目录结构

一个基础的参考目录结构如下。

```
MyProject
├── src
│ ├── config
│ │ └── config.default.ts
│ ├── entity
│ │ ├── book.entity.ts
│ │ ├── index.ts
│ │ └── base.ts
│ ├── configuration.ts
│ └── service
├── .gitignore
├── package.json
├── README.md
└── tsconfig.json
```



### 定义 Entity

定义基础的 Entity。
Expand All @@ -137,9 +161,10 @@ export abstract class BaseEntity {
定义实际的 Entity,包含一对多,多对多等关系。

```typescript
// src/entity/book.entity.ts
import { Cascade, Collection, Entity, ManyToMany, ManyToOne, Property } from '@mikro-orm/core';
import { Author, BookTag, Publisher } from './index';
import { BaseEntity } from './BaseEntity';
import { BaseEntity } from './base';

@Entity()
export class Book extends BaseEntity {
Expand Down Expand Up @@ -194,10 +219,16 @@ export default (appInfo) => {
mikro: {
dataSource: {
default: {
entities: [Author, Book, BookTag, Publisher, BaseEntity],
dbName: join(__dirname, '../../test.sqlite'),
driver: SqliteDriver, // 这里使用了 sqlite 做示例
allowGlobalContext: true,
// 实体形式
entities: [Author, Book, BookTag, Publisher, BaseEntity],
// 支持如下的扫描形式,为了兼容我们可以同时进行.js和.ts匹配️
entities: [
'entity', // 指定目录
'**/entity/*.entity.{j,t}s', // 通配加后缀匹配
],
}
}
}
Expand All @@ -219,10 +250,16 @@ export default (appInfo) => {
mikro: {
dataSource: {
default: {
entities: [Author, Book, BookTag, Publisher, BaseEntity],
dbName: join(__dirname, '../../test.sqlite'),
type: 'sqlite', // 这里使用了 sqlite 做示例
allowGlobalContext: true,
// 实体形式
entities: [Author, Book, BookTag, Publisher, BaseEntity],
// 支持如下的扫描形式,为了兼容我们可以同时进行.js和.ts匹配️
entities: [
'entity', // 指定目录
'**/entity/*.entity.{j,t}s', // 通配加后缀匹配
],
}
}
}
Expand All @@ -233,7 +270,11 @@ export default (appInfo) => {
</TabItem>
</Tabs>

如需以目录扫描形式关联,请参考 [数据源管理](../data_source)
:::tip

mikro 的 `entities` 字段配置已经经过框架处理,该字段配置请不要参考原始文档。

:::



Expand All @@ -249,7 +290,8 @@ export default (appInfo) => {
:::

```typescript
import { Book } from './entity';
// src/service/book.service.ts
import { Book } from './entity/book.entity';
import { Provide } from '@midwayjs/core';
import { InjectEntityManager, InjectRepository } from '@midwayjs/mikro';
import { QueryOrder } from '@mikro-orm/core';
Expand Down
18 changes: 5 additions & 13 deletions site/docs/extensions/orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,16 @@ export default {
username: '*******',
password: '*******',
database: undefined,
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true,注意会丢数据
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true,注意会丢数据
logging: false,

// 配置实体模型
entities: [Photo],

// 支持如下的扫描形式,为了兼容我们可以同时进行.js和.ts匹配,⬇️
// 支持如下的扫描形式,为了兼容我们可以同时进行.js和.ts匹配
entities: [
'entity', // 特定目录
'**/abc/**', // 仅获取包含 abc 字符的目录下的文件
'abc/**/*.{j,t}s', // 特定目录 + 通配
'abc/*.entity.{j,t}s', // 匹配后缀
'entity', // 特定目录
'**/*.entity.{j,t}s', // 通配加后缀匹配
'**/*.{j,t}s', // 后缀匹配
]
}
}
Expand All @@ -453,14 +449,10 @@ export default {
```
:::tip

如果使用的数据库已经有表结构同步的功能,比如云数据库,最好不要开启。如果一定要使用,synchronize 配置最好仅在开发阶段,或者第一次使用,避免造成一致性问题。

- 1. 如果使用的数据库已经有表结构同步的功能,比如云数据库,最好不要开启。如果一定要使用,synchronize 配置最好仅在开发阶段,或者第一次使用,避免造成一致性问题。
- 2. `entities` 字段配置已经经过框架处理,该字段配置请不要参考原始文档。
:::

如需以目录扫描形式关联,请参考 [数据源管理](../data_source)




`type` 字段你可以使用其他的数据库类型,包括`mysql`, `mariadb`, `postgres`, `cockroachdb`, `sqlite`, `mssql`, `oracle`, `cordova`, `nativescript`, `react-native`, `expo`, or `mongodb`

Expand Down
58 changes: 43 additions & 15 deletions site/docs/extensions/sequelize.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ npm install mongodb --save

下面的文档,我们将以 `mysql2` 作为示例。


### Directory structure

一个基础的参考目录结构如下。


```
MyProject
├── src
│ ├── config
│ │ └── config.default.ts
│ ├── entity
│ │ └── person.entity.ts
│ ├── configuration.ts
│ └── service
├── .gitignore
├── package.json
├── README.md
└── tsconfig.json
```


## 启用组件

`src/configuration.ts` 文件中启用组件。
Expand Down Expand Up @@ -113,10 +135,10 @@ export class MainConfiguration implements ILifeCycle {

我们通过模型和数据库关联,在应用中的模型就是数据库表,在 Sequelize 中,模型是和实体绑定的,每一个实体(Entity) 文件,即是 Model,也是实体(Entity)。

在示例中,需要一个实体,我们这里拿 `person` 举例。新建 entity 目录,在其中添加实体文件 `person.ts` ,一个简单的实体如下。
在示例中,需要一个实体,我们这里拿 `person` 举例。新建 entity 目录,在其中添加实体文件 `person.entity.ts` ,一个简单的实体如下。

```typescript
// src/entity/person.ts
// src/entity/person.entity.ts
import { Table, Model, Column, HasMany } from 'sequelize-typescript';

@Table
Expand Down Expand Up @@ -255,7 +277,7 @@ export class Person extends Model {
```typescript
// src/config/config.default.ts

import { Person } from '../entity/person';
import { Person } from '../entity/person.entity';

export default {
// ...
Expand All @@ -272,9 +294,17 @@ export default {
dialect: 'mysql',
define: { charset: 'utf8' },
timezone: '+08:00',
entities: [Person],
// 本地的时候,可以通过 sync: true 直接 createTable
sync: false,

// 实体形式
entities: [Person],

// 支持如下的扫描形式,为了兼容我们可以同时进行.js和.ts匹配️
entities: [
'entity', // 指定目录
'**/entity/*.entity.{j,t}s', // 通配加后缀匹配
],
},

// 第二个数据源
Expand All @@ -286,8 +316,6 @@ export default {
};
```

如需以目录扫描形式关联,请参考 [数据源管理](../data_source)



## 模型关联
Expand Down Expand Up @@ -387,7 +415,7 @@ books: Array<Book & {BookAuthor: BookAuthor}>;

```typescript
import { Table, Column, Model, BelongsTo, ForeignKey } from 'sequelize-typescript';
import { User } from './User';
import { User } from './user.entity';

@Table
export class Photo extends Model {
Expand Down Expand Up @@ -427,7 +455,7 @@ ReferenceError: Cannot access 'Photo' before initialization

```typescript
import { Table, Column, Model, BelongsTo, ForeignKey } from 'sequelize-typescript';
import { User } from './User';
import { User } from './user.entity';

@Table
export class Photo extends Model {
Expand All @@ -451,7 +479,7 @@ export class Photo extends Model {

```typescript
import { Provide } from '@midwayjs/core';
import { Person } from '../entity/person';
import { Person } from '../entity/person.entity';

@Provide()
export class PersonService {
Expand All @@ -466,7 +494,7 @@ export class PersonService {

```typescript
import { Provide } from '@midwayjs/core';
import { Person } from '../entity/person';
import { Person } from '../entity/person.entity';

@Provide()
export class PersonService {
Expand Down Expand Up @@ -499,7 +527,7 @@ Repository 模式可以将查找、创建等静态操作从模型定义中分离
```typescript
// src/config/config.default.ts

import { Person } from '../entity/person';
import { Person } from '../entity/person.entity';

export default {
// ...
Expand Down Expand Up @@ -529,8 +557,8 @@ export default {
```typescript
import { Controller, Get } from '@midwayjs/core';
import { InjectRepository } from '@midwayjs/sequelize';
import { Photo } from '../entity/photo';
import { User } from '../entity/user';
import { Photo } from '../entity/photo.entity';
import { User } from '../entity/user.entity';
import { Op } from 'sequelize';
import { Repository } from 'sequelize-typescript';

Expand Down Expand Up @@ -585,8 +613,8 @@ export class HomeController {
```typescript
import { Controller } from '@midwayjs/core';
import { InjectRepository } from '@midwayjs/sequelize';
import { Photo } from '../entity/photo';
import { User } from '../entity/user';
import { Photo } from '../entity/photo.entity';
import { User } from '../entity/user.entity';
import { Repository } from 'sequelize-typescript';

@Controller('/')
Expand Down
Loading

0 comments on commit bd50df7

Please sign in to comment.