Fixed sqldocument delete and update issue

This commit is contained in:
sunilyadav840 2021-07-23 20:27:47 +05:30
parent ea21ab2580
commit 52807a9a0e
4 changed files with 55 additions and 67 deletions

View File

@ -88,6 +88,8 @@ export interface SubscriptionPolicies {
export interface Resource {
_partitionKeyValue?: string;
_partitionKey?: string;
_attachments?: string;
_rid: string;
_self: string;
_etag: string;

View File

@ -14,10 +14,11 @@ export interface IDocumentsTabContentState {
isFilterOptionVisible: boolean;
isEditorVisible: boolean;
documentContent: string;
selectedSqlDocumentContent: string;
documentIds: Array<DocumentId>;
documentSqlIds: Array<Resource>;
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;

View File

@ -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<DocumentsTab, I
{
key: "_id",
name: isPreferredApiMongoDB ? "_id" : "id",
minWidth: 90,
maxWidth: 140,
minWidth: 50,
maxWidth: 100,
isResizable: true,
isCollapsible: true,
data: "string",
@ -96,7 +97,7 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
key: "column2",
name: props.partitionKeyPropertyHeader,
minWidth: 50,
maxWidth: 60,
maxWidth: 100,
isResizable: true,
isCollapsible: true,
data: "number",
@ -128,6 +129,7 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
documentSqlIds: [],
isEditorContentEdited: false,
isAllDocumentsVisible: false,
selectedSqlDocumentContent: this.initialDocumentContent,
};
}
@ -160,7 +162,7 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
try {
const sqlQuery = querySqlDocuments(this.props.collection.databaseId, this.props.collection.id(), query, options);
const querySqlDocumentsData = await sqlQuery.fetchNext();
this.setState({ documentSqlIds: querySqlDocumentsData.resources.length ? querySqlDocumentsData.resources : [] });
this.setState({ documentSqlIds: querySqlDocumentsData.resources?.length ? querySqlDocumentsData.resources : [] });
} catch (error) {
this.props.isExecutionError(true);
const errorMessage = getErrorMessage(error);
@ -243,13 +245,9 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
};
private handleRowContent = (row: DocumentId | Resource): void => {
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<DocumentsTab, I
);
};
private updateSqlContent = (row: Resource, formattedDocumentContent: string): void => {
private updateSqlContent = async (row: Resource): Promise<void> => {
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<DocumentsTab, I
};
async updateSqlDocument(): Promise<void> {
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<DocumentsTab, I
private async onDeleteExisitingDocumentClick(): Promise<void> {
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<DocumentsTab, I
);
this.queryDocumentsData();
} else {
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
);
await deleteSqlDocument(collection as ViewModels.Collection, selectedDocumentId);
const { selectedSqlDocumentId } = this.state;
await deleteSqlDocument(collection as ViewModels.Collection, selectedSqlDocumentId);
this.querySqlDocumentsData();
}
this.setTraceSuccess(Action.DeleteDocument, startKey);
@ -506,11 +489,9 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
}
private onRevertExisitingDocumentClick(): void {
const { selectedDocumentId, selectedSqlDocumentId } = this.state;
const { selectedDocumentId, selectedSqlDocumentContent } = this.state;
const documentContent =
userContext.apiType === "Mongo"
? formatDocumentContent(selectedDocumentId)
: formatSqlDocumentContent(selectedSqlDocumentId);
userContext.apiType === "Mongo" ? formatDocumentContent(selectedDocumentId) : selectedSqlDocumentContent;
this.setState({
documentContent: documentContent,
});
@ -551,7 +532,8 @@ export default class DocumentsTabContent extends React.Component<DocumentsTab, I
try {
const savedDocument = await createSqlDocuments(collection, document);
if (savedDocument) {
this.handleRowContent(savedDocument as Resource);
const formattedDocumentContent = formatSqlDocumentContent((savedDocument as unknown) as Resource);
this.setState({ documentContent: formattedDocumentContent });
this.setDefaultUpdateTabButtonVisibility();
this.setTraceSuccess(Action.CreateDocument, startKey);
}

View File

@ -45,10 +45,12 @@ describe("DocumentTabUtils", () => {
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 });
});
});
});