Skip to content

Commit

Permalink
docs: update sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 4, 2024
1 parent ee6f969 commit 17b8b23
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 38 deletions.
93 changes: 83 additions & 10 deletions site/docs/extensions/sequelize.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- 3.1 修改为数据源的形式 `sequelize.dataSource`
- 3.2 将实体模型在数据源的 `entities` 字段中声明



## 安装依赖

```bash
Expand Down Expand Up @@ -118,13 +120,13 @@ export class MainConfiguration implements ILifeCycle {
import { Table, Model, Column, HasMany } from 'sequelize-typescript';

@Table
class Hobby extends Model {
export class Hobby extends Model {
@Column
name: string;
}

@Table
class Person extends Model {
export class Person extends Model {
@Column
name: string;

Expand All @@ -145,7 +147,7 @@ class Person extends Model {
timestamps: true,
...
})
class Person extends Model {}
export class Person extends Model {}
```


Expand All @@ -162,7 +164,7 @@ class Person extends Model {}
import { Table, Model, PrimaryKey } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@PrimaryKey
name: string;
}
Expand All @@ -178,7 +180,7 @@ class Person extends Model {
import { Table, Model, CreatedAt, UpdatedAt, DeletedAt } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@CreatedAt
creationDate: Date;

Expand All @@ -204,7 +206,7 @@ class Person extends Model {
import { Table, Model, Column } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@Column
name: string;
}
Expand All @@ -216,7 +218,7 @@ class Person extends Model {
import { Table, Column, DataType } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@Column(DataType.TEXT)
name: string;
}
Expand All @@ -230,7 +232,7 @@ class Person extends Model {
import { Table, Model, Column, DataType } from 'sequelize-typescript'

@Table
class Person extends Model {
export class Person extends Model {
@Column({
type: DataType.FLOAT,
comment: 'Some value',
Expand Down Expand Up @@ -292,6 +294,12 @@ export default {

可以通过 `HasMany``@HasOne``@BelongsTo``@BelongsToMany``@ForeignKey` 装饰器在模型中直接描述关系。

:::tip

你不需要在数据库中创建外键也可以使用这个功能。

:::

### 一对多

```typescript
Expand Down Expand Up @@ -407,6 +415,32 @@ export class User extends Model {



### 模型循环依赖

如果你使用了 `@BelongsTo` 装饰器,很容易触发一个模型循环依赖的错误,比如:

```
ReferenceError: Cannot access 'Photo' before initialization
```

你可以将类型使用 `ReturnType` 包裹起来。

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

@Table
export class Photo extends Model {
// ...
@BelongsTo(() => User)
user: ReturnType<() => User>;
}
```





## 静态操作方法

如果是单个数据源,可以使用下面的静态方法。
Expand Down Expand Up @@ -671,9 +705,48 @@ export class MainConfiguration {



### 2、生成实体列

请参考社区提供的模块,如 [sequelize-typescript-generator](https://github.com/spinlud/sequelize-typescript-generator)



### 3、Raw Query

如果遇到比较复杂的,可以使用 [raw query 方法](https://sequelize.org/v5/manual/raw-queries.html)



### 4、TS2612 错误

如果你的模型列报了 TS2612 错误,比如:

```
src/entity/AesTenantConfigInfo.ts:29:6 - error TS2612: Property 'id' will overwrite the base property in 'Model<AesTenantConfigInfoAttributes, AesTenantConfigInfoAttributes>'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
29 id?: number;
~~
```

可以将其赋一个空值。

```typescript
import { Table, Column } from 'sequelize-typescript';

@Table
export class User extends Model {
@Column({
primaryKey: true,
autoIncrement: true,
type: DataType.BIGINT,
})
id?: number = undefined;
}
```



## 其他

- 上面的文档,翻译自 sequelize-typescript,更多 API ,请参考 [英文文档](<(https://github.com/sequelize/sequelize-typescrip)>)
- 一些 [案例](https://github.com/ddzyan/midway-practice)
- 如果遇到比较复杂的,可以使用 [raw query 方法](https://sequelize.org/v5/manual/raw-queries.html)]
- 框架不直接提供实体列生成工具,请参考社区提供的模块,如 [sequelize-typescript-generator](https://github.com/spinlud/sequelize-typescript-generator)
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,28 @@ Multiple agent configurations

// Proxy configuration type
export default {
default: {
// Some multiplexed values for each policy will be merged with the following policies.
},
strategy: {
gw: {
// https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png
match: /\/tfs \//,
host: 'https://gw.alicdn.com',
},
g: {
// https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js
match: /\/bdimg\/(.*)$ /,
target: 'https://sm.bdimg.com/$1',
httpProxy: {
default: {
// Some multiplexed values for each policy will be merged with the following policies.
},
httpBin: {
// https://httpbin.org/
match: /\/httpbin\/(.*)$ /,
target: 'https://httpbin.org/$1',
strategy: {
gw: {
// https://gw.alicdn.com/tfs/TB1.1EzoBBh1e4jSZFhXXcC9VXa-48-48.png
match: /\/tfs \//,
host: 'https://gw.alicdn.com',
},
g: {
// https://g.alicdn.com/mtb/lib-mtop/2.6.1/mtop.js
match: /\/bdimg\/(.*)$ /,
target: 'https://sm.bdimg.com/$1',
},
httpBin: {
// https://httpbin.org/
match: /\/httpbin\/(.*)$ /,
target: 'https://httpbin.org/$1',
},
},
},
}
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ In the example, you need an entity. Let's take `person` as an example. Create an
import { Table, Model, Column, HasMany } from 'sequelize-typescript';

@Table
class Hobby extends Model {
export class Hobby extends Model {
@Column
name: string;
}

@Table
class Person extends Model {
export class Person extends Model {
@Column
name: string;

Expand All @@ -145,7 +145,7 @@ The `@Table` decorator can be used without passing any parameters. For more info
timestamps: true
...
})
class Person extends Model {}
export class Person extends Model {}
```


Expand All @@ -162,7 +162,7 @@ For example:
import { Table, Model, PrimaryKey } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@PrimaryKey
name: string;
}
Expand All @@ -178,7 +178,7 @@ for example:
import { Table, Model, CreatedAt, UpdatedAt, DeletedAt } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@CreatedAt
creationDate: Date;

Expand All @@ -204,7 +204,7 @@ The @Column decorator is used to label normal columns and can be used without pa
import { Table, Model, Column } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@Column
name: string;
}
Expand All @@ -216,7 +216,7 @@ Or specify the column type.
import { Table, Column, DataType } from 'sequelize-typescript';

@Table
class Person extends Model {
export class Person extends Model {
@Column(DataType.TEXT)
name: string;
}
Expand All @@ -230,7 +230,7 @@ For example:
import { Table, Model, Column, DataType } from 'sequelize-typescript'

@Table
class Person extends Model {
export class Person extends Model {
@Column({
type: DataType.FLOAT
comment: 'Some value',
Expand Down Expand Up @@ -403,6 +403,36 @@ export class User extends Model {
}
```



### Model Cyclic Dependency

If you use the `@BelongsTo` decorator, it is easy to trigger a model circular dependency error, such as:

```
ReferenceError: Cannot access 'Photo' before initialization
```

You can wrap types with `ReturnType`.

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

@Table
export class Photo extends Model {
// ...
@BelongsTo(() => User)
user: ReturnType<() => User>;
}
```







## Static operation method

If it is a single data source, you can use the following static method.
Expand Down Expand Up @@ -662,15 +692,27 @@ export class MainConfiguration {

## Common problem



### 1. Dialect needs to be explicitly supplied as of v4.0.0

The reason is that the data source in the configuration does not specify the `dialect` field, which confirms the structure, format of the data source and the result of the configuration merging.



### 2. Generate entity columns

Please refer to the modules provided by the community, such as [sequelize-typescript-generator](https://github.com/spinlud/sequelize-typescript-generator)



### 3. Raw Query

If you encounter something more complex, you can use the [raw query method](https://sequelize.org/v5/manual/raw-queries.html)



## Other

- The above document is translated from sequelize-typescript. For more API, please refer to the [English document](<(https://github.com/sequelize/sequelize-typescrip)>).
- Some [cases](https://github.com/ddzyan/midway-practice)
- If you encounter complex problems, you can use the [raw query method](https://sequelize.org/v5/manual/raw-queries.html)
- The framework does not directly provide entity column generation tools. Please refer to the modules provided by the community, such as [sequelize-typescript-generator](https://github.com/spinlud/sequelize-typescript-generator)

0 comments on commit 17b8b23

Please sign in to comment.