# SDK 类型声明

## 接口

### DataModelMethods\

模型操作方法接口定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 属性

##### create()

> **create**: (`params`) => `Promise`\\>\>

创建单条数据的方法。

###### Example

```js
models.<model_name>.create({
  data: {
    // 模型字段数据
  }
}).then(({ data }) => {
  console.log(data.id); // 输出创建的数据ID
});
```

###### 参数

| 参数          | 类型     | 描述                     |
| :------------ | :------- | :----------------------- |
| `params`      | `object` | 包含创建数据的参数对象。 |
| `params.data` | `T`      | -                        |

###### 返回

`Promise`\\>\>

##### createMany()

> **createMany**: (`params`) => `Promise`\\>\>

创建多条数据的方法。

###### Example

```js
models.<model_name>.createMany({
  data: [
    // 模型字段数据数组
  ]
}).then(({ data }) => {
  console.log(data.idList); // 输出创建的数据ID列表
});
```

###### 参数

| 参数          | 类型     | 描述                         |
| :------------ | :------- | :--------------------------- |
| `params`      | `object` | 包含创建数据数组的参数对象。 |
| `params.data` | `T`[]    | -                            |

###### 返回

`Promise`\\>\>

##### delete()

> **delete**: (`params`) => `Promise`\\>\>

删除单条数据的方法。

###### Example

```js
models.<model_name>.delete({
  filter: {
    where: {
      // 筛选条件，例如根据ID删除特定记录
      _id: {
        $eq: "特定ID值"
      }
    }
  }
}).then(({ data }) => {
  console.log(data.count); // 输出删除的数据条数
});
```

###### 参数

