UNPKG

@itwin/core-backend

Version:
92 lines 3.59 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Schema */ import { ConcreteEntityTypes } from "@itwin/core-common"; import { Id64 } from "@itwin/core-bentley"; import { Model } from "./Model"; import { Element } from "./Element"; import { ElementAspect } from "./ElementAspect"; import { Relationship } from "./Relationship"; import * as assert from "assert"; /** * Utilities for the [EntityReference]($common) type which is a kind of strings * @alpha */ export var EntityReferences; (function (EntityReferences) { function isModel(id) { return id[0] === ConcreteEntityTypes.Model; } EntityReferences.isModel = isModel; function isElement(id) { return id[0] === ConcreteEntityTypes.Element; } EntityReferences.isElement = isElement; function isElementAspect(id) { return id[0] === ConcreteEntityTypes.ElementAspect; } EntityReferences.isElementAspect = isElementAspect; function isRelationship(id) { return id[0] === ConcreteEntityTypes.Relationship; } EntityReferences.isRelationship = isRelationship; function toId64(id) { return id.slice(1); } EntityReferences.toId64 = toId64; /** split a concrete entity id into its type and raw id */ function split(id) { return [id[0], id.slice(1)]; } EntityReferences.split = split; /** used by the transformer to figure out where to check for the existence in a db of a concrete element id * @internal */ function isValid(id) { return Id64.isValid(toId64(id)); } EntityReferences.isValid = isValid; /** create the invalid id for a concrete entity type * @internal */ function makeInvalid(type) { return `${type}${Id64.invalid}`; } EntityReferences.makeInvalid = makeInvalid; /** create an EntityReference given an entity */ function from(entity) { const type = typeFromClass(entity.constructor); return `${type}${entity.id}`; } EntityReferences.from = from; /** create an EntityReference given an id and a JavaScript class */ function fromClass(id, entityClass) { const type = typeFromClass(entityClass); return `${type}${id}`; } EntityReferences.fromClass = fromClass; /** Create an EntityReference quickly from an exact reference type and id */ function fromEntityType(id, type) { return `${type}${id}`; } EntityReferences.fromEntityType = fromEntityType; /** @internal the argument entityClass be concrete (i.e. not the Entity abstract base class) */ function typeFromClass(entityClass) { if (entityClass.is(Element)) return ConcreteEntityTypes.Element; else if (entityClass.is(ElementAspect)) return ConcreteEntityTypes.ElementAspect; else if (entityClass.is(Model)) return ConcreteEntityTypes.Model; else if (entityClass.is(Relationship)) return ConcreteEntityTypes.Relationship; else assert(false, "unknown or abstract entity type passed to EntityReferences.from"); } EntityReferences.typeFromClass = typeFromClass; })(EntityReferences || (EntityReferences = {})); //# sourceMappingURL=EntityReferences.js.map