@itwin/core-backend 3.4.0-dev.56 → 3.4.0-dev.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/lib/cjs/AliCloudStorageService.d.ts +23 -23
  2. package/lib/cjs/AliCloudStorageService.js +71 -71
  3. package/lib/cjs/AliCloudStorageService.js.map +1 -1
  4. package/lib/cjs/BackendHubAccess.d.ts +224 -224
  5. package/lib/cjs/BackendHubAccess.js +41 -41
  6. package/lib/cjs/BackendHubAccess.js.map +1 -1
  7. package/lib/cjs/BackendLoggerCategory.d.ts +91 -91
  8. package/lib/cjs/BackendLoggerCategory.js +99 -99
  9. package/lib/cjs/BackendLoggerCategory.js.map +1 -1
  10. package/lib/cjs/BisCoreSchema.d.ts +21 -21
  11. package/lib/cjs/BisCoreSchema.js +60 -60
  12. package/lib/cjs/BisCoreSchema.js.map +1 -1
  13. package/lib/cjs/BriefcaseManager.d.ts +165 -165
  14. package/lib/cjs/BriefcaseManager.js +405 -405
  15. package/lib/cjs/BriefcaseManager.js.map +1 -1
  16. package/lib/cjs/Category.d.ts +153 -153
  17. package/lib/cjs/Category.js +243 -243
  18. package/lib/cjs/Category.js.map +1 -1
  19. package/lib/cjs/ChangeSummaryManager.d.ts +187 -187
  20. package/lib/cjs/ChangeSummaryManager.js +445 -445
  21. package/lib/cjs/ChangeSummaryManager.js.map +1 -1
  22. package/lib/cjs/ChangedElementsDb.d.ts +99 -99
  23. package/lib/cjs/ChangedElementsDb.js +165 -165
  24. package/lib/cjs/ChangedElementsDb.js.map +1 -1
  25. package/lib/cjs/ChangedElementsManager.d.ts +39 -39
  26. package/lib/cjs/ChangedElementsManager.js +88 -88
  27. package/lib/cjs/ChangedElementsManager.js.map +1 -1
  28. package/lib/cjs/CheckpointManager.d.ts +120 -120
  29. package/lib/cjs/CheckpointManager.js +343 -343
  30. package/lib/cjs/CheckpointManager.js.map +1 -1
  31. package/lib/cjs/ClassRegistry.d.ts +71 -71
  32. package/lib/cjs/ClassRegistry.js +215 -215
  33. package/lib/cjs/ClassRegistry.js.map +1 -1
  34. package/lib/cjs/CloudSqlite.d.ts +349 -328
  35. package/lib/cjs/CloudSqlite.d.ts.map +1 -1
  36. package/lib/cjs/CloudSqlite.js +151 -135
  37. package/lib/cjs/CloudSqlite.js.map +1 -1
  38. package/lib/cjs/CloudStorageBackend.d.ts +58 -58
  39. package/lib/cjs/CloudStorageBackend.js +173 -173
  40. package/lib/cjs/CloudStorageBackend.js.map +1 -1
  41. package/lib/cjs/CodeService.d.ts +396 -391
  42. package/lib/cjs/CodeService.d.ts.map +1 -1
  43. package/lib/cjs/CodeService.js +63 -63
  44. package/lib/cjs/CodeService.js.map +1 -1
  45. package/lib/cjs/CodeSpecs.d.ts +50 -50
  46. package/lib/cjs/CodeSpecs.js +112 -112
  47. package/lib/cjs/CodeSpecs.js.map +1 -1
  48. package/lib/cjs/ConcurrentQuery.d.ts +11 -11
  49. package/lib/cjs/ConcurrentQuery.js +30 -30
  50. package/lib/cjs/ConcurrentQuery.js.map +1 -1
  51. package/lib/cjs/CustomViewState3dCreator.d.ts +32 -32
  52. package/lib/cjs/CustomViewState3dCreator.js +113 -113
  53. package/lib/cjs/CustomViewState3dCreator.js.map +1 -1
  54. package/lib/cjs/DevTools.d.ts +71 -71
  55. package/lib/cjs/DevTools.js +154 -154
  56. package/lib/cjs/DevTools.js.map +1 -1
  57. package/lib/cjs/DisplayStyle.d.ts +106 -106
  58. package/lib/cjs/DisplayStyle.js +256 -256
  59. package/lib/cjs/DisplayStyle.js.map +1 -1
  60. package/lib/cjs/ECDb.d.ts +191 -191
  61. package/lib/cjs/ECDb.js +350 -350
  62. package/lib/cjs/ECDb.js.map +1 -1
  63. package/lib/cjs/ECSchemaXmlContext.d.ts +18 -18
  64. package/lib/cjs/ECSchemaXmlContext.js +41 -41
  65. package/lib/cjs/ECSchemaXmlContext.js.map +1 -1
  66. package/lib/cjs/ECSqlStatement.d.ts +466 -466
  67. package/lib/cjs/ECSqlStatement.js +791 -791
  68. package/lib/cjs/ECSqlStatement.js.map +1 -1
  69. package/lib/cjs/Element.d.ts +1109 -1109
  70. package/lib/cjs/Element.js +1420 -1420
  71. package/lib/cjs/Element.js.map +1 -1
  72. package/lib/cjs/ElementAspect.d.ts +156 -156
  73. package/lib/cjs/ElementAspect.js +178 -178
  74. package/lib/cjs/ElementAspect.js.map +1 -1
  75. package/lib/cjs/ElementGraphics.d.ts +9 -9
  76. package/lib/cjs/ElementGraphics.js +41 -41
  77. package/lib/cjs/ElementGraphics.js.map +1 -1
  78. package/lib/cjs/ElementTreeWalker.d.ts +162 -162
  79. package/lib/cjs/ElementTreeWalker.js +409 -409
  80. package/lib/cjs/ElementTreeWalker.js.map +1 -1
  81. package/lib/cjs/Entity.d.ts +76 -76
  82. package/lib/cjs/Entity.js +82 -82
  83. package/lib/cjs/Entity.js.map +1 -1
  84. package/lib/cjs/ExportGraphics.d.ts +317 -317
  85. package/lib/cjs/ExportGraphics.js +208 -208
  86. package/lib/cjs/ExportGraphics.js.map +1 -1
  87. package/lib/cjs/ExternalSource.d.ts +94 -94
  88. package/lib/cjs/ExternalSource.js +125 -125
  89. package/lib/cjs/ExternalSource.js.map +1 -1
  90. package/lib/cjs/GeoCoordConfig.d.ts +19 -19
  91. package/lib/cjs/GeoCoordConfig.js +82 -82
  92. package/lib/cjs/GeoCoordConfig.js.map +1 -1
  93. package/lib/cjs/GeometrySummary.d.ts +4 -4
  94. package/lib/cjs/GeometrySummary.js +380 -380
  95. package/lib/cjs/GeometrySummary.js.map +1 -1
  96. package/lib/cjs/HubMock.d.ts +93 -93
  97. package/lib/cjs/HubMock.js +190 -190
  98. package/lib/cjs/HubMock.js.map +1 -1
  99. package/lib/cjs/IModelCloneContext.d.ts +85 -85
  100. package/lib/cjs/IModelCloneContext.js +150 -150
  101. package/lib/cjs/IModelCloneContext.js.map +1 -1
  102. package/lib/cjs/IModelDb.d.ts +1164 -1164
  103. package/lib/cjs/IModelDb.js +2324 -2324
  104. package/lib/cjs/IModelDb.js.map +1 -1
  105. package/lib/cjs/IModelHost.d.ts +361 -361
  106. package/lib/cjs/IModelHost.js +489 -489
  107. package/lib/cjs/IModelHost.js.map +1 -1
  108. package/lib/cjs/IModelJsFs.d.ts +62 -62
  109. package/lib/cjs/IModelJsFs.d.ts.map +1 -1
  110. package/lib/cjs/IModelJsFs.js +140 -140
  111. package/lib/cjs/IModelJsFs.js.map +1 -1
  112. package/lib/cjs/IpcHost.d.ts +98 -98
  113. package/lib/cjs/IpcHost.js +234 -234
  114. package/lib/cjs/IpcHost.js.map +1 -1
  115. package/lib/cjs/LineStyle.d.ts +311 -311
  116. package/lib/cjs/LineStyle.js +295 -295
  117. package/lib/cjs/LineStyle.js.map +1 -1
  118. package/lib/cjs/LocalHub.d.ts +162 -162
  119. package/lib/cjs/LocalHub.js +587 -587
  120. package/lib/cjs/LocalHub.js.map +1 -1
  121. package/lib/cjs/LocalhostIpcHost.d.ts +20 -20
  122. package/lib/cjs/LocalhostIpcHost.js +81 -81
  123. package/lib/cjs/LocalhostIpcHost.js.map +1 -1
  124. package/lib/cjs/Material.d.ts +103 -103
  125. package/lib/cjs/Material.js +147 -147
  126. package/lib/cjs/Material.js.map +1 -1
  127. package/lib/cjs/Model.d.ts +386 -386
  128. package/lib/cjs/Model.js +471 -471
  129. package/lib/cjs/Model.js.map +1 -1
  130. package/lib/cjs/NativeAppStorage.d.ts +51 -51
  131. package/lib/cjs/NativeAppStorage.js +202 -202
  132. package/lib/cjs/NativeAppStorage.js.map +1 -1
  133. package/lib/cjs/NativeHost.d.ts +52 -52
  134. package/lib/cjs/NativeHost.js +168 -168
  135. package/lib/cjs/NativeHost.js.map +1 -1
  136. package/lib/cjs/NavigationRelationship.d.ts +156 -156
  137. package/lib/cjs/NavigationRelationship.js +225 -225
  138. package/lib/cjs/NavigationRelationship.js.map +1 -1
  139. package/lib/cjs/PromiseMemoizer.d.ts +51 -51
  140. package/lib/cjs/PromiseMemoizer.js +101 -101
  141. package/lib/cjs/PromiseMemoizer.js.map +1 -1
  142. package/lib/cjs/PropertyStore.d.ts +132 -132
  143. package/lib/cjs/PropertyStore.d.ts.map +1 -1
  144. package/lib/cjs/PropertyStore.js +11 -11
  145. package/lib/cjs/PropertyStore.js.map +1 -1
  146. package/lib/cjs/Relationship.d.ts +400 -400
  147. package/lib/cjs/Relationship.js +465 -465
  148. package/lib/cjs/Relationship.js.map +1 -1
  149. package/lib/cjs/RpcBackend.d.ts +28 -28
  150. package/lib/cjs/RpcBackend.js +126 -126
  151. package/lib/cjs/RpcBackend.js.map +1 -1
  152. package/lib/cjs/SQLiteDb.d.ts +219 -219
  153. package/lib/cjs/SQLiteDb.js +214 -214
  154. package/lib/cjs/SQLiteDb.js.map +1 -1
  155. package/lib/cjs/Schema.d.ts +55 -55
  156. package/lib/cjs/Schema.js +83 -83
  157. package/lib/cjs/Schema.js.map +1 -1
  158. package/lib/cjs/ServerBasedLocks.d.ts +57 -57
  159. package/lib/cjs/ServerBasedLocks.js +168 -168
  160. package/lib/cjs/ServerBasedLocks.js.map +1 -1
  161. package/lib/cjs/SqliteStatement.d.ts +283 -283
  162. package/lib/cjs/SqliteStatement.js +448 -448
  163. package/lib/cjs/SqliteStatement.js.map +1 -1
  164. package/lib/cjs/Texture.d.ts +58 -58
  165. package/lib/cjs/Texture.js +82 -82
  166. package/lib/cjs/Texture.js.map +1 -1
  167. package/lib/cjs/TileStorage.d.ts +18 -18
  168. package/lib/cjs/TileStorage.d.ts.map +1 -1
  169. package/lib/cjs/TileStorage.js +90 -90
  170. package/lib/cjs/TileStorage.js.map +1 -1
  171. package/lib/cjs/TxnManager.d.ts +202 -202
  172. package/lib/cjs/TxnManager.js +356 -356
  173. package/lib/cjs/TxnManager.js.map +1 -1
  174. package/lib/cjs/ViewDefinition.d.ts +435 -435
  175. package/lib/cjs/ViewDefinition.js +635 -635
  176. package/lib/cjs/ViewDefinition.js.map +1 -1
  177. package/lib/cjs/ViewStateHydrator.d.ts +13 -13
  178. package/lib/cjs/ViewStateHydrator.js +103 -103
  179. package/lib/cjs/ViewStateHydrator.js.map +1 -1
  180. package/lib/cjs/assets/IModelChange.02.00.00.ecschema.xml +90 -90
  181. package/lib/cjs/assets/Settings/Schemas/Cloud.Schema.json +67 -67
  182. package/lib/cjs/assets/Settings/Schemas/Gcs.schema.json +31 -31
  183. package/lib/cjs/assets/Settings/Schemas/Workspace.Schema.json +52 -52
  184. package/lib/cjs/assets/Settings/backend.setting.json5 +138 -138
  185. package/lib/cjs/core-backend.d.ts +182 -182
  186. package/lib/cjs/core-backend.js +202 -202
  187. package/lib/cjs/core-backend.js.map +1 -1
  188. package/lib/cjs/domains/FunctionalElements.d.ts +101 -101
  189. package/lib/cjs/domains/FunctionalElements.js +145 -145
  190. package/lib/cjs/domains/FunctionalElements.js.map +1 -1
  191. package/lib/cjs/domains/FunctionalSchema.d.ts +13 -13
  192. package/lib/cjs/domains/FunctionalSchema.js +40 -40
  193. package/lib/cjs/domains/FunctionalSchema.js.map +1 -1
  194. package/lib/cjs/domains/GenericElements.d.ts +177 -177
  195. package/lib/cjs/domains/GenericElements.js +263 -263
  196. package/lib/cjs/domains/GenericElements.js.map +1 -1
  197. package/lib/cjs/domains/GenericSchema.d.ts +10 -10
  198. package/lib/cjs/domains/GenericSchema.js +28 -28
  199. package/lib/cjs/domains/GenericSchema.js.map +1 -1
  200. package/lib/cjs/rpc-impl/DevToolsRpcImpl.d.ts +15 -15
  201. package/lib/cjs/rpc-impl/DevToolsRpcImpl.js +32 -32
  202. package/lib/cjs/rpc-impl/DevToolsRpcImpl.js.map +1 -1
  203. package/lib/cjs/rpc-impl/IModelReadRpcImpl.d.ts +45 -45
  204. package/lib/cjs/rpc-impl/IModelReadRpcImpl.js +262 -262
  205. package/lib/cjs/rpc-impl/IModelReadRpcImpl.js.map +1 -1
  206. package/lib/cjs/rpc-impl/IModelTileRpcImpl.d.ts +22 -22
  207. package/lib/cjs/rpc-impl/IModelTileRpcImpl.js +204 -204
  208. package/lib/cjs/rpc-impl/IModelTileRpcImpl.js.map +1 -1
  209. package/lib/cjs/rpc-impl/RpcBriefcaseUtility.d.ts +36 -36
  210. package/lib/cjs/rpc-impl/RpcBriefcaseUtility.js +168 -168
  211. package/lib/cjs/rpc-impl/RpcBriefcaseUtility.js.map +1 -1
  212. package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.d.ts +16 -16
  213. package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.js +43 -43
  214. package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.js.map +1 -1
  215. package/lib/cjs/rpc-impl/WipRpcImpl.d.ts +15 -15
  216. package/lib/cjs/rpc-impl/WipRpcImpl.js +37 -37
  217. package/lib/cjs/rpc-impl/WipRpcImpl.js.map +1 -1
  218. package/lib/cjs/workspace/Settings.d.ts +186 -186
  219. package/lib/cjs/workspace/Settings.js +155 -155
  220. package/lib/cjs/workspace/Settings.js.map +1 -1
  221. package/lib/cjs/workspace/SettingsSchemas.d.ts +75 -75
  222. package/lib/cjs/workspace/SettingsSchemas.js +191 -191
  223. package/lib/cjs/workspace/SettingsSchemas.js.map +1 -1
  224. package/lib/cjs/workspace/Workspace.d.ts +414 -414
  225. package/lib/cjs/workspace/Workspace.js +543 -543
  226. package/lib/cjs/workspace/Workspace.js.map +1 -1
  227. package/package.json +14 -14
