Merge branch 'main' into feat/gantt-block

This commit is contained in:
katherinehhh 2023-01-28 10:36:30 +08:00
commit 0ca3e79099
9 changed files with 340 additions and 334 deletions

View File

@ -1,6 +1,6 @@
# Filter Operators
用于 Repository 的 find、findOne、findAndCount、count 等 API 的 filter 参数中:
Used in the filter parameters of the `find`, `findOne`, `findAndCount`, `count`, etc. APIs of repository:
```ts
const repository = db.getRepository('books');
@ -8,47 +8,47 @@ const repository = db.getRepository('books');
repository.find({
filter: {
title: {
$eq: '春秋',
$eq: 'Spring and Autumn',
}
}
});
```
为了支持 JSON 化NocoBase 中将查询运算符以 $ 为前缀的字符串标识。
To support JSON, NocoBase identifies query operators as a string prefixed with $.
另外NocoBase 也提供了扩展运算符的 API详见 [`db.registerOperators()`](../database#registeroperators)。
Moreover, NocoBase provides API to extend operators. Refer to [`db.registerOperators()`](../database#registeroperators).
## 通用运算符
## General Operators
### `$eq`
判断字段值是否相等于指定值。相当于 SQL 的 `=`
Check if the field value is equal to the specified value. Equivalent to `=` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$eq: '春秋',
$eq: 'Spring and Autumn',
}
}
});
```
等同于 `title: '春秋'`
Equal to `title: 'Spring and Autumn'`
### `$ne`
判断字段值是否不等于指定值。相当于 SQL 的 `!=`
Check if the field value is not equal to the specified value. Equivalent to `!=` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$ne: '春秋',
$ne: 'Spring and Autumn',
}
}
});
@ -56,9 +56,9 @@ repository.find({
### `$is`
判断字段值是否为指定值。相当于 SQL 的 `IS`
Check if the field value is the specified value. Equivalent to `IS` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -72,9 +72,9 @@ repository.find({
### `$not`
判断字段值是否不为指定值。相当于 SQL 的 `IS NOT`
Check if the field value is not the specified value. Equivalent to `IS NOT` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -88,9 +88,10 @@ repository.find({
### `$col`
判断字段值是否等于另一个字段的值。相当于 SQL 的 `=`
Check if the field value is equal to the value of another field. Equivalent to `=` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -104,15 +105,15 @@ repository.find({
### `$in`
判断字段值是否在指定数组中。相当于 SQL 的 `IN`
Check if the field value is in the specified array. Equivalent to `IN` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$in: ['春秋', '战国'],
$in: ['Spring and Autumn', 'Warring States'],
}
}
});
@ -120,15 +121,15 @@ repository.find({
### `$notIn`
判断字段值是否不在指定数组中。相当于 SQL 的 `NOT IN`
Check if the field value is not in the specified array. Equivalent to `NOT IN` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notIn: ['春秋', '战国'],
$notIn: ['Spring and Autumn', 'Warring States'],
}
}
});
@ -136,9 +137,9 @@ repository.find({
### `$empty`
判断一般字段是否为空,如果是字符串字段,判断是否为空串,如果是数组字段,判断是否为空数组。
Check if the general field is empty. For string field, check if it is an empty string; for array field, check if it is an empty array.
**示例**
**Example**
```ts
repository.find({
@ -152,9 +153,9 @@ repository.find({
### `$notEmpty`
判断一般字段是否不为空,如果是字符串字段,判断是否不为空串,如果是数组字段,判断是否不为空数组。
Check if the general field is not empty. For string field, check if it is not an empty string; for array field, check if it is not an empty array.
**示例**
**Example**
```ts
repository.find({
@ -166,19 +167,19 @@ repository.find({
});
```
## 逻辑运算符
## Logical Operators
### `$and`
逻辑 AND。相当于 SQL 的 `AND`
Logical AND. Equivalent to `AND` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
$and: [
{ title: '诗经' },
{ title: 'Book of Songs' },
{ isbn: '1234567890' },
]
}
@ -187,30 +188,30 @@ repository.find({
### `$or`
逻辑 OR。相当于 SQL 的 `OR`
Logical OR. Equivalent to `OR` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
$or: [
{ title: '诗经' },
{ title: 'Book of Songs' },
{ publishedAt: { $lt: '0000-00-00T00:00:00Z' } },
]
}
});
```
## 布尔类型字段运算符
## Boolean Field Operators
用于布尔类型字段 `type: 'boolean'`
For boolean fields: `type: 'boolean'`
### `$isFalsy`
判断布尔类型字段值是否为假。布尔字段值为 `false``0``NULL` 的情况都会被判断为 `$isFalsy: true`
Check if a Boolean field value is false. Boolean field values of `false`, `0` and `NULL` are all judged to be `$isFalsy: true`.
**示例**
**Example**
```ts
repository.find({
@ -224,9 +225,9 @@ repository.find({
### `$isTruly`
判断布尔类型字段值是否为真。布尔字段值为 `true``1` 的情况都会被判断为 `$isTruly: true`
Check if a Boolean field value is true. Boolean field values of `true` and `1` are all judged to be `$isTruly: true`.
**示例**
**Example**
```ts
repository.find({
@ -238,9 +239,9 @@ repository.find({
})
```
## 数字类型字段运算符
## Numeric Type Field Operators
用于数字类型字段,包括:
For numeric type fields, including:
- `type: 'integer'`
- `type: 'float'`
@ -250,9 +251,9 @@ repository.find({
### `$gt`
判断字段值是否大于指定值。相当于 SQL 的 `>`
Check if the field value is greater than the specified value. Equivalent to `>` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -266,9 +267,9 @@ repository.find({
### `$gte`
判断字段值是否大于等于指定值。相当于 SQL 的 `>=`
Check if the field value is equal to or greater than the specified value. Equivalent to `>=` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -282,9 +283,9 @@ repository.find({
### `$lt`
判断字段值是否小于指定值。相当于 SQL 的 `<`
Check if the field value is less than the specified value. Equivalent to `<` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -298,9 +299,9 @@ repository.find({
### `$lte`
判断字段值是否小于等于指定值。相当于 SQL 的 `<=`
Check if the field value is equal to or less than the specified value. Equivalent to `<=` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -314,9 +315,9 @@ repository.find({
### `$between`
判断字段值是否在指定的两个值之间。相当于 SQL 的 `BETWEEN`
Check if the field value is between the specified two values. Equivalent to `BETWEEN` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -330,9 +331,9 @@ repository.find({
### `$notBetween`
判断字段值是否不在指定的两个值之间。相当于 SQL 的 `NOT BETWEEN`
Check if the field value is not between the specified two values. Equivalent to `NOT BETWEEN` in SQL.
**示例**
**Example**
```ts
repository.find({
@ -344,21 +345,21 @@ repository.find({
});
```
## 字符串类型字段运算符
## String Type Field Operators
用于字符串类型字段,包括 `string`
For string type fields, including `string`.
### `$includes`
判断字符串字段是否包含指定子串。
Check if the string field contains the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$includes: '三字经',
$includes: 'Three Character Classic',
}
}
})
@ -366,15 +367,15 @@ repository.find({
### `$notIncludes`
判断字符串字段是否不包含指定子串。
Check if the string field does not contain the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notIncludes: '三字经',
$notIncludes: 'Three Character Classic',
}
}
})
@ -382,15 +383,15 @@ repository.find({
### `$startsWith`
判断字符串字段是否以指定子串开头。
Check if the string field starts with the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$startsWith: '三字经',
$startsWith: 'Three Character Classic',
}
}
})
@ -398,15 +399,15 @@ repository.find({
### `$notStatsWith`
判断字符串字段是否不以指定子串开头。
Check if the string field does not start with the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notStatsWith: '三字经',
$notStatsWith: 'Three Character Classic',
}
}
})
@ -414,15 +415,15 @@ repository.find({
### `$endsWith`
判断字符串字段是否以指定子串结尾。
Check if the string field ends with the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$endsWith: '三字经',
$endsWith: 'Three Character Classic',
}
}
})
@ -430,15 +431,15 @@ repository.find({
### `$notEndsWith`
判断字符串字段是否不以指定子串结尾。
Check if the string field does not end with the specified substring.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notEndsWith: '三字经',
$notEndsWith: 'Three Character Classic',
}
}
})
@ -446,15 +447,15 @@ repository.find({
### `$like`
判断字段值是否包含指定的字符串。相当于 SQL 的 `LIKE`
Check if the field value contains the specified string. Equivalent to `LIKE` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$like: '计算机',
$like: 'Computer',
}
}
});
@ -462,15 +463,15 @@ repository.find({
### `$notLike`
判断字段值是否不包含指定的字符串。相当于 SQL 的 `NOT LIKE`
Check if the field value does not contain the specified string. Equivalent to `NOT LIKE` in SQL.
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notLike: '计算机',
$notLike: 'Computer',
}
}
});
@ -478,9 +479,9 @@ repository.find({
### `$iLike`
判断字段值是否包含指定的字符串,忽略大小写。相当于 SQL 的 `ILIKE`(仅 PG 适用)。
Check if a field value contains the specified string, case ignored. Equivalent to `ILIKE` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
@ -494,9 +495,9 @@ repository.find({
### `$notILike`
判断字段值是否不包含指定的字符串,忽略大小写。相当于 SQL 的 `NOT ILIKE`(仅 PG 适用)。
Check if a field value does not contain the specified string, case ignored. Equivalent to `NOT ILIKE` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
@ -510,15 +511,15 @@ repository.find({
### `$regexp`
判断字段值是否匹配指定的正则表达式。相当于 SQL 的 `REGEXP`(仅 PG 适用)。
Check if the field value matches the specified regular expression. Equivalent to `REGEXP` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$regexp: '^计算机',
$regexp: '^Computer',
}
}
});
@ -526,15 +527,15 @@ repository.find({
### `$notRegexp`
判断字段值是否不匹配指定的正则表达式。相当于 SQL 的 `NOT REGEXP`(仅 PG 适用)。
Check if the field value does not match the specified regular expression. Equivalent to `NOT REGEXP` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
filter: {
title: {
$notRegexp: '^计算机',
$notRegexp: '^Computer',
}
}
});
@ -542,9 +543,9 @@ repository.find({
### `$iRegexp`
判断字段值是否匹配指定的正则表达式,忽略大小写。相当于 SQL 的 `~*`(仅 PG 适用)。
Check if the field value matches the specified regular expression, case ignored. Equivalent to `~*` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
@ -558,9 +559,9 @@ repository.find({
### `$notIRegexp`
判断字段值是否不匹配指定的正则表达式,忽略大小写。相当于 SQL 的 `!~*`(仅 PG 适用)。
Check if the field value does not match the specified regular expression, case ignored. Equivalent to `!~*` in SQL (PG only).
**示例**
**Example**
```ts
repository.find({
@ -572,15 +573,15 @@ repository.find({
});
```
## 日期类型字段运算符
## Date Type Field Operators
用于日期类型字段 `type: 'date'`
For date type fields: `type: 'date'`
### `$dateOn`
判断日期字段是否在某天内。
Check if the date field value is within a certain day.
**示例**
**Example**
```ts
repository.find({
@ -594,9 +595,9 @@ repository.find({
### `$dateNotOn`
判断日期字段是否不在某天内。
Check if the date field value is not within a certain day.
**示例**
**Example**
```ts
repository.find({
@ -610,9 +611,9 @@ repository.find({
### `$dateBefore`
判断日期字段是否在某个值之前。相当于小于传入的日期值。
Check if the date field value is before a certain value, i.e., less than the one passed in.
**示例**
**Example**
```ts
repository.find({
@ -626,9 +627,9 @@ repository.find({
### `$dateNotBefore`
判断日期字段是否不在某个值之前。相当于大于等于传入的日期值。
Check if the date field value is not before a certain value, i.e., equal to or greater than the one passed in.
**示例**
**Example**
```ts
repository.find({
@ -642,9 +643,9 @@ repository.find({
### `$dateAfter`
判断日期字段是否在某个值之后。相当于大于传入的日期值。
Check if the date field value is after a certain value, i.e., greater than the one passed in.
**示例**
**Example**
```ts
repository.find({
@ -658,9 +659,9 @@ repository.find({
### `$dateNotAfter`
判断日期字段是否不在某个值之后。相当于小于等于传入的日期值。
Check if the date field value is not after a certain value, i.e., equal to or greater than the one passed in.
**示例**
**Example**
```ts
repository.find({
@ -672,21 +673,21 @@ repository.find({
})
```
## 数组类型字段运算符
## Array Type Field Operators
用于数组类型字段 `type: 'array'`
For array type fields: `type: 'array'`
### `$match`
判断数组字段的值是否匹配指定数组中的值。
Check if the array field values match values of the specified array.
**示例**
**Example**
```ts
repository.find({
filter: {
tags: {
$match: ['文学', '历史'],
$match: ['literature', 'history'],
}
}
})
@ -694,15 +695,15 @@ repository.find({
### `$notMatch`
判断数组字段的值是否不匹配指定数组中的值。
Check if the array field values do not match values of the specified array.
**示例**
**Example**
```ts
repository.find({
filter: {
tags: {
$notMatch: ['文学', '历史'],
$notMatch: ['literature', 'history'],
}
}
})
@ -710,15 +711,15 @@ repository.find({
### `$anyOf`
判断数组字段的值是否包含指定数组中的任意值。
Check if the array field values contain any of the values of the specified array.
**示例**
**Example**
```ts
repository.find({
filter: {
tags: {
$anyOf: ['文学', '历史'],
$anyOf: ['literature', 'history'],
}
}
})
@ -726,15 +727,15 @@ repository.find({
### `$noneOf`
判断数组字段的值是否不包含指定数组中的任意值。
Check if the array field values contain none of the values of the specified array.
**示例**
**Example**
```ts
repository.find({
filter: {
tags: {
$noneOf: ['文学', '历史'],
$noneOf: ['literature', 'history'],
}
}
})
@ -742,9 +743,9 @@ repository.find({
### `$arrayEmpty`
判断数组字段是否为空。
Check if the array field is empty.
**示例**
**Example**
```ts
repository.find({
@ -758,9 +759,9 @@ repository.find({
### `$arrayNotEmpty`
判断数组字段是否不为空。
Check if the array field is not empty.
**示例**
**Example**
```ts
repository.find({
@ -772,9 +773,9 @@ repository.find({
});
```
## 关系字段类型运算符
## Relational Field Type Operators
用于判断关系是否存在,字段类型包括:
For checking if a relationship exists, field types include:
- `type: 'hasOne'`
- `type: 'hasMany'`
@ -783,9 +784,9 @@ repository.find({
### `$exists`
有关系数据
There is relational data existing.
**示例**
**Example**
```ts
repository.find({
@ -799,9 +800,9 @@ repository.find({
### `$notExists`
无关系数据
There is no relational data existing.
**示例**
**Example**
```ts
repository.find({

View File

@ -1,43 +1,43 @@
# BelongsToManyRepository
`BelongsToManyRepository` 是用于处理 `BelongsToMany` 关系的 `Relation Repository`
不同于其他关系类型,`BelongsToMany` 类型的关系需要通过中间表来记录。
`Nocobase` 中定义关联关系,可自动创建中间表,也可以明确指定中间表。
`BelongsToManyRepository` is the `Relation Repository` for handling `BelongsToMany` relationships.
## 类方法
Unlike other relationship types, the `BelongsToMany` type of relationship needs to be recorded through an intermediate table. The intermediate table can be created automatically or explicitly specified when defining association relationships in NocoBase.
## Class Method
### `find()`
查找关联对象
Find associated objects.
**签名**
**Signature**
* `async find(options?: FindOptions): Promise<M[]>`
**详细信息**
**Detailed Information**
查询参数与 [`Repository.find()`](../repository.md#find) 一致。
Query parameters are the same as [`Repository.find()`](../repository.md#find).
### `findOne()`
查找关联对象,仅返回一条记录
Find associated objects, only to return one record.
**签名**
**Signature**
* `async findOne(options?: FindOneOptions): Promise<M>`
<embed src="../shared/find-one.md"></embed>
### `count()`
返回符合查询条件的记录数
Return the number of records matching the query criteria.
**签名**
**Signature**
* `async count(options?: CountOptions)`
**类型**
**Type**
```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
@ -46,22 +46,23 @@ interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where'
### `findAndCount()`
从数据库查询特定条件的数据集和结果数。
Find datasets from the database with the specified filtering conditions and return the number of results.
**签名**
**Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
**类型**
**Type**
```typescript
type FindAndCountOptions = CommonFindOptions
```
### `create()`
创建关联对象
Create associated objects.
**签名**
**Signature**
* `async create(options?: CreateOptions): Promise<M>`
@ -69,9 +70,9 @@ type FindAndCountOptions = CommonFindOptions
### `update()`
更新符合条件的关联对象
Update associated objects that match the conditions.
**签名**
**Signature**
* `async update(options?: UpdateOptions): Promise<M>`
@ -79,9 +80,9 @@ type FindAndCountOptions = CommonFindOptions
### `destroy()`
删除符合条件的关联对象
Delete associated objects.
**签名**
**Signature**
* `async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>`
@ -89,15 +90,15 @@ type FindAndCountOptions = CommonFindOptions
### `add()`
添加新的关联对象
Add new associated objects.
**签名**
**Signature**
* `async add(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions
): Promise<void>`
**类型**
**Type**
```typescript
type PrimaryKeyWithThroughValues = [TargetKey, Values];
@ -107,11 +108,12 @@ interface AssociatedOptions extends Transactionable {
}
```
**详细信息**
**Detailed Information**
可以直接传入关联对象的 `targetKey`,也可将 `targetKey` 与中间表的字段值一并传入。
Pass the `targetKey` of the associated object directly, or pass the `targetKey` along with the field values of the intermediate table.
**Example**
**示例**
```typescript
const t1 = await Tag.repository.create({
values: { name: 't1' },
@ -127,12 +129,12 @@ const p1 = await Post.repository.create({
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
// 传入 targetKey
// Pass in the targetKey
PostTagRepository.add([
t1.id, t2.id
]);
// 传入中间表字段
// Pass in intermediate table fields
PostTagRepository.add([
[t1.id, { tagged_at: '123' }],
[t2.id, { tagged_at: '456' }],
@ -141,25 +143,28 @@ PostTagRepository.add([
### `set()`
设置关联对象
Set the associated objects.
**Signature**
**签名**
* async set(
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
): Promise<void>
**详细信息**
参数同 [add()](#add)
**Detailed Information**
Parameters are the same as [add()](#add).
### `remove()`
Remove the association with the given objects.
移除与给定对象之间的关联关系
**Signature**
**签名**
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
**类型**
**Type**
```typescript
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[];
@ -168,14 +173,14 @@ interface AssociatedOptions extends Transactionable {
### `toggle()`
切换关联对象。
Toggle the associated object.
In some business scenarios, it is often needed to toggle the associated object. For example, user adds a product into collection, and the user cancels the collection and collect it again. Using the `toggle` method can quickly implement similar functions.
在一些业务场景中,经常需要切换关联对象,比如用户收藏商品,用户可以取消收藏,也可以再次收藏。使用 `toggle` 方法可以快速实现类似功能。
**签名**
**Signature**
* `async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void>`
**详细信息**
**Detailed Information**
`toggle` 方法会自动判断关联对象是否已经存在,如果存在则移除,如果不存在则添加。
The `toggle` method automatically checks whether the associated object already exists, and removes it if it does, or adds it if it does not.

View File

@ -1,4 +1,3 @@
## BelongsToRepository
其接口与 [HasOneRepository](./has-one-repository.md) 一致。
`BelongsToRepository` 是用于处理 `BelongsTo` 关系的 `Repository`,它提供了一些便捷的方法来处理 `BelongsTo` 关系。
The interface is the same as [HasOneRepository](./has-one-repository.md). `BelongsToRepository` is the `Repository` for handling `BelongsTo` relationships, and it provides some convenient methods to handle `BelongsTo` relationships.

View File

@ -1,42 +1,42 @@
# HasManyRepository
`HasManyRepository` 是用于处理 `HasMany` 关系的 `Relation Repository`
`HasManyRepository` is the `Relation Repository` for handling `HasMany` relationships.
## 类方法
## Class Method
### `find()`
查找关联对象
Find associated objects.
**签名**
**Signature**
* `async find(options?: FindOptions): Promise<M[]>`
**详细信息**
**Detailed Information**
查询参数与 [`Repository.find()`](../repository.md#find) 一致。
Query parameters are the same as [`Repository.find()`](../repository.md#find).
### `findOne()`
查找关联对象,仅返回一条记录
Find associated objects, only to return one record.
**签名**
**Signature**
* `async findOne(options?: FindOneOptions): Promise<M>`
<embed src="../shared/find-one.md"></embed>
### `count()`
返回符合查询条件的记录数
Return the number of records matching the query criteria.
**签名**
**Signature**
* `async count(options?: CountOptions)`
**类型**
**Type**
```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
@ -45,23 +45,23 @@ interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where'
### `findAndCount()`
从数据库查询特定条件的数据集和结果数。
Find datasets from the database with the specified filtering conditions and return the number of results.
**签名**
**Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>`
**类型**
**Type**
```typescript
type FindAndCountOptions = CommonFindOptions
```
### `create()`
创建关联对象
Create associated objects.
**签名**
**Signature**
* `async create(options?: CreateOptions): Promise<M>`
@ -69,9 +69,9 @@ type FindAndCountOptions = CommonFindOptions
### `update()`
更新符合条件的关联对象
Update associated objects that match the conditions.
**签名**
**Signature**
* `async update(options?: UpdateOptions): Promise<M>`
@ -79,9 +79,9 @@ type FindAndCountOptions = CommonFindOptions
### `destroy()`
删除符合条件的关联对象
Delete associated objects.
**签名**
**Signature**
* `async destroy(options?: TK | DestroyOptions): Promise<M>`
@ -89,44 +89,45 @@ type FindAndCountOptions = CommonFindOptions
### `add()`
添加对象关联关系
Add association relationships between objects.
**Signature**
**签名**
* `async add(options: TargetKey | TargetKey[] | AssociatedOptions)`
**类型**
**Type**
```typescript
interface AssociatedOptions extends Transactionable {
tk?: TargetKey | TargetKey[];
}
```
**详细信息**
**Detailed Information**
* `tk` - 关联对象的 targetKey 值,可以是单个值,也可以是数组。
* `tk` - The targetKey value of the associated object, either as a single value or an array.
<embed src="../shared/transaction.md"></embed>
### `remove()`
移除与给定对象之间的关联关系
Remove the association with the given objects.
**Signature**
**签名**
* `async remove(options: TargetKey | TargetKey[] | AssociatedOptions)`
**详细信息**
**Detailed Information**
参数同 [`add()`](#add) 方法。
Same parameters as the [`add()`](#add) method.
### `set()`
设置当前关系的关联对象
Set the associated object of the current relationship.
**签名**
**Signature**
* `async set(options: TargetKey | TargetKey[] | AssociatedOptions)`
**详细信息**
参数同 [`add()`](#add) 方法。
**Detailed Information**
Same parameters as the [`add()`](#add) method.

View File

@ -1,7 +1,8 @@
# HasOneRepository
## 概览
`HasOneRepository``HasOne` 类型的关联 Repository。
## Overview
`HasOneRepository` is the associated repository of type `HasOne`.
```typescript
const User = db.collection({
@ -21,28 +22,24 @@ const user = await User.repository.create({
values: { name: 'u1' },
});
// 获取到关联 Repository
// Get the associated repository
const userProfileRepository = User.repository.relation('profile').of(user.get('id'));
// 也可直接初始化
// Or to initialize directly
new HasOneRepository(User, 'profile', user.get('id'));
```
## 类方法
## Class Method
### `find()`
查找关联对象
Find associated objects.
**签名**
**Signature**
* `async find(options?: SingleRelationFindOption): Promise<Model<any> | null>`
**类型**
**Type**
```typescript
interface SingleRelationFindOption extends Transactionable {
@ -53,27 +50,28 @@ interface SingleRelationFindOption extends Transactionable {
}
```
**详细信息**
**Detailed Information**
查询参数与 [`Repository.find()`](../repository.md#find) 一致。
Query parameters are the same as [`Repository.find()`](../repository.md#find).
**示例**
**Example**
```typescript
const profile = await UserProfileRepository.find();
// 关联对象不存在时,返回 null
// Return null if the associated object does not exist
```
### `create()`
创建关联对象
**签名**
Create associated objects.
**Signature**
* `async create(options?: CreateOptions): Promise<Model>`
<embed src="../shared/create-options.md"></embed>
**示例**
**Example**
```typescript
const profile = await UserProfileRepository.create({
@ -90,21 +88,19 @@ console.log(profile.toJSON());
createdAt: 2022-09-24T13:59:40.025Z
}
*/
```
### `update()`
更新关联对象
Update associated objects.
**签名**
**Signature**
* `async update(options: UpdateOptions): Promise<Model>`
<embed src="../shared/update-options.md"></embed>
**示例**
**Example**
```typescript
const profile = await UserProfileRepository.update({
@ -116,17 +112,17 @@ profile.get('avatar'); // 'avatar2'
### `remove()`
移除关联对象,仅解除关联关系,不删除关联对象
Remove associated objects. Only to unassociate, not to delete the associated object.
**签名**
**Signature**
* `async remove(options?: Transactionable): Promise<void>`
**详细信息**
**Detailed Information**
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**示例**
**Example**
```typescript
await UserProfileRepository.remove();
@ -137,18 +133,17 @@ await Profile.repository.count() === 1; // true
### `destroy()`
删除关联对象
Delete associated objects.
**签名**
**Signature**
* `async destroy(options?: Transactionable): Promise<Boolean>`
**Detailed Information**
**详细信息**
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。
**示例**
**Example**
```typescript
await UserProfileRepository.destroy();
@ -158,25 +153,25 @@ await Profile.repository.count() === 0; // true
### `set()`
设置关联对象
Set associated objects.
**签名**
**Signature**
* `async set(options: TargetKey | SetOption): Promise<void>`
**类型**
**Type**
```typescript
interface SetOption extends Transactionable {
tk?: TargetKey;
}
````
**详细信息**
**Detailed Information**
* tk: 设置关联对象的 targetKey
* transaction: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。
* tk: Set the targetKey of the associated object.
* transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**示例**
**Example**
```typescript
const newProfile = await Profile.repository.create({

View File

@ -34,6 +34,7 @@ userRepository.find({
});
```
#### Operator
The `filter` parameter in the `Repository` also provides a variety of operators to perform more diverse queries.
@ -71,17 +72,17 @@ Control the output fields by the `fields`, `except`, and `appends` parameters wh
* `appends`: Append output associated fields
```javascript
// The result contains only the <i>id</i> and <i>name</i> fields
// The result contains only the id and name fields
userRepository.find({
fields: ["id", "name"],
});
// The result does not contain only the <i>password</i> field
// The result does not contain only the password field
userRepository.find({
except: ["password"],
});
// The result contains data associated with the <i>posts</i> object
// The result contains data associated with the posts object
userRepository.find({
appends: ["posts"],
});
@ -92,7 +93,7 @@ userRepository.find({
The `filter` parameter supports filtering by associated fields, for example:
```javascript
// Find the <i>user</i> objects whose associated posts have title of "post title"
// Find the user objects whose associated posts have title of "post title"
userRepository.find({
filter: {
"posts.title": "post title"
@ -103,7 +104,7 @@ userRepository.find({
Associated fields can also be nested:
```javascript
// Find the <i>user</i> objects whose associated posts have comments containing "keywords"
// Find the user objects whose associated posts have comments containing "keywords"
await userRepository.find({
filter: {
"posts.comments.content": {
@ -170,16 +171,15 @@ await userRepository.create([
age: 20,
},
])
```
#### Create Association
Create associated objects at the same time of creating data. Similar to query, nested use of associated objects is also supported. For example:
Create associated objects at the same time of creating data. Like query, nested use of associated objects is also supported. For example:
```javascript
await userRepository.create({
name: "张三",
name: "Mark",
age: 18,
posts: [
{
@ -196,7 +196,7 @@ await userRepository.create({
},
],
});
// When crearing a user, creat a post to associate with the user, and create tags to associate with the post
// When creating a user, create a post to associate with the user, and create tags to associate with the post
```
If the associated object is already in the database, you can pass its ID to create an association with it.
@ -273,7 +273,7 @@ await userRepository.update({
age: 20,
name: "Alex",
},
whitelist: ["age"], // Only update the <i>age</i> field
whitelist: ["age"], // Only update the age field
});
````
@ -330,7 +330,7 @@ await userRepository.destroy({
## Constructor
It is usually not called directly by the developer, the instantiation is done mainly by specifying a coressponding repository type that is already registered in the parameter of `db.colletion()`. Repository type is registered through `db.registerRepositories()`.
It is usually not called directly by the developer, the instantiation is done mainly by specifying a corresponding repository type that is already registered in the parameter of `db.colletion()`. Repository type is registered through `db.registerRepositories()`.
**Signature**
@ -367,28 +367,28 @@ await books.myQuery('SELECT * FROM books;');
### `database`
上下文所在的数据库管理实例。
The database management instance where the context is located.
### `collection`
对应的数据表管理实例。
The corresponding data table management instance.
### `model`
对应的数据模型类。
## 实例方法
The corresponding data model class.
## Instance Method
### `find()`
从数据库查询数据集,可指定筛选条件、排序等。
Find datasets from the database with the specified filtering conditions and sorting, etc.
**Signature**
* `async find(options?: FindOptions): Promise<Model[]>`
**类型**
**Type**
```typescript
type Filter = FilterWithOperator | FilterWithValue | FilterAnd | FilterOr;
type Appends = string[];
@ -416,14 +416,14 @@ interface CommonFindOptions extends Transactionable {
type FindOptions = SequelizeFindOptions & CommonFindOptions & FilterByTk;
```
**详细信息**
**Detailed Information**
#### `filter: Filter`
查询条件,用于过滤数据结果。传入的查询参数中,`key` 为查询的字段名,`value` 可传要查询的值,
也可配合使用操作符进行其他条件的数据筛选。
Query conditions for filtering data results. In the query parameters that passed in, `key` is the name of the field, `value` is the corresponding value. Operators can be used in conjunction with other filtering conditions.
```typescript
// 查询 name 为 foo并且 age 大于 18 的记录
// Find records with name "foo" and age above 18
repository.find({
filter: {
name: "foo",
@ -433,39 +433,43 @@ repository.find({
}
})
```
更多操作符请参考 [查询操作符](./operators.md)。
Refer to [Operators](./operators.md) for more information.
#### `filterByTk: TargetKey`
通过 `TargetKey` 查询数据,为 `filter` 参数的便捷方法。`TargetKey` 具体是哪一个字段,
可在 `Collection` 中进行[配置](./collection.md#filtertargetkey),默认为 `primaryKey`
Query data by `TargetKey`, this is shortcut for the `filter` parameter. The field of `TargetKey` can be [configured](./collection.md#filtertargetkey) in `Collection`, the default is `primaryKey`.
```typescript
// 默认情况下,查找 id 为 1 的记录
// By default, find records with id 1
repository.find({
filterByTk: 1,
});
```
#### `fields: string[]`
查询列,用户控制数据字段结果。传入此参数之后,只会返回指定的字段。
Query columns. It is used to control which data fields to output. With this parameter, only the specified fields will be returned.
#### `except: string[]`
排除列,用于控制数据字段结果。传入此参数之后,传入的字段将不会输出。
Exclude columns. It is used to control which data fields to output. With this parameter, the specified fields will not be returned.
#### `appends: string[]`
追加列,用于加载关联数据。传入此参数之后,指定的关联字段将一并输出。
Append columns. It is used to load associated data. With this parameter, the specified associated fields will be returned together.
#### `sort: string[] | string`
指定查询结果排序方式,传入参数为字段名称,默认按照升序 `asc` 排序,若需按降序 `desc` 排序,
可在字段名称前加上 `-` 符号,如:`['-id', 'name']`,表示按 `id desc, name asc` 排序。
Specify the sorting method of the query results. The input parameter is the name of the field, by default is to sort in the ascending order (`asc`); a `-` symbol needs to be added before the field name to sort in the descending order (`desc`). For example, `['-id', 'name']` means to sort by `id desc, name asc`.
#### `limit: number`
限制结果数量,同 `SQL` 中的 `limit`
Limit the number of results, same as `limit` in `SQL`.
#### `offset: number`
查询偏移量,同 `SQL` 中的 `offset`
The offset of the query, same as `offset` in `SQL`.
**Example**
@ -485,7 +489,7 @@ const results = await posts.find({
### `findOne()`
从数据库查询特定条件的单条数据。相当于 Sequelize 中的 `Model.findOne()`
Find a single piece of data from the database for specific conditions. Equivalent to `Model.findOne()` in Sequelize.
**Signature**
@ -505,13 +509,14 @@ const result = await posts.findOne({
### `count()`
从数据库查询特定条件的数据总数。相当于 Sequelize 中的 `Model.count()`
Query a certain amount of data from the database for specific conditions. Equivalent to `Model.count()` in Sequelize.
**Signature**
* `count(options?: CountOptions): Promise<number>`
**类型**
**Type**
```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
@ -525,32 +530,32 @@ const books = db.getRepository('books');
const count = await books.count({
filter: {
title: '三字经'
title: 'Three character classic'
}
});
```
### `findAndCount()`
从数据库查询特定条件的数据集和结果数。相当于 Sequelize 中的 `Model.findAndCountAll()`
Find datasets from the database with the specified filtering conditions and return the number of results. Equivalent to `Model.findAndCountAll()` in Sequelize.
**Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]>`
**类型**
**Type**
```typescript
type FindAndCountOptions = Omit<SequelizeAndCountOptions, 'where' | 'include' | 'order'> & CommonFindOptions;
```
**详细信息**
**Detailed Information**
查询参数与 `find()` 相同。返回值为一个数组,第一个元素为查询结果,第二个元素为结果总数。
The query parameters are the same as `find()`. An array is returned with the first element of the query results, and the second element of the total number of results.
### `create()`
向数据表插入一条新创建的数据。相当于 Sequelize 中的 `Model.create()`。当要创建的数据对象携带关系字段的信息时,会一并创建或更新相应的关系数据记录。
Inserts a newly created data into the data table. Equivalent to `Model.create()` in Sequelize. When the data object to be created carries any associated field, the corresponding associated data record is created or updated along with it.
**Signature**
@ -565,11 +570,11 @@ const posts = db.getRepository('posts');
const result = await posts.create({
values: {
title: 'NocoBase 1.0 发布日志',
title: 'NocoBase 1.0 Release Notes',
tags: [
// 有关系表主键值时为更新该条数据
// Update data when there is a primary key and value of the associated table
{ id: 1 },
// 没有主键值时为创建新数据
// Create data when there is no primary key and value
{ name: 'NocoBase' },
]
},
@ -578,23 +583,24 @@ const result = await posts.create({
### `createMany()`
向数据表插入多条新创建的数据。相当于多次调用 `create()` 方法。
Inserts multiple newly created data into the data table. This is equivalent to calling the `create()` method multiple times.
**Signature**
* `createMany(options: CreateManyOptions): Promise<Model[]>`
**类型**
**Type**
```typescript
interface CreateManyOptions extends BulkCreateOptions {
records: Values[];
}
```
**详细信息**
**Detailed Information**
* `records`:要创建的记录的数据对象数组。
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。
* `records`: An array of data objects to be created.
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**Example**
@ -604,16 +610,16 @@ const posts = db.getRepository('posts');
const results = await posts.createMany({
records: [
{
title: 'NocoBase 1.0 发布日志',
title: 'NocoBase 1.0 Release Notes',
tags: [
// 有关系表主键值时为更新该条数据
// Update data when there is a primary key and value of the associated table
{ id: 1 },
// 没有主键值时为创建新数据
// Create data when there is no primary key and value
{ name: 'NocoBase' },
]
},
{
title: 'NocoBase 1.1 发布日志',
title: 'NocoBase 1.1 Release Notes',
tags: [
{ id: 1 }
]
@ -624,7 +630,7 @@ const results = await posts.createMany({
### `update()`
更新数据表中的数据。相当于 Sequelize 中的 `Model.update()`。当要更新的数据对象携带关系字段的信息时,会一并创建或更新相应的关系数据记录。
Update data in the data table. Equivalent to `Model.update()` in Sequelize. When the data object to be updated carries any associated field, the corresponding associated data record is created or updated along with it.
**Signature**
@ -640,26 +646,26 @@ const posts = db.getRepository('posts');
const result = await posts.update({
filterByTk: 1,
values: {
title: 'NocoBase 1.0 发布日志',
title: 'NocoBase 1.0 Release Notes',
tags: [
// 有关系表主键值时为更新该条数据
// Update data when there is a primary key and value of the associated table
{ id: 1 },
// 没有主键值时为创建新数据
// Create data when there is no primary key and value
{ name: 'NocoBase' },
]
},
});
```
### `destory()`
### `destroy()`
删除数据表中的数据。相当于 Sequelize 中的 `Model.destroy()`
Delete data from the data table. Equivalent to `Model.destroy()` in Sequelize.
**Signature**
* `async destory(options?: TargetKey | TargetKey[] | DestoryOptions): Promise<number>`
* `async destroy(options?: TargetKey | TargetKey[] | DestoryOptions): Promise<number>`
**类型**
**Type**
```typescript
interface DestroyOptions extends SequelizeDestroyOptions {
@ -670,10 +676,9 @@ interface DestroyOptions extends SequelizeDestroyOptions {
}
```
**详细信息**
**Detailed Information**
* `filter`指定要删除的记录的过滤条件。Filter 详细用法可参考 [`find()`](#find) 方法。
* `filterByTk`:按 TargetKey 指定要删除的记录的过滤条件。
* `truncate`: 是否清空表数据,在没有传入 `filter``filterByTk` 参数时有效。
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。
![image](https://user-images.githubusercontent.com/63629092/213859493-1eb7eb5b-0368-41e8-8fb5-d0d735afd429.png)
* `filter`Specify the filtering conditions of the records to be deleted. Refer to the [`find()`](#find) method for the detailed usage of the filter.
* `filterByTk`Specify the filtering conditions by TargetKey.
* `truncate`: Whether to empty the table data, this parameter is valid if no `filter` or `filterByTk` parameter is passed.
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.

View File

@ -1,6 +1,6 @@
# Filter Operators
用于 Repository 的 find、findOne、findAndCount、count 等 API 的 filter 参数中:
用于 Repository 的 `find``findOne``findAndCount``count` 等 API 的 filter 参数中:
```ts
const repository = db.getRepository('books');

View File

@ -2,7 +2,7 @@
`BelongsToManyRepository` 是用于处理 `BelongsToMany` 关系的 `Relation Repository`
不同于其他关系类型,`BelongsToMany` 类型的关系需要通过中间表来记录。
`Nocobase` 中定义关联关系,可自动创建中间表,也可以明确指定中间表。
NocoBase 中定义关联关系,可自动创建中间表,也可以明确指定中间表。
## 类方法

View File

@ -448,7 +448,7 @@ repository.find({
```
#### `fields: string[]`
查询列,用控制数据字段结果。传入此参数之后,只会返回指定的字段。
查询列,用控制数据字段结果。传入此参数之后,只会返回指定的字段。
#### `except: string[]`
排除列,用于控制数据字段结果。传入此参数之后,传入的字段将不会输出。
@ -650,7 +650,7 @@ const result = await posts.update({
});
```
### `destory()`
### `destroy()`
删除数据表中的数据。相当于 Sequelize 中的 `Model.destroy()`