Update repository.md

This commit is contained in:
Pearl C 2023-01-26 21:14:23 +08:00 committed by GitHub
parent f6132828a3
commit 266df9f6c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -367,28 +367,27 @@ await books.myQuery('SELECT * FROM books;');
### `database` ### `database`
上下文所在的数据库管理实例。 The database management instance where the context is located.
### `collection` ### `collection`
对应的数据表管理实例。 The corresponding data table management instance.
### `model` ### `model`
对应的数据模型类。 The corresponding data model class.
## 实例方法
## Instance Method
### `find()` ### `find()`
从数据库查询数据集,可指定筛选条件、排序等。 Find datasets from the database with the specified filtering conditions and sorting, etc.
**Signature** **Signature**
* `async find(options?: FindOptions): Promise<Model[]>` * `async find(options?: FindOptions): Promise<Model[]>`
**类型** **Type**
```typescript ```typescript
type Filter = FilterWithOperator | FilterWithValue | FilterAnd | FilterOr; type Filter = FilterWithOperator | FilterWithValue | FilterAnd | FilterOr;
type Appends = string[]; type Appends = string[];
@ -416,14 +415,14 @@ interface CommonFindOptions extends Transactionable {
type FindOptions = SequelizeFindOptions & CommonFindOptions & FilterByTk; type FindOptions = SequelizeFindOptions & CommonFindOptions & FilterByTk;
``` ```
**详细信息** **Detailed Information**
#### `filter: Filter` #### `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 ```typescript
// 查询 name 为 foo并且 age 大于 18 的记录 // Find records with name "foo" and age above 18
repository.find({ repository.find({
filter: { filter: {
name: "foo", name: "foo",
@ -433,39 +432,43 @@ repository.find({
} }
}) })
``` ```
更多操作符请参考 [查询操作符](./operators.md)。
Refer to [Operators](./operators.md) for more information.
#### `filterByTk: TargetKey` #### `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 ```typescript
// By default, find records with id 1
// 默认情况下,查找 id 为 1 的记录
repository.find({ repository.find({
filterByTk: 1, filterByTk: 1,
}); });
``` ```
#### `fields: string[]` #### `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[]` #### `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[]` #### `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` #### `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` #### `limit: number`
限制结果数量,同 `SQL` 中的 `limit`
Limit the number of results, same as `limit` in `SQL`.
#### `offset: number` #### `offset: number`
查询偏移量,同 `SQL` 中的 `offset`
The offset of the query, same as `offset` in `SQL`.
**Example** **Example**
@ -485,7 +488,7 @@ const results = await posts.find({
### `findOne()` ### `findOne()`
从数据库查询特定条件的单条数据。相当于 Sequelize 中的 `Model.findOne()` Find a single piece of data from the database for specific conditions. Equivalent to `Model.findOne()` in Sequelize.
**Signature** **Signature**
@ -505,13 +508,13 @@ const result = await posts.findOne({
### `count()` ### `count()`
从数据库查询特定条件的数据总数。相当于 Sequelize 中的 `Model.count()` Query a certain amount of data from the database for specific conditions. Equivalent to `Model.count()` in Sequelize.
**Signature** **Signature**
* `count(options?: CountOptions): Promise<number>` * `count(options?: CountOptions): Promise<number>`
**类型** **Type**
```typescript ```typescript
interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable { interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter; filter?: Filter;
@ -525,7 +528,7 @@ const books = db.getRepository('books');
const count = await books.count({ const count = await books.count({
filter: { filter: {
title: '三字经' title: 'Three character classic'
} }
}); });
``` ```
@ -533,24 +536,24 @@ const count = await books.count({
### `findAndCount()` ### `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** **Signature**
* `async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]>` * `async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]>`
**类型** **Type**
```typescript ```typescript
type FindAndCountOptions = Omit<SequelizeAndCountOptions, 'where' | 'include' | 'order'> & CommonFindOptions; 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()` ### `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 assiciated field, the corresponding assiciated data record is created or updated along with it.
**Signature** **Signature**
@ -565,11 +568,11 @@ const posts = db.getRepository('posts');
const result = await posts.create({ const result = await posts.create({
values: { values: {
title: 'NocoBase 1.0 发布日志', title: 'NocoBase 1.0 Release Notes',
tags: [ tags: [
// 有关系表主键值时为更新该条数据 // Update data when there is a primary key and value of the associated table
{ id: 1 }, { id: 1 },
// 没有主键值时为创建新数据 // Create data when there is no primary key and value
{ name: 'NocoBase' }, { name: 'NocoBase' },
] ]
}, },
@ -578,23 +581,23 @@ const result = await posts.create({
### `createMany()` ### `createMany()`
向数据表插入多条新创建的数据。相当于多次调用 `create()` 方法。 Inserts multiple newly created data into the data table. This is equivalent to calling the `create()` method multiple times.
**Signature** **Signature**
* `createMany(options: CreateManyOptions): Promise<Model[]>` * `createMany(options: CreateManyOptions): Promise<Model[]>`
**类型** **Type**
```typescript ```typescript
interface CreateManyOptions extends BulkCreateOptions { interface CreateManyOptions extends BulkCreateOptions {
records: Values[]; records: Values[];
} }
``` ```
**详细信息** **Detailed Information**
* `records`:要创建的记录的数据对象数组。 * `records`: An array of data objects to be created.
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。 * `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**Example** **Example**
@ -604,16 +607,16 @@ const posts = db.getRepository('posts');
const results = await posts.createMany({ const results = await posts.createMany({
records: [ records: [
{ {
title: 'NocoBase 1.0 发布日志', title: 'NocoBase 1.0 Release Notes',
tags: [ tags: [
// 有关系表主键值时为更新该条数据 // Update data when there is a primary key and value of the associated table
{ id: 1 }, { id: 1 },
// 没有主键值时为创建新数据 // Create data when there is no primary key and value
{ name: 'NocoBase' }, { name: 'NocoBase' },
] ]
}, },
{ {
title: 'NocoBase 1.1 发布日志', title: 'NocoBase 1.1 Release Notes',
tags: [ tags: [
{ id: 1 } { id: 1 }
] ]
@ -624,7 +627,7 @@ const results = await posts.createMany({
### `update()` ### `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** **Signature**
@ -640,11 +643,11 @@ const posts = db.getRepository('posts');
const result = await posts.update({ const result = await posts.update({
filterByTk: 1, filterByTk: 1,
values: { values: {
title: 'NocoBase 1.0 发布日志', title: 'NocoBase 1.0 Release Notes',
tags: [ tags: [
// 有关系表主键值时为更新该条数据 // Update data when there is a primary key and value of the associated table
{ id: 1 }, { id: 1 },
// 没有主键值时为创建新数据 // Create data when there is no primary key and value
{ name: 'NocoBase' }, { name: 'NocoBase' },
] ]
}, },
@ -653,13 +656,13 @@ const result = await posts.update({
### `destory()` ### `destory()`
删除数据表中的数据。相当于 Sequelize 中的 `Model.destroy()` Delete data from the data table. Equivalent to `Model.destroy()` in Sequelize.
**Signature** **Signature**
* `async destory(options?: TargetKey | TargetKey[] | DestoryOptions): Promise<number>` * `async destory(options?: TargetKey | TargetKey[] | DestoryOptions): Promise<number>`
**类型** **Type**
```typescript ```typescript
interface DestroyOptions extends SequelizeDestroyOptions { interface DestroyOptions extends SequelizeDestroyOptions {
@ -670,10 +673,9 @@ interface DestroyOptions extends SequelizeDestroyOptions {
} }
``` ```
**详细信息** **Detailed Information**
* `filter`指定要删除的记录的过滤条件。Filter 详细用法可参考 [`find()`](#find) 方法。 * `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`:按 TargetKey 指定要删除的记录的过滤条件。 * `filterByTk`Specify the filtering conditions by TargetKey.
* `truncate`: 是否清空表数据,在没有传入 `filter``filterByTk` 参数时有效。 * `truncate`: Whether to empty the table data, this parameter is valid if no `filter` or `filterByTk` parameter is passed.
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。 * `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
![image](https://user-images.githubusercontent.com/63629092/213859493-1eb7eb5b-0368-41e8-8fb5-d0d735afd429.png)