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