Decorators(装饰器(Decorators))¶
:::callout{theme="warning"} The following documentation is specific to TypeScript v1 functions. For more robust capabilities, including support for Ontology SDK and configurable resource requests, we recommend migrating to TypeScript v2. :::
TypeScript ↗ functions are declared as methods of a TypeScript class ↗. There are a few requirements for a function to be discovered and published:
- The method must be
public - The class the method belongs to must be exported from the
functions-typescript/src/index.tsfile - The method must be decorated with one of the following decorators imported from the
@foundry/functions-apipackage: @Function()for generic functions.@OntologyEditFunction()for functions that will back an Action.- Object provenance information may be optionally specified with the
@Edits([object type])decorator when using the@OntologyEditFunction()method. - Object provenance information will be inferred on a best-efforts basis using the static analysis of code if the
@Edits([object type])decorator is absent.
- Object provenance information may be optionally specified with the
@Query({ apiName: "userDefinedAPIName"})for read-only queries that you want to execute through Foundry API. Note that this decorator should not be used in addition to the@Functiondecorator; it should be used on its own.
Here are examples of functions that are correctly exported in this way:
import { Function, OntologyEditFunction, Query, Integer, Edits } from "@foundry/functions-api";
import { Employee } from "@foundry/ontology-api";
export class MyUsefulFunctions {
@Function()
public incrementNumber(x: Integer): Integer {
return x + 1;
}
@Edits(Employee)
@OntologyEditFunction()
public updateName(employee: Employee, newName: string): void {
employee.firstName = newName;
}
@Query({ apiName: "getEmployeesByName" })
public async getEmployeesByName(name: string): Promise<ObjectSet<Employee>> {
return Objects.search().employee().filter(employee => employee.firstName.exactMatch(name));
}
}
Any method that is private or not decorated with the relevant decorations will not be published to the function registry. This allows users to create helper functions and utilities for reuse or organization.
:::callout{title="Republishing"} Note that each function in a TypeScript repository is uniquely defined by its class name and method name—if you change the name of the class or method, the function will be published under a new identifier. :::
中文翻译¶
装饰器(Decorators)¶
:::callout{theme="warning"} 以下文档专门针对TypeScript v1函数。如需更强大的功能,包括对本体论SDK(Ontology SDK)和可配置资源请求的支持,我们建议迁移到TypeScript v2。 :::
TypeScript ↗函数被声明为TypeScript类 ↗的方法。要使函数能被发现和发布,需要满足以下几个要求:
- 方法必须是
public(公共的) - 方法所属的类必须从
functions-typescript/src/index.ts文件中导出 - 方法必须使用从
@foundry/functions-api包中导入的以下装饰器之一进行装饰: - 通用函数使用
@Function()装饰器。 - 用于支持操作(Action)的函数使用
@OntologyEditFunction()装饰器。- 使用
@OntologyEditFunction()方法时,可以通过@Edits([对象类型])装饰器可选地指定对象溯源信息(Object provenance information)。 - 如果缺少
@Edits([对象类型])装饰器,对象溯源信息将通过代码的静态分析尽最大努力进行推断。
- 使用
- 对于希望通过Foundry API执行的只读查询,使用
@Query({ apiName: "userDefinedAPIName"})装饰器。请注意,此装饰器不应与@Function装饰器同时使用;它应单独使用。
以下是以这种方式正确导出的函数示例:
import { Function, OntologyEditFunction, Query, Integer, Edits } from "@foundry/functions-api";
import { Employee } from "@foundry/ontology-api";
export class MyUsefulFunctions {
@Function()
public incrementNumber(x: Integer): Integer {
return x + 1;
}
@Edits(Employee)
@OntologyEditFunction()
public updateName(employee: Employee, newName: string): void {
employee.firstName = newName;
}
@Query({ apiName: "getEmployeesByName" })
public async getEmployeesByName(name: string): Promise<ObjectSet<Employee>> {
return Objects.search().employee().filter(employee => employee.firstName.exactMatch(name));
}
}
任何私有方法或未使用相关装饰器进行装饰的方法都不会被发布到函数注册表(Function Registry)中。这允许用户创建辅助函数(Helper Function)和工具函数(Utility)以便重用或组织代码。
:::callout{title="重新发布(Republishing)"} 请注意,TypeScript仓库中的每个函数由其类名和方法名唯一标识——如果您更改了类名或方法名,该函数将使用新的标识符发布。 :::