mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-23 10:51:30 +00:00
* Fixes issue where empty partition key is treated like nonexistent key for Tables API * Updated format * Refactor fix * Prettier * Fixes issue when CRUD with no parition key for all APIs * Format fix * Refactor to explicitly check for Tables
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { Item, RequestOptions } from "@azure/cosmos";
|
|
import { CollectionBase } from "../../Contracts/ViewModels";
|
|
import DocumentId from "../../Explorer/Tree/DocumentId";
|
|
import { logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
|
|
import { HttpHeaders } from "../Constants";
|
|
import { client } from "../CosmosClient";
|
|
import { getEntityName } from "../DocumentUtility";
|
|
import { handleError } from "../ErrorHandlingUtils";
|
|
import { getPartitionKeyValue } from "./getPartitionKeyValue";
|
|
|
|
export const readDocument = async (collection: CollectionBase, documentId: DocumentId): Promise<Item> => {
|
|
const entityName = getEntityName();
|
|
const clearMessage = logConsoleProgress(`Reading ${entityName} ${documentId.id()}`);
|
|
|
|
try {
|
|
const options: RequestOptions =
|
|
documentId.partitionKey.kind === "MultiHash"
|
|
? {
|
|
[HttpHeaders.partitionKey]: documentId.partitionKeyValue,
|
|
}
|
|
: {};
|
|
const response = await client()
|
|
.database(collection.databaseId)
|
|
.container(collection.id())
|
|
.item(documentId.id(), getPartitionKeyValue(documentId))
|
|
.read(options);
|
|
|
|
return response?.resource;
|
|
} catch (error) {
|
|
handleError(error, "ReadDocument", `Failed to read ${entityName} ${documentId.id()}`);
|
|
throw error;
|
|
} finally {
|
|
clearMessage();
|
|
}
|
|
};
|