When writing an Ontology edit function that creates objects, you may want to generate a unique ID for the newly created object.
TypeScript v2 has no built-in or platform-specific UUID generator. Use a standard UUID library such as the uuid package ↗ from npm.
Add the uuid package to your repository's dependencies:
Copied!1 2npm install uuid npm install --save-dev @types/uuid
Copied!1 2 3 4 5 6import { v4 as uuidv4 } from 'uuid'; export default function createFlightScenario(): void { const uniqueId = uuidv4(); // Use uniqueId as the primary key for your new object }
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.
In TypeScript v1 functions, use the @foundry/functions-utils package to generate a globally unique identifier.
The @foundry/functions-utils package is installed by default, but if the package is not present in the package.json file:
"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.
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.
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13import { 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(); } }