Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

TypeScript v1 함수 데코레이터

TSv1 함수는 public 클래스 메서드로 정의하고 클래스를 functions-typescript/src/index.ts에서 내보내야 합니다. 다음 중 역할에 맞는 데코레이터를 사용해야 레지스트리에 게시됩니다.

데코레이터용도
Function일반 계산 함수
OntologyEditFunction액션에 연결할 온톨로지 편집 함수
QueryAPI 이름을 부여할 수 있는 읽기 전용 쿼리

Query는 Function과 함께 붙이지 않고 단독으로 사용합니다. 편집 함수에는 Edits로 대상 유형을 명시합니다. Edits가 없으면 런타임이 코드 정적 분석으로 추론을 시도하지만 놓칠 수 있으므로 명시 선언이 권장됩니다.

세 종류의 메서드

가상의 Employee 유형은 문자열 firstName을 가지며 검색에 사용할 수 있도록 Searchable로 설정되어 있다고 가정합니다.

import { Function, OntologyEditFunction, Query, Integer, Edits } from "@foundry/functions-api";
import { Employee, Objects, ObjectSet } 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 getEmployeesByName(name: string): ObjectSet<Employee> {
        return Objects.search().employee().filter(employee => employee.firstName.exactMatch(name));
    }
}

incrementNumber는 결과가 Integer 범위 안에 들어오는 입력을 사용해야 합니다. getEmployeesByName은 객체를 모두 로드하지 않고 집합 정의를 반환합니다.

위 클래스를 myUsefulFunctions.ts에 두었다면 index.ts에 다음 내보내기를 추가합니다.

export { MyUsefulFunctions } from "./myUsefulFunctions";

private 메서드나 관련 데코레이터가 없는 메서드는 게시하지 않으므로 내부 helper로 사용할 수 있습니다. 편집 helper의 변경도 같은 실행의 편집 저장소에 수집됩니다.

함수 식별은 클래스 이름과 메서드 이름을 사용합니다. 둘 중 하나를 바꾸면 새 식별자의 함수로 게시됩니다. 쿼리 API 이름과 코드 메서드 이름도 구분해야 합니다.