注意:以下翻译的准确性尚未经过验证。这是使用 AIP ↗ 从原始英文文本进行的机器翻译。
函数代码库可以被用于在与CipherText对象属性交互,从而实现复杂的逻辑,如批量加密或批量解密。要开始使用函数,请参见本教程。
对于下面的例子,假设我们有一个EncryptedCustomer
对象,具有以下属性:
name
id
我们将编写两个函数与此对象交互:
decryptEncryptedCustomer()
将接收一个EncryptedCustomer
对象并返回明文名称。updateEncryptedName()
将接收一个EncryptedCustomer
对象和一个newName
,并将该对象的加密名称更新为newName
。在此示例中,我们解密并返回EncryptedCustomer
对象的name
属性。
// 从 "@foundry/functions-api" 导入所需模块
import { Function, Integer, OntologyEditFunction, Edits } from "@foundry/functions-api";
// 从 "@foundry/ontology-api" 导入所需模块
import { Objects, EncryptedCustomers } from "@foundry/ontology-api";
// 定义一个函数装饰器
@Function()
public async decryptEncryptedCustomer(customer: EncryptedCustomers): Promise<string | undefined> {
// 异步解密客户的名字并返回结果,如果名字不存在则返回 undefined
return await customer.name?.decryptAsync();
}
在下面的示例中,我们更新了EncryptedCustomer
对象的name
属性。请注意,本示例中的函数与任何更新对象的函数一样,必须使用@OntologyEditFunction()
和@Edits(EncryptedCustomers)
进行注解。另外请注意,在预览中运行此函数实际上不会编辑该对象。
Copied!1 2 3 4 5 6 7 8 9 10 11
import { Function, Integer, OntologyEditFunction, Edits } from "@foundry/functions-api"; import { Objects, EncryptedCustomers } from "@foundry/ontology-api"; // 使用装饰器定义一个本体编辑函数 @OntologyEditFunction() // 指定该函数会编辑 EncryptedCustomers 对象 @Edits(EncryptedCustomers) public async updateEncryptedName(customer: EncryptedCustomers, newName: string): Promise<void> { // 异步更新客户的加密名称为 newName await customer.name?.updateAsync(newName); }