@itwin/core-backend 5.6.0-dev.1 → 5.6.0-dev.11

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 (38) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/lib/cjs/ChannelControl.d.ts +29 -0
  3. package/lib/cjs/ChannelControl.d.ts.map +1 -1
  4. package/lib/cjs/ChannelControl.js.map +1 -1
  5. package/lib/cjs/IModelDb.d.ts +152 -16
  6. package/lib/cjs/IModelDb.d.ts.map +1 -1
  7. package/lib/cjs/IModelDb.js +163 -76
  8. package/lib/cjs/IModelDb.js.map +1 -1
  9. package/lib/cjs/SqliteChangesetReader.d.ts +5 -0
  10. package/lib/cjs/SqliteChangesetReader.d.ts.map +1 -1
  11. package/lib/cjs/SqliteChangesetReader.js +7 -0
  12. package/lib/cjs/SqliteChangesetReader.js.map +1 -1
  13. package/lib/cjs/internal/ChannelAdmin.d.ts +2 -1
  14. package/lib/cjs/internal/ChannelAdmin.d.ts.map +1 -1
  15. package/lib/cjs/internal/ChannelAdmin.js +23 -0
  16. package/lib/cjs/internal/ChannelAdmin.js.map +1 -1
  17. package/lib/esm/ChannelControl.d.ts +29 -0
  18. package/lib/esm/ChannelControl.d.ts.map +1 -1
  19. package/lib/esm/ChannelControl.js.map +1 -1
  20. package/lib/esm/IModelDb.d.ts +152 -16
  21. package/lib/esm/IModelDb.d.ts.map +1 -1
  22. package/lib/esm/IModelDb.js +162 -75
  23. package/lib/esm/IModelDb.js.map +1 -1
  24. package/lib/esm/SqliteChangesetReader.d.ts +5 -0
  25. package/lib/esm/SqliteChangesetReader.d.ts.map +1 -1
  26. package/lib/esm/SqliteChangesetReader.js +7 -0
  27. package/lib/esm/SqliteChangesetReader.js.map +1 -1
  28. package/lib/esm/internal/ChannelAdmin.d.ts +2 -1
  29. package/lib/esm/internal/ChannelAdmin.d.ts.map +1 -1
  30. package/lib/esm/internal/ChannelAdmin.js +23 -0
  31. package/lib/esm/internal/ChannelAdmin.js.map +1 -1
  32. package/lib/esm/test/hubaccess/Rebase.test.js +98 -1
  33. package/lib/esm/test/hubaccess/Rebase.test.js.map +1 -1
  34. package/lib/esm/test/schema/SchemaImportCallbacks.test.d.ts +2 -0
  35. package/lib/esm/test/schema/SchemaImportCallbacks.test.d.ts.map +1 -0
  36. package/lib/esm/test/schema/SchemaImportCallbacks.test.js +916 -0
  37. package/lib/esm/test/schema/SchemaImportCallbacks.test.js.map +1 -0
  38. package/package.json +13 -13
@@ -7,7 +7,7 @@
7
7
  * @module iModels
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.StandaloneDb = exports.SnapshotDb = exports.BriefcaseDb = exports.IModelDb = exports.BriefcaseLocalValue = void 0;
10
+ exports.StandaloneDb = exports.SnapshotDb = exports.BriefcaseDb = exports.IModelDb = exports.DataTransformationStrategy = exports.BriefcaseLocalValue = void 0;
11
11
  const fs = require("fs");
12
12
  const path_1 = require("path");
13
13
  const touch = require("touch");
@@ -84,6 +84,24 @@ class IModelSettings extends SettingsImpl_1.SettingsImpl {
84
84
  yield* IModelHost_1.IModelHost.appWorkspace.settings.getSettingEntries(name);
85
85
  }
86
86
  }
