@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
54 lines • 2.69 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 {
_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.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.fromJsonSync(schemaProps, context || new SchemaContext());
}
}
//# sourceMappingURL=SchemaJsonLocater.js.map