跳到主要内容

阿里云 OTS 库(@serverless-cd/ots-orm)

针对阿里云表格存储 OTS 的 ORM 库,降低用户使用 SDK 的使用成本

安装

$ npm install @serverless-cd/ots-orm --save

示例

import Orm from "@serverless-cd/ots-orm";

const orm = new Orm(
{
accessKeyId: "ACCESS_KEY_ID",
accessKeySecret: "ACCESS_KEY_SECRET",
instanceName: "instanceName",
region: "region",
},
"tableName",
"indexName"
);

// orm.find();

参数解析

const orm = new Orm(config, tableName, indexName)

参数说明类型必填默认值
config密钥信息配置OrmConfig-
tableName数据表名称string-
indexName数据表索引string-

OrmConfig

参数说明类型必填默认值
accessKeyIdaliyun 密钥 accessKeyIdstring-
accessKeySecretaliyun 密钥 accessKeySecretstring-
instanceName表格存储实例名称string-
region表格存储实例名称的地区string-
endpoint实例访问地址string-
maxRetries最大重试次数number20

方法

创建表数据 create / update

示例

const primaryKey = [{ id: "xxx" }];
const attributeColumns = {
string: "12345",
obj: { key: "value" },
array: [1, 2, 3],
number: 234,
boolean: true,
};
await orm.create(primaryKey, attributeColumns);
await orm.update(primaryKey, attributeColumns);

参数解析

参数说明类型必填默认值
primaryKey行的主键Array<Record<string, any>>-
attributeColumns行的属性列Record<string, any>-

返回值

返回示例

{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}

详细描述
返回的是操作行数据,类型: Record<string, any>

删除表数据 delete

示例

const primaryKey = [{ id: "xxx" }];
await orm.delete(primaryKey);

参数解析

参数说明类型必填默认值
primaryKey行的主键Array<Record<string, any>>-

返回值

批量删除表数据 batchDelete

示例

const primaryKeys = [[{ id: "xxx" }], [{ id: "xx2" }]];
await orm.batchDelete(primaryKeys);

参数解析

参数说明类型必填默认值
primaryKeys各行的主键Array<Array<Record<string, any>>>-

返回值

根据主键查询数据 findByPrimary

示例

const primaryKey = [{ id: "xxx" }];
await orm.findByPrimary(primaryKey);

参数解析

参数说明类型必填默认值
primaryKey行的主键Array<Record<string, any>>-

返回值

查询到数据示例

{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}

为查询到数据示例

{}

详细描述
如果没查询到则为空对象,否则为类型: Record<string, any>

查询数据(多元索引查询) find

示例

const { totalCount, result } = await orm.find();

参数解析

参数说明类型必填默认值
pageSize本次查询需要返回的最大数量number10
currentPage本次查询的开始位置页码number1
orderKeys根据传入字段排序Array<string>-
[key: string]查询关键词any-

返回值

返回示例

{
"result": [
{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}
],
"totalCount": 1
}

详细描述

参数类型说明
resultArray<Record<string, any>>返回数据集合
totalCountnumber返回数据总量

查询单个数据(多元索引查询) findOne

示例

const { totalCount, result } = await orm.findOne();

参数解析

参数说明类型必填默认值
pageSize本次查询需要返回的最大数量number10
currentPage本次查询的开始位置页码number1
orderKeys根据传入字段排序Array<string>-
[key: string]查询关键词any-

返回值

查询到数据示例

{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}

为查询到数据示例

{}

详细描述
如果没查询到则为空对象,否则为类型: Record<string, any>

查询所有数据(多元索引查询) findAll

示例

const { totalCount, result } = await orm.findAll();

参数解析

参数说明类型必填默认值
orderKeys根据传入字段排序Array<string>-
[key: string]查询关键词any-

返回值

返回示例

{
"result": [
{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}
],
"totalCount": 1
}

详细描述

参数类型说明
resultArray<Record<string, any>>返回数据集合
totalCountnumber返回数据总量

模糊查询(多元索引查询) findByLike

示例

const params = {
currentPage: 1,
pageSize: 10,
};
const { totalCount, result } = await orm.findByLike(params);

参数解析

参数说明类型必填默认值
sort根据传入字段排序。<Record<string, 0 | 1>-
[key: String]模糊查询关键字any-

返回值

返回示例

{
"result": [
{
"id": "xxx",
"string": "12345",
"obj": { "key": "value" },
"array": [1, 2, 3],
"number": 234,
"boolean": true
}
],
"totalCount": 1
}

详细描述

参数类型说明
resultArray<Record<string, any>>返回数据集合
totalCountnumber返回数据总量