87
+ /**
88
+ * Strategy for transforming data during schema import.
89
+ * @beta
90
+ */
91
+ var DataTransformationStrategy;
92
+ (function (DataTransformationStrategy) {
93
+ /** No data transformation will be performed after schema import. */
94
+ DataTransformationStrategy["None"] = "None";
95
+ /** Data transformation will be performed using a temporary snapshot created before schema import.
96
+ * Useful for complex transformations requiring full read access to complete pre-import state for lazy conversion.
97
+ * Note: Creates a complete copy of the briefcase file, which may be large.
98
+ */
99
+ DataTransformationStrategy["Snapshot"] = "Snapshot";
100
+ /** Data transformation will be performed using in-memory cached data created before schema import.
101
+ * Useful for lightweight transformations involving limited data.
102
+ */
103
+ DataTransformationStrategy["InMemory"] = "InMemory";
104
+ })(DataTransformationStrategy || (exports.DataTransformationStrategy = DataTransformationStrategy = {}));
87
105
  /** An iModel database file. The database file can either be a briefcase or a snapshot.
88
106
  * @see [Accessing iModels]($docs/learning/backend/AccessingIModels.md)
89
107
  * @see [About IModelDb]($docs/learning/backend/IModelDb.md)
@@ -238,11 +256,8 @@ class IModelDb extends core_common_1.IModel {
238
256
  * @note There are some reserve tablespace names that cannot be used. They are 'main', 'schema_sync_db', 'ecchange' & 'temp'
239
257
  * @param fileName IModel file name
240
258
  * @param alias identifier for the attached file. This identifier is used to access schema from the attached file. e.g. if alias is 'abc' then schema can be accessed using 'abc.MySchema.MyClass'
241
- *
242
- * *Example:*
243
- * ``` ts
244
- * [[include:IModelDb_attachDb.code]]
245
- * ```
259
+ * @example
260
+ * [[include:IModelDb_attachDb.code]]
246
261
  */
