diff --git a/src/Contracts/DataModels.ts b/src/Contracts/DataModels.ts index 482de6985..e16ebdf73 100644 --- a/src/Contracts/DataModels.ts +++ b/src/Contracts/DataModels.ts @@ -88,6 +88,8 @@ export interface SubscriptionPolicies { export interface Resource { _partitionKeyValue?: string; + _partitionKey?: string; + _attachments?: string; _rid: string; _self: string; _etag: string; diff --git a/src/Explorer/Tabs/DocumentTabUtils.tsx b/src/Explorer/Tabs/DocumentTabUtils.tsx index 768e99526..e7cc8912d 100644 --- a/src/Explorer/Tabs/DocumentTabUtils.tsx +++ b/src/Explorer/Tabs/DocumentTabUtils.tsx @@ -14,10 +14,11 @@ export interface IDocumentsTabContentState { isFilterOptionVisible: boolean; isEditorVisible: boolean; documentContent: string; + selectedSqlDocumentContent: string; documentIds: Array; documentSqlIds: Array; selectedDocumentId?: DocumentId; - selectedSqlDocumentId?: Resource; + selectedSqlDocumentId?: DocumentId; isEditorContentEdited: boolean; isAllDocumentsVisible: boolean; } @@ -79,14 +80,15 @@ export function formatDocumentContent(row: DocumentId): string { } export function formatSqlDocumentContent(row: Resource): string { - const { id, _rid, _self, _ts, _etag, _partitionKeyValue } = row; + const { id, _rid, _self, _ts, _etag, _partitionKey, _attachments } = row; const documentContent = JSON.stringify({ id: id || "", _rid: _rid || "", _self: _self || "", _ts: _ts || "", _etag: _etag || "", - _partitionKeyValue: _partitionKeyValue || "", + _attachments: _attachments || "", + _partitionKey: _partitionKey || "", }); const formattedDocumentContent = documentContent.replace(/,/g, ",\n").replace("{", "{\n").replace("}", "\n}"); return formattedDocumentContent; diff --git a/src/Explorer/Tabs/DocumentsTabContent.tsx b/src/Explorer/Tabs/DocumentsTabContent.tsx index a6511630e..82cfcffae 100644 --- a/src/Explorer/Tabs/DocumentsTabContent.tsx +++ b/src/Explorer/Tabs/DocumentsTabContent.tsx @@ -27,6 +27,7 @@ import { Areas } from "../../Common/Constants"; import { createDocument as createSqlDocuments } from "../../Common/dataAccess/createDocument"; import { deleteDocument as deleteSqlDocument } from "../../Common/dataAccess/deleteDocument"; import { queryDocuments as querySqlDocuments } from "../../Common/dataAccess/queryDocuments"; +import { readDocument } from "../../Common/dataAccess/readDocument"; import { updateDocument as updateSqlDocuments } from "../../Common/dataAccess/updateDocument"; import { getEntityName } from "../../Common/DocumentUtility"; import { getErrorMessage, getErrorStack } from "../../Common/ErrorHandlingUtils"; @@ -78,8 +79,8 @@ export default class DocumentsTabContent extends React.Component { - const formattedDocumentContent = - userContext.apiType === "Mongo" - ? formatDocumentContent(row as DocumentId) - : formatSqlDocumentContent(row as Resource); userContext.apiType === "Mongo" - ? this.updateContent(row as DocumentId, formattedDocumentContent) - : this.updateSqlContent(row as Resource, formattedDocumentContent); + ? this.updateContent(row as DocumentId, formatDocumentContent(row as DocumentId)) + : this.updateSqlContent(row as Resource); this.setDefaultUpdateTabButtonVisibility(); }; @@ -266,12 +264,17 @@ export default class DocumentsTabContent extends React.Component { + private updateSqlContent = async (row: Resource): Promise => { + const selectedDocumentId: DocumentId = new DocumentId(this.props as DocumentsTab, row, row._partitionKeyValue); + + const content = await readDocument(this.props.collection, selectedDocumentId); + const formattedDocumentContent = formatSqlDocumentContent((content as unknown) as Resource); this.setState( { documentContent: formattedDocumentContent, isEditorVisible: true, - selectedSqlDocumentId: row, + selectedSqlDocumentContent: formattedDocumentContent, + selectedSqlDocumentId: selectedDocumentId, }, () => { this.updateTabButton(); @@ -287,26 +290,15 @@ export default class DocumentsTabContent extends React.Component { - const { partitionKey, partitionKeyProperty, isExecutionError, isExecuting, collection } = this.props; - const { documentContent } = this.state; - const partitionKeyArray = extractPartitionKey( - this.state.selectedSqlDocumentId, - getPartitionKeyDefinition(partitionKey, partitionKeyProperty) as PartitionKeyDefinition - ); - - const partitionKeyValue = partitionKeyArray && partitionKeyArray[0]; - const selectedDocumentId = new DocumentId( - this.props as DocumentsTab, - this.state.selectedSqlDocumentId, - partitionKeyValue - ); + const { isExecutionError, isExecuting, collection } = this.props; + const { documentContent, selectedSqlDocumentId } = this.state; isExecutionError(false); const startKey: number = this.getStartKey(Action.UpdateDocument); try { isExecuting(true); const updateSqlDocumentRes = await updateSqlDocuments( collection as ViewModels.Collection, - selectedDocumentId, + selectedSqlDocumentId, JSON.parse(documentContent) ); if (updateSqlDocumentRes) { @@ -464,7 +456,7 @@ export default class DocumentsTabContent extends React.Component { const confirmationMessage = `Are you sure you want to delete the selected ${getEntityName()} ?`; - const { isExecuting, collection, partitionKey, partitionKeyProperty } = this.props; + const { isExecuting, collection } = this.props; const startKey: number = this.getStartKey(Action.DeleteDocument); this.props.collection.container.showOkCancelModalDialog( confirmationMessage, @@ -481,17 +473,8 @@ export default class DocumentsTabContent extends React.Component { fakeDocumentData._self = "testSelf"; fakeDocumentData._ts = "testTs"; fakeDocumentData._etag = "testEtag"; - fakeDocumentData._partitionKeyValue = "testPartitionKeyValue"; + fakeDocumentData._attachments = "testAttachments"; + fakeDocumentData._partitionKey = "testPartitionKey"; + const formattedContent: string = DocumentTabUtils.formatSqlDocumentContent(fakeDocumentData); expect(formattedContent).toBe( - `{\n"id":"testId",\n"_rid":"testRid",\n"_self":"testSelf",\n"_ts":"testTs",\n"_etag":"testEtag",\n"_partitionKeyValue":"testPartitionKeyValue"\n}` + `{\n"id":"testId",\n"_rid":"testRid",\n"_self":"testSelf",\n"_ts":"testTs",\n"_etag":"testEtag",\n"_attachments":"testAttachments",\n"_partitionKey":"testPartitionKey"\n}` ); }); @@ -58,31 +60,31 @@ describe("DocumentTabUtils", () => { fakeDocumentData._self = undefined; fakeDocumentData._ts = undefined; fakeDocumentData._etag = undefined; - fakeDocumentData._partitionKeyValue = undefined; + fakeDocumentData._attachments = undefined; + fakeDocumentData._partitionKey = undefined; const formattedContent: string = DocumentTabUtils.formatSqlDocumentContent(fakeDocumentData); expect(formattedContent).toBe( - `{\n"id":"",\n"_rid":"",\n"_self":"",\n"_ts":"",\n"_etag":"",\n"_partitionKeyValue":""\n}` + `{\n"id":"",\n"_rid":"",\n"_self":"",\n"_ts":"",\n"_etag":"",\n"_attachments":"",\n"_partitionKey":""\n}` ); }); + }); + describe("getPartitionKeyDefinition()", () => { + const partitionKey = {} as PartitionKey; + partitionKey.kind = "Hash"; + partitionKey.version = 1; + partitionKey.systemKey = true; + const partitionKeyProperty = "testPartitionKey"; - describe("getPartitionKeyDefinition()", () => { - const partitionKey = {} as PartitionKey; - partitionKey.kind = "Hash"; - partitionKey.version = 1; - partitionKey.systemKey = true; - const partitionKeyProperty = "testPartitionKey"; + it("should return formatted partitionKey with formatted path.", () => { + partitionKey.paths = ["test"]; + const formattedPartitionKey = DocumentTabUtils.getPartitionKeyDefinition(partitionKey, partitionKeyProperty); + expect(formattedPartitionKey).toEqual({ kind: "Hash", version: 1, systemKey: true, paths: ["test"] }); + }); - it("should return formatted partitionKey with formatted path.", () => { - partitionKey.paths = ["test"]; - const formattedPartitionKey = DocumentTabUtils.getPartitionKeyDefinition(partitionKey, partitionKeyProperty); - expect(formattedPartitionKey).toEqual({ kind: "Hash", version: 1, systemKey: true, paths: ["test"] }); - }); - - it("should return partitionKey with undefined paths if paths is undefined.", () => { - partitionKey.paths = undefined; - const formattedPartitionKey = DocumentTabUtils.getPartitionKeyDefinition(partitionKey, partitionKeyProperty); - expect(formattedPartitionKey).toEqual({ kind: "Hash", version: 1, systemKey: true, paths: undefined }); - }); + it("should return partitionKey with undefined paths if paths is undefined.", () => { + partitionKey.paths = undefined; + const formattedPartitionKey = DocumentTabUtils.getPartitionKeyDefinition(partitionKey, partitionKeyProperty); + expect(formattedPartitionKey).toEqual({ kind: "Hash", version: 1, systemKey: true, paths: undefined }); }); }); });