@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
61 lines • 2.96 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.SchemaLoader = void 0;
const Context_1 = require("./Context");
const ECObjects_1 = require("./ECObjects");
const Exception_1 = require("./Exception");
const SchemaJsonLocater_1 = require("./SchemaJsonLocater");
const SchemaKey_1 = require("./SchemaKey");
/**
* A utility class for loading EC Schema objects using a function that returns schema json for a given schema name.
* Loaded schemas are held in memory within a schema context managed by SchemaLoader.
* The SchemaLoader object should be held in memory if multiple calls to [[getSchema]] or [[tryGetSchema]]
* is a possibility, thereby avoiding unnecessary schema retrievals from the function.
*
* Since the development of this class, the IModelDb class has been enhanced to include a schema context.
* In most cases, that is sufficient so a SchemaLoader is not needed. This class is likely to be removed in the future.
*
* ** Example **
* ```ts
* [[include:IModelSchemas.loadFromDb]]
* ```
* @beta This will no longer be needed as of 5.0.0 since IModelDb now has a schema context.
*/
class SchemaLoader {
_context;
constructor(getSchema) {
this._context = new Context_1.SchemaContext();
const locater = new SchemaJsonLocater_1.SchemaJsonLocater(getSchema);
this._context.addLocater(locater);
}
/** Get a schema by name
* @param schemaName a string with the name of the schema to load.
* @throws [ECSchemaError]($ecschema-metadata) if the schema is not found or cannot be loaded.
*/
getSchema(schemaName) {
const schema = this.tryGetSchema(schemaName);
if (!schema)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.UnableToLocateSchema, `reading schema=${schemaName}`);
return schema;
}
/** Attempts to get a schema by name
* @param schemaName a string with the name of the schema to load.
* @throws [ECSchemaError]($ecschema-metadata) if the schema exists, but cannot be loaded.
*/
tryGetSchema(schemaName) {
// SchemaKey version is not used when locating schema in an iModel, so the version is arbitrary.
const key = new SchemaKey_1.SchemaKey(schemaName, new SchemaKey_1.ECVersion(1, 0, 0));
const schema = this._context.getSchemaSync(key, ECObjects_1.SchemaMatchType.Latest);
return schema;
}
/** Gets the SchemaContext used by the loader. */
get context() {
return this._context;
}
}
exports.SchemaLoader = SchemaLoader;
//# sourceMappingURL=SchemaLoader.js.map