@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
69 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaJsonLocater = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const Context_1 = require("./Context");
const Schema_1 = require("./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
*/
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_1.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_1.Schema.fromJsonSync(schemaProps, context || new Context_1.SchemaContext());
}
}
exports.SchemaJsonLocater = SchemaJsonLocater;
//# sourceMappingURL=SchemaJsonLocater.js.map