UNPKG

@itwin/ecschema-rpcinterface-impl

Version:

Schema RPC Interface backend implementation

72 lines 3.52 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import * as backend from "@itwin/core-backend"; import { QueryRowFormat, RpcInterface, RpcManager } from "@itwin/core-common"; import { ECSchemaRpcInterface } from "@itwin/ecschema-rpcinterface-common"; /** * Implementation of the SchemaRpcInterface. * @internal */ export class ECSchemaRpcImpl extends RpcInterface { /** * Registers the RPC interface with its corresponding implementation class. */ static register() { RpcManager.registerImpl(ECSchemaRpcInterface, ECSchemaRpcImpl); } /** * Gets an iModelDb instance. It is important that the database has been opened before * otherwise it can't be found. * @param tokenProps The iModelToken props that hold the information which iModel is used. * @returns Instance of IModelDb. */ async getIModelDatabase(tokenProps) { return new Promise((resolve) => { resolve(backend.IModelDb.findByKey(tokenProps.key)); }); } /** * Returns an array of SchemaKeyProps that exists in the current iModel context. The client can call * SchemaKey.fromJson() to parse the props to a SchemaKey. * @param tokenProps The iModelToken props that hold the information which iModel is used. * @returns An array of SchemaKeyProps. */ async getSchemaKeys(tokenProps) { const schemaKeyProps = []; const iModelDb = await this.getIModelDatabase(tokenProps); const schemaNameQuery = `SELECT Name as schemaName, VersionMajor as read, VersionWrite as write, VersionMinor as minor FROM main.meta.ECSchemaDef`; for await (const row of iModelDb.createQueryReader(schemaNameQuery, undefined, { rowFormat: QueryRowFormat.UseJsPropertyNames })) { const schemaDefinitionRow = row.toRow(); const schemaFullName = schemaDefinitionRow.schemaName; const read = Number(schemaDefinitionRow.read); const write = Number(schemaDefinitionRow.write); const minor = Number(schemaDefinitionRow.minor); schemaKeyProps.push({ name: schemaFullName, read, write, minor }); } return schemaKeyProps; } /** * Gets the schema JSON for the current iModel context and returns the schema as a SchemaProps which * the client can call Schema.fromJson() to return a Schema. * @param tokenProps The iModelToken props that hold the information which iModel is used. * @param schemaName The name of the schema that shall be returned. * @returns The SchemaProps. */ async getSchemaJSON(tokenProps, schemaName) { if (schemaName === undefined || schemaName.length < 1) { throw new Error(`Schema name must not be undefined or empty.`); } const iModelDb = await this.getIModelDatabase(tokenProps); try { return iModelDb[backend._nativeDb].getSchemaProps(schemaName); } catch (e) { if (e.message && e.message === "schema not found") return undefined; throw (e); } } } //# sourceMappingURL=ECSchemaRpcImpl.js.map