cosmos-explorer/src/Common/DataAccessUtilityBase.ts

183 lines
5.6 KiB
TypeScript
Raw Normal View History

import {
ConflictDefinition,
FeedOptions,
ItemDefinition,
OfferDefinition,
QueryIterator,
Resource
} from "@azure/cosmos";
import { RequestOptions } from "@azure/cosmos/dist-esm";
import Q from "q";
import { configContext, Platform } from "../ConfigContext";
import * as DataModels from "../Contracts/DataModels";
import { MessageTypes } from "../Contracts/ExplorerContracts";
import * as ViewModels from "../Contracts/ViewModels";
2020-07-27 16:05:25 -05:00
import ConflictId from "../Explorer/Tree/ConflictId";
import DocumentId from "../Explorer/Tree/DocumentId";
import StoredProcedure from "../Explorer/Tree/StoredProcedure";
import { LocalStorageUtility, StorageKey } from "../Shared/StorageUtility";
import { OfferUtils } from "../Utils/OfferUtils";
import * as Constants from "./Constants";
import { client } from "./CosmosClient";
import * as HeadersUtility from "./HeadersUtility";
import { sendCachedDataMessage } from "./MessageHandler";
export function getCommonQueryOptions(options: FeedOptions): any {
const storedItemPerPageSetting: number = LocalStorageUtility.getEntryNumber(StorageKey.ActualItemPerPage);
options = options || {};
options.populateQueryMetrics = true;
options.enableScanInQuery = options.enableScanInQuery || true;
if (!options.partitionKey) {
options.forceQueryPlan = true;
}
options.maxItemCount =
options.maxItemCount ||
(storedItemPerPageSetting !== undefined && storedItemPerPageSetting) ||
Constants.Queries.itemsPerPage;
options.maxDegreeOfParallelism = LocalStorageUtility.getEntryNumber(StorageKey.MaxDegreeOfParellism);
return options;
}
2020-07-24 16:45:48 -05:00
export function queryDocuments(
databaseId: string,
containerId: string,
query: string,
options: any
): Q.Promise<QueryIterator<ItemDefinition & Resource>> {
options = getCommonQueryOptions(options);
const documentsIterator = client()
2020-07-24 16:45:48 -05:00
.database(databaseId)
.container(containerId)
.items.query(query, options);
return Q(documentsIterator);
}
2020-07-27 16:05:25 -05:00
export function getPartitionKeyHeaderForConflict(conflictId: ConflictId): Object {
2020-07-24 16:45:48 -05:00
const partitionKeyDefinition: DataModels.PartitionKey = conflictId.partitionKey;
const partitionKeyValue: any = conflictId.partitionKeyValue;
return getPartitionKeyHeader(partitionKeyDefinition, partitionKeyValue);
}
export function getPartitionKeyHeader(partitionKeyDefinition: DataModels.PartitionKey, partitionKeyValue: any): Object {
if (!partitionKeyDefinition) {
return undefined;
}
2020-07-24 16:45:48 -05:00
if (partitionKeyValue === undefined) {
return [{}];
}
2020-07-24 16:45:48 -05:00
return [partitionKeyValue];
}
export function updateDocument(
collection: ViewModels.CollectionBase,
documentId: DocumentId,
newDocument: any
): Q.Promise<any> {
const partitionKey = documentId.partitionKeyValue;
2020-07-24 16:45:48 -05:00
return Q(
client()
2020-07-24 16:45:48 -05:00
.database(collection.databaseId)
.container(collection.id())
.item(documentId.id(), partitionKey)
.replace(newDocument)
.then(response => response.resource)
2020-07-24 16:45:48 -05:00
);
}
export function executeStoredProcedure(
2020-07-24 16:45:48 -05:00
collection: ViewModels.Collection,
storedProcedure: StoredProcedure,
partitionKeyValue: any,
params: any[]
): Q.Promise<any> {
// TODO remove this deferred. Kept it because of timeout code at bottom of function
const deferred = Q.defer<any>();
client()
.database(collection.databaseId)
.container(collection.id())
.scripts.storedProcedure(storedProcedure.id())
.execute(partitionKeyValue, params, { enableScriptLogging: true })
.then(response =>
deferred.resolve({
result: response.resource,
scriptLogs: response.headers[Constants.HttpHeaders.scriptLogResults]
})
)
.catch(error => deferred.reject(error));
return deferred.promise.timeout(
Constants.ClientDefaults.requestTimeoutMs,
`Request timed out while executing stored procedure ${storedProcedure.id()}`
2020-07-24 16:45:48 -05:00
);
}
2020-07-24 16:45:48 -05:00
export function createDocument(collection: ViewModels.CollectionBase, newDocument: any): Q.Promise<any> {
return Q(
client()
2020-07-24 16:45:48 -05:00
.database(collection.databaseId)
.container(collection.id())
.items.create(newDocument)
.then(response => response.resource)
2020-07-24 16:45:48 -05:00
);
}
export function readDocument(collection: ViewModels.CollectionBase, documentId: DocumentId): Q.Promise<any> {
const partitionKey = documentId.partitionKeyValue;
2020-07-24 16:45:48 -05:00
return Q(
client()
2020-07-24 16:45:48 -05:00
.database(collection.databaseId)
.container(collection.id())
.item(documentId.id(), partitionKey)
.read()
.then(response => response.resource)
2020-07-24 16:45:48 -05:00
);
}
2020-07-27 16:05:25 -05:00
export function deleteDocument(collection: ViewModels.CollectionBase, documentId: DocumentId): Q.Promise<any> {
2020-07-24 16:45:48 -05:00
const partitionKey = documentId.partitionKeyValue;
2020-07-24 16:45:48 -05:00
return Q(
client()
2020-07-24 16:45:48 -05:00
.database(collection.databaseId)
.container(collection.id())
.item(documentId.id(), partitionKey)
.delete()
);
}
2020-07-24 16:45:48 -05:00
export function deleteConflict(
collection: ViewModels.CollectionBase,
2020-07-27 16:05:25 -05:00
conflictId: ConflictId,
2020-07-24 16:45:48 -05:00
options: any = {}
): Q.Promise<any> {
options.partitionKey = options.partitionKey || getPartitionKeyHeaderForConflict(conflictId);
2020-07-24 16:45:48 -05:00
return Q(
client()
2020-07-24 16:45:48 -05:00
.database(collection.databaseId)
.container(collection.id())
.conflict(conflictId.id())
.delete(options)
);
}
2020-07-24 16:45:48 -05:00
export function queryConflicts(
databaseId: string,
containerId: string,
query: string,
options: any
): Q.Promise<QueryIterator<ConflictDefinition & Resource>> {
const documentsIterator = client()
2020-07-24 16:45:48 -05:00
.database(databaseId)
.container(containerId)
.conflicts.query(query, options);
return Q(documentsIterator);
}