跳到主要内容

阿里云 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最大重试次数number否20

方法​

创建表数据 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本次查询需要返回的最大数量number否10
currentPage本次查询的开始位置页码number否1
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本次查询需要返回的最大数量number否10
currentPage本次查询的开始位置页码number否1
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返回数据总量