Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Apr 2, 2024
1 parent 52ff431 commit a116170
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
18 changes: 17 additions & 1 deletion plugin/ajv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,23 @@ exports.teggDal = {

## standalone 模式

默认内置。
### Install

```shell
# tegg 注解
npm i --save @eggjs/tegg
# tegg ajv 插件
npm i --save @eggjs/tegg-ajv-plugin
```

### Prepare

```json
// tsconfig.json
{
"extends": "@eggjs/tsconfig"
}
```

## Usage

Expand Down
28 changes: 23 additions & 5 deletions plugin/dal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## egg 模式

### Install

```shell
# tegg 注解
npm i --save @eggjs/tegg
Expand All @@ -15,6 +16,7 @@ npm i --save @eggjs/tegg-dal-plugin
```

### Prepare

```json
// tsconfig.json
{
Expand All @@ -40,6 +42,7 @@ exports.teggDal = {
## standalone 模式

### Install

```shell
# tegg 注解
npm i --save @eggjs/tegg
Expand All @@ -48,6 +51,7 @@ npm i --save @eggjs/tegg-dal-plugin
```

### Prepare

```json
// tsconfig.json
{
Expand All @@ -58,6 +62,7 @@ npm i --save @eggjs/tegg-dal-plugin
## Usage

### module.yml

通过 module.yml 来配置 module 中的 mysql 数据源。

```yaml
Expand All @@ -73,6 +78,7 @@ dataSource:
```
### Table
`TableModel` 定义一个表结构,包括表配置、列、索引。

```ts
Expand Down Expand Up @@ -108,6 +114,7 @@ export class Foo {
详细参数定义如下,具体参数值可以参考 https://dev.mysql.com/doc/refman/8.0/en/create-table.html

建表参数,使用方式为 `@Table(parmas?: TableParams)`

```ts
export interface TableParams {
// 数据库表名
Expand All @@ -134,6 +141,7 @@ export interface TableParams {
```

建索引参数,使用方式为 `@Index(parmas?: IndexParams)`

```ts
export interface IndexParams {
// 索引的列
Expand All @@ -153,6 +161,7 @@ export interface IndexParams {
```

建列参数,使用方式为 `@Column(type: ColumnTypeParams, parmas?: ColumnParams)`

```ts
export interface ColumnParams {
// 列名,默认转换规则 userName 至 user_name
Expand All @@ -174,6 +183,7 @@ export interface ColumnParams {
```

支持的类型

```ts
export enum ColumnType {
// Numeric
Expand Down Expand Up @@ -225,6 +235,7 @@ export enum ColumnType {
支持的类型参数,详细可参考 https://dev.mysql.com/doc/refman/8.0/en/data-types.html

如果 mysql 类型和 ts 类型对应关系不确定可直接使用 `ColumnTsType` 类型,如

```ts
import { Table, Index, Column, ColumnType, IndexType, ColumnTsType } from '@eggjs/tegg/dal';
Expand Down Expand Up @@ -506,7 +517,8 @@ export interface GeometryCollectionParams {
```

### 目录结构
运行 `egg-bin dal gen` 即可生成 `dal` 相关目录,包括 dao、extension、structure

运行 `egg-bin dal gen` 即可生成 `dal` 相关目录,包括 dao、extension、structure

```plain
dal
Expand All @@ -519,7 +531,7 @@ dal
└── structure
├── Foo.json
└── Foo.sql
```
```

- dao: 表访问类,生成的 BaseDAO 请勿修改,其中包含了根据表结构生成的基础访问方法,如 insert/update/delete 以及根据索引信息生成的 find 方法
- extension: 扩展文件,如果需要自定义 sql,需要在 extension 文件中定义
Expand All @@ -543,8 +555,9 @@ export class FooRepository {
}
```

#### 自定义 Sql
1. 在 extension 中定义自定义 Sql
#### 自定义 SQL

1. 在 extension 中定义自定义 SQL

```ts
// dal/extension/FooExtension.ts
Expand All @@ -559,6 +572,7 @@ export default {
```

2. 在 dao 中定义自定义方法

```ts
import { SingletonProto, AccessLevel } from '@eggjs/tegg';
import { BaseFooDAO } from './base/BaseFooDAO';
Expand All @@ -577,6 +591,7 @@ export default class FooDAO extends BaseFooDAO {
```

支持的自定义 filter

```
- toPoint
- toLine
Expand All @@ -589,6 +604,7 @@ export default class FooDAO extends BaseFooDAO {
```
支持自定义 block 来简化 sql, 如内置的 allColumns
```ts
export default {
findByName: {
Expand All @@ -599,7 +615,8 @@ export default {
```

### DataSource
DataSource 仅能在 DAO 中使用,可以将 mysql 返回的数据反序列化为类。支持的方法有

DataSource 仅能在 DAO 中使用,可以将 MySQL 返回的数据反序列化为类。支持的方法有

```ts
export interface DataSource<T> {
Expand Down Expand Up @@ -634,6 +651,7 @@ dataSource:
```
可以通过以下 SQL 来查看数据库时区
```sql
SELECT @@GLOBAL.time_zone;
```
2 changes: 1 addition & 1 deletion standalone/standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@eggjs/tegg-background-task": "^3.35.1",
"@eggjs/tegg-common-util": "^3.35.1",
"@eggjs/tegg-dal-plugin": "^3.35.1",
"@eggjs/tegg-ajv-plugin": "^3.35.1",
"@eggjs/tegg-dynamic-inject-runtime": "^3.35.1",
"@eggjs/tegg-lifecycle": "^3.35.1",
"@eggjs/tegg-loader": "^3.35.1",
Expand All @@ -55,6 +54,7 @@
"access": "public"
},
"devDependencies": {
"@eggjs/tegg-ajv-plugin": "^3.35.1",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
Expand Down
12 changes: 10 additions & 2 deletions standalone/standalone/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ describe('standalone/standalone/test/index.test.ts', () => {
describe('ajv runner', () => {
it('should throw AjvInvalidParamError', async () => {
await assert.rejects(async () => {
await main<string>(path.join(__dirname, './fixtures/ajv-module'));
await main<string>(path.join(__dirname, './fixtures/ajv-module'), {
dependencies: [
path.dirname(require.resolve('@eggjs/tegg-ajv-plugin/package.json')),
],
});
}, (err: any) => {
assert.equal(err.name, 'AjvInvalidParamError');
assert.equal(err.message, 'Validation Failed');
Expand All @@ -275,7 +279,11 @@ describe('standalone/standalone/test/index.test.ts', () => {
});

it('should pass', async () => {
const result = await main<string>(path.join(__dirname, './fixtures/ajv-module-pass'));
const result = await main<string>(path.join(__dirname, './fixtures/ajv-module-pass'), {
dependencies: [
path.dirname(require.resolve('@eggjs/tegg-ajv-plugin/package.json')),
],
});
assert.equal(result, '{"body":{"fullname":"mock fullname","skipDependencies":true,"registryName":"ok"}}');
});
});
Expand Down

0 comments on commit a116170

Please sign in to comment.