@@ -1,216 +1,216 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Schema
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.MetaDataRegistry = exports.ClassRegistry = void 0;
11
- const core_bentley_1 = require("@itwin/core-bentley");
12
- const core_common_1 = require("@itwin/core-common");
13
- const Entity_1 = require("./Entity");
14
- const Element_1 = require("./Element");
15
- const Schema_1 = require("./Schema");
16
- const isGeneratedClassTag = Symbol("isGeneratedClassTag");
17
- /** The mapping between a BIS class name (in the form "schema:class") and its JavaScript constructor function
18
- * @public
19
- */
20
- class ClassRegistry {
21
- /** @internal */
22
- static isNotFoundError(err) { return (err instanceof core_common_1.IModelError) && (err.errorNumber === core_bentley_1.IModelStatus.NotFound); }
23
- /** @internal */
24
- static makeMetaDataNotFoundError(className) { return new core_common_1.IModelError(core_bentley_1.IModelStatus.NotFound, `metadata not found for ${className}`); }
25
- /** @internal */
26
- static register(entityClass, schema) {
27
- entityClass.schema = schema;
28
- const key = (`${schema.schemaName}:${entityClass.className}`).toLowerCase();
29
- if (this._classMap.has(key)) {
30
- const errMsg = `Class ${key} is already registered. Make sure static className member is correct on JavaScript class ${entityClass.name}`;
31
- core_bentley_1.Logger.logError("core-frontend.classRegistry", errMsg);
32
- throw new Error(errMsg);
33
- }
34
- this._classMap.set(key, entityClass);
35
- }
36
- /** Generate a proxy Schema for a domain that has not been registered. */
37
- static generateProxySchema(domain, iModel) {
38
- const hasBehavior = iModel.withPreparedSqliteStatement(`
39
- SELECT NULL FROM [ec_CustomAttribute] [c]
40
- JOIN [ec_schema] [s] ON [s].[Id] = [c].[ContainerId]
41
- JOIN [ec_class] [e] ON [e].[Id] = [c].[ClassId]
42
- JOIN [ec_schema] [b] ON [e].[SchemaId] = [b].[Id]
43
- WHERE [c].[ContainerType] = 1 AND [s].[Name] = ? AND [b].[Name] || '.' || [e].[name] = ?`, (stmt) => {
44
- stmt.bindString(1, domain);
45
- stmt.bindString(2, "BisCore.SchemaHasBehavior");
46
- return stmt.step() === core_bentley_1.DbResult.BE_SQLITE_ROW;
47
- });
48
- const schemaClass = class extends Schema_1.Schema {
49
- static get schemaName() { return domain; }
50
- static get missingRequiredBehavior() { return hasBehavior; }
51
- };
52
- Schema_1.Schemas.registerSchema(schemaClass); // register the class before we return it.
53
- return schemaClass;
54
- }
55
- /** Generate a JavaScript class from Entity metadata.
56
- * @param entityMetaData The Entity metadata that defines the class
57
- */
58
- static generateClassForEntity(entityMetaData, iModel) {
59
- var _a, _b;
60
- const name = entityMetaData.ecclass.split(":");
61
- const domainName = name[0];
62
- const className = name[1];
63
- if (0 === entityMetaData.baseClasses.length) // metadata must contain a superclass
64
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadArg, `class ${name} has no superclass`);
65
- // make sure schema exists
66
- let schema = Schema_1.Schemas.getRegisteredSchema(domainName);
67
- if (undefined === schema)
68
- schema = this.generateProxySchema(domainName, iModel); // no schema found, create it too
69
- const superclass = this._classMap.get(entityMetaData.baseClasses[0].toLowerCase());
70
- if (undefined === superclass)
71
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.NotFound, `cannot find superclass for class ${name}`);
72
- // user defined class hierarchies may skip a class in the hierarchy, and therefore their JS base class cannot
73
- // be used to tell if there are any generated classes in the hierarchy
74
- let generatedClassHasNonGeneratedNonCoreAncestor = false;
75
- let currentSuperclass = superclass;
76
- const MAX_ITERS = 1000;
77
- for (let i = 0; i < MAX_ITERS; ++i) {
78
- if (currentSuperclass.schema.schemaName === "BisCore")
79
- break;
80
- if (!currentSuperclass.isGeneratedClass) {
81
- generatedClassHasNonGeneratedNonCoreAncestor = true;
82
- break;
83
- }
84
- const superclassMetaData = iModel.classMetaDataRegistry.find(currentSuperclass.classFullName);
85
- if (superclassMetaData === undefined)
86
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, `could not find the metadata for class '${currentSuperclass.name}', class metadata should be loaded by now`);
87
- const maybeNextSuperclass = this.getClass(superclassMetaData.baseClasses[0], iModel);
88
- if (maybeNextSuperclass === undefined)
89
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, `could not find the base class of '${currentSuperclass.name}', all generated classes must have a base class`);
90
- currentSuperclass = maybeNextSuperclass;
91
- }
92
- const generatedClass = (_b = class extends superclass {
93
- static get className() { return className; }
94
- static get isGeneratedClass() { return this.hasOwnProperty(isGeneratedClassTag); }
95
- },
96
- _a = isGeneratedClassTag,
97
- _b[_a] = true,
98
- _b);
99
- // the above creates an anonymous class. For help debugging, set the "constructor.name" property to be the same as the bisClassName.
100
- Object.defineProperty(generatedClass, "name", { get: () => className }); // this is the (only) way to change that readonly property.
101
- const navigationProps = Object.entries(entityMetaData.properties)
102
- .filter(([_propName, prop]) => prop.isNavigation)
103
- .map(([propName, _prop]) => propName);
104
- const isElement = (t) => t.is(Element_1.Element); // Entity.is check but with type information to avoid casts later
105
- // a class only gets an automatic `collectReferenceIds` implementation if:
106
- // - it derives from `BisCore:Element`
107
- // - it is not in the `BisCore` schema
108
- // - there are no ancestors with manually registered JS implementations, (excluding BisCore base classes)
109
- if (!generatedClassHasNonGeneratedNonCoreAncestor && isElement(superclass)) {
110
- Object.defineProperty(generatedClass.prototype, "collectReferenceIds", {
111
- // first prototype of `this` is its class
112
- value(referenceIds) {
113
- // eslint-disable-next-line @typescript-eslint/dot-notation
114
- const superImpl = superclass.prototype["collectReferenceIds"];
115
- superImpl.call(this, referenceIds);
116
- for (const navProp of navigationProps) {
117
- const relatedElem = this[navProp]; // cast to any since subclass can have any extensions
118
- if (!relatedElem || !core_bentley_1.Id64.isValid(relatedElem.id))
119
- continue;
120
- referenceIds.add(relatedElem.id);
121
- }
122
- },
123
- // defaults for methods on a prototype (required for sinon to stub out methods on tests)
124
- writable: true,
125
- configurable: true,
126
- });
127
- }
128
- // if the schema is a proxy for a domain with behavior, throw exceptions for all protected operations
129
- if (schema.missingRequiredBehavior) {
130
- const throwError = () => {
131
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.WrongHandler, `Schema [${domainName}] not registered, but is marked with SchemaHasBehavior`);
132
- };
133
- superclass.protectedOperations.forEach((operation) => generatedClass[operation] = throwError);
134
- }
135
- this.register(generatedClass, schema); // register it before returning
136
- return generatedClass;
137
- }
138
- /** Register all of the classes found in the given module that derive from Entity. See the example in [[Schema]]
139
- * @param moduleObj The module to search for subclasses of Entity
140
- * @param schema The schema for all found classes
141
- */
142
- static registerModule(moduleObj, schema) {
143
- for (const thisMember in moduleObj) { // eslint-disable-line guard-for-in
144
- const thisClass = moduleObj[thisMember];
145
- if (thisClass.prototype instanceof Entity_1.Entity)
146
- this.register(thisClass, schema);
147
- }
148
- }
149
- /**
150
- * This function fetches the specified Entity from the imodel, generates a JavaScript class for it, and registers the generated
151
- * class. This function also ensures that all of the base classes of the Entity exist and are registered.
152
- */
153
- static generateClass(classFullName, iModel) {
154
- const metadata = iModel.classMetaDataRegistry.find(classFullName);
155
- if (metadata === undefined || metadata.ecclass === undefined)
156
- throw this.makeMetaDataNotFoundError(classFullName);
157
- // Make sure we have all base classes registered.
158
- if (metadata.baseClasses && (0 !== metadata.baseClasses.length))
159
- this.getClass(metadata.baseClasses[0], iModel);
160
- // Now we can generate the class from the classDef.
161
- return this.generateClassForEntity(metadata, iModel);
162
- }
163
- /** Find a registered class by classFullName.
164
- * @param classFullName class to find
165
- * @param iModel The IModel that contains the class definitions
166
- * @returns The Entity class or undefined
167
- */
168
- static findRegisteredClass(classFullName) {
169
- return this._classMap.get(classFullName.toLowerCase());
170
- }
171
- /** Get the Entity class for the specified Entity className.
172
- * @param classFullName The full BIS class name of the Entity
173
- * @param iModel The IModel that contains the class definitions
174
- * @returns The Entity class
175
- */
176
- static getClass(classFullName, iModel) {
177
- const key = classFullName.toLowerCase();
178
- const ctor = this._classMap.get(key);
179
- return ctor ? ctor : this.generateClass(key, iModel);
180
- }
181
- /** Unregister a class, by name, if one is already registered.
182
- * This function is not normally needed, but is useful for cases where a generated *proxy* class needs to be replaced by the *real* class.
183
- * @param classFullName Name of the class to unregister
184
- * @return true if the class was unregistered
185
- * @internal
186
- */
187
- static unregisterCLass(classFullName) { return this._classMap.delete(classFullName.toLowerCase()); }
188
- /** Unregister all classes from a schema.
189
- * This function is not normally needed, but is useful for cases where a generated *proxy* schema needs to be replaced by the *real* schema.
190
- * @param schema Name of the schema to unregister
191
- * @internal
192
- */
193
- static unregisterClassesFrom(schema) {
194
- for (const entry of Array.from(this._classMap)) {
195
- if (entry[1].schema === schema)
196
- this.unregisterCLass(entry[0]);
197
- }
198
- }
199
- }
200
- exports.ClassRegistry = ClassRegistry;
201
- ClassRegistry._classMap = new Map();
202
- /**
203
- * A cache that records the mapping between class names and class metadata
204
- * @internal
205
- */
206
- class MetaDataRegistry {
207
- constructor() {
208
- this._registry = new Map();
209
- }
210
- /** Get the specified Entity metadata */
211
- find(classFullName) { return this._registry.get(classFullName.toLowerCase()); }
212
- /** Add metadata to the cache */
213
- add(classFullName, metaData) { this._registry.set(classFullName.toLowerCase(), metaData); }
214
- }
215
- exports.MetaDataRegistry = MetaDataRegistry;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Schema
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.MetaDataRegistry = exports.ClassRegistry = void 0;
11
+ const core_bentley_1 = require("@itwin/core-bentley");
12
+ const core_common_1 = require("@itwin/core-common");
13
+ const Entity_1 = require("./Entity");
14
+ const Element_1 = require("./Element");
15
+ const Schema_1 = require("./Schema");
16
+ const isGeneratedClassTag = Symbol("isGeneratedClassTag");
17
+ /** The mapping between a BIS class name (in the form "schema:class") and its JavaScript constructor function
18
+ * @public
19
+ */
20
+ class ClassRegistry {
21
+ /** @internal */
22
+ static isNotFoundError(err) { return (err instanceof core_common_1.IModelError) && (err.errorNumber === core_bentley_1.IModelStatus.NotFound); }
23
+ /** @internal */
24
+ static makeMetaDataNotFoundError(className) { return new core_common_1.IModelError(core_bentley_1.IModelStatus.NotFound, `metadata not found for ${className}`); }
25
+ /** @internal */
26
+ static register(entityClass, schema) {
27
+ entityClass.schema = schema;
28
+ const key = (`${schema.schemaName}:${entityClass.className}`).toLowerCase();
29
+ if (this._classMap.has(key)) {
30
+ const errMsg = `Class ${key} is already registered. Make sure static className member is correct on JavaScript class ${entityClass.name}`;
31
+ core_bentley_1.Logger.logError("core-frontend.classRegistry", errMsg);
32
+ throw new Error(errMsg);
33
+ }
34
+ this._classMap.set(key, entityClass);
35
+ }
36
+ /** Generate a proxy Schema for a domain that has not been registered. */
37
+ static generateProxySchema(domain, iModel) {
38
+ const hasBehavior = iModel.withPreparedSqliteStatement(`
39
+ SELECT NULL FROM [ec_CustomAttribute] [c]
40
+ JOIN [ec_schema] [s] ON [s].[Id] = [c].[ContainerId]
41
+ JOIN [ec_class] [e] ON [e].[Id] = [c].[ClassId]
42
+ JOIN [ec_schema] [b] ON [e].[SchemaId] = [b].[Id]
43
+ WHERE [c].[ContainerType] = 1 AND [s].[Name] = ? AND [b].[Name] || '.' || [e].[name] = ?`, (stmt) => {
44
+ stmt.bindString(1, domain);
45
+ stmt.bindString(2, "BisCore.SchemaHasBehavior");
46
+ return stmt.step() === core_bentley_1.DbResult.BE_SQLITE_ROW;
47
+ });
48
+ const schemaClass = class extends Schema_1.Schema {
49
+ static get schemaName() { return domain; }
50
+ static get missingRequiredBehavior() { return hasBehavior; }
51
+ };
52
+ Schema_1.Schemas.registerSchema(schemaClass); // register the class before we return it.
53
+ return schemaClass;
54
+ }
55
+ /** Generate a JavaScript class from Entity metadata.
56
+ * @param entityMetaData The Entity metadata that defines the class
57
+ */
58
+ static generateClassForEntity(entityMetaData, iModel) {
59
+ var _a, _b;
60
+ const name = entityMetaData.ecclass.split(":");
61
+ const domainName = name[0];
62
+ const className = name[1];
63
+ if (0 === entityMetaData.baseClasses.length) // metadata must contain a superclass
64
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadArg, `class ${name} has no superclass`);
65
+ // make sure schema exists
66
+ let schema = Schema_1.Schemas.getRegisteredSchema(domainName);
67
+ if (undefined === schema)
68
+ schema = this.generateProxySchema(domainName, iModel); // no schema found, create it too
69
+ const superclass = this._classMap.get(entityMetaData.baseClasses[0].toLowerCase());
70
+ if (undefined === superclass)
71
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.NotFound, `cannot find superclass for class ${name}`);
72
+ // user defined class hierarchies may skip a class in the hierarchy, and therefore their JS base class cannot
73
+ // be used to tell if there are any generated classes in the hierarchy
74
+ let generatedClassHasNonGeneratedNonCoreAncestor = false;
75
+ let currentSuperclass = superclass;
76
+ const MAX_ITERS = 1000;
77
+ for (let i = 0; i < MAX_ITERS; ++i) {
78
+ if (currentSuperclass.schema.schemaName === "BisCore")
79
+ break;
80
+ if (!currentSuperclass.isGeneratedClass) {
81
+ generatedClassHasNonGeneratedNonCoreAncestor = true;
82
+ break;
83
+ }
84
+ const superclassMetaData = iModel.classMetaDataRegistry.find(currentSuperclass.classFullName);
85
+ if (superclassMetaData === undefined)
86
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, `could not find the metadata for class '${currentSuperclass.name}', class metadata should be loaded by now`);
87
+ const maybeNextSuperclass = this.getClass(superclassMetaData.baseClasses[0], iModel);
88
+ if (maybeNextSuperclass === undefined)
89
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, `could not find the base class of '${currentSuperclass.name}', all generated classes must have a base class`);
90
+ currentSuperclass = maybeNextSuperclass;
91
+ }
92
+ const generatedClass = (_b = class extends superclass {
93
+ static get className() { return className; }
94
+ static get isGeneratedClass() { return this.hasOwnProperty(isGeneratedClassTag); }
95
+ },
96
+ _a = isGeneratedClassTag,
97
+ _b[_a] = true,
98
+ _b);
99
+ // the above creates an anonymous class. For help debugging, set the "constructor.name" property to be the same as the bisClassName.
100
+ Object.defineProperty(generatedClass, "name", { get: () => className }); // this is the (only) way to change that readonly property.
101
+ const navigationProps = Object.entries(entityMetaData.properties)
102
+ .filter(([_propName, prop]) => prop.isNavigation)
103
+ .map(([propName, _prop]) => propName);
104
+ const isElement = (t) => t.is(Element_1.Element); // Entity.is check but with type information to avoid casts later
105
+ // a class only gets an automatic `collectReferenceIds` implementation if:
106
+ // - it derives from `BisCore:Element`
107
+ // - it is not in the `BisCore` schema
108
+ // - there are no ancestors with manually registered JS implementations, (excluding BisCore base classes)
109
+ if (!generatedClassHasNonGeneratedNonCoreAncestor && isElement(superclass)) {
110
+ Object.defineProperty(generatedClass.prototype, "collectReferenceIds", {
111
+ // first prototype of `this` is its class
112
+ value(referenceIds) {
113
+ // eslint-disable-next-line @typescript-eslint/dot-notation
114
+ const superImpl = superclass.prototype["collectReferenceIds"];
115
+ superImpl.call(this, referenceIds);
116
+ for (const navProp of navigationProps) {
117
+ const relatedElem = this[navProp]; // cast to any since subclass can have any extensions
118
+ if (!relatedElem || !core_bentley_1.Id64.isValid(relatedElem.id))
119
+ continue;
120
+ referenceIds.add(relatedElem.id);
121
+ }
122
+ },
123
+ // defaults for methods on a prototype (required for sinon to stub out methods on tests)
124
+ writable: true,
125
+ configurable: true,
126
+ });
127
+ }
128
+ // if the schema is a proxy for a domain with behavior, throw exceptions for all protected operations
129
+ if (schema.missingRequiredBehavior) {
130
+ const throwError = () => {
131
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.WrongHandler, `Schema [${domainName}] not registered, but is marked with SchemaHasBehavior`);
132
+ };
133
+ superclass.protectedOperations.forEach((operation) => generatedClass[operation] = throwError);
134
+ }
135
+ this.register(generatedClass, schema); // register it before returning
136
+ return generatedClass;
137
+ }
138
+ /** Register all of the classes found in the given module that derive from Entity. See the example in [[Schema]]
139
+ * @param moduleObj The module to search for subclasses of Entity
140
+ * @param schema The schema for all found classes
141
+ */
142
+ static registerModule(moduleObj, schema) {
143
+ for (const thisMember in moduleObj) { // eslint-disable-line guard-for-in
144
+ const thisClass = moduleObj[thisMember];
145
+ if (thisClass.prototype instanceof Entity_1.Entity)
146
+ this.register(thisClass, schema);
147
+ }
148
+ }
149
+ /**
150
+ * This function fetches the specified Entity from the imodel, generates a JavaScript class for it, and registers the generated
151
+ * class. This function also ensures that all of the base classes of the Entity exist and are registered.
152
+ */
153
+ static generateClass(classFullName, iModel) {
154
+ const metadata = iModel.classMetaDataRegistry.find(classFullName);
155
+ if (metadata === undefined || metadata.ecclass === undefined)
156
+ throw this.makeMetaDataNotFoundError(classFullName);
157
+ // Make sure we have all base classes registered.
158
+ if (metadata.baseClasses && (0 !== metadata.baseClasses.length))
159
+ this.getClass(metadata.baseClasses[0], iModel);
160
+ // Now we can generate the class from the classDef.
161
+ return this.generateClassForEntity(metadata, iModel);
162
+ }
163
+ /** Find a registered class by classFullName.
164
+ * @param classFullName class to find
165
+ * @param iModel The IModel that contains the class definitions
166
+ * @returns The Entity class or undefined
167
+ */
168
+ static findRegisteredClass(classFullName) {
169
+ return this._classMap.get(classFullName.toLowerCase());
170
+ }
171
+ /** Get the Entity class for the specified Entity className.
172
+ * @param classFullName The full BIS class name of the Entity
173
+ * @param iModel The IModel that contains the class definitions
174
+ * @returns The Entity class
175
+ */
176
+ static getClass(classFullName, iModel) {
177
+ const key = classFullName.toLowerCase();
178
+ const ctor = this._classMap.get(key);
179
+ return ctor ? ctor : this.generateClass(key, iModel);
180
+ }
181
+ /** Unregister a class, by name, if one is already registered.
182
+ * This function is not normally needed, but is useful for cases where a generated *proxy* class needs to be replaced by the *real* class.
183
+ * @param classFullName Name of the class to unregister
184
+ * @return true if the class was unregistered
185
+ * @internal
186
+ */
187
+ static unregisterCLass(classFullName) { return this._classMap.delete(classFullName.toLowerCase()); }
188
+ /** Unregister all classes from a schema.
189
+ * This function is not normally needed, but is useful for cases where a generated *proxy* schema needs to be replaced by the *real* schema.
190
+ * @param schema Name of the schema to unregister
191
+ * @internal
192
+ */
193
+ static unregisterClassesFrom(schema) {
194
+ for (const entry of Array.from(this._classMap)) {
195
+ if (entry[1].schema === schema)
196
+ this.unregisterCLass(entry[0]);
197
+ }
198
+ }
199
+ }
200
+ exports.ClassRegistry = ClassRegistry;
201
+ ClassRegistry._classMap = new Map();
202
+ /**
203
+ * A cache that records the mapping between class names and class metadata
204
+ * @internal
205
+ */
206
+ class MetaDataRegistry {
207
+ constructor() {
208
+ this._registry = new Map();
209
+ }
210
+ /** Get the specified Entity metadata */
211
+ find(classFullName) { return this._registry.get(classFullName.toLowerCase()); }
212
+ /** Add metadata to the cache */
213
+ add(classFullName, metaData) { this._registry.set(classFullName.toLowerCase(), metaData); }
214
+ }
215
+ exports.MetaDataRegistry = MetaDataRegistry;
216
216
  //# sourceMappingURL=ClassRegistry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ClassRegistry.js","sourceRoot":"","sources":["../../src/ClassRegistry.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAEH,sDAAoF;AACpF,oDAAiF;AACjF,qCAAkC;AAClC,uCAAoC;AAEpC,qCAA2C;AAE3C,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAE1D;;GAEG;AACH,MAAa,aAAa;IAExB,gBAAgB;IACT,MAAM,CAAC,eAAe,CAAC,GAAQ,IAAI,OAAO,CAAC,GAAG,YAAY,yBAAW,CAAC,IAAI,CAAC,GAAG,CAAC,WAAW,KAAK,2BAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC/H,gBAAgB;IACT,MAAM,CAAC,yBAAyB,CAAC,SAAiB,IAAiB,OAAO,IAAI,yBAAW,CAAC,2BAAY,CAAC,QAAQ,EAAE,0BAA0B,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC;IACjK,gBAAgB;IACT,MAAM,CAAC,QAAQ,CAAC,WAA0B,EAAE,MAAqB;QACtE,WAAW,CAAC,MAAM,GAAG,MAAM,CAAC;QAC5B,MAAM,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,UAAU,IAAI,WAAW,CAAC,SAAS,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;QAC5E,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAC3B,MAAM,MAAM,GAAG,SAAS,GAAG,4FAA4F,WAAW,CAAC,IAAI,EAAE,CAAC;YAC1I,qBAAM,CAAC,QAAQ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QACvD,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YAC9C,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM;gBAC5B,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAClC;IACH,CAAC;;AA9MH,sCA+MC;AA9MyB,uBAAS,GAAG,IAAI,GAAG,EAAyB,CAAC;AAgNvE;;;GAGG;AACH,MAAa,gBAAgB;IAA7B;QACU,cAAS,GAAgC,IAAI,GAAG,EAA0B,CAAC;IAOrF,CAAC;IALC,wCAAwC;IACjC,IAAI,CAAC,aAAqB,IAAgC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC;IAE1H,gCAAgC;IACzB,GAAG,CAAC,aAAqB,EAAE,QAAwB,IAAU,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;CACjI;AARD,4CAQC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Schema\n */\n\nimport { DbResult, Id64, Id64Set, IModelStatus, Logger } from \"@itwin/core-bentley\";\nimport { EntityMetaData, IModelError, RelatedElement } from \"@itwin/core-common\";\nimport { Entity } from \"./Entity\";\nimport { Element } from \"./Element\";\nimport { IModelDb } from \"./IModelDb\";\nimport { Schema, Schemas } from \"./Schema\";\n\nconst isGeneratedClassTag = Symbol(\"isGeneratedClassTag\");\n\n/** The mapping between a BIS class name (in the form \"schema:class\") and its JavaScript constructor function\n * @public\n */\nexport class ClassRegistry {\n private static readonly _classMap = new Map<string, typeof Entity>();\n /** @internal */\n public static isNotFoundError(err: any) { return (err instanceof IModelError) && (err.errorNumber === IModelStatus.NotFound); }\n /** @internal */\n public static makeMetaDataNotFoundError(className: string): IModelError { return new IModelError(IModelStatus.NotFound, `metadata not found for ${className}`); }\n /** @internal */\n public static register(entityClass: typeof Entity, schema: typeof Schema) {\n entityClass.schema = schema;\n const key = (`${schema.schemaName}:${entityClass.className}`).toLowerCase();\n if (this._classMap.has(key)) {\n const errMsg = `Class ${key} is already registered. Make sure static className member is correct on JavaScript class ${entityClass.name}`;\n Logger.logError(\"core-frontend.classRegistry\", errMsg);\n throw new Error(errMsg);\n }\n\n this._classMap.set(key, entityClass);\n }\n\n /** Generate a proxy Schema for a domain that has not been registered. */\n private static generateProxySchema(domain: string, iModel: IModelDb): typeof Schema {\n const hasBehavior = iModel.withPreparedSqliteStatement(`\n SELECT NULL FROM [ec_CustomAttribute] [c]\n JOIN [ec_schema] [s] ON [s].[Id] = [c].[ContainerId]\n JOIN [ec_class] [e] ON [e].[Id] = [c].[ClassId]\n JOIN [ec_schema] [b] ON [e].[SchemaId] = [b].[Id]\n WHERE [c].[ContainerType] = 1 AND [s].[Name] = ? AND [b].[Name] || '.' || [e].[name] = ?`, (stmt) => {\n stmt.bindString(1, domain);\n stmt.bindString(2, \"BisCore.SchemaHasBehavior\");\n return stmt.step() === DbResult.BE_SQLITE_ROW;\n });\n\n const schemaClass = class extends Schema {\n public static override get schemaName() { return domain; }\n public static override get missingRequiredBehavior() { return hasBehavior; }\n };\n\n Schemas.registerSchema(schemaClass); // register the class before we return it.\n return schemaClass;\n }\n\n /** Generate a JavaScript class from Entity metadata.\n * @param entityMetaData The Entity metadata that defines the class\n */\n private static generateClassForEntity(entityMetaData: EntityMetaData, iModel: IModelDb): typeof Entity {\n const name = entityMetaData.ecclass.split(\":\");\n const domainName = name[0];\n const className = name[1];\n\n if (0 === entityMetaData.baseClasses.length) // metadata must contain a superclass\n throw new IModelError(IModelStatus.BadArg, `class ${name} has no superclass`);\n\n // make sure schema exists\n let schema = Schemas.getRegisteredSchema(domainName);\n if (undefined === schema)\n schema = this.generateProxySchema(domainName, iModel); // no schema found, create it too\n\n const superclass = this._classMap.get(entityMetaData.baseClasses[0].toLowerCase());\n if (undefined === superclass)\n throw new IModelError(IModelStatus.NotFound, `cannot find superclass for class ${name}`);\n\n // user defined class hierarchies may skip a class in the hierarchy, and therefore their JS base class cannot\n // be used to tell if there are any generated classes in the hierarchy\n let generatedClassHasNonGeneratedNonCoreAncestor = false;\n let currentSuperclass = superclass;\n const MAX_ITERS = 1000;\n for (let i = 0; i < MAX_ITERS; ++i) {\n if (currentSuperclass.schema.schemaName === \"BisCore\")\n break;\n\n if (!currentSuperclass.isGeneratedClass) {\n generatedClassHasNonGeneratedNonCoreAncestor = true;\n break;\n }\n const superclassMetaData = iModel.classMetaDataRegistry.find(currentSuperclass.classFullName);\n if (superclassMetaData === undefined)\n throw new IModelError(IModelStatus.BadSchema, `could not find the metadata for class '${currentSuperclass.name}', class metadata should be loaded by now`);\n const maybeNextSuperclass = this.getClass(superclassMetaData.baseClasses[0], iModel);\n if (maybeNextSuperclass === undefined)\n throw new IModelError(IModelStatus.BadSchema, `could not find the base class of '${currentSuperclass.name}', all generated classes must have a base class`);\n currentSuperclass = maybeNextSuperclass;\n }\n\n const generatedClass = class extends superclass {\n public static override get className() { return className; }\n private static [isGeneratedClassTag] = true;\n public static override get isGeneratedClass() { return this.hasOwnProperty(isGeneratedClassTag); }\n };\n\n // the above creates an anonymous class. For help debugging, set the \"constructor.name\" property to be the same as the bisClassName.\n Object.defineProperty(generatedClass, \"name\", { get: () => className }); // this is the (only) way to change that readonly property.\n\n const navigationProps = Object.entries(entityMetaData.properties)\n .filter(([_propName, prop]) => prop.isNavigation)\n .map(([propName, _prop]) => propName);\n\n const isElement = (t: typeof Entity): t is typeof Element => t.is(Element); // Entity.is check but with type information to avoid casts later\n\n // a class only gets an automatic `collectReferenceIds` implementation if:\n // - it derives from `BisCore:Element`\n // - it is not in the `BisCore` schema\n // - there are no ancestors with manually registered JS implementations, (excluding BisCore base classes)\n if (!generatedClassHasNonGeneratedNonCoreAncestor && isElement(superclass)) {\n Object.defineProperty(\n generatedClass.prototype,\n \"collectReferenceIds\",\n {\n // first prototype of `this` is its class\n value(this: typeof generatedClass, referenceIds: Id64Set) {\n // eslint-disable-next-line @typescript-eslint/dot-notation\n const superImpl = superclass.prototype[\"collectReferenceIds\"];\n superImpl.call(this, referenceIds);\n for (const navProp of navigationProps) {\n const relatedElem: RelatedElement | undefined = (this as any)[navProp]; // cast to any since subclass can have any extensions\n if (!relatedElem || !Id64.isValid(relatedElem.id))\n continue;\n\n referenceIds.add(relatedElem.id);\n }\n },\n // defaults for methods on a prototype (required for sinon to stub out methods on tests)\n writable: true,\n configurable: true,\n }\n );\n }\n\n // if the schema is a proxy for a domain with behavior, throw exceptions for all protected operations\n if (schema.missingRequiredBehavior) {\n const throwError = () => {\n throw new IModelError(IModelStatus.WrongHandler, `Schema [${domainName}] not registered, but is marked with SchemaHasBehavior`);\n };\n\n superclass.protectedOperations.forEach((operation) => (generatedClass as any)[operation] = throwError);\n }\n\n this.register(generatedClass, schema); // register it before returning\n return generatedClass;\n }\n\n /** Register all of the classes found in the given module that derive from Entity. See the example in [[Schema]]\n * @param moduleObj The module to search for subclasses of Entity\n * @param schema The schema for all found classes\n */\n public static registerModule(moduleObj: any, schema: typeof Schema) {\n for (const thisMember in moduleObj) { // eslint-disable-line guard-for-in\n const thisClass = moduleObj[thisMember];\n if (thisClass.prototype instanceof Entity)\n this.register(thisClass, schema);\n }\n }\n\n /**\n * This function fetches the specified Entity from the imodel, generates a JavaScript class for it, and registers the generated\n * class. This function also ensures that all of the base classes of the Entity exist and are registered.\n */\n private static generateClass(classFullName: string, iModel: IModelDb): typeof Entity {\n const metadata: EntityMetaData | undefined = iModel.classMetaDataRegistry.find(classFullName);\n if (metadata === undefined || metadata.ecclass === undefined)\n throw this.makeMetaDataNotFoundError(classFullName);\n\n // Make sure we have all base classes registered.\n if (metadata.baseClasses && (0 !== metadata.baseClasses.length))\n this.getClass(metadata.baseClasses[0], iModel);\n\n // Now we can generate the class from the classDef.\n return this.generateClassForEntity(metadata, iModel);\n }\n\n /** Find a registered class by classFullName.\n * @param classFullName class to find\n * @param iModel The IModel that contains the class definitions\n * @returns The Entity class or undefined\n */\n public static findRegisteredClass(classFullName: string): typeof Entity | undefined {\n return this._classMap.get(classFullName.toLowerCase());\n }\n\n /** Get the Entity class for the specified Entity className.\n * @param classFullName The full BIS class name of the Entity\n * @param iModel The IModel that contains the class definitions\n * @returns The Entity class\n */\n public static getClass(classFullName: string, iModel: IModelDb): typeof Entity {\n const key = classFullName.toLowerCase();\n const ctor = this._classMap.get(key);\n return ctor ? ctor : this.generateClass(key, iModel);\n }\n\n /** Unregister a class, by name, if one is already registered.\n * This function is not normally needed, but is useful for cases where a generated *proxy* class needs to be replaced by the *real* class.\n * @param classFullName Name of the class to unregister\n * @return true if the class was unregistered\n * @internal\n */\n public static unregisterCLass(classFullName: string) { return this._classMap.delete(classFullName.toLowerCase()); }\n /** Unregister all classes from a schema.\n * This function is not normally needed, but is useful for cases where a generated *proxy* schema needs to be replaced by the *real* schema.\n * @param schema Name of the schema to unregister\n * @internal\n */\n public static unregisterClassesFrom(schema: typeof Schema) {\n for (const entry of Array.from(this._classMap)) {\n if (entry[1].schema === schema)\n this.unregisterCLass(entry[0]);\n }\n }\n}\n\n/**\n * A cache that records the mapping between class names and class metadata\n * @internal\n */\nexport class MetaDataRegistry {\n private _registry: Map<string, EntityMetaData> = new Map<string, EntityMetaData>();\n\n /** Get the specified Entity metadata */\n public find(classFullName: string): EntityMetaData | undefined { return this._registry.get(classFullName.toLowerCase()); }\n\n /** Add metadata to the cache */\n public add(classFullName: string, metaData: EntityMetaData): void { this._registry.set(classFullName.toLowerCase(), metaData); }\n}\n"]}
