跳到主要内容

Storage Api

通过使用 storage api,可以实现应用自定义数据的存储.

如果要使用 storage api,需要在 manifest 中自定义 entities

自定义 entities

自定义 entities 会存储为应用数据,可以通过 storage api 操作 entities 的数据。

storage:
entities:
- name: <custom entity name>
attributes:
<attribute1>:
type: <type>
<attribute2>:
type: <type>
<attributeN>:
type: <type>
indexes:
- <attributeN>
- <attributeN>
警告

entities 中的字段不能被删除,只能新增字段或者更新属性。

attribute type 支持以下类型

  • String
  • Boolean
  • Date
  • Float

attribute 还支持其他属性

  • required
  • defaultValue

例如:

storage:
entities:
- name: <custom entity name>
attributes:
<attribute1>:
type: <type>
required: true
defaultValue: default-string

indexes

索引支持普通索引和唯一索引的创建。

直接列出字段名称即为普通索引。例如:

storage:
entities:
- name: <custom entity name>
attributes:
<attribute1>:
type: <type>
<attribute2>:
type: <type>
<attribute3>:
type: <type>
indexes:
- attribute1,attribute2
- attribute3

会创建两个索引,分别是:

  1. attribute1 和 attribute2 的联合索引
  2. attribute3 的单独索引

也可以创建唯一索引,例如:

storage:
entities:
- name: <custom entity name>
attributes:
<attribute1>:
type: <type>
<attribute2>:
type: <type>
<attribute3>:
type: <type>
indexes:
- name: unqiue_index_name
fields:
- attribute1
- attribute2
unique: true

引入 storage api

安装 @giteeteam/apps-api,执行:

npm install @giteeteam/apps-api

引入 @giteeteam/apps-api

import { storage } from "@giteeteam/apps-api";

特性

  • Immutable 每次操作都会生成一个新的对象
  • Chainable 操作可以链式调用
警告

因为操作都是 immutable 的,所以每次操作都会生成一个新的对象,所以推荐使用链式调用,防止上下文查询丢失。

查询

基础查询

const query = storage.entity("<custom-entity>").query();

query
.equalTo("xxx", "xxx")
.notEqualTo("xxx", "xxx")
.greaterThan("playerAge", 18)
.limit(10);

// query one
await query.first();
// query all
await query.find();

query.ascending("score");
query.descending("score");

// Restricts to wins < 50
query.lessThan("wins", 50);

// Restricts to wins <= 50
query.lessThanOrEqualTo("wins", 50);

// Restricts to wins > 50
query.greaterThan("wins", 50);

// Restricts to wins >= 50
query.greaterThanOrEqualTo("wins", 50);

// Finds scores from any of Jonathan, Dario, or Shawn
query.containedIn("playerName", [
"Jonathan Walsh",
"Dario Wunsch",
"Shawn Simon",
]);

// Finds scores from anyone who is neither Jonathan, Dario, nor Shawn
query.notContainedIn("playerName", [
"Jonathan Walsh",
"Dario Wunsch",
"Shawn Simon",
]);

// Finds objects that have the score set
query.exists("score");

// Finds objects that don't have the score set
query.doesNotExist("score");

query.select("score", "playerName");
// remove undesired fields while retrieving the rest:
query.exclude("playerName");

query.startsWith("name", "Big Daddy's");

组合查询

query = storage.entity("ItemRank").query();
query.equalTo("xxx", "xxx");

query2 = storage.entity("ItemRank").query();
query2.equalTo("xxx", "xxx");

storage.query.or(query, query2);

统计

await query.count();

分页

// 限制返回条数
query.limit(100)
// 跳过条数,用于分页
query.skip(10)

添加/更新/删除

await storage.entity("<custom-entity>").add({
name: "",
values: {},
});

await storage.entity("<custom-entity>").set("objectId", {
name: "",
values: {},
});

await storage.entity("<custom-entity>").delete("objectId");