@itwin/core-backend 3.6.0-dev.60 → 3.6.0-dev.65
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.
- package/CHANGELOG.md +16 -1
- package/lib/cjs/AliCloudStorageService.d.ts +23 -23
- package/lib/cjs/AliCloudStorageService.js +71 -71
- package/lib/cjs/AliCloudStorageService.js.map +1 -1
- package/lib/cjs/BackendHubAccess.d.ts +224 -224
- package/lib/cjs/BackendHubAccess.js +41 -41
- package/lib/cjs/BackendHubAccess.js.map +1 -1
- package/lib/cjs/BackendLoggerCategory.d.ts +91 -91
- package/lib/cjs/BackendLoggerCategory.js +99 -99
- package/lib/cjs/BackendLoggerCategory.js.map +1 -1
- package/lib/cjs/BisCoreSchema.d.ts +21 -21
- package/lib/cjs/BisCoreSchema.js +60 -60
- package/lib/cjs/BisCoreSchema.js.map +1 -1
- package/lib/cjs/BriefcaseManager.d.ts +170 -170
- package/lib/cjs/BriefcaseManager.js +414 -414
- package/lib/cjs/BriefcaseManager.js.map +1 -1
- package/lib/cjs/Category.d.ts +153 -153
- package/lib/cjs/Category.js +243 -243
- package/lib/cjs/Category.js.map +1 -1
- package/lib/cjs/ChangeSummaryManager.d.ts +187 -187
- package/lib/cjs/ChangeSummaryManager.js +447 -447
- package/lib/cjs/ChangeSummaryManager.js.map +1 -1
- package/lib/cjs/ChangedElementsDb.d.ts +99 -99
- package/lib/cjs/ChangedElementsDb.js +165 -165
- package/lib/cjs/ChangedElementsDb.js.map +1 -1
- package/lib/cjs/ChangedElementsManager.d.ts +39 -39
- package/lib/cjs/ChangedElementsManager.js +88 -88
- package/lib/cjs/ChangedElementsManager.js.map +1 -1
- package/lib/cjs/CheckpointManager.d.ts +121 -121
- package/lib/cjs/CheckpointManager.js +349 -349
- package/lib/cjs/CheckpointManager.js.map +1 -1
- package/lib/cjs/ClassRegistry.d.ts +79 -79
- package/lib/cjs/ClassRegistry.js +250 -250
- package/lib/cjs/ClassRegistry.js.map +1 -1
- package/lib/cjs/CloudSqlite.d.ts +350 -350
- package/lib/cjs/CloudSqlite.js +153 -153
- package/lib/cjs/CloudSqlite.js.map +1 -1
- package/lib/cjs/CloudStorageBackend.d.ts +58 -58
- package/lib/cjs/CloudStorageBackend.js +173 -173
- package/lib/cjs/CloudStorageBackend.js.map +1 -1
- package/lib/cjs/CodeService.d.ts +396 -396
- package/lib/cjs/CodeService.js +63 -63
- package/lib/cjs/CodeService.js.map +1 -1
- package/lib/cjs/CodeSpecs.d.ts +55 -55
- package/lib/cjs/CodeSpecs.js +124 -124
- package/lib/cjs/CodeSpecs.js.map +1 -1
- package/lib/cjs/ConcurrentQuery.d.ts +13 -13
- package/lib/cjs/ConcurrentQuery.js +36 -36
- package/lib/cjs/ConcurrentQuery.js.map +1 -1
- package/lib/cjs/CustomViewState3dCreator.d.ts +21 -21
- package/lib/cjs/CustomViewState3dCreator.js +81 -81
- package/lib/cjs/CustomViewState3dCreator.js.map +1 -1
- package/lib/cjs/DevTools.d.ts +71 -71
- package/lib/cjs/DevTools.js +154 -154
- package/lib/cjs/DevTools.js.map +1 -1
- package/lib/cjs/DisplayStyle.d.ts +106 -106
- package/lib/cjs/DisplayStyle.js +256 -256
- package/lib/cjs/DisplayStyle.js.map +1 -1
- package/lib/cjs/ECDb.d.ts +191 -191
- package/lib/cjs/ECDb.js +350 -350
- package/lib/cjs/ECDb.js.map +1 -1
- package/lib/cjs/ECSchemaXmlContext.d.ts +18 -18
- package/lib/cjs/ECSchemaXmlContext.js +41 -41
- package/lib/cjs/ECSchemaXmlContext.js.map +1 -1
- package/lib/cjs/ECSqlStatement.d.ts +473 -473
- package/lib/cjs/ECSqlStatement.js +791 -791
- package/lib/cjs/ECSqlStatement.js.map +1 -1
- package/lib/cjs/Element.d.ts +1109 -1109
- package/lib/cjs/Element.js +1427 -1427
- package/lib/cjs/Element.js.map +1 -1
- package/lib/cjs/ElementAspect.d.ts +179 -179
- package/lib/cjs/ElementAspect.js +211 -211
- package/lib/cjs/ElementAspect.js.map +1 -1
- package/lib/cjs/ElementGraphics.d.ts +9 -9
- package/lib/cjs/ElementGraphics.js +41 -41
- package/lib/cjs/ElementGraphics.js.map +1 -1
- package/lib/cjs/ElementTreeWalker.d.ts +162 -162
- package/lib/cjs/ElementTreeWalker.js +409 -409
- package/lib/cjs/ElementTreeWalker.js.map +1 -1
- package/lib/cjs/Entity.d.ts +106 -106
- package/lib/cjs/Entity.js +134 -134
- package/lib/cjs/Entity.js.map +1 -1
- package/lib/cjs/EntityReferences.d.ts +49 -49
- package/lib/cjs/EntityReferences.js +95 -95
- package/lib/cjs/EntityReferences.js.map +1 -1
- package/lib/cjs/ExportGraphics.d.ts +317 -317
- package/lib/cjs/ExportGraphics.js +208 -208
- package/lib/cjs/ExportGraphics.js.map +1 -1
- package/lib/cjs/ExternalSource.d.ts +96 -96
- package/lib/cjs/ExternalSource.js +131 -131
- package/lib/cjs/ExternalSource.js.map +1 -1
- package/lib/cjs/GeoCoordConfig.d.ts +19 -19
- package/lib/cjs/GeoCoordConfig.js +85 -85
- package/lib/cjs/GeoCoordConfig.js.map +1 -1
- package/lib/cjs/GeometrySummary.d.ts +4 -4
- package/lib/cjs/GeometrySummary.js +380 -380
- package/lib/cjs/GeometrySummary.js.map +1 -1
- package/lib/cjs/HubMock.d.ts +94 -94
- package/lib/cjs/HubMock.js +222 -222
- package/lib/cjs/HubMock.js.map +1 -1
- package/lib/cjs/IModelCloneContext.d.ts +8 -8
- package/lib/cjs/IModelCloneContext.js +15 -15
- package/lib/cjs/IModelCloneContext.js.map +1 -1
- package/lib/cjs/IModelDb.d.ts +1196 -1196
- package/lib/cjs/IModelDb.js +2392 -2392
- package/lib/cjs/IModelDb.js.map +1 -1
- package/lib/cjs/IModelElementCloneContext.d.ts +89 -89
- package/lib/cjs/IModelElementCloneContext.js +159 -159
- package/lib/cjs/IModelElementCloneContext.js.map +1 -1
- package/lib/cjs/IModelHost.d.ts +369 -369
- package/lib/cjs/IModelHost.js +496 -496
- package/lib/cjs/IModelHost.js.map +1 -1
- package/lib/cjs/IModelJsFs.d.ts +62 -62
- package/lib/cjs/IModelJsFs.js +140 -140
- package/lib/cjs/IModelJsFs.js.map +1 -1
- package/lib/cjs/IpcHost.d.ts +104 -104
- package/lib/cjs/IpcHost.js +278 -278
- package/lib/cjs/IpcHost.js.map +1 -1
- package/lib/cjs/LineStyle.d.ts +311 -311
- package/lib/cjs/LineStyle.js +295 -295
- package/lib/cjs/LineStyle.js.map +1 -1
- package/lib/cjs/LocalHub.d.ts +162 -162
- package/lib/cjs/LocalHub.js +585 -585
- package/lib/cjs/LocalHub.js.map +1 -1
- package/lib/cjs/LocalhostIpcHost.d.ts +20 -20
- package/lib/cjs/LocalhostIpcHost.js +81 -81
- package/lib/cjs/LocalhostIpcHost.js.map +1 -1
- package/lib/cjs/Material.d.ts +131 -131
- package/lib/cjs/Material.js +181 -181
- package/lib/cjs/Material.js.map +1 -1
- package/lib/cjs/Model.d.ts +392 -392
- package/lib/cjs/Model.js +485 -485
- package/lib/cjs/Model.js.map +1 -1
- package/lib/cjs/NativeAppStorage.d.ts +51 -51
- package/lib/cjs/NativeAppStorage.js +202 -202
- package/lib/cjs/NativeAppStorage.js.map +1 -1
- package/lib/cjs/NativeHost.d.ts +52 -52
- package/lib/cjs/NativeHost.js +163 -163
- package/lib/cjs/NativeHost.js.map +1 -1
- package/lib/cjs/NavigationRelationship.d.ts +156 -156
- package/lib/cjs/NavigationRelationship.js +225 -225
- package/lib/cjs/NavigationRelationship.js.map +1 -1
- package/lib/cjs/PromiseMemoizer.d.ts +51 -51
- package/lib/cjs/PromiseMemoizer.js +101 -101
- package/lib/cjs/PromiseMemoizer.js.map +1 -1
- package/lib/cjs/PropertyStore.d.ts +132 -132
- package/lib/cjs/PropertyStore.js +11 -11
- package/lib/cjs/PropertyStore.js.map +1 -1
- package/lib/cjs/Relationship.d.ts +416 -416
- package/lib/cjs/Relationship.js +494 -494
- package/lib/cjs/Relationship.js.map +1 -1
- package/lib/cjs/RpcBackend.d.ts +5 -5
- package/lib/cjs/RpcBackend.d.ts.map +1 -1
- package/lib/cjs/RpcBackend.js +32 -31
- package/lib/cjs/RpcBackend.js.map +1 -1
- package/lib/cjs/SQLiteDb.d.ts +219 -219
- package/lib/cjs/SQLiteDb.js +214 -214
- package/lib/cjs/SQLiteDb.js.map +1 -1
- package/lib/cjs/Schema.d.ts +55 -55
- package/lib/cjs/Schema.js +83 -83
- package/lib/cjs/Schema.js.map +1 -1
- package/lib/cjs/ServerBasedLocks.d.ts +57 -57
- package/lib/cjs/ServerBasedLocks.js +168 -168
- package/lib/cjs/ServerBasedLocks.js.map +1 -1
- package/lib/cjs/SqliteStatement.d.ts +360 -360
- package/lib/cjs/SqliteStatement.js +586 -586
- package/lib/cjs/SqliteStatement.js.map +1 -1
- package/lib/cjs/Texture.d.ts +58 -58
- package/lib/cjs/Texture.js +82 -82
- package/lib/cjs/Texture.js.map +1 -1
- package/lib/cjs/TileStorage.d.ts +18 -18
- package/lib/cjs/TileStorage.js +90 -90
- package/lib/cjs/TileStorage.js.map +1 -1
- package/lib/cjs/TxnManager.d.ts +202 -202
- package/lib/cjs/TxnManager.js +356 -356
- package/lib/cjs/TxnManager.js.map +1 -1
- package/lib/cjs/ViewDefinition.d.ts +439 -439
- package/lib/cjs/ViewDefinition.js +646 -646
- package/lib/cjs/ViewDefinition.js.map +1 -1
- package/lib/cjs/ViewStateHydrator.d.ts +13 -13
- package/lib/cjs/ViewStateHydrator.js +103 -103
- package/lib/cjs/ViewStateHydrator.js.map +1 -1
- package/lib/cjs/assets/IModelChange.02.00.00.ecschema.xml +90 -90
- package/lib/cjs/assets/Settings/Schemas/Cloud.Schema.json +67 -67
- package/lib/cjs/assets/Settings/Schemas/Gcs.schema.json +31 -31
- package/lib/cjs/assets/Settings/Schemas/Workspace.Schema.json +52 -52
- package/lib/cjs/assets/Settings/backend.setting.json5 +138 -138
- package/lib/cjs/core-backend.d.ts +184 -184
- package/lib/cjs/core-backend.js +204 -204
- package/lib/cjs/core-backend.js.map +1 -1
- package/lib/cjs/domains/FunctionalElements.d.ts +101 -101
- package/lib/cjs/domains/FunctionalElements.js +145 -145
- package/lib/cjs/domains/FunctionalElements.js.map +1 -1
- package/lib/cjs/domains/FunctionalSchema.d.ts +13 -13
- package/lib/cjs/domains/FunctionalSchema.js +40 -40
- package/lib/cjs/domains/FunctionalSchema.js.map +1 -1
- package/lib/cjs/domains/GenericElements.d.ts +177 -177
- package/lib/cjs/domains/GenericElements.js +263 -263
- package/lib/cjs/domains/GenericElements.js.map +1 -1
- package/lib/cjs/domains/GenericSchema.d.ts +10 -10
- package/lib/cjs/domains/GenericSchema.js +28 -28
- package/lib/cjs/domains/GenericSchema.js.map +1 -1
- package/lib/cjs/rpc/multipart.d.ts +11 -11
- package/lib/cjs/rpc/multipart.d.ts.map +1 -1
- package/lib/cjs/rpc/multipart.js +71 -70
- package/lib/cjs/rpc/multipart.js.map +1 -1
- package/lib/cjs/rpc/tracing.d.ts +28 -28
- package/lib/cjs/rpc/tracing.d.ts.map +1 -1
- package/lib/cjs/rpc/tracing.js +74 -73
- package/lib/cjs/rpc/tracing.js.map +1 -1
- package/lib/cjs/rpc/web/logging.d.ts +9 -9
- package/lib/cjs/rpc/web/logging.d.ts.map +1 -1
- package/lib/cjs/rpc/web/logging.js +43 -42
- package/lib/cjs/rpc/web/logging.js.map +1 -1
- package/lib/cjs/rpc/web/request.d.ts +6 -6
- package/lib/cjs/rpc/web/request.d.ts.map +1 -1
- package/lib/cjs/rpc/web/request.js +74 -73
- package/lib/cjs/rpc/web/request.js.map +1 -1
- package/lib/cjs/rpc/web/response.d.ts +6 -6
- package/lib/cjs/rpc/web/response.d.ts.map +1 -1
- package/lib/cjs/rpc/web/response.js +106 -105
- package/lib/cjs/rpc/web/response.js.map +1 -1
- package/lib/cjs/rpc-impl/DevToolsRpcImpl.d.ts +15 -15
- package/lib/cjs/rpc-impl/DevToolsRpcImpl.js +32 -32
- package/lib/cjs/rpc-impl/DevToolsRpcImpl.js.map +1 -1
- package/lib/cjs/rpc-impl/IModelReadRpcImpl.d.ts +47 -47
- package/lib/cjs/rpc-impl/IModelReadRpcImpl.d.ts.map +1 -1
- package/lib/cjs/rpc-impl/IModelReadRpcImpl.js +294 -291
- package/lib/cjs/rpc-impl/IModelReadRpcImpl.js.map +1 -1
- package/lib/cjs/rpc-impl/IModelTileRpcImpl.d.ts +22 -22
- package/lib/cjs/rpc-impl/IModelTileRpcImpl.d.ts.map +1 -1
- package/lib/cjs/rpc-impl/IModelTileRpcImpl.js +205 -202
- package/lib/cjs/rpc-impl/IModelTileRpcImpl.js.map +1 -1
- package/lib/cjs/rpc-impl/RpcBriefcaseUtility.d.ts +36 -36
- package/lib/cjs/rpc-impl/RpcBriefcaseUtility.js +168 -168
- package/lib/cjs/rpc-impl/RpcBriefcaseUtility.js.map +1 -1
- package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.d.ts +16 -16
- package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.js +43 -43
- package/lib/cjs/rpc-impl/SnapshotIModelRpcImpl.js.map +1 -1
- package/lib/cjs/rpc-impl/WipRpcImpl.d.ts +15 -15
- package/lib/cjs/rpc-impl/WipRpcImpl.js +37 -37
- package/lib/cjs/rpc-impl/WipRpcImpl.js.map +1 -1
- package/lib/cjs/workspace/Settings.d.ts +186 -186
- package/lib/cjs/workspace/Settings.js +155 -155
- package/lib/cjs/workspace/Settings.js.map +1 -1
- package/lib/cjs/workspace/SettingsSchemas.d.ts +75 -75
- package/lib/cjs/workspace/SettingsSchemas.js +191 -191
- package/lib/cjs/workspace/SettingsSchemas.js.map +1 -1
- package/lib/cjs/workspace/Workspace.d.ts +414 -414
- package/lib/cjs/workspace/Workspace.js +543 -543
- package/lib/cjs/workspace/Workspace.js.map +1 -1
- package/package.json +11 -11
|
@@ -1,544 +1,544 @@
|
|
|
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 Workspace
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.EditableWorkspaceDb = exports.ITwinWorkspaceDb = exports.ITwinWorkspaceContainer = exports.ITwinWorkspace = exports.WorkspaceSetting = void 0;
|
|
11
|
-
const crypto_1 = require("crypto");
|
|
12
|
-
const fs = require("fs-extra");
|
|
13
|
-
const path_1 = require("path");
|
|
14
|
-
const semver = require("semver");
|
|
15
|
-
const core_bentley_1 = require("@itwin/core-bentley");
|
|
16
|
-
const core_common_1 = require("@itwin/core-common");
|
|
17
|
-
const CloudSqlite_1 = require("../CloudSqlite");
|
|
18
|
-
const IModelHost_1 = require("../IModelHost");
|
|
19
|
-
const IModelJsFs_1 = require("../IModelJsFs");
|
|
20
|
-
const SQLiteDb_1 = require("../SQLiteDb");
|
|
21
|
-
const Settings_1 = require("./Settings");
|
|
22
|
-
const SettingsSchemas_1 = require("./SettingsSchemas");
|
|
23
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
24
|
-
// cspell:ignore rowid primarykey julianday
|
|
25
|
-
/** The Settings used by Workspace api
|
|
26
|
-
* @beta
|
|
27
|
-
*/
|
|
28
|
-
exports.WorkspaceSetting = {
|
|
29
|
-
Accounts: "cloud/accounts",
|
|
30
|
-
Containers: "cloud/containers",
|
|
31
|
-
Databases: "workspace/databases",
|
|
32
|
-
};
|
|
33
|
-
/** @internal */
|
|
34
|
-
class ITwinWorkspace {
|
|
35
|
-
constructor(settings, opts) {
|
|
36
|
-
var _a;
|
|
37
|
-
this._containers = new Map();
|
|
38
|
-
this.settings = settings;
|
|
39
|
-
this.containerDir = (_a = opts === null || opts === void 0 ? void 0 : opts.containerDir) !== null && _a !== void 0 ? _a : (0, path_1.join)(IModelHost_1.IModelHost.cacheDir, "Workspace");
|
|
40
|
-
this._cloudCache = opts === null || opts === void 0 ? void 0 : opts.testCloudCache;
|
|
41
|
-
let settingsFiles = opts === null || opts === void 0 ? void 0 : opts.settingsFiles;
|
|
42
|
-
if (settingsFiles) {
|
|
43
|
-
if (typeof settingsFiles === "string")
|
|
44
|
-
settingsFiles = [settingsFiles];
|
|
45
|
-
settingsFiles.forEach((file) => settings.addFile(file, Settings_1.SettingsPriority.application));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
get cloudCache() {
|
|
49
|
-
if (undefined === this._cloudCache)
|
|
50
|
-
this._cloudCache = ITwinWorkspace.getSharedCloudCache();
|
|
51
|
-
return this._cloudCache;
|
|
52
|
-
}
|
|
53
|
-
static getSharedCloudCache() {
|
|
54
|
-
if (undefined === this._sharedCloudCache) {
|
|
55
|
-
const rootDir = (0, path_1.join)(IModelHost_1.IModelHost.cacheDir, "Workspace", "cloud");
|
|
56
|
-
IModelJsFs_1.IModelJsFs.recursiveMkDirSync(rootDir);
|
|
57
|
-
this._sharedCloudCache = CloudSqlite_1.CloudSqlite.createCloudCache({ rootDir, cacheSize: "20G", name: "workspace" });
|
|
58
|
-
}
|
|
59
|
-
return this._sharedCloudCache;
|
|
60
|
-
}
|
|
61
|
-
static finalize() {
|
|
62
|
-
if (this._sharedCloudCache) {
|
|
63
|
-
this._sharedCloudCache.destroy();
|
|
64
|
-
this._sharedCloudCache = undefined;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
addContainer(toAdd) {
|
|
68
|
-
if (undefined !== this._containers.get(toAdd.id))
|
|
69
|
-
throw new Error("container already exists in workspace");
|
|
70
|
-
this._containers.set(toAdd.id, toAdd);
|
|
71
|
-
}
|
|
72
|
-
findContainer(containerId) {
|
|
73
|
-
return this._containers.get(containerId);
|
|
74
|
-
}
|
|
75
|
-
getContainer(props, account) {
|
|
76
|
-
var _a;
|
|
77
|
-
return (_a = this.findContainer(props.containerId)) !== null && _a !== void 0 ? _a : new ITwinWorkspaceContainer(this, props, account);
|
|
78
|
-
}
|
|
79
|
-
getWorkspaceDbFromProps(dbProps, containerProps, account) {
|
|
80
|
-
return this.getContainer(containerProps, account).getWorkspaceDb(dbProps);
|
|
81
|
-
}
|
|
82
|
-
async getWorkspaceDb(dbAlias, tokenFunc) {
|
|
83
|
-
const dbProps = this.resolveDatabase(dbAlias);
|
|
84
|
-
const containerProps = this.resolveContainer(dbProps.containerName);
|
|
85
|
-
const account = containerProps.accountName !== "" ? this.resolveAccount(containerProps.accountName) : undefined;
|
|
86
|
-
let container = this.findContainer(containerProps.containerId);
|
|
87
|
-
if (undefined === container) {
|
|
88
|
-
if (tokenFunc && account)
|
|
89
|
-
containerProps.accessToken = await tokenFunc(containerProps, account);
|
|
90
|
-
container = this.getContainer(containerProps, account);
|
|
91
|
-
}
|
|
92
|
-
return container === null || container === void 0 ? void 0 : container.getWorkspaceDb(dbProps);
|
|
93
|
-
}
|
|
94
|
-
loadSettingsDictionary(settingRsc, db, priority) {
|
|
95
|
-
const setting = db.getString(settingRsc);
|
|
96
|
-
if (undefined === setting)
|
|
97
|
-
throw new Error(`could not load setting resource ${settingRsc}`);
|
|
98
|
-
this.settings.addJson(`${db.container.id}/${db.dbName}/${settingRsc}`, priority, setting);
|
|
99
|
-
}
|
|
100
|
-
close() {
|
|
101
|
-
this.settings.close();
|
|
102
|
-
for (const [_id, container] of this._containers)
|
|
103
|
-
container.close();
|
|
104
|
-
this._containers.clear();
|
|
105
|
-
}
|
|
106
|
-
resolveAccount(accountName) {
|
|
107
|
-
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Accounts, (val) => {
|
|
108
|
-
if (Array.isArray(val)) {
|
|
109
|
-
for (const entry of val) {
|
|
110
|
-
if (typeof entry === "object" && entry.name === accountName)
|
|
111
|
-
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Accounts, accountName);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return undefined; // keep going through all settings dictionaries
|
|
115
|
-
});
|
|
116
|
-
if (resolved === undefined)
|
|
117
|
-
throw new Error(`no setting "${exports.WorkspaceSetting.Accounts}" entry for "${accountName}"`);
|
|
118
|
-
return resolved;
|
|
119
|
-
}
|
|
120
|
-
resolveContainer(containerName) {
|
|
121
|
-
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Containers, (val) => {
|
|
122
|
-
if (Array.isArray(val)) {
|
|
123
|
-
for (const entry of val) {
|
|
124
|
-
if (typeof entry === "object" && entry.name === containerName)
|
|
125
|
-
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Containers, containerName);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return undefined; // keep going through all settings dictionaries
|
|
129
|
-
});
|
|
130
|
-
if (resolved === undefined)
|
|
131
|
-
throw new Error(`no setting "${exports.WorkspaceSetting.Containers}" entry for "${containerName}"`);
|
|
132
|
-
return resolved;
|
|
133
|
-
}
|
|
134
|
-
resolveDatabase(databaseName) {
|
|
135
|
-
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Databases, (val) => {
|
|
136
|
-
if (Array.isArray(val)) {
|
|
137
|
-
for (const entry of val) {
|
|
138
|
-
if (typeof entry === "object" && entry.name === databaseName)
|
|
139
|
-
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Databases, databaseName);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return undefined; // keep going through all settings dictionaries
|
|
143
|
-
});
|
|
144
|
-
if (resolved === undefined)
|
|
145
|
-
throw new Error(`no setting "${exports.WorkspaceSetting.Databases}" entry for "${databaseName}"`);
|
|
146
|
-
return resolved;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.ITwinWorkspace = ITwinWorkspace;
|
|
150
|
-
/** @internal */
|
|
151
|
-
class ITwinWorkspaceContainer {
|
|
152
|
-
constructor(workspace, props, account) {
|
|
153
|
-
this._wsDbs = new Map();
|
|
154
|
-
ITwinWorkspaceContainer.validateContainerId(props.containerId);
|
|
155
|
-
this.workspace = workspace;
|
|
156
|
-
this.id = props.containerId;
|
|
157
|
-
if ((account === null || account === void 0 ? void 0 : account.accessName) && account.storageType)
|
|
158
|
-
this.cloudContainer = CloudSqlite_1.CloudSqlite.createCloudContainer({ accessToken: "", ...props, ...account });
|
|
159
|
-
workspace.addContainer(this);
|
|
160
|
-
this.filesDir = (0, path_1.join)(this.dirName, "Files");
|
|
161
|
-
const cloudContainer = this.cloudContainer;
|
|
162
|
-
if (undefined === cloudContainer)
|
|
163
|
-
return;
|
|
164
|
-
cloudContainer.connect(this.workspace.cloudCache);
|
|
165
|
-
if (false !== props.syncOnConnect) {
|
|
166
|
-
try {
|
|
167
|
-
cloudContainer.checkForChanges();
|
|
168
|
-
}
|
|
169
|
-
catch (e) {
|
|
170
|
-
// must be offline
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
get dirName() { return (0, path_1.join)(this.workspace.containerDir, this.id); }
|
|
175
|
-
static noLeadingOrTrailingSpaces(name, msg) {
|
|
176
|
-
if (name.trim() !== name)
|
|
177
|
-
throw new Error(`${msg} [${name}] may not have leading or tailing spaces`);
|
|
178
|
-
}
|
|
179
|
-
/** rules for ContainerIds (from Azure, see https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata)
|
|
180
|
-
* - may only contain lower case letters, numbers or dashes
|
|
181
|
-
* - may not start or end with with a dash nor have more than one dash in a row
|
|
182
|
-
* - may not be shorter than 3 or longer than 63 characters
|
|
183
|
-
*/
|
|
184
|
-
static validateContainerId(id) {
|
|
185
|
-
if (!/^(?=.{3,63}$)[a-z0-9]+(-[a-z0-9]+)*$/g.test(id))
|
|
186
|
-
throw new Error(`invalid containerId: [${id}]`);
|
|
187
|
-
}
|
|
188
|
-
static validateDbName(dbName) {
|
|
189
|
-
if (dbName === "" || dbName.length > 255 || /[#\.<>:"/\\"`'|?*\u0000-\u001F]/g.test(dbName) || /^(con|prn|aux|nul|com\d|lpt\d)$/i.test(dbName))
|
|
190
|
-
throw new Error(`invalid dbName: [${dbName}]`);
|
|
191
|
-
this.noLeadingOrTrailingSpaces(dbName, "dbName");
|
|
192
|
-
}
|
|
193
|
-
static validateVersion(version) {
|
|
194
|
-
var _a, _b;
|
|
195
|
-
version = version !== null && version !== void 0 ? version : "1.0.0";
|
|
196
|
-
if (version) {
|
|
197
|
-
const opts = { loose: true, includePrerelease: true };
|
|
198
|
-
// clean allows prerelease, so try it first. If that fails attempt to coerce it (coerce strips prerelease even if you say not to.)
|
|
199
|
-
const semVersion = (_a = semver.clean(version, opts)) !== null && _a !== void 0 ? _a : (_b = semver.coerce(version, opts)) === null || _b === void 0 ? void 0 : _b.version;
|
|
200
|
-
if (!semVersion)
|
|
201
|
-
throw new Error("invalid version specification");
|
|
202
|
-
version = semVersion;
|
|
203
|
-
}
|
|
204
|
-
return version;
|
|
205
|
-
}
|
|
206
|
-
static parseDbFileName(dbFileName) {
|
|
207
|
-
const parts = dbFileName.split(":");
|
|
208
|
-
return { dbName: parts[0], version: parts[1] };
|
|
209
|
-
}
|
|
210
|
-
static makeDbFileName(dbName, version) {
|
|
211
|
-
return `${dbName}:${this.validateVersion(version)}`;
|
|
212
|
-
}
|
|
213
|
-
static resolveCloudFileName(cloudContainer, props) {
|
|
214
|
-
var _a;
|
|
215
|
-
const dbName = props.dbName;
|
|
216
|
-
const dbs = cloudContainer.queryDatabases(`${dbName}*`); // get all databases that start with dbName
|
|
217
|
-
const versions = [];
|
|
218
|
-
for (const db of dbs) {
|
|
219
|
-
const thisDb = ITwinWorkspaceContainer.parseDbFileName(db);
|
|
220
|
-
if (thisDb.dbName === dbName && "string" === typeof thisDb.version && thisDb.version.length > 0)
|
|
221
|
-
versions.push(thisDb.version);
|
|
222
|
-
}
|
|
223
|
-
if (versions.length === 0)
|
|
224
|
-
versions[0] = "1.0.0";
|
|
225
|
-
const range = (_a = props.version) !== null && _a !== void 0 ? _a : "*";
|
|
226
|
-
try {
|
|
227
|
-
const version = semver.maxSatisfying(versions, range, { loose: true, includePrerelease: props.includePrerelease });
|
|
228
|
-
if (version)
|
|
229
|
-
return `${dbName}:${version}`;
|
|
230
|
-
}
|
|
231
|
-
catch (e) {
|
|
232
|
-
}
|
|
233
|
-
throw new Error(`No version of [${dbName}] available for "${range}"`);
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Create a copy of an existing [[WorkspaceDb]] in a cloud container with a new version number.
|
|
237
|
-
* @param cloudContainer The attached cloud container holding the existing WorkspaceDb
|
|
238
|
-
* @param fromProps Properties that describe the source WorkspaceDb for the new version
|
|
239
|
-
* @param versionType The type of version increment to apply to the existing version.
|
|
240
|
-
* @note This requires that the cloudContainer is attached and the write lock on the container be held. The copy should be modified with
|
|
241
|
-
* new content before the write lock is released, and thereafter should never be modified again.
|
|
242
|
-
*/
|
|
243
|
-
static async makeNewVersion(cloudContainer, fromProps, versionType) {
|
|
244
|
-
const oldName = this.resolveCloudFileName(cloudContainer, fromProps);
|
|
245
|
-
const oldDb = this.parseDbFileName(oldName);
|
|
246
|
-
const newVersion = semver.inc(oldDb.version, versionType);
|
|
247
|
-
if (!newVersion)
|
|
248
|
-
throw new Error("invalid version");
|
|
249
|
-
const newName = this.makeDbFileName(oldDb.dbName, newVersion);
|
|
250
|
-
await cloudContainer.copyDatabase(oldName, newName);
|
|
251
|
-
return { oldName, newName };
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Convert a WorkspaceDb.Props specification into a DbFileName. For cloud-based containers, this resolves to the dbName, incorporating the appropriate
|
|
255
|
-
* version. For file-based containers, this returns a local filename of a WorkspaceDb file with the extension ".itwin-workspace"
|
|
256
|
-
*/
|
|
257
|
-
resolveDbFileName(props) {
|
|
258
|
-
const cloudContainer = this.cloudContainer;
|
|
259
|
-
if (undefined === cloudContainer)
|
|
260
|
-
return (0, path_1.join)(this.dirName, `${props.dbName}.${ITwinWorkspaceDb.fileExt}`); // local file, versions not allowed
|
|
261
|
-
return ITwinWorkspaceContainer.resolveCloudFileName(cloudContainer, props);
|
|
262
|
-
}
|
|
263
|
-
addWorkspaceDb(toAdd) {
|
|
264
|
-
if (undefined !== this._wsDbs.get(toAdd.dbName))
|
|
265
|
-
throw new Error(`workspaceDb ${toAdd.dbName} already exists in workspace`);
|
|
266
|
-
this._wsDbs.set(toAdd.dbName, toAdd);
|
|
267
|
-
}
|
|
268
|
-
getWorkspaceDb(props) {
|
|
269
|
-
var _a;
|
|
270
|
-
const db = (_a = this._wsDbs.get(props.dbName)) !== null && _a !== void 0 ? _a : new ITwinWorkspaceDb(props, this);
|
|
271
|
-
if (!db.isOpen)
|
|
272
|
-
db.open();
|
|
273
|
-
return db;
|
|
274
|
-
}
|
|
275
|
-
dropWorkspaceDb(toDrop) {
|
|
276
|
-
const name = toDrop.dbName;
|
|
277
|
-
const wsDb = this._wsDbs.get(name);
|
|
278
|
-
if (wsDb === toDrop) {
|
|
279
|
-
this._wsDbs.delete(name);
|
|
280
|
-
wsDb.close();
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
close() {
|
|
284
|
-
var _a;
|
|
285
|
-
for (const [_name, db] of this._wsDbs)
|
|
286
|
-
db.close();
|
|
287
|
-
this._wsDbs.clear();
|
|
288
|
-
(_a = this.cloudContainer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
289
|
-
}
|
|
290
|
-
/** Delete all local files extracted by [[WorkspaceDb.getFile]] for this container. */
|
|
291
|
-
purgeContainerFiles() {
|
|
292
|
-
IModelJsFs_1.IModelJsFs.purgeDirSync(this.filesDir);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
exports.ITwinWorkspaceContainer = ITwinWorkspaceContainer;
|
|
296
|
-
/**
|
|
297
|
-
* Implementation of WorkspaceDb
|
|
298
|
-
* @internal
|
|
299
|
-
*/
|
|
300
|
-
class ITwinWorkspaceDb {
|
|
301
|
-
constructor(props, container) {
|
|
302
|
-
/** the SQLiteDb for this WorkspaceDb*/
|
|
303
|
-
this.sqliteDb = new SQLiteDb_1.SQLiteDb();
|
|
304
|
-
/** called before db is closed */
|
|
305
|
-
this.onClose = new core_bentley_1.BeEvent();
|
|
306
|
-
ITwinWorkspaceContainer.validateDbName(props.dbName);
|
|
307
|
-
this.dbName = props.dbName;
|
|
308
|
-
this.container = container;
|
|
309
|
-
this.dbFileName = container.resolveDbFileName(props);
|
|
310
|
-
container.addWorkspaceDb(this);
|
|
311
|
-
}
|
|
312
|
-
/** true if this WorkspaceDb is currently open */
|
|
313
|
-
get isOpen() { return this.sqliteDb.isOpen; }
|
|
314
|
-
queryFileResource(rscName) {
|
|
315
|
-
const info = this.sqliteDb.nativeDb.queryEmbeddedFile(rscName);
|
|
316
|
-
if (undefined === info)
|
|
317
|
-
return undefined;
|
|
318
|
-
// since resource names can contain illegal characters, path separators, etc., we make the local file name from its hash, in hex.
|
|
319
|
-
let localFileName = (0, path_1.join)(this.container.filesDir, (0, crypto_1.createHash)("sha1").update(this.dbFileName).update(rscName).digest("hex"));
|
|
320
|
-
if (info.fileExt !== "") // since some applications may expect to see the extension, append it here if it was supplied.
|
|
321
|
-
localFileName = `${localFileName}.${info.fileExt}`;
|
|
322
|
-
return { localFileName, info };
|
|
323
|
-
}
|
|
324
|
-
open() {
|
|
325
|
-
this.sqliteDb.openDb(this.dbFileName, core_bentley_1.OpenMode.Readonly, this.container.cloudContainer);
|
|
326
|
-
}
|
|
327
|
-
close() {
|
|
328
|
-
if (this.isOpen) {
|
|
329
|
-
this.onClose.raiseEvent();
|
|
330
|
-
this.sqliteDb.closeDb();
|
|
331
|
-
this.container.dropWorkspaceDb(this);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
getString(rscName) {
|
|
335
|
-
return this.sqliteDb.withSqliteStatement("SELECT value from strings WHERE id=?", (stmt) => {
|
|
336
|
-
stmt.bindString(1, rscName);
|
|
337
|
-
return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValueString(0) : undefined;
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
/** Get a BlobIO reader for a blob WorkspaceResource.
|
|
341
|
-
* @note when finished, caller *must* call `close` on the BlobIO.
|
|
342
|
-
*/
|
|
343
|
-
getBlobReader(rscName) {
|
|
344
|
-
return this.sqliteDb.withSqliteStatement("SELECT rowid from blobs WHERE id=?", (stmt) => {
|
|
345
|
-
stmt.bindString(1, rscName);
|
|
346
|
-
const blobReader = SQLiteDb_1.SQLiteDb.createBlobIO();
|
|
347
|
-
blobReader.open(this.sqliteDb.nativeDb, { tableName: "blobs", columnName: "value", row: stmt.getValueInteger(0) });
|
|
348
|
-
return blobReader;
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
getBlob(rscName) {
|
|
352
|
-
return this.sqliteDb.withSqliteStatement("SELECT value from blobs WHERE id=?", (stmt) => {
|
|
353
|
-
stmt.bindString(1, rscName);
|
|
354
|
-
return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValueBlob(0) : undefined;
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
getFile(rscName, targetFileName) {
|
|
358
|
-
const file = this.queryFileResource(rscName);
|
|
359
|
-
if (!file)
|
|
360
|
-
return undefined;
|
|
361
|
-
const info = file.info;
|
|
362
|
-
const localFileName = targetFileName !== null && targetFileName !== void 0 ? targetFileName : file.localFileName;
|
|
363
|
-
// check whether the file is already up to date.
|
|
364
|
-
const stat = fs.existsSync(localFileName) && fs.statSync(localFileName);
|
|
365
|
-
if (stat && Math.round(stat.mtimeMs) === info.date && stat.size === info.size)
|
|
366
|
-
return localFileName; // yes, we're done
|
|
367
|
-
// extractEmbeddedFile fails if the file exists or if the directory does not exist
|
|
368
|
-
if (stat)
|
|
369
|
-
fs.removeSync(localFileName);
|
|
370
|
-
else
|
|
371
|
-
IModelJsFs_1.IModelJsFs.recursiveMkDirSync((0, path_1.dirname)(localFileName));
|
|
372
|
-
this.sqliteDb.nativeDb.extractEmbeddedFile({ name: rscName, localFileName });
|
|
373
|
-
const date = new Date(info.date);
|
|
374
|
-
fs.utimesSync(localFileName, date, date); // set the last-modified date of the file to match date in container
|
|
375
|
-
fs.chmodSync(localFileName, "0444"); // set file readonly
|
|
376
|
-
return localFileName;
|
|
377
|
-
}
|
|
378
|
-
prefetch(opts) {
|
|
379
|
-
const cloudContainer = this.container.cloudContainer;
|
|
380
|
-
if (cloudContainer === undefined)
|
|
381
|
-
throw new Error("no cloud container to prefetch");
|
|
382
|
-
return CloudSqlite_1.CloudSqlite.startCloudPrefetch(cloudContainer, this.dbFileName, opts);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
exports.ITwinWorkspaceDb = ITwinWorkspaceDb;
|
|
386
|
-
/** file extension for local WorkspaceDbs */
|
|
387
|
-
ITwinWorkspaceDb.fileExt = "itwin-workspace";
|
|
388
|
-
/**
|
|
389
|
-
* An editable [[WorkspaceDb]]. This is used by administrators for creating and modifying `WorkspaceDb`s.
|
|
390
|
-
* For cloud-backed containers, the write token must be obtained before this class may be used. Only one user at at time
|
|
391
|
-
* may be editing.
|
|
392
|
-
* @internal
|
|
393
|
-
*/
|
|
394
|
-
class EditableWorkspaceDb extends ITwinWorkspaceDb {
|
|
395
|
-
static validateResourceName(name) {
|
|
396
|
-
ITwinWorkspaceContainer.noLeadingOrTrailingSpaces(name, "resource name");
|
|
397
|
-
if (name.length > 1024)
|
|
398
|
-
throw new Error("resource name too long");
|
|
399
|
-
}
|
|
400
|
-
validateResourceSize(val) {
|
|
401
|
-
const len = typeof val === "string" ? val.length : val.byteLength;
|
|
402
|
-
if (len > (1024 * 1024 * 1024)) // one gigabyte
|
|
403
|
-
throw new Error("value is too large");
|
|
404
|
-
}
|
|
405
|
-
open() {
|
|
406
|
-
this.sqliteDb.openDb(this.dbFileName, core_bentley_1.OpenMode.ReadWrite, this.container.cloudContainer);
|
|
407
|
-
}
|
|
408
|
-
getFileModifiedTime(localFileName) {
|
|
409
|
-
return Math.round(fs.statSync(localFileName).mtimeMs);
|
|
410
|
-
}
|
|
411
|
-
performWriteSql(rscName, sql, bind) {
|
|
412
|
-
this.sqliteDb.withSqliteStatement(sql, (stmt) => {
|
|
413
|
-
stmt.bindString(1, rscName);
|
|
414
|
-
bind === null || bind === void 0 ? void 0 : bind(stmt);
|
|
415
|
-
const rc = stmt.step();
|
|
416
|
-
if (core_bentley_1.DbResult.BE_SQLITE_DONE !== rc) {
|
|
417
|
-
if (core_bentley_1.DbResult.BE_SQLITE_CONSTRAINT_PRIMARYKEY === rc)
|
|
418
|
-
throw new core_common_1.IModelError(rc, `resource "${rscName}" already exists`);
|
|
419
|
-
throw new core_common_1.IModelError(rc, `workspace [${sql}]`);
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
this.sqliteDb.saveChanges();
|
|
423
|
-
}
|
|
424
|
-
async createDb(version) {
|
|
425
|
-
if (!this.container.cloudContainer) {
|
|
426
|
-
EditableWorkspaceDb.createEmpty(this.dbFileName);
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
// currently the only way to create a workspaceDb in a cloud container is to create a temporary workspaceDb and upload it.
|
|
430
|
-
const tempDbFile = (0, path_1.join)(IModelHost_1.KnownLocations.tmpdir, `empty.${ITwinWorkspaceDb.fileExt}`);
|
|
431
|
-
if (fs.existsSync(tempDbFile))
|
|
432
|
-
|
|
433
|
-
EditableWorkspaceDb.createEmpty(tempDbFile);
|
|
434
|
-
this.dbFileName = ITwinWorkspaceContainer.makeDbFileName(this.dbName, version);
|
|
435
|
-
await CloudSqlite_1.CloudSqlite.uploadDb(this.container.cloudContainer, { localFileName: tempDbFile, dbName: this.dbFileName });
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
this.open();
|
|
439
|
-
}
|
|
440
|
-
/** Create a new, empty, EditableWorkspaceDb for importing Workspace resources. */
|
|
441
|
-
static createEmpty(fileName) {
|
|
442
|
-
const db = new SQLiteDb_1.SQLiteDb();
|
|
443
|
-
IModelJsFs_1.IModelJsFs.recursiveMkDirSync((0, path_1.dirname)(fileName));
|
|
444
|
-
db.createDb(fileName);
|
|
445
|
-
const timeStampCol = "lastMod TIMESTAMP NOT NULL DEFAULT(julianday('now'))";
|
|
446
|
-
db.executeSQL(`CREATE TABLE strings(id TEXT PRIMARY KEY NOT NULL,value TEXT,${timeStampCol})`);
|
|
447
|
-
db.executeSQL(`CREATE TABLE blobs(id TEXT PRIMARY KEY NOT NULL,value BLOB,${timeStampCol})`);
|
|
448
|
-
const createTrigger = (tableName) => {
|
|
449
|
-
db.executeSQL(`CREATE TRIGGER ${tableName}_timeStamp AFTER UPDATE ON ${tableName} WHEN old.lastMod=new.lastMod AND old.lastMod != julianday('now') BEGIN UPDATE ${tableName} SET lastMod=julianday('now') WHERE id=new.id; END`);
|
|
450
|
-
};
|
|
451
|
-
createTrigger("strings");
|
|
452
|
-
createTrigger("blobs");
|
|
453
|
-
db.closeDb(true);
|
|
454
|
-
}
|
|
455
|
-
/** Add a new string resource to this WorkspaceDb.
|
|
456
|
-
* @param rscName The name of the string resource.
|
|
457
|
-
* @param val The string to save.
|
|
458
|
-
*/
|
|
459
|
-
addString(rscName, val) {
|
|
460
|
-
EditableWorkspaceDb.validateResourceName(rscName);
|
|
461
|
-
this.validateResourceSize(val);
|
|
462
|
-
this.performWriteSql(rscName, "INSERT INTO strings(id,value) VALUES(?,?)", (stmt) => stmt.bindString(2, val));
|
|
463
|
-
}
|
|
464
|
-
/** Update an existing string resource with a new value.
|
|
465
|
-
* @param rscName The name of the string resource.
|
|
466
|
-
* @param val The new value.
|
|
467
|
-
* @throws if rscName does not exist
|
|
468
|
-
*/
|
|
469
|
-
updateString(rscName, val) {
|
|
470
|
-
this.validateResourceSize(val);
|
|
471
|
-
this.performWriteSql(rscName, "UPDATE strings SET value=?2 WHERE id=?1", (stmt) => stmt.bindString(2, val));
|
|
472
|
-
}
|
|
473
|
-
/** Remove a string resource. */
|
|
474
|
-
removeString(rscName) {
|
|
475
|
-
this.performWriteSql(rscName, "DELETE FROM strings WHERE id=?");
|
|
476
|
-
}
|
|
477
|
-
/** Add a new blob resource to this WorkspaceDb.
|
|
478
|
-
* @param rscName The name of the blob resource.
|
|
479
|
-
* @param val The blob to save.
|
|
480
|
-
*/
|
|
481
|
-
addBlob(rscName, val) {
|
|
482
|
-
EditableWorkspaceDb.validateResourceName(rscName);
|
|
483
|
-
this.validateResourceSize(val);
|
|
484
|
-
this.performWriteSql(rscName, "INSERT INTO blobs(id,value) VALUES(?,?)", (stmt) => stmt.bindBlob(2, val));
|
|
485
|
-
}
|
|
486
|
-
/** Update an existing blob resource with a new value.
|
|
487
|
-
* @param rscName The name of the blob resource.
|
|
488
|
-
* @param val The new value.
|
|
489
|
-
* @throws if rscName does not exist
|
|
490
|
-
*/
|
|
491
|
-
updateBlob(rscName, val) {
|
|
492
|
-
this.validateResourceSize(val);
|
|
493
|
-
this.performWriteSql(rscName, "UPDATE blobs SET value=?2 WHERE id=?1", (stmt) => stmt.bindBlob(2, val));
|
|
494
|
-
}
|
|
495
|
-
/** Get a BlobIO writer for a previously-added blob WorkspaceResource.
|
|
496
|
-
* @note after writing is complete, caller must call `close` on the BlobIO and must call `saveChanges` on the `db`.
|
|
497
|
-
*/
|
|
498
|
-
getBlobWriter(rscName) {
|
|
499
|
-
return this.sqliteDb.withSqliteStatement("SELECT rowid from blobs WHERE id=?", (stmt) => {
|
|
500
|
-
stmt.bindString(1, rscName);
|
|
501
|
-
const blobWriter = SQLiteDb_1.SQLiteDb.createBlobIO();
|
|
502
|
-
blobWriter.open(this.sqliteDb.nativeDb, { tableName: "blobs", columnName: "value", row: stmt.getValueInteger(0), writeable: true });
|
|
503
|
-
return blobWriter;
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
/** Remove a blob resource. */
|
|
507
|
-
removeBlob(rscName) {
|
|
508
|
-
this.performWriteSql(rscName, "DELETE FROM blobs WHERE id=?");
|
|
509
|
-
}
|
|
510
|
-
/** Copy the contents of an existing local file into this WorkspaceDb as a file resource.
|
|
511
|
-
* @param rscName The name of the file resource.
|
|
512
|
-
* @param localFileName The name of a local file to be read.
|
|
513
|
-
* @param fileExt The extension (do not include the leading ".") to be appended to the generated fileName
|
|
514
|
-
* when this WorkspaceDb is extracted from the WorkspaceDb. By default the characters after the last "." in `localFileName`
|
|
515
|
-
* are used. Pass this argument to override that.
|
|
516
|
-
*/
|
|
517
|
-
addFile(rscName, localFileName, fileExt) {
|
|
518
|
-
EditableWorkspaceDb.validateResourceName(rscName);
|
|
519
|
-
fileExt = fileExt !== null && fileExt !== void 0 ? fileExt : (0, path_1.extname)(localFileName);
|
|
520
|
-
if ((fileExt === null || fileExt === void 0 ? void 0 : fileExt[0]) === ".")
|
|
521
|
-
fileExt = fileExt.slice(1);
|
|
522
|
-
this.sqliteDb.nativeDb.embedFile({ name: rscName, localFileName, date: this.getFileModifiedTime(localFileName), fileExt });
|
|
523
|
-
}
|
|
524
|
-
/** Replace an existing file resource with the contents of another local file.
|
|
525
|
-
* @param rscName The name of the file resource.
|
|
526
|
-
* @param localFileName The name of a local file to be read.
|
|
527
|
-
* @throws if rscName does not exist
|
|
528
|
-
*/
|
|
529
|
-
updateFile(rscName, localFileName) {
|
|
530
|
-
this.queryFileResource(rscName); // throws if not present
|
|
531
|
-
this.sqliteDb.nativeDb.replaceEmbeddedFile({ name: rscName, localFileName, date: this.getFileModifiedTime(localFileName) });
|
|
532
|
-
}
|
|
533
|
-
/** Remove a file resource. */
|
|
534
|
-
removeFile(rscName) {
|
|
535
|
-
const file = this.queryFileResource(rscName);
|
|
536
|
-
if (undefined === file)
|
|
537
|
-
throw new Error(`file resource "${rscName}" does not exist`);
|
|
538
|
-
if (file && fs.existsSync(file.localFileName))
|
|
539
|
-
fs.unlinkSync(file.localFileName);
|
|
540
|
-
this.sqliteDb.nativeDb.removeEmbeddedFile(rscName);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
exports.EditableWorkspaceDb = EditableWorkspaceDb;
|
|
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 Workspace
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.EditableWorkspaceDb = exports.ITwinWorkspaceDb = exports.ITwinWorkspaceContainer = exports.ITwinWorkspace = exports.WorkspaceSetting = void 0;
|
|
11
|
+
const crypto_1 = require("crypto");
|
|
12
|
+
const fs = require("fs-extra");
|
|
13
|
+
const path_1 = require("path");
|
|
14
|
+
const semver = require("semver");
|
|
15
|
+
const core_bentley_1 = require("@itwin/core-bentley");
|
|
16
|
+
const core_common_1 = require("@itwin/core-common");
|
|
17
|
+
const CloudSqlite_1 = require("../CloudSqlite");
|
|
18
|
+
const IModelHost_1 = require("../IModelHost");
|
|
19
|
+
const IModelJsFs_1 = require("../IModelJsFs");
|
|
20
|
+
const SQLiteDb_1 = require("../SQLiteDb");
|
|
21
|
+
const Settings_1 = require("./Settings");
|
|
22
|
+
const SettingsSchemas_1 = require("./SettingsSchemas");
|
|
23
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
24
|
+
// cspell:ignore rowid primarykey julianday
|
|
25
|
+
/** The Settings used by Workspace api
|
|
26
|
+
* @beta
|
|
27
|
+
*/
|
|
28
|
+
exports.WorkspaceSetting = {
|
|
29
|
+
Accounts: "cloud/accounts",
|
|
30
|
+
Containers: "cloud/containers",
|
|
31
|
+
Databases: "workspace/databases",
|
|
32
|
+
};
|
|
33
|
+
/** @internal */
|
|
34
|
+
class ITwinWorkspace {
|
|
35
|
+
constructor(settings, opts) {
|
|
36
|
+
var _a;
|
|
37
|
+
this._containers = new Map();
|
|
38
|
+
this.settings = settings;
|
|
39
|
+
this.containerDir = (_a = opts === null || opts === void 0 ? void 0 : opts.containerDir) !== null && _a !== void 0 ? _a : (0, path_1.join)(IModelHost_1.IModelHost.cacheDir, "Workspace");
|
|
40
|
+
this._cloudCache = opts === null || opts === void 0 ? void 0 : opts.testCloudCache;
|
|
41
|
+
let settingsFiles = opts === null || opts === void 0 ? void 0 : opts.settingsFiles;
|
|
42
|
+
if (settingsFiles) {
|
|
43
|
+
if (typeof settingsFiles === "string")
|
|
44
|
+
settingsFiles = [settingsFiles];
|
|
45
|
+
settingsFiles.forEach((file) => settings.addFile(file, Settings_1.SettingsPriority.application));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
get cloudCache() {
|
|
49
|
+
if (undefined === this._cloudCache)
|
|
50
|
+
this._cloudCache = ITwinWorkspace.getSharedCloudCache();
|
|
51
|
+
return this._cloudCache;
|
|
52
|
+
}
|
|
53
|
+
static getSharedCloudCache() {
|
|
54
|
+
if (undefined === this._sharedCloudCache) {
|
|
55
|
+
const rootDir = (0, path_1.join)(IModelHost_1.IModelHost.cacheDir, "Workspace", "cloud");
|
|
56
|
+
IModelJsFs_1.IModelJsFs.recursiveMkDirSync(rootDir);
|
|
57
|
+
this._sharedCloudCache = CloudSqlite_1.CloudSqlite.createCloudCache({ rootDir, cacheSize: "20G", name: "workspace" });
|
|
58
|
+
}
|
|
59
|
+
return this._sharedCloudCache;
|
|
60
|
+
}
|
|
61
|
+
static finalize() {
|
|
62
|
+
if (this._sharedCloudCache) {
|
|
63
|
+
this._sharedCloudCache.destroy();
|
|
64
|
+
this._sharedCloudCache = undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
addContainer(toAdd) {
|
|
68
|
+
if (undefined !== this._containers.get(toAdd.id))
|
|
69
|
+
throw new Error("container already exists in workspace");
|
|
70
|
+
this._containers.set(toAdd.id, toAdd);
|
|
71
|
+
}
|
|
72
|
+
findContainer(containerId) {
|
|
73
|
+
return this._containers.get(containerId);
|
|
74
|
+
}
|
|
75
|
+
getContainer(props, account) {
|
|
76
|
+
var _a;
|
|
77
|
+
return (_a = this.findContainer(props.containerId)) !== null && _a !== void 0 ? _a : new ITwinWorkspaceContainer(this, props, account);
|
|
78
|
+
}
|
|
79
|
+
getWorkspaceDbFromProps(dbProps, containerProps, account) {
|
|
80
|
+
return this.getContainer(containerProps, account).getWorkspaceDb(dbProps);
|
|
81
|
+
}
|
|
82
|
+
async getWorkspaceDb(dbAlias, tokenFunc) {
|
|
83
|
+
const dbProps = this.resolveDatabase(dbAlias);
|
|
84
|
+
const containerProps = this.resolveContainer(dbProps.containerName);
|
|
85
|
+
const account = containerProps.accountName !== "" ? this.resolveAccount(containerProps.accountName) : undefined;
|
|
86
|
+
let container = this.findContainer(containerProps.containerId);
|
|
87
|
+
if (undefined === container) {
|
|
88
|
+
if (tokenFunc && account)
|
|
89
|
+
containerProps.accessToken = await tokenFunc(containerProps, account);
|
|
90
|
+
container = this.getContainer(containerProps, account);
|
|
91
|
+
}
|
|
92
|
+
return container === null || container === void 0 ? void 0 : container.getWorkspaceDb(dbProps);
|
|
93
|
+
}
|
|
94
|
+
loadSettingsDictionary(settingRsc, db, priority) {
|
|
95
|
+
const setting = db.getString(settingRsc);
|
|
96
|
+
if (undefined === setting)
|
|
97
|
+
throw new Error(`could not load setting resource ${settingRsc}`);
|
|
98
|
+
this.settings.addJson(`${db.container.id}/${db.dbName}/${settingRsc}`, priority, setting);
|
|
99
|
+
}
|
|
100
|
+
close() {
|
|
101
|
+
this.settings.close();
|
|
102
|
+
for (const [_id, container] of this._containers)
|
|
103
|
+
container.close();
|
|
104
|
+
this._containers.clear();
|
|
105
|
+
}
|
|
106
|
+
resolveAccount(accountName) {
|
|
107
|
+
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Accounts, (val) => {
|
|
108
|
+
if (Array.isArray(val)) {
|
|
109
|
+
for (const entry of val) {
|
|
110
|
+
if (typeof entry === "object" && entry.name === accountName)
|
|
111
|
+
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Accounts, accountName);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return undefined; // keep going through all settings dictionaries
|
|
115
|
+
});
|
|
116
|
+
if (resolved === undefined)
|
|
117
|
+
throw new Error(`no setting "${exports.WorkspaceSetting.Accounts}" entry for "${accountName}"`);
|
|
118
|
+
return resolved;
|
|
119
|
+
}
|
|
120
|
+
resolveContainer(containerName) {
|
|
121
|
+
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Containers, (val) => {
|
|
122
|
+
if (Array.isArray(val)) {
|
|
123
|
+
for (const entry of val) {
|
|
124
|
+
if (typeof entry === "object" && entry.name === containerName)
|
|
125
|
+
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Containers, containerName);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return undefined; // keep going through all settings dictionaries
|
|
129
|
+
});
|
|
130
|
+
if (resolved === undefined)
|
|
131
|
+
throw new Error(`no setting "${exports.WorkspaceSetting.Containers}" entry for "${containerName}"`);
|
|
132
|
+
return resolved;
|
|
133
|
+
}
|
|
134
|
+
resolveDatabase(databaseName) {
|
|
135
|
+
const resolved = this.settings.resolveSetting(exports.WorkspaceSetting.Databases, (val) => {
|
|
136
|
+
if (Array.isArray(val)) {
|
|
137
|
+
for (const entry of val) {
|
|
138
|
+
if (typeof entry === "object" && entry.name === databaseName)
|
|
139
|
+
return SettingsSchemas_1.SettingsSchemas.validateArrayObject(entry, exports.WorkspaceSetting.Databases, databaseName);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return undefined; // keep going through all settings dictionaries
|
|
143
|
+
});
|
|
144
|
+
if (resolved === undefined)
|
|
145
|
+
throw new Error(`no setting "${exports.WorkspaceSetting.Databases}" entry for "${databaseName}"`);
|
|
146
|
+
return resolved;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.ITwinWorkspace = ITwinWorkspace;
|
|
150
|
+
/** @internal */
|
|
151
|
+
class ITwinWorkspaceContainer {
|
|
152
|
+
constructor(workspace, props, account) {
|
|
153
|
+
this._wsDbs = new Map();
|
|
154
|
+
ITwinWorkspaceContainer.validateContainerId(props.containerId);
|
|
155
|
+
this.workspace = workspace;
|
|
156
|
+
this.id = props.containerId;
|
|
157
|
+
if ((account === null || account === void 0 ? void 0 : account.accessName) && account.storageType)
|
|
158
|
+
this.cloudContainer = CloudSqlite_1.CloudSqlite.createCloudContainer({ accessToken: "", ...props, ...account });
|
|
159
|
+
workspace.addContainer(this);
|
|
160
|
+
this.filesDir = (0, path_1.join)(this.dirName, "Files");
|
|
161
|
+
const cloudContainer = this.cloudContainer;
|
|
162
|
+
if (undefined === cloudContainer)
|
|
163
|
+
return;
|
|
164
|
+
cloudContainer.connect(this.workspace.cloudCache);
|
|
165
|
+
if (false !== props.syncOnConnect) {
|
|
166
|
+
try {
|
|
167
|
+
cloudContainer.checkForChanges();
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
// must be offline
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
get dirName() { return (0, path_1.join)(this.workspace.containerDir, this.id); }
|
|
175
|
+
static noLeadingOrTrailingSpaces(name, msg) {
|
|
176
|
+
if (name.trim() !== name)
|
|
177
|
+
throw new Error(`${msg} [${name}] may not have leading or tailing spaces`);
|
|
178
|
+
}
|
|
179
|
+
/** rules for ContainerIds (from Azure, see https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata)
|
|
180
|
+
* - may only contain lower case letters, numbers or dashes
|
|
181
|
+
* - may not start or end with with a dash nor have more than one dash in a row
|
|
182
|
+
* - may not be shorter than 3 or longer than 63 characters
|
|
183
|
+
*/
|
|
184
|
+
static validateContainerId(id) {
|
|
185
|
+
if (!/^(?=.{3,63}$)[a-z0-9]+(-[a-z0-9]+)*$/g.test(id))
|
|
186
|
+
throw new Error(`invalid containerId: [${id}]`);
|
|
187
|
+
}
|
|
188
|
+
static validateDbName(dbName) {
|
|
189
|
+
if (dbName === "" || dbName.length > 255 || /[#\.<>:"/\\"`'|?*\u0000-\u001F]/g.test(dbName) || /^(con|prn|aux|nul|com\d|lpt\d)$/i.test(dbName))
|
|
190
|
+
throw new Error(`invalid dbName: [${dbName}]`);
|
|
191
|
+
this.noLeadingOrTrailingSpaces(dbName, "dbName");
|
|
192
|
+
}
|
|
193
|
+
static validateVersion(version) {
|
|
194
|
+
var _a, _b;
|
|
195
|
+
version = version !== null && version !== void 0 ? version : "1.0.0";
|
|
196
|
+
if (version) {
|
|
197
|
+
const opts = { loose: true, includePrerelease: true };
|
|
198
|
+
// clean allows prerelease, so try it first. If that fails attempt to coerce it (coerce strips prerelease even if you say not to.)
|
|
199
|
+
const semVersion = (_a = semver.clean(version, opts)) !== null && _a !== void 0 ? _a : (_b = semver.coerce(version, opts)) === null || _b === void 0 ? void 0 : _b.version;
|
|
200
|
+
if (!semVersion)
|
|
201
|
+
throw new Error("invalid version specification");
|
|
202
|
+
version = semVersion;
|
|
203
|
+
}
|
|
204
|
+
return version;
|
|
205
|
+
}
|
|
206
|
+
static parseDbFileName(dbFileName) {
|
|
207
|
+
const parts = dbFileName.split(":");
|
|
208
|
+
return { dbName: parts[0], version: parts[1] };
|
|
209
|
+
}
|
|
210
|
+
static makeDbFileName(dbName, version) {
|
|
211
|
+
return `${dbName}:${this.validateVersion(version)}`;
|
|
212
|
+
}
|
|
213
|
+
static resolveCloudFileName(cloudContainer, props) {
|
|
214
|
+
var _a;
|
|
215
|
+
const dbName = props.dbName;
|
|
216
|
+
const dbs = cloudContainer.queryDatabases(`${dbName}*`); // get all databases that start with dbName
|
|
217
|
+
const versions = [];
|
|
218
|
+
for (const db of dbs) {
|
|
219
|
+
const thisDb = ITwinWorkspaceContainer.parseDbFileName(db);
|
|
220
|
+
if (thisDb.dbName === dbName && "string" === typeof thisDb.version && thisDb.version.length > 0)
|
|
221
|
+
versions.push(thisDb.version);
|
|
222
|
+
}
|
|
223
|
+
if (versions.length === 0)
|
|
224
|
+
versions[0] = "1.0.0";
|
|
225
|
+
const range = (_a = props.version) !== null && _a !== void 0 ? _a : "*";
|
|
226
|
+
try {
|
|
227
|
+
const version = semver.maxSatisfying(versions, range, { loose: true, includePrerelease: props.includePrerelease });
|
|
228
|
+
if (version)
|
|
229
|
+
return `${dbName}:${version}`;
|
|
230
|
+
}
|
|
231
|
+
catch (e) {
|
|
232
|
+
}
|
|
233
|
+
throw new Error(`No version of [${dbName}] available for "${range}"`);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Create a copy of an existing [[WorkspaceDb]] in a cloud container with a new version number.
|
|
237
|
+
* @param cloudContainer The attached cloud container holding the existing WorkspaceDb
|
|
238
|
+
* @param fromProps Properties that describe the source WorkspaceDb for the new version
|
|
239
|
+
* @param versionType The type of version increment to apply to the existing version.
|
|
240
|
+
* @note This requires that the cloudContainer is attached and the write lock on the container be held. The copy should be modified with
|
|
241
|
+
* new content before the write lock is released, and thereafter should never be modified again.
|
|
242
|
+
*/
|
|
243
|
+
static async makeNewVersion(cloudContainer, fromProps, versionType) {
|
|
244
|
+
const oldName = this.resolveCloudFileName(cloudContainer, fromProps);
|
|
245
|
+
const oldDb = this.parseDbFileName(oldName);
|
|
246
|
+
const newVersion = semver.inc(oldDb.version, versionType);
|
|
247
|
+
if (!newVersion)
|
|
248
|
+
throw new Error("invalid version");
|
|
249
|
+
const newName = this.makeDbFileName(oldDb.dbName, newVersion);
|
|
250
|
+
await cloudContainer.copyDatabase(oldName, newName);
|
|
251
|
+
return { oldName, newName };
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Convert a WorkspaceDb.Props specification into a DbFileName. For cloud-based containers, this resolves to the dbName, incorporating the appropriate
|
|
255
|
+
* version. For file-based containers, this returns a local filename of a WorkspaceDb file with the extension ".itwin-workspace"
|
|
256
|
+
*/
|
|
257
|
+
resolveDbFileName(props) {
|
|
258
|
+
const cloudContainer = this.cloudContainer;
|
|
259
|
+
if (undefined === cloudContainer)
|
|
260
|
+
return (0, path_1.join)(this.dirName, `${props.dbName}.${ITwinWorkspaceDb.fileExt}`); // local file, versions not allowed
|
|
261
|
+
return ITwinWorkspaceContainer.resolveCloudFileName(cloudContainer, props);
|
|
262
|
+
}
|
|
263
|
+
addWorkspaceDb(toAdd) {
|
|
264
|
+
if (undefined !== this._wsDbs.get(toAdd.dbName))
|
|
265
|
+
throw new Error(`workspaceDb ${toAdd.dbName} already exists in workspace`);
|
|
266
|
+
this._wsDbs.set(toAdd.dbName, toAdd);
|
|
267
|
+
}
|
|
268
|
+
getWorkspaceDb(props) {
|
|
269
|
+
var _a;
|
|
270
|
+
const db = (_a = this._wsDbs.get(props.dbName)) !== null && _a !== void 0 ? _a : new ITwinWorkspaceDb(props, this);
|
|
271
|
+
if (!db.isOpen)
|
|
272
|
+
db.open();
|
|
273
|
+
return db;
|
|
274
|
+
}
|
|
275
|
+
dropWorkspaceDb(toDrop) {
|
|
276
|
+
const name = toDrop.dbName;
|
|
277
|
+
const wsDb = this._wsDbs.get(name);
|
|
278
|
+
if (wsDb === toDrop) {
|
|
279
|
+
this._wsDbs.delete(name);
|
|
280
|
+
wsDb.close();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
close() {
|
|
284
|
+
var _a;
|
|
285
|
+
for (const [_name, db] of this._wsDbs)
|
|
286
|
+
db.close();
|
|
287
|
+
this._wsDbs.clear();
|
|
288
|
+
(_a = this.cloudContainer) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
289
|
+
}
|
|
290
|
+
/** Delete all local files extracted by [[WorkspaceDb.getFile]] for this container. */
|
|
291
|
+
purgeContainerFiles() {
|
|
292
|
+
IModelJsFs_1.IModelJsFs.purgeDirSync(this.filesDir);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
exports.ITwinWorkspaceContainer = ITwinWorkspaceContainer;
|
|
296
|
+
/**
|
|
297
|
+
* Implementation of WorkspaceDb
|
|
298
|
+
* @internal
|
|
299
|
+
*/
|
|
300
|
+
class ITwinWorkspaceDb {
|
|
301
|
+
constructor(props, container) {
|
|
302
|
+
/** the SQLiteDb for this WorkspaceDb*/
|
|
303
|
+
this.sqliteDb = new SQLiteDb_1.SQLiteDb();
|
|
304
|
+
/** called before db is closed */
|
|
305
|
+
this.onClose = new core_bentley_1.BeEvent();
|
|
306
|
+
ITwinWorkspaceContainer.validateDbName(props.dbName);
|
|
307
|
+
this.dbName = props.dbName;
|
|
308
|
+
this.container = container;
|
|
309
|
+
this.dbFileName = container.resolveDbFileName(props);
|
|
310
|
+
container.addWorkspaceDb(this);
|
|
311
|
+
}
|
|
312
|
+
/** true if this WorkspaceDb is currently open */
|
|
313
|
+
get isOpen() { return this.sqliteDb.isOpen; }
|
|
314
|
+
queryFileResource(rscName) {
|
|
315
|
+
const info = this.sqliteDb.nativeDb.queryEmbeddedFile(rscName);
|
|
316
|
+
if (undefined === info)
|
|
317
|
+
return undefined;
|
|
318
|
+
// since resource names can contain illegal characters, path separators, etc., we make the local file name from its hash, in hex.
|
|
319
|
+
let localFileName = (0, path_1.join)(this.container.filesDir, (0, crypto_1.createHash)("sha1").update(this.dbFileName).update(rscName).digest("hex"));
|
|
320
|
+
if (info.fileExt !== "") // since some applications may expect to see the extension, append it here if it was supplied.
|
|
321
|
+
localFileName = `${localFileName}.${info.fileExt}`;
|
|
322
|
+
return { localFileName, info };
|
|
323
|
+
}
|
|
324
|
+
open() {
|
|
325
|
+
this.sqliteDb.openDb(this.dbFileName, core_bentley_1.OpenMode.Readonly, this.container.cloudContainer);
|
|
326
|
+
}
|
|
327
|
+
close() {
|
|
328
|
+
if (this.isOpen) {
|
|
329
|
+
this.onClose.raiseEvent();
|
|
330
|
+
this.sqliteDb.closeDb();
|
|
331
|
+
this.container.dropWorkspaceDb(this);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
getString(rscName) {
|
|
335
|
+
return this.sqliteDb.withSqliteStatement("SELECT value from strings WHERE id=?", (stmt) => {
|
|
336
|
+
stmt.bindString(1, rscName);
|
|
337
|
+
return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValueString(0) : undefined;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
/** Get a BlobIO reader for a blob WorkspaceResource.
|
|
341
|
+
* @note when finished, caller *must* call `close` on the BlobIO.
|
|
342
|
+
*/
|
|
343
|
+
getBlobReader(rscName) {
|
|
344
|
+
return this.sqliteDb.withSqliteStatement("SELECT rowid from blobs WHERE id=?", (stmt) => {
|
|
345
|
+
stmt.bindString(1, rscName);
|
|
346
|
+
const blobReader = SQLiteDb_1.SQLiteDb.createBlobIO();
|
|
347
|
+
blobReader.open(this.sqliteDb.nativeDb, { tableName: "blobs", columnName: "value", row: stmt.getValueInteger(0) });
|
|
348
|
+
return blobReader;
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
getBlob(rscName) {
|
|
352
|
+
return this.sqliteDb.withSqliteStatement("SELECT value from blobs WHERE id=?", (stmt) => {
|
|
353
|
+
stmt.bindString(1, rscName);
|
|
354
|
+
return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValueBlob(0) : undefined;
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
getFile(rscName, targetFileName) {
|
|
358
|
+
const file = this.queryFileResource(rscName);
|
|
359
|
+
if (!file)
|
|
360
|
+
return undefined;
|
|
361
|
+
const info = file.info;
|
|
362
|
+
const localFileName = targetFileName !== null && targetFileName !== void 0 ? targetFileName : file.localFileName;
|
|
363
|
+
// check whether the file is already up to date.
|
|
364
|
+
const stat = fs.existsSync(localFileName) && fs.statSync(localFileName);
|
|
365
|
+
if (stat && Math.round(stat.mtimeMs) === info.date && stat.size === info.size)
|
|
366
|
+
return localFileName; // yes, we're done
|
|
367
|
+
// extractEmbeddedFile fails if the file exists or if the directory does not exist
|
|
368
|
+
if (stat)
|
|
369
|
+
fs.removeSync(localFileName);
|
|
370
|
+
else
|
|
371
|
+
IModelJsFs_1.IModelJsFs.recursiveMkDirSync((0, path_1.dirname)(localFileName));
|
|
372
|
+
this.sqliteDb.nativeDb.extractEmbeddedFile({ name: rscName, localFileName });
|
|
373
|
+
const date = new Date(info.date);
|
|
374
|
+
fs.utimesSync(localFileName, date, date); // set the last-modified date of the file to match date in container
|
|
375
|
+
fs.chmodSync(localFileName, "0444"); // set file readonly
|
|
376
|
+
return localFileName;
|
|
377
|
+
}
|
|
378
|
+
prefetch(opts) {
|
|
379
|
+
const cloudContainer = this.container.cloudContainer;
|
|
380
|
+
if (cloudContainer === undefined)
|
|
381
|
+
throw new Error("no cloud container to prefetch");
|
|
382
|
+
return CloudSqlite_1.CloudSqlite.startCloudPrefetch(cloudContainer, this.dbFileName, opts);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
exports.ITwinWorkspaceDb = ITwinWorkspaceDb;
|
|
386
|
+
/** file extension for local WorkspaceDbs */
|
|
387
|
+
ITwinWorkspaceDb.fileExt = "itwin-workspace";
|
|
388
|
+
/**
|
|
389
|
+
* An editable [[WorkspaceDb]]. This is used by administrators for creating and modifying `WorkspaceDb`s.
|
|
390
|
+
* For cloud-backed containers, the write token must be obtained before this class may be used. Only one user at at time
|
|
391
|
+
* may be editing.
|
|
392
|
+
* @internal
|
|
393
|
+
*/
|
|
394
|
+
class EditableWorkspaceDb extends ITwinWorkspaceDb {
|
|
395
|
+
static validateResourceName(name) {
|
|
396
|
+
ITwinWorkspaceContainer.noLeadingOrTrailingSpaces(name, "resource name");
|
|
397
|
+
if (name.length > 1024)
|
|
398
|
+
throw new Error("resource name too long");
|
|
399
|
+
}
|
|
400
|
+
validateResourceSize(val) {
|
|
401
|
+
const len = typeof val === "string" ? val.length : val.byteLength;
|
|
402
|
+
if (len > (1024 * 1024 * 1024)) // one gigabyte
|
|
403
|
+
throw new Error("value is too large");
|
|
404
|
+
}
|
|
405
|
+
open() {
|
|
406
|
+
this.sqliteDb.openDb(this.dbFileName, core_bentley_1.OpenMode.ReadWrite, this.container.cloudContainer);
|
|
407
|
+
}
|
|
408
|
+
getFileModifiedTime(localFileName) {
|
|
409
|
+
return Math.round(fs.statSync(localFileName).mtimeMs);
|
|
410
|
+
}
|
|
411
|
+
performWriteSql(rscName, sql, bind) {
|
|
412
|
+
this.sqliteDb.withSqliteStatement(sql, (stmt) => {
|
|
413
|
+
stmt.bindString(1, rscName);
|
|
414
|
+
bind === null || bind === void 0 ? void 0 : bind(stmt);
|
|
415
|
+
const rc = stmt.step();
|
|
416
|
+
if (core_bentley_1.DbResult.BE_SQLITE_DONE !== rc) {
|
|
417
|
+
if (core_bentley_1.DbResult.BE_SQLITE_CONSTRAINT_PRIMARYKEY === rc)
|
|
418
|
+
throw new core_common_1.IModelError(rc, `resource "${rscName}" already exists`);
|
|
419
|
+
throw new core_common_1.IModelError(rc, `workspace [${sql}]`);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
this.sqliteDb.saveChanges();
|
|
423
|
+
}
|
|
424
|
+
async createDb(version) {
|
|
425
|
+
if (!this.container.cloudContainer) {
|
|
426
|
+
EditableWorkspaceDb.createEmpty(this.dbFileName);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
// currently the only way to create a workspaceDb in a cloud container is to create a temporary workspaceDb and upload it.
|
|
430
|
+
const tempDbFile = (0, path_1.join)(IModelHost_1.KnownLocations.tmpdir, `empty.${ITwinWorkspaceDb.fileExt}`);
|
|
431
|
+
if (fs.existsSync(tempDbFile))
|
|
432
|
+
IModelJsFs_1.IModelJsFs.removeSync(tempDbFile);
|
|
433
|
+
EditableWorkspaceDb.createEmpty(tempDbFile);
|
|
434
|
+
this.dbFileName = ITwinWorkspaceContainer.makeDbFileName(this.dbName, version);
|
|
435
|
+
await CloudSqlite_1.CloudSqlite.uploadDb(this.container.cloudContainer, { localFileName: tempDbFile, dbName: this.dbFileName });
|
|
436
|
+
IModelJsFs_1.IModelJsFs.removeSync(tempDbFile);
|
|
437
|
+
}
|
|
438
|
+
this.open();
|
|
439
|
+
}
|
|
440
|
+
/** Create a new, empty, EditableWorkspaceDb for importing Workspace resources. */
|
|
441
|
+
static createEmpty(fileName) {
|
|
442
|
+
const db = new SQLiteDb_1.SQLiteDb();
|
|
443
|
+
IModelJsFs_1.IModelJsFs.recursiveMkDirSync((0, path_1.dirname)(fileName));
|
|
444
|
+
db.createDb(fileName);
|
|
445
|
+
const timeStampCol = "lastMod TIMESTAMP NOT NULL DEFAULT(julianday('now'))";
|
|
446
|
+
db.executeSQL(`CREATE TABLE strings(id TEXT PRIMARY KEY NOT NULL,value TEXT,${timeStampCol})`);
|
|
447
|
+
db.executeSQL(`CREATE TABLE blobs(id TEXT PRIMARY KEY NOT NULL,value BLOB,${timeStampCol})`);
|
|
448
|
+
const createTrigger = (tableName) => {
|
|
449
|
+
db.executeSQL(`CREATE TRIGGER ${tableName}_timeStamp AFTER UPDATE ON ${tableName} WHEN old.lastMod=new.lastMod AND old.lastMod != julianday('now') BEGIN UPDATE ${tableName} SET lastMod=julianday('now') WHERE id=new.id; END`);
|
|
450
|
+
};
|
|
451
|
+
createTrigger("strings");
|
|
452
|
+
createTrigger("blobs");
|
|
453
|
+
db.closeDb(true);
|
|
454
|
+
}
|
|
455
|
+
/** Add a new string resource to this WorkspaceDb.
|
|
456
|
+
* @param rscName The name of the string resource.
|
|
457
|
+
* @param val The string to save.
|
|
458
|
+
*/
|
|
459
|
+
addString(rscName, val) {
|
|
460
|
+
EditableWorkspaceDb.validateResourceName(rscName);
|
|
461
|
+
this.validateResourceSize(val);
|
|
462
|
+
this.performWriteSql(rscName, "INSERT INTO strings(id,value) VALUES(?,?)", (stmt) => stmt.bindString(2, val));
|
|
463
|
+
}
|
|
464
|
+
/** Update an existing string resource with a new value.
|
|
465
|
+
* @param rscName The name of the string resource.
|
|
466
|
+
* @param val The new value.
|
|
467
|
+
* @throws if rscName does not exist
|
|
468
|
+
*/
|
|
469
|
+
updateString(rscName, val) {
|
|
470
|
+
this.validateResourceSize(val);
|
|
471
|
+
this.performWriteSql(rscName, "UPDATE strings SET value=?2 WHERE id=?1", (stmt) => stmt.bindString(2, val));
|
|
472
|
+
}
|
|
473
|
+
/** Remove a string resource. */
|
|
474
|
+
removeString(rscName) {
|
|
475
|
+
this.performWriteSql(rscName, "DELETE FROM strings WHERE id=?");
|
|
476
|
+
}
|
|
477
|
+
/** Add a new blob resource to this WorkspaceDb.
|
|
478
|
+
* @param rscName The name of the blob resource.
|
|
479
|
+
* @param val The blob to save.
|
|
480
|
+
*/
|
|
481
|
+
addBlob(rscName, val) {
|
|
482
|
+
EditableWorkspaceDb.validateResourceName(rscName);
|
|
483
|
+
this.validateResourceSize(val);
|
|
484
|
+
this.performWriteSql(rscName, "INSERT INTO blobs(id,value) VALUES(?,?)", (stmt) => stmt.bindBlob(2, val));
|
|
485
|
+
}
|
|
486
|
+
/** Update an existing blob resource with a new value.
|
|
487
|
+
* @param rscName The name of the blob resource.
|
|
488
|
+
* @param val The new value.
|
|
489
|
+
* @throws if rscName does not exist
|
|
490
|
+
*/
|
|
491
|
+
updateBlob(rscName, val) {
|
|
492
|
+
this.validateResourceSize(val);
|
|
493
|
+
this.performWriteSql(rscName, "UPDATE blobs SET value=?2 WHERE id=?1", (stmt) => stmt.bindBlob(2, val));
|
|
494
|
+
}
|
|
495
|
+
/** Get a BlobIO writer for a previously-added blob WorkspaceResource.
|
|
496
|
+
* @note after writing is complete, caller must call `close` on the BlobIO and must call `saveChanges` on the `db`.
|
|
497
|
+
*/
|
|
498
|
+
getBlobWriter(rscName) {
|
|
499
|
+
return this.sqliteDb.withSqliteStatement("SELECT rowid from blobs WHERE id=?", (stmt) => {
|
|
500
|
+
stmt.bindString(1, rscName);
|
|
501
|
+
const blobWriter = SQLiteDb_1.SQLiteDb.createBlobIO();
|
|
502
|
+
blobWriter.open(this.sqliteDb.nativeDb, { tableName: "blobs", columnName: "value", row: stmt.getValueInteger(0), writeable: true });
|
|
503
|
+
return blobWriter;
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
/** Remove a blob resource. */
|
|
507
|
+
removeBlob(rscName) {
|
|
508
|
+
this.performWriteSql(rscName, "DELETE FROM blobs WHERE id=?");
|
|
509
|
+
}
|
|
510
|
+
/** Copy the contents of an existing local file into this WorkspaceDb as a file resource.
|
|
511
|
+
* @param rscName The name of the file resource.
|
|
512
|
+
* @param localFileName The name of a local file to be read.
|
|
513
|
+
* @param fileExt The extension (do not include the leading ".") to be appended to the generated fileName
|
|
514
|
+
* when this WorkspaceDb is extracted from the WorkspaceDb. By default the characters after the last "." in `localFileName`
|
|
515
|
+
* are used. Pass this argument to override that.
|
|
516
|
+
*/
|
|
517
|
+
addFile(rscName, localFileName, fileExt) {
|
|
518
|
+
EditableWorkspaceDb.validateResourceName(rscName);
|
|
519
|
+
fileExt = fileExt !== null && fileExt !== void 0 ? fileExt : (0, path_1.extname)(localFileName);
|
|
520
|
+
if ((fileExt === null || fileExt === void 0 ? void 0 : fileExt[0]) === ".")
|
|
521
|
+
fileExt = fileExt.slice(1);
|
|
522
|
+
this.sqliteDb.nativeDb.embedFile({ name: rscName, localFileName, date: this.getFileModifiedTime(localFileName), fileExt });
|
|
523
|
+
}
|
|
524
|
+
/** Replace an existing file resource with the contents of another local file.
|
|
525
|
+
* @param rscName The name of the file resource.
|
|
526
|
+
* @param localFileName The name of a local file to be read.
|
|
527
|
+
* @throws if rscName does not exist
|
|
528
|
+
*/
|
|
529
|
+
updateFile(rscName, localFileName) {
|
|
530
|
+
this.queryFileResource(rscName); // throws if not present
|
|
531
|
+
this.sqliteDb.nativeDb.replaceEmbeddedFile({ name: rscName, localFileName, date: this.getFileModifiedTime(localFileName) });
|
|
532
|
+
}
|
|
533
|
+
/** Remove a file resource. */
|
|
534
|
+
removeFile(rscName) {
|
|
535
|
+
const file = this.queryFileResource(rscName);
|
|
536
|
+
if (undefined === file)
|
|
537
|
+
throw new Error(`file resource "${rscName}" does not exist`);
|
|
538
|
+
if (file && fs.existsSync(file.localFileName))
|
|
539
|
+
fs.unlinkSync(file.localFileName);
|
|
540
|
+
this.sqliteDb.nativeDb.removeEmbeddedFile(rscName);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
exports.EditableWorkspaceDb = EditableWorkspaceDb;
|
|
544
544
|
//# sourceMappingURL=Workspace.js.map
|