@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
65 lines • 3.21 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { SchemaContext } from "./Context";
import { Schema } from "./Metadata/Schema";
/**
* An ISchemaLocater implementation for locating and retrieving EC Schema objects using a function
* that returns the Schema Json for a given schema name
* @public @preview
*/
export class SchemaJsonLocater {
_getSchemaProps;
constructor(getSchemaProps) {
// Since the getSchemaProps may throw an error, but the locater contract defines that
// getSchema should return undefined if the schema could not be located, we wrap the provided
// lookup function in a safe block.
this._getSchemaProps = (schemaName) => {
try {
return getSchemaProps(schemaName);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (error) {
return undefined;
}
};
}
/** Get a schema by [SchemaKey]
* @param schemaKey The [SchemaKey] that identifies the schema.
* @param matchType The [SchemaMatchType] to used for comparing schema versions.
* @param context The [SchemaContext] used to facilitate schema location.
* @throws [ECSchemaError]($ecschema-metadata) if the schema exists, but cannot be loaded.
*/
async getSchema(schemaKey, matchType, context) {
await this.getSchemaInfo(schemaKey, matchType, context);
return context.getCachedSchema(schemaKey, matchType);
}
/**
* Gets the schema info which matches the provided SchemaKey. The schema info may be returned before the schema is fully loaded.
* @param schemaKey The SchemaKey describing the schema to get from the cache.
* @param matchType The match type to use when locating the schema
*/
async getSchemaInfo(schemaKey, matchType, context) {
const schemaProps = this._getSchemaProps(schemaKey.name);
if (!schemaProps)
return undefined;
const schemaInfo = await Schema.startLoadingFromJson(schemaProps, context);
if (schemaInfo !== undefined && schemaInfo.schemaKey.matches(schemaKey, matchType))
return schemaInfo;
return undefined;
}
/** Get a schema by [SchemaKey] synchronously.
* @param schemaKey The [SchemaKey] that identifies the schema.
* @param matchType The [SchemaMatchType] to used for comparing schema versions.
* @param context The [SchemaContext] used to facilitate schema location.
* @throws [Error]($ecschema-metadata) if the schema exists, but cannot be loaded.
*/
getSchemaSync(schemaKey, _matchType, context) {
const schemaProps = this._getSchemaProps(schemaKey.name);
if (!schemaProps)
return undefined;
return Schema.fromJsonSync(schemaProps, context || new SchemaContext());
}
}
//# sourceMappingURL=SchemaJsonLocater.js.map