1
+ {"version":3,"file":"ClassRegistry.js","sourceRoot":"","sources":["../../src/ClassRegistry.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;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AAS,GAAgC,IAAI,GAAG,EAA0B,CAAC;IAOrF,CAAC;IALC,wCAAwC;IACjC,IAAI,CAAC,aAAqB,IAAgC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC;IAE1H,gCAAgC;IACzB,GAAG,CAAC,aAAqB,EAAE,QAAwB,IAAU,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,aAAa,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;CACjI;AARD,4CAQC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Schema\r\n */\r\n\r\nimport { DbResult, Id64, Id64Set, IModelStatus, Logger } from \"@itwin/core-bentley\";\r\nimport { EntityMetaData, IModelError, RelatedElement } from \"@itwin/core-common\";\r\nimport { Entity } from \"./Entity\";\r\nimport { Element } from \"./Element\";\r\nimport { IModelDb } from \"./IModelDb\";\r\nimport { Schema, Schemas } from \"./Schema\";\r\n\r\nconst isGeneratedClassTag = Symbol(\"isGeneratedClassTag\");\r\n\r\n/** The mapping between a BIS class name (in the form \"schema:class\") and its JavaScript constructor function\r\n * @public\r\n */\r\nexport class ClassRegistry {\r\n private static readonly _classMap = new Map<string, typeof Entity>();\r\n /** @internal */\r\n public static isNotFoundError(err: any) { return (err instanceof IModelError) && (err.errorNumber === IModelStatus.NotFound); }\r\n /** @internal */\r\n public static makeMetaDataNotFoundError(className: string): IModelError { return new IModelError(IModelStatus.NotFound, `metadata not found for ${className}`); }\r\n /** @internal */\r\n public static register(entityClass: typeof Entity, schema: typeof Schema) {\r\n entityClass.schema = schema;\r\n const key = (`${schema.schemaName}:${entityClass.className}`).toLowerCase();\r\n if (this._classMap.has(key)) {\r\n const errMsg = `Class ${key} is already registered. Make sure static className member is correct on JavaScript class ${entityClass.name}`;\r\n Logger.logError(\"core-frontend.classRegistry\", errMsg);\r\n throw new Error(errMsg);\r\n }\r\n\r\n this._classMap.set(key, entityClass);\r\n }\r\n\r\n /** Generate a proxy Schema for a domain that has not been registered. */\r\n private static generateProxySchema(domain: string, iModel: IModelDb): typeof Schema {\r\n const hasBehavior = iModel.withPreparedSqliteStatement(`\r\n SELECT NULL FROM [ec_CustomAttribute] [c]\r\n JOIN [ec_schema] [s] ON [s].[Id] = [c].[ContainerId]\r\n JOIN [ec_class] [e] ON [e].[Id] = [c].[ClassId]\r\n JOIN [ec_schema] [b] ON [e].[SchemaId] = [b].[Id]\r\n WHERE [c].[ContainerType] = 1 AND [s].[Name] = ? AND [b].[Name] || '.' || [e].[name] = ?`, (stmt) => {\r\n stmt.bindString(1, domain);\r\n stmt.bindString(2, \"BisCore.SchemaHasBehavior\");\r\n return stmt.step() === DbResult.BE_SQLITE_ROW;\r\n });\r\n\r\n const schemaClass = class extends Schema {\r\n public static override get schemaName() { return domain; }\r\n public static override get missingRequiredBehavior() { return hasBehavior; }\r\n };\r\n\r\n Schemas.registerSchema(schemaClass); // register the class before we return it.\r\n return schemaClass;\r\n }\r\n\r\n /** Generate a JavaScript class from Entity metadata.\r\n * @param entityMetaData The Entity metadata that defines the class\r\n */\r\n private static generateClassForEntity(entityMetaData: EntityMetaData, iModel: IModelDb): typeof Entity {\r\n const name = entityMetaData.ecclass.split(\":\");\r\n const domainName = name[0];\r\n const className = name[1];\r\n\r\n if (0 === entityMetaData.baseClasses.length) // metadata must contain a superclass\r\n throw new IModelError(IModelStatus.BadArg, `class ${name} has no superclass`);\r\n\r\n // make sure schema exists\r\n let schema = Schemas.getRegisteredSchema(domainName);\r\n if (undefined === schema)\r\n schema = this.generateProxySchema(domainName, iModel); // no schema found, create it too\r\n\r\n const superclass = this._classMap.get(entityMetaData.baseClasses[0].toLowerCase());\r\n if (undefined === superclass)\r\n throw new IModelError(IModelStatus.NotFound, `cannot find superclass for class ${name}`);\r\n\r\n // user defined class hierarchies may skip a class in the hierarchy, and therefore their JS base class cannot\r\n // be used to tell if there are any generated classes in the hierarchy\r\n let generatedClassHasNonGeneratedNonCoreAncestor = false;\r\n let currentSuperclass = superclass;\r\n const MAX_ITERS = 1000;\r\n for (let i = 0; i < MAX_ITERS; ++i) {\r\n if (currentSuperclass.schema.schemaName === \"BisCore\")\r\n break;\r\n\r\n if (!currentSuperclass.isGeneratedClass) {\r\n generatedClassHasNonGeneratedNonCoreAncestor = true;\r\n break;\r\n }\r\n const superclassMetaData = iModel.classMetaDataRegistry.find(currentSuperclass.classFullName);\r\n if (superclassMetaData === undefined)\r\n throw new IModelError(IModelStatus.BadSchema, `could not find the metadata for class '${currentSuperclass.name}', class metadata should be loaded by now`);\r\n const maybeNextSuperclass = this.getClass(superclassMetaData.baseClasses[0], iModel);\r\n if (maybeNextSuperclass === undefined)\r\n throw new IModelError(IModelStatus.BadSchema, `could not find the base class of '${currentSuperclass.name}', all generated classes must have a base class`);\r\n currentSuperclass = maybeNextSuperclass;\r\n }\r\n\r\n const generatedClass = class extends superclass {\r\n public static override get className() { return className; }\r\n private static [isGeneratedClassTag] = true;\r\n public static override get isGeneratedClass() { return this.hasOwnProperty(isGeneratedClassTag); }\r\n };\r\n\r\n // the above creates an anonymous class. For help debugging, set the \"constructor.name\" property to be the same as the bisClassName.\r\n Object.defineProperty(generatedClass, \"name\", { get: () => className }); // this is the (only) way to change that readonly property.\r\n\r\n const navigationProps = Object.entries(entityMetaData.properties)\r\n .filter(([_propName, prop]) => prop.isNavigation)\r\n .map(([propName, _prop]) => propName);\r\n\r\n const isElement = (t: typeof Entity): t is typeof Element => t.is(Element); // Entity.is check but with type information to avoid casts later\r\n\r\n // a class only gets an automatic `collectReferenceIds` implementation if:\r\n // - it derives from `BisCore:Element`\r\n // - it is not in the `BisCore` schema\r\n // - there are no ancestors with manually registered JS implementations, (excluding BisCore base classes)\r\n if (!generatedClassHasNonGeneratedNonCoreAncestor && isElement(superclass)) {\r\n Object.defineProperty(\r\n generatedClass.prototype,\r\n \"collectReferenceIds\",\r\n {\r\n // first prototype of `this` is its class\r\n value(this: typeof generatedClass, referenceIds: Id64Set) {\r\n // eslint-disable-next-line @typescript-eslint/dot-notation\r\n const superImpl = superclass.prototype[\"collectReferenceIds\"];\r\n superImpl.call(this, referenceIds);\r\n for (const navProp of navigationProps) {\r\n const relatedElem: RelatedElement | undefined = (this as any)[navProp]; // cast to any since subclass can have any extensions\r\n if (!relatedElem || !Id64.isValid(relatedElem.id))\r\n continue;\r\n\r\n referenceIds.add(relatedElem.id);\r\n }\r\n },\r\n // defaults for methods on a prototype (required for sinon to stub out methods on tests)\r\n writable: true,\r\n configurable: true,\r\n }\r\n );\r\n }\r\n\r\n // if the schema is a proxy for a domain with behavior, throw exceptions for all protected operations\r\n if (schema.missingRequiredBehavior) {\r\n const throwError = () => {\r\n throw new IModelError(IModelStatus.WrongHandler, `Schema [${domainName}] not registered, but is marked with SchemaHasBehavior`);\r\n };\r\n\r\n superclass.protectedOperations.forEach((operation) => (generatedClass as any)[operation] = throwError);\r\n }\r\n\r\n this.register(generatedClass, schema); // register it before returning\r\n return generatedClass;\r\n }\r\n\r\n /** Register all of the classes found in the given module that derive from Entity. See the example in [[Schema]]\r\n * @param moduleObj The module to search for subclasses of Entity\r\n * @param schema The schema for all found classes\r\n */\r\n public static registerModule(moduleObj: any, schema: typeof Schema) {\r\n for (const thisMember in moduleObj) { // eslint-disable-line guard-for-in\r\n const thisClass = moduleObj[thisMember];\r\n if (thisClass.prototype instanceof Entity)\r\n this.register(thisClass, schema);\r\n }\r\n }\r\n\r\n /**\r\n * This function fetches the specified Entity from the imodel, generates a JavaScript class for it, and registers the generated\r\n * class. This function also ensures that all of the base classes of the Entity exist and are registered.\r\n */\r\n private static generateClass(classFullName: string, iModel: IModelDb): typeof Entity {\r\n const metadata: EntityMetaData | undefined = iModel.classMetaDataRegistry.find(classFullName);\r\n if (metadata === undefined || metadata.ecclass === undefined)\r\n throw this.makeMetaDataNotFoundError(classFullName);\r\n\r\n // Make sure we have all base classes registered.\r\n if (metadata.baseClasses && (0 !== metadata.baseClasses.length))\r\n this.getClass(metadata.baseClasses[0], iModel);\r\n\r\n // Now we can generate the class from the classDef.\r\n return this.generateClassForEntity(metadata, iModel);\r\n }\r\n\r\n /** Find a registered class by classFullName.\r\n * @param classFullName class to find\r\n * @param iModel The IModel that contains the class definitions\r\n * @returns The Entity class or undefined\r\n */\r\n public static findRegisteredClass(classFullName: string): typeof Entity | undefined {\r\n return this._classMap.get(classFullName.toLowerCase());\r\n }\r\n\r\n /** Get the Entity class for the specified Entity className.\r\n * @param classFullName The full BIS class name of the Entity\r\n * @param iModel The IModel that contains the class definitions\r\n * @returns The Entity class\r\n */\r\n public static getClass(classFullName: string, iModel: IModelDb): typeof Entity {\r\n const key = classFullName.toLowerCase();\r\n const ctor = this._classMap.get(key);\r\n return ctor ? ctor : this.generateClass(key, iModel);\r\n }\r\n\r\n /** Unregister a class, by name, if one is already registered.\r\n * This function is not normally needed, but is useful for cases where a generated *proxy* class needs to be replaced by the *real* class.\r\n * @param classFullName Name of the class to unregister\r\n * @return true if the class was unregistered\r\n * @internal\r\n */\r\n public static unregisterCLass(classFullName: string) { return this._classMap.delete(classFullName.toLowerCase()); }\r\n /** Unregister all classes from a schema.\r\n * This function is not normally needed, but is useful for cases where a generated *proxy* schema needs to be replaced by the *real* schema.\r\n * @param schema Name of the schema to unregister\r\n * @internal\r\n */\r\n public static unregisterClassesFrom(schema: typeof Schema) {\r\n for (const entry of Array.from(this._classMap)) {\r\n if (entry[1].schema === schema)\r\n this.unregisterCLass(entry[0]);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * A cache that records the mapping between class names and class metadata\r\n * @internal\r\n */\r\nexport class MetaDataRegistry {\r\n private _registry: Map<string, EntityMetaData> = new Map<string, EntityMetaData>();\r\n\r\n /** Get the specified Entity metadata */\r\n public find(classFullName: string): EntityMetaData | undefined { return this._registry.get(classFullName.toLowerCase()); }\r\n\r\n /** Add metadata to the cache */\r\n public add(classFullName: string, metaData: EntityMetaData): void { this._registry.set(classFullName.toLowerCase(), metaData); }\r\n}\r\n"]}