| 参数            | 类型                                           | 描述                     |
| :-------------- | :--------------------------------------------- | :----------------------- |
| `params`        | `object`                                       | 包含筛选条件的参数对象。 |
| `params.filter` | [`FilterParams`](globals#filterparamst)\ | -                        |

###### 返回

`Promise`\\>\>

##### deleteMany()

> **deleteMany**: (`params`) => `Promise`\\>\>

删除多条数据的方法。

###### Example

```js
models.<model_name>.deleteMany({
  filter: {
    where: {
      // 筛选条件，例如删除所有满足特定条件的记录
    }
  }
}).then(({ data }) => {
  console.log(data.count); // 输出删除的数据条数
});
```

###### 参数

| 参数            | 类型                                           | 描述                     |
| :-------------- | :--------------------------------------------- | :----------------------- |
| `params`        | `object`                                       | 包含筛选条件的参数对象。 |
| `params.filter` | [`FilterParams`](globals#filterparamst)\ | -                        |

###### 返回

`Promise`\\>\>

##### get()

> **get**: (`params`) => `Promise`\\>

获取单条数据的方法。

###### Example

```js
models.<model_name>.get({
  filter: {
    where: {
      // 筛选条件
    }
  },
  select: {
    $master: true // 选择主表所有字段
  }
}).then(({ data }) => {
  console.log(data); // 输出查询到的数据
});
```

###### 参数

| 参数             | 类型                                           | 描述                               |
| :--------------- | :--------------------------------------------- | :--------------------------------- |
| `params`         | `object`                                       | 包含筛选条件和选择字段的参数对象。 |
| `params.filter`  | [`FilterParams`](globals#filterparamst)\ | -                                  |
| `params.select`? | [`SelectParams`](globals#selectparamst)\ | -                                  |

###### 返回

`Promise`\\>

##### list()

> **list**: (`params`) => `Promise`\\>

获取多条数据的方法。

###### Example

```js
models.<model_name>.list({
  filter: {
    where: {
      // 筛选条件
    }
  },
  select: {
    $master: true // 选择主表所有字段
  },
  getCount: true, // 开启用来获取总数
  pageSize: 10, // 分页大小
  pageNumber: 1, // 当前页码
  orderBy: [{ createdAt: 'desc' }] // 排序参数
}).then(({ data }) => {
  console.log(data.records, data.total); // 输出查询到的数据列表和总数
});
```

###### 参数

| 参数                 | 类型                                           | 描述                                                                               |
| :------------------- | :--------------------------------------------- | :--------------------------------------------------------------------------------- |
| `params`             | `object`                                       | 包含筛选条件、选择字段、分页和排序选项的参数对象。                                 |
| `params.filter`?     | [`FilterParams`](globals#filterparamst)\ | 过滤条件                                                                           |
| `params.getCount`?   | `boolean`                                      | 是否获取 filter 命中条件的查询条数                                                 |
| `params.orderBy`?    | [`OrderByParams`](globals#orderbyparams)[]     | 排序参数，当前仅支持最多 3 字段排序                                                |
| `params.pageNumber`? | `number`                                       | 分页数目                                                                           |
| `params.pageSize`?   | `number`                                       | 分页大小，建议指定，如需设置为其他值，需要和 pageNumber 配合使用，两者同时指定才会生效 |
| `params.select`?     | [`SelectParams`](globals#selectparamst)\ | 可以指定返回本表或者关联表的字段，如果想查询本表所有字段，请使用 { $master: true } |

###### 返回

`Promise`\\>

##### update()

> **update**: (`params`) => `Promise`\\>\>

更新单条数据的方法。

###### Example

```js
models.<model_name>.update({
  data: {
    // 更新的数据字段
  },
  filter: {
    where: {
      // 筛选条件
    }
  }
}).then(({ data }) => {
  console.log(data.count); // 输出更新的数据条数
});
```

###### 参数

| 参数            | 类型                                           | 描述                               |
| :-------------- | :--------------------------------------------- | :--------------------------------- |
| `params`        | `object`                                       | 包含更新数据和筛选条件的参数对象。 |
| `params.data`   | `T`                                            | -                                  |
| `params.filter` | [`FilterParams`](globals#filterparamst)\ | -                                  |

###### 返回

`Promise`\\>\>

##### updateMany()

> **updateMany**: (`params`) => `Promise`\\>\>

更新多条数据的方法。

###### Example

```js
models.<model_name>.updateMany({
  data: {
    // 更新的数据字段
  },
  filter: {
    where: {
      // 筛选条件
    }
  }
}).then(({ data }) => {
  console.log(data.count); // 输出更新的数据条数
});
```

###### 参数

| 参数            | 类型                                           | 描述                               |
| :-------------- | :--------------------------------------------- | :--------------------------------- |
| `params`        | `object`                                       | 包含更新数据和筛选条件的参数对象。 |
| `params.data`   | `T`                                            | -                                  |
| `params.filter` | [`FilterParams`](globals#filterparamst)\ | -                                  |

###### 返回

`Promise`\\>\>

##### upsert()

> **upsert**: (`params`) => `Promise`\\>\>

创建或者更新的方法

###### Example

```js
models.<model_name>.upsert({
  update: {
    // 更新的数据字段
  },
  create: {
    // 创建的数据字段
  },
  filter: {
    where: {
      // 筛选条件
    }
  }
}).then(({ data }) => {
  console.log(data.count); // 输出更新的数据条数
});
```

###### 参数

| 参数            | 类型                                           | 描述                                           |
| :-------------- | :--------------------------------------------- | :--------------------------------------------- |
| `params`        | `object`                                       | 包含创建或者更新对象以及和筛选条件的参数对象。 |
| `params.create` | `T`                                            | -                                              |
| `params.filter` | [`FilterParams`](globals#filterparamst)\ | -                                              |
| `params.update` | `T`                                            | -                                              |

###### 返回

`Promise`\\>\>

---

### Model

基础 Model 类型定义

#### 属性

##### \_id?

> `optional` **\_id**: `string`

##### \_openid?

> `optional` **\_openid**: `string`

##### createBy?

> `optional` **createBy**: `string`

##### createdAt?

> `optional` **createdAt**: `number`

##### owner?

> `optional` **owner**: `string`

##### updateBy?

> `optional` **updateBy**: `string`

##### updatedAt?

> `optional` **updatedAt**: `number`

## 类型别名

### BasicComparisonOperator

> **BasicComparisonOperator**: `"$eq"` \| `"$neq"` \| `"$gt"` \| `"$gte"` \| `"$lt"` \| `"$lte"` \| `"$in"` \| `"$nin"`

基础比较运算符类型定义。

---

### ComparisonOperator

> **ComparisonOperator**: [`BasicComparisonOperator`](globals#basiccomparisonoperator) \| [`SpecialComparisonOperator`](globals#specialcomparisonoperator)

比较运算符类型定义，包括基础和特殊运算符。

#### Example

```js
$eq: 等于;
```

---

### CreateManyResponse\

> **CreateManyResponse**\: `object`

创建多条记录的响应类型定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### idList

> **idList**: `string`[]

创建的记录的 ID 列表。

---

### CreateResponse\

> **CreateResponse**\: `object`

数据创建方法的返回类型定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### id

> **id**: `string`

---

### DeleteManyResponse\

> **DeleteManyResponse**\: [`UpdateResponse`](globals#updateresponset)\

删除多条记录的响应类型定义，与更新操作的响应类型相同。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

---

### DeleteResponse\

> **DeleteResponse**\: `object`

删除操作的响应类型定义，用于表示删除操作影响的记录数量。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### count

> **count**: `number`

删除操作影响的记录数量。
如果 count 为 0，表示没有记录被删除；
如果 count 大于 0，表示有相应数量的记录被成功删除。

---

### FilterCondition

> **FilterCondition**: `{ [key in ComparisonOperator]?: any }`

复杂查询条件类型定义

#### Example

```js
{
 *  "$eq": "val"
 * }
```

---

### FilterConditionItem\

> **FilterConditionItem**\: `{ [key in keyof T]?: FilterCondition }`

过滤参数类型定义。

此类型定义允许对模型字段进行条件过滤，支持复杂的查询操作。

#### Examples

示例 1: 使用`$and`运算符来组合条件，确保所有条件都满足。

```json
{
  "$and": [
    {
      "key": {
        "$eq": "val"
      }
    }
  ]
}
```

示例 2: 使用`$and`运算符来组合条件，其中包含`$in`运算符来检查数组包含性。

```json
{
  "$and": [
    {
      "key1": {
        "$in": ["foo", "bar"]
      }
    },
    {
      "key2": {
        "$in": [1, 2]
      }
    }
  ]
}
```

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

---

### FilterObject\

> **FilterObject**\: \{ \[operator in LogicalOperator\]?: FilterConditionItem\\[\] \| FilterObject\ \}

定义过滤参数的类型。

#### Examples

```js
{
  "$and": [
    {
      "title": {
        "$eq": "hello"
      }
    }
  ]
}
```

```js
{
  "$or": [
    {
      "$and": [
        {
          "title": {
            "$eq": "hello"
          }
        },
        {
          "body": {
            "$neq": "world"
          }
        }
      ]
    },
    {
      "createdBy": {
        "$eq": "xxx"
      }
    }
  ]
}
```

#### 类型参数

| 类型参数 | 描述                                                                           |
| :------- | :----------------------------------------------------------------------------- |
| `T`      | 
表示模型字段的类型。

此类型定义允许使用复杂的查询条件来过滤数据。
 |

---

### FilterParams\

> **FilterParams**\: `object`

定义过滤参数的类型。

#### Example

```js
{
    relateWhere: {
      comments: {
        where: {
          comment: {
            $nempty: true,
          },
        },
      },
    },
    where: {},
  }
```

#### 类型参数

| 类型参数 | 描述                 |
| :------- | :------------------- |
| `T`      | 表示模型字段的类型。 |

#### 类型声明

##### relateWhere?

> `optional` **relateWhere**: `{ [K in RelationKeys<T>]?: Object }`

关联关系查询

##### where?

> `optional` **where**: [`FilterConditionItem`](globals#filterconditionitemt)\ \| [`FilterObject`](globals#filterobjectt)\

基础查询

---

### ListParams\

> **ListParams**\: `object`

list 方法参数定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### filter?

> `optional` **filter**: [`FilterParams`](globals#filterparamst)\

##### getCount?

> `optional` **getCount**: `boolean`

##### orderBy?

> `optional` **orderBy**: [`OrderByParams`](globals#orderbyparams)[]

##### pageNumber?

> `optional` **pageNumber**: `number`

##### pageSize?

> `optional` **pageSize**: `number`

##### relateWhere?

> `optional` **relateWhere**: `any`

##### select?

> `optional` **select**: [`SelectParams`](globals#selectparamst)\ \| `object`

---

### ListResponse\

> **ListResponse**\: [`MethodResponse`](globals#methodresponset)\

数据列表方法的返回类型定义。

#### 类型声明

##### records

> **records**: `T`[]

##### total?

> `optional` **total**: `number`

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

---

### LogicalOperator

> **LogicalOperator**: `"$and"` \| `"$or"`

逻辑运算符类型定义。

---

### MethodResponse\

> **MethodResponse**\: `object`

模型操作方法的返回类型定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 返回数据的类型。 |

#### 类型声明

##### data

> **data**: `T`

返回的数据。

##### requestId?

> `optional` **requestId**: `string`

请求的唯一标识符。

---

### OrderByParams

> **OrderByParams**: `object`

排序参数结构定义。

#### Example

```js
{
  "createdAt": "asc",
 }
```

#### 索引签名

\[`key`: `string`\]: `"asc"` \| `"desc"`

---

### RelationField\

> **RelationField**\: `T` _extends_ `object` ? `U` : `never`

#### 类型参数

| 类型参数 |
| :------- |
| `T`      |

---

### SelectParams\

> **SelectParams**\: \{ \[K in keyof T\]?: T\[K\] extends (infer U)\[\] \| undefined ? SelectParams\ \| boolean : T\[K\] extends object \| undefined ? SelectParams\ \| boolean : boolean \} & `object`

选择参数结构定义，用于指定查询时返回的字段。

#### Examples

```js
{
   "key1": true,
 }
```

```js
{
  $master: true,
 }
```

```js
{
  $master: true,
  comments: {
   comment: true,
  }
 }
```

#### 类型声明

##### $master?

> `optional` **$master**: `boolean`

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

---

### SpecialComparisonOperator

> **SpecialComparisonOperator**: `"$search"` \| `"$nsearch"` \| `"$empty"` \| `"$nempty"`

特殊比较运算符类型定义

---

### UpdateResponse\

> **UpdateResponse**\: `object`

更新操作的响应类型定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### count

> **count**: `number`

更新操作影响的记录数量。

---

### UpsertResponse\

> **UpsertResponse**\: `object`

创建或者更新的响应类型定义。

#### 类型参数

| 类型参数 | 描述             |
| :------- | :--------------- |
| `T`      | 模型字段的类型。 |

#### 类型声明

##### count

> **count**: `number`

变更的条数，返回非 0 值代表更新成功

##### id

> **id**: `string`

变更的条数，返回非 `""` 值代表创建成功

### OrmRawQueryClient

运行原生 SQL 的 ORM 客户端接口

#### 属性

##### $runSQL()?

此方法仅支持服务端调用

> `optional` **$runSQL**: (`sql`, `params`?, `config`?) => `Promise`\\>

###### 参数

| 参数      | 类型                                           | 描述         |
| :-------- | :--------------------------------------------- | :----------- |
| `sql`     | `string`                                       | sql 语句     |
| `params`? | `Record`\                    | sql 模板变量 |
| `config`? | [`SQLCommandParams`](globals#sqlcommandparams) | 配置         |

###### 返回

`Promise`\\>

##### $runSQLRaw()?

此方法仅支持服务端调用

> `optional` **$runSQLRaw**: (`sql`, `config`?) => `Promise`\\>

###### 参数

| 参数      | 类型                                           | 描述     |
| :-------- | :--------------------------------------------- | :------- |
| `sql`     | `string`                                       | sql 语句 |
| `config`? | [`SQLCommandParams`](globals#sqlcommandparams) | 配置     |

###### 返回

`Promise`\\>

---

### SQLCommandParams

#### 属性

##### timeout?

> `optional` **timeout**: `number`

超时时间，默认是 5s，最大不超过 **15s**