247
262
  attachDb(fileName, alias) {
248
263
  if (alias.toLowerCase() === "main" || alias.toLowerCase() === "schema_sync_db" || alias.toLowerCase() === "ecchange" || alias.toLowerCase() === "temp") {
@@ -255,10 +270,8 @@ class IModelDb extends core_common_1.IModel {
255
270
  * @note There are some reserve tablespace names that cannot be used. They are 'main', 'schema_sync_db', 'ecchange' & 'temp'
256
271
  * @param alias identifer that was used in the call to [[attachDb]]
257
272
  *
258
- * *Example:*
259
- * ``` ts
260
- * [[include:IModelDb_attachDb.code]]
261
- * ```
273
+ * @example [[include:IModelDb_attachDb.code]]
274
+ *
262
275
  */
263
276
  detachDb(alias) {
264
277
  if (alias.toLowerCase() === "main" || alias.toLowerCase() === "schema_sync_db" || alias.toLowerCase() === "ecchange" || alias.toLowerCase() === "temp") {
@@ -267,11 +280,15 @@ class IModelDb extends core_common_1.IModel {
267
280
  this.clearCaches();
268
281
  this[Symbols_1._nativeDb].detachDb(alias);
269
282
  }
270
- /** Close this IModel, if it is currently open, and save changes if it was opened in ReadWrite mode. */
271
- close() {
283
+ /** Close this IModel, if it is currently open, and save changes if it was opened in ReadWrite mode.
284
+ * @param options Options for closing the iModel.
285
+ */
286
+ close(options) {
272
287
  if (!this.isOpen)
273
288
  return; // don't continue if already closed
274
289
  this.beforeClose();
290
+ if (options?.optimize)
291
+ this.optimize();
275
292
  IModelDb._openDbs.delete(this._fileKey);
276
293
  this._workspace?.close();
277
294
  this.locks[Symbols_1._close]();
@@ -282,6 +299,40 @@ class IModelDb extends core_common_1.IModel {
282
299
  this.saveChanges();
283
300
  this[Symbols_1._nativeDb].closeFile();
284
301
  }
302
+ /** Optimize this iModel by vacuuming, and analyzing.
303
+ *
304
+ * @note This operation requires exclusive access to the database and may take some time on large files.
305
+ * @beta
306
+ */
307
+ optimize() {
308
+ // Vacuum to reclaim space and defragment
309
+ this.vacuum();
310
+ // Analyze to update statistics for query optimizer
311
+ this.analyze();
312
+ }
313
+ /**
314
+ * Vacuum the model to reclaim space and defragment.
315
+ * @throws [[IModelError]] if the iModel is not open or is read-only.
316
+ * @beta
317
+ */
318
+ vacuum() {
319
+ if (!this.isOpen || this.isReadonly)
320
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "IModel is not open or is read-only");
321
+ this[Symbols_1._nativeDb].clearECDbCache();
322
+ this[Symbols_1._nativeDb].vacuum();
323
+ }
324
+ /**
325
+ * Update SQLite query optimizer statistics for this iModel.
326
+ * This helps SQLite choose better query plans.
327
+ *
328
+ * @throws [[IModelError]] if the iModel is not open or is read-only.
329
+ * @beta
330
+ */
331
+ analyze() {
332
+ if (!this.isOpen || this.isReadonly)
333
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "IModel is not open or is read-only");
334
+ this[Symbols_1._nativeDb].analyze();
335
+ }
285
336
  /** @internal */
286
337
  async refreshContainerForRpc(_userAccessToken) { }
287
338
  /** Event called when the iModel is about to be closed. */
@@ -723,21 +774,68 @@ class IModelDb extends core_common_1.IModel {
723
774
  this.clearCaches();
724
775
  }
725
776
  }
726
- /** Import an ECSchema. On success, the schema definition is stored in the iModel.
727
- * This method is asynchronous (must be awaited) because, in the case where this IModelDb is a briefcase, this method first obtains the schema lock from the iModel server.
728
- * You must import a schema into an iModel before you can insert instances of the classes in that schema. See [[Element]]
729
- * @param schemaFileName array of Full paths to ECSchema.xml files to be imported.
730
- * @param {SchemaImportOptions} options - options during schema import.
731
- * @throws [[IModelError]] if the schema lock cannot be obtained or there is a problem importing the schema.
732
- * @note Changes are saved if importSchemas is successful and abandoned if not successful.
733
- * - You can use NativeLoggerCategory to turn on the native logs. You can also control [what exactly is logged by the loggers](https://www.itwinjs.org/learning/common/logging/#controlling-what-is-logged).
734
- * - See [Schema Versioning]($docs/bis/guide/schema-evolution/schema-versioning-and-generations.md) for more information on acceptable changes to schemas.
735
- * @note This method should not be called from {TxnManager.withIndirectTxnModeAsync} or {RebaseHandler.recompute}.
736
- * @see querySchemaVersion
777
+ /** Helper to clean up snapshot resources safely
778
+ * @internal
737
779
  */
738
- async importSchemas(schemaFileNames, options) {
739
- if (schemaFileNames.length === 0)
740
- return;
780
+ cleanupSnapshot(resources) {
781
+ if (resources.snapshot) {
782
+ const pathName = resources.snapshot.pathName;
783
+ resources.snapshot.close();
784
+ if (pathName && IModelJsFs_1.IModelJsFs.existsSync(pathName)) {
785
+ IModelJsFs_1.IModelJsFs.removeSync(pathName);
786
+ }
787
+ }
788
+ }
789
+ async preSchemaImportCallback(callback, context) {
790
+ const callbackResources = {
791
+ transformStrategy: DataTransformationStrategy.None,
792
+ };
793
+ try {
794
+ if (callback?.preSchemaImportCallback) {
795
+ const callbackResult = await callback.preSchemaImportCallback(context);
796
+ callbackResources.transformStrategy = callbackResult.transformStrategy;
797
+ if (callbackResult.transformStrategy === DataTransformationStrategy.Snapshot) {
798
+ // Create temporary snapshot file
799
+ const snapshotDb = SnapshotDb.createFrom(this, `${this.pathName}.snapshot-${Date.now()}`);
800
+ callbackResources.snapshot = snapshotDb;
801
+ }
802
+ else if (callbackResult.transformStrategy === DataTransformationStrategy.InMemory) {
803
+ if (callbackResult.cachedData === undefined) {
804
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "InMemory transform strategy requires cachedData to be provided.");
805
+ }
806
+ callbackResources.cachedData = callbackResult.cachedData;
807
+ }
808
+ }
809
+ }
810
+ catch (callbackError) {
811
+ this.abandonChanges();
812
+ this.cleanupSnapshot(callbackResources);
813
+ throw new core_common_1.IModelError(callbackError.errorNumber ?? core_bentley_1.IModelStatus.BadRequest, `Failed to execute preSchemaImportCallback: ${callbackError.message}`);
814
+ }
815
+ return callbackResources;
816
+ }
817
+ async postSchemaImportCallback(callback, context) {
818
+ if (context.resources.transformStrategy === DataTransformationStrategy.Snapshot && (context.resources.snapshot === undefined || !IModelJsFs_1.IModelJsFs.existsSync(context.resources.snapshot.pathName))) {
819
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Snapshot transform strategy requires a snapshot to be created");
820
+ }
821
+ if (context.resources.transformStrategy === DataTransformationStrategy.InMemory && context.resources.cachedData === undefined) {
822
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "InMemory transform strategy requires cachedData to be provided.");
823
+ }
824
+ try {
825
+ if (callback?.postSchemaImportCallback)
826
+ await callback.postSchemaImportCallback(context);
827
+ }
828
+ catch (callbackError) {
829
+ this.abandonChanges();
830
+ throw new core_common_1.IModelError(callbackError.errorNumber ?? core_bentley_1.IModelStatus.BadRequest, `Failed to execute postSchemaImportCallback: ${callbackError.message}`);
831
+ }
832
+ finally {
833
+ // Always clean up snapshot, whether success or error
834
+ this.cleanupSnapshot(context.resources);
835
+ }
836
+ }
837
+ /** Shared implementation for importing schemas from file or string. */
838
+ async importSchemasInternal(schemas, options, nativeImportOp) {
741
839
  if (this instanceof BriefcaseDb) {
742
840
  if (this.txns.rebaser.isRebasing) {
743
841
  throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot import schemas while rebasing");
@@ -746,13 +844,25 @@ class IModelDb extends core_common_1.IModel {
746
844
  throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot import schemas while in an indirect change scope");
747
845
  }
748
846
  }
847
+ if (options?.channelUpgrade) {
848
+ try {
849
+ await this.channels.upgradeChannel(options.channelUpgrade, this, options.data);
850
+ }
851
+ catch (error) {
852
+ this.abandonChanges();
853
+ throw error;
854
+ }
855
+ }
856
+ let preSchemaImportCallbackResult = { transformStrategy: DataTransformationStrategy.None };
857
+ if (options?.schemaImportCallbacks?.preSchemaImportCallback)
858
+ preSchemaImportCallbackResult = await this.preSchemaImportCallback(options.schemaImportCallbacks, { iModel: this, data: options.data, schemaData: schemas });
749
859
  const maybeCustomNativeContext = options?.ecSchemaXmlContext?.nativeContext;
750
860
  if (this[Symbols_1._nativeDb].schemaSyncEnabled()) {
751
861
  await SchemaSync_1.SchemaSync.withLockedAccess(this, { openMode: core_bentley_1.OpenMode.Readonly, operationName: "schema sync" }, async (syncAccess) => {
752
862
  const schemaSyncDbUri = syncAccess.getUri();
753
863
  this.saveChanges();
754
864
  try {
755
- this[Symbols_1._nativeDb].importSchemas(schemaFileNames, { schemaLockHeld: false, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
865
+ nativeImportOp(schemas, { schemaLockHeld: false, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
756
866
  }
757
867
  catch (outerErr) {
758
868
  if (core_bentley_1.DbResult.BE_SQLITE_ERROR_DataTransformRequired === outerErr.errorNumber) {
@@ -760,7 +870,7 @@ class IModelDb extends core_common_1.IModel {
760
870
  if (this[Symbols_1._nativeDb].getITwinId() !== core_bentley_1.Guid.empty)
761
871
  await this.acquireSchemaLock();
762
872
  try {
763
- this[Symbols_1._nativeDb].importSchemas(schemaFileNames, { schemaLockHeld: true, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
873
+ nativeImportOp(schemas, { schemaLockHeld: true, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
764
874
  }
765
875
  catch (innerErr) {
766
876
  throw new core_common_1.IModelError(innerErr.errorNumber, innerErr.message);
@@ -780,71 +890,48 @@ class IModelDb extends core_common_1.IModel {
780
890
  if (this[Symbols_1._nativeDb].getITwinId() !== core_bentley_1.Guid.empty) // if this iModel is associated with an iTwin, importing schema requires the schema lock
781
891
  await this.acquireSchemaLock();
782
892
  try {
783
- this[Symbols_1._nativeDb].importSchemas(schemaFileNames, nativeImportOptions);
893
+ nativeImportOp(schemas, nativeImportOptions);
784
894
  }
785
895
  catch (err) {
786
896
  throw new core_common_1.IModelError(err.errorNumber, err.message);
787
897
  }
788
898
  }
789
899
  this.clearCaches();
900
+ if (options?.schemaImportCallbacks?.postSchemaImportCallback)
901
+ await this.postSchemaImportCallback(options.schemaImportCallbacks, { iModel: this, resources: preSchemaImportCallbackResult, data: options.data });
902
+ }
903
+ /** Import an ECSchema. On success, the schema definition is stored in the iModel.
904
+ * This method is asynchronous (must be awaited) because, in the case where this IModelDb is a briefcase, this method first obtains the schema lock from the iModel server.
905
+ * You must import a schema into an iModel before you can insert instances of the classes in that schema. See [[Element]]
906
+ * @param schemaFileName array of Full paths to ECSchema.xml files to be imported.
907
+ * @param {SchemaImportOptions} options - options during schema import.
908
+ * @throws [[IModelError]] if the schema lock cannot be obtained or there is a problem importing the schema.
909
+ * @note Changes are saved if importSchemas is successful and abandoned if not successful.
910
+ * - You can use NativeLoggerCategory to turn on the native logs. You can also control [what exactly is logged by the loggers](https://www.itwinjs.org/learning/common/logging/#controlling-what-is-logged).
911
+ * - See [Schema Versioning]($docs/bis/guide/schema-evolution/schema-versioning-and-generations.md) for more information on acceptable changes to schemas.
912
+ * @note This method should not be called from {TxnManager.withIndirectTxnModeAsync} or {RebaseHandler.recompute}.
913
+ * @see querySchemaVersion
914
+ */
915
+ async importSchemas(schemaFileNames, options) {
916
+ if (schemaFileNames.length === 0)
917
+ return;
918
+ await this.importSchemasInternal(schemaFileNames, options, (schemas, importOptions) => this[Symbols_1._nativeDb].importSchemas(schemas, importOptions));
790
919
  }
791
920
  /** Import ECSchema(s) serialized to XML. On success, the schema definition is stored in the iModel.
792
921
  * This method is asynchronous (must be awaited) because, in the case where this IModelDb is a briefcase, this method first obtains the schema lock from the iModel server.
793
922
  * You must import a schema into an iModel before you can insert instances of the classes in that schema. See [[Element]]
794
923
  * @param serializedXmlSchemas The xml string(s) created from a serialized ECSchema.
924
+ * @param {SchemaImportOptions} options - options during schema import.
795
925
  * @throws [[IModelError]] if the schema lock cannot be obtained or there is a problem importing the schema.
796
926
  * @note Changes are saved if importSchemaStrings is successful and abandoned if not successful.
797
927
  * @note This method should not be called from {TxnManager.withIndirectTxnModeAsync} or {RebaseHandler.recompute}.
798
928
  * @see querySchemaVersion
799
929
  * @alpha
800
930
  */
801
- async importSchemaStrings(serializedXmlSchemas) {
931
+ async importSchemaStrings(serializedXmlSchemas, options) {
802
932
  if (serializedXmlSchemas.length === 0)
803
933
  return;
804
- if (this instanceof BriefcaseDb) {
805
- if (this.txns.rebaser.isRebasing) {
806
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot import schemas while rebasing");
807
- }
808
- if (this.txns.isIndirectChanges) {
809
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot import schemas while in an indirect change scope");
810
- }
811
- }
812
- if (this[Symbols_1._nativeDb].schemaSyncEnabled()) {
813
- await SchemaSync_1.SchemaSync.withLockedAccess(this, { openMode: core_bentley_1.OpenMode.Readonly, operationName: "schemaSync" }, async (syncAccess) => {
814
- const schemaSyncDbUri = syncAccess.getUri();
815
- this.saveChanges();
816
- try {
817
- this[Symbols_1._nativeDb].importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: false, schemaSyncDbUri });
818
- }
819
- catch (outerErr) {
820
- if (core_bentley_1.DbResult.BE_SQLITE_ERROR_DataTransformRequired === outerErr.errorNumber) {
821
- this.abandonChanges();
822
- if (this[Symbols_1._nativeDb].getITwinId() !== core_bentley_1.Guid.empty)
823
- await this.acquireSchemaLock();
824
- try {
825
- this[Symbols_1._nativeDb].importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true, schemaSyncDbUri });
826
- }
827
- catch (innerErr) {
828
- throw new core_common_1.IModelError(innerErr.errorNumber, innerErr.message);
829
- }
830
- }
831
- else {
832
- throw new core_common_1.IModelError(outerErr.errorNumber, outerErr.message);
833
- }
834
- }
835
- });
836
- }
837
- else {
838
- if (this.iTwinId && this.iTwinId !== core_bentley_1.Guid.empty) // if this iModel is associated with an iTwin, importing schema requires the schema lock
839
- await this.acquireSchemaLock();
840
- try {
841
- this[Symbols_1._nativeDb].importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true });
842
- }
843
- catch (err) {
844
- throw new core_common_1.IModelError(err.errorNumber, err.message);
845
- }
846
- }
847
- this.clearCaches();
934
+ await this.importSchemasInternal(serializedXmlSchemas, options, (schemas, importOptions) => this[Symbols_1._nativeDb].importXmlSchemas(schemas, importOptions));
848
935
  }
849
936
  /** Find an opened instance of any subclass of IModelDb, by filename
850
937
  * @note this method returns an IModelDb if the filename is open for *any* subclass of IModelDb
@@ -3214,11 +3301,11 @@ class BriefcaseDb extends IModelDb {
3214
3301
  });
3215
3302
  this.txns._onChangesPushed(this.changeset);
3216
3303
  }
3217
- close() {
3304
+ close(options) {
3218
3305
  if (this.isBriefcase && this.isOpen && !this.isReadonly && this.txns.rebaser.inProgress()) {
3219
3306
  this.abandonChanges();
3220
3307
  }
3221
- super.close();
3308
+ super.close(options);
3222
3309
  this.onClosed.raiseEvent();
3223
3310
  }
3224
3311
  }