跳转至

Generate unique IDs for new objects(为新对象生成唯一ID)

:::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. :::

When writing an Ontology edit function that creates objects, you may want to generate a unique ID for the newly created object. You can set this up in functions by using the @foundry/functions-utils package to generate a globally unique identifier.

Import the package

The @foundry/functions-utils package is installed by default, but if the package is not present in the package.json file:

  • In the "dependencies" section, add "@foundry/functions-utils": "0.1.0"

As mentioned in the documentation on adding dependencies, remember to restart Code Assist to have the new package available for autocomplete.

Use the package in code

To generate a unique ID, you can use the Uuid.random() utility function from the @foundry/functions-utils package. The below code example shows how you could use the random function in an example Ontology edit function.

import { OntologyEditFunction, Timestamp } from "@foundry/functions-api";
import { Objects } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class ExampleEditFunctions {
    @Edits(FlightScenario)
    @OntologyEditFunction()
    public createFlightScenario(): void {
        const scenario = Objects.create().flightScenarios(Uuid.random());
        scenario.scenarioName = "New scenario";
        scenario.creationTime = Timestamp.now();
    }
}

中文翻译


为新对象生成唯一ID

:::callout{theme="warning"} 以下文档仅适用于TypeScript v1函数。如需更强大的功能(包括对本体论SDK(Ontology SDK)和可配置资源请求的支持),建议迁移至TypeScript v2,详情请参阅语言特性支持。 :::

在编写创建对象的本体论编辑函数(Ontology edit function)时,您可能需要为新创建的对象生成唯一ID。您可以通过使用@foundry/functions-utils包中的全局唯一标识符生成功能来实现这一需求。

导入包

@foundry/functions-utils包默认已安装,但如果package.json文件中未包含该包:

  • "dependencies"部分添加"@foundry/functions-utils": "0.1.0"

添加依赖项文档所述,请记得重启Code Assist以使新包可用于自动补全。

在代码中使用包

要生成唯一ID,您可以使用@foundry/functions-utils包中的Uuid.random()工具函数。以下代码示例展示了如何在示例本体论编辑函数中使用random函数。

import { OntologyEditFunction, Timestamp } from "@foundry/functions-api";
import { Objects } from "@foundry/ontology-api";
import { Uuid } from "@foundry/functions-utils";

export class ExampleEditFunctions {
    @Edits(FlightScenario)
    @OntologyEditFunction()
    public createFlightScenario(): void {
        const scenario = Objects.create().flightScenarios(Uuid.random());
        scenario.scenarioName = "New scenario";
        scenario.creationTime = Timestamp.now();
    }
}