From 2f32a676d04da58d5b718f039979da0ee0cbc39e Mon Sep 17 00:00:00 2001 From: vchske Date: Tue, 13 Dec 2022 11:36:39 -0800 Subject: [PATCH] Fixes issue when CRUD with no parition key for all APIs (#1362) * 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 --- src/Common/dataAccess/deleteDocument.ts | 3 ++- src/Common/dataAccess/getPartitionKeyValue.ts | 12 ++++++++++++ src/Common/dataAccess/readDocument.ts | 3 ++- src/Common/dataAccess/updateDocument.ts | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/Common/dataAccess/getPartitionKeyValue.ts diff --git a/src/Common/dataAccess/deleteDocument.ts b/src/Common/dataAccess/deleteDocument.ts index 9f4565c94..5caef9e0e 100644 --- a/src/Common/dataAccess/deleteDocument.ts +++ b/src/Common/dataAccess/deleteDocument.ts @@ -4,6 +4,7 @@ import { logConsoleInfo, logConsoleProgress } from "../../Utils/NotificationCons import { client } from "../CosmosClient"; import { getEntityName } from "../DocumentUtility"; import { handleError } from "../ErrorHandlingUtils"; +import { getPartitionKeyValue } from "./getPartitionKeyValue"; export const deleteDocument = async (collection: CollectionBase, documentId: DocumentId): Promise => { const entityName: string = getEntityName(); @@ -13,7 +14,7 @@ export const deleteDocument = async (collection: CollectionBase, documentId: Doc await client() .database(collection.databaseId) .container(collection.id()) - .item(documentId.id(), documentId.partitionKeyValue?.length === 0 ? "" : documentId.partitionKeyValue) + .item(documentId.id(), getPartitionKeyValue(documentId)) .delete(); logConsoleInfo(`Successfully deleted ${entityName} ${documentId.id()}`); } catch (error) { diff --git a/src/Common/dataAccess/getPartitionKeyValue.ts b/src/Common/dataAccess/getPartitionKeyValue.ts new file mode 100644 index 000000000..4d2e24dcb --- /dev/null +++ b/src/Common/dataAccess/getPartitionKeyValue.ts @@ -0,0 +1,12 @@ +import { userContext } from "UserContext"; +import DocumentId from "../../Explorer/Tree/DocumentId"; + +export const getPartitionKeyValue = (documentId: DocumentId) => { + if (userContext.apiType === "Tables" && documentId.partitionKeyValue?.length === 0) { + return ""; + } + if (documentId.partitionKeyValue?.length === 0) { + return undefined; + } + return documentId.partitionKeyValue; +}; diff --git a/src/Common/dataAccess/readDocument.ts b/src/Common/dataAccess/readDocument.ts index 4ff63dfb6..9b7f224e0 100644 --- a/src/Common/dataAccess/readDocument.ts +++ b/src/Common/dataAccess/readDocument.ts @@ -6,6 +6,7 @@ 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 => { const entityName = getEntityName(); @@ -21,7 +22,7 @@ export const readDocument = async (collection: CollectionBase, documentId: Docum const response = await client() .database(collection.databaseId) .container(collection.id()) - .item(documentId.id(), documentId.partitionKeyValue?.length === 0 ? "" : documentId.partitionKeyValue) + .item(documentId.id(), getPartitionKeyValue(documentId)) .read(options); return response?.resource; diff --git a/src/Common/dataAccess/updateDocument.ts b/src/Common/dataAccess/updateDocument.ts index 350bfc734..ff145a6ec 100644 --- a/src/Common/dataAccess/updateDocument.ts +++ b/src/Common/dataAccess/updateDocument.ts @@ -6,6 +6,7 @@ import { logConsoleInfo, logConsoleProgress } from "../../Utils/NotificationCons import { client } from "../CosmosClient"; import { getEntityName } from "../DocumentUtility"; import { handleError } from "../ErrorHandlingUtils"; +import { getPartitionKeyValue } from "./getPartitionKeyValue"; export const updateDocument = async ( collection: CollectionBase, @@ -25,7 +26,7 @@ export const updateDocument = async ( const response = await client() .database(collection.databaseId) .container(collection.id()) - .item(documentId.id(), documentId.partitionKeyValue?.length === 0 ? "" : documentId.partitionKeyValue) + .item(documentId.id(), getPartitionKeyValue(documentId)) .replace(newDocument, options); logConsoleInfo(`Successfully updated ${entityName} ${documentId.id()}`);