Skip to content

Commit f8e2385

Browse files
authored
fix(web): cannot update database record with ObjectId #1524 (#1723)
* fix(web): cannot update database record with ObjectId #1524 * fix conflict of ejson-shell-parser with bson
1 parent 54e6814 commit f8e2385

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

web/package-lock.json

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
"@tanstack/react-query": "^4.29.7",
2424
"ansi_up": "^6.0.2",
2525
"axios": "^1.4.0",
26+
"bson": "^4.6.3",
2627
"clsx": "^1.2.1",
2728
"dayjs": "^1.11.7",
2829
"dotenv": "^16.0.3",
29-
"ejson-shell-parser": "^1.2.4",
30+
"ejson-shell-parser": "^2.0.0",
3031
"focus-visible": "^5.2.0",
3132
"framer-motion": "^10.12.16",
3233
"i18next": "^22.5.0",
@@ -83,4 +84,4 @@
8384
"*.css": "stylelint --fix",
8485
"*.scss": "stylelint --syntax=scss --fix"
8586
}
86-
}
87+
}

web/src/pages/app/database/CollectionDataList/mods/DataPanel/index.tsx

+21-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
InputLeftElement,
99
useColorMode,
1010
} from "@chakra-ui/react";
11+
import { EJSON } from "bson";
1112
import { t } from "i18next";
1213
import { throttle } from "lodash";
1314

@@ -33,6 +34,19 @@ import "./index.css";
3334

3435
import useGlobalStore from "@/pages/globalStore";
3536

37+
function stringifyEJSONAndFormat(data: any) {
38+
// get stringified EJSON
39+
const ejson = EJSON.stringify(data);
40+
41+
// parse ejson string to json object
42+
const parsed = JSON.parse(ejson);
43+
44+
// format json object
45+
const formatted = JSON.stringify(parsed, null, 2);
46+
47+
return formatted;
48+
}
49+
3650
export default function DataPanel() {
3751
const [currentData, setCurrentData] = useState<any>({ data: undefined, record: "{}" });
3852
const globalStore = useGlobalStore();
@@ -133,7 +147,7 @@ export default function DataPanel() {
133147
const handleData = async () => {
134148
let params = {};
135149
try {
136-
params = JSON.parse(currentData.record);
150+
params = EJSON.parse(currentData.record) as any;
137151
if (Object.keys(params).length === 0) {
138152
globalStore.showError(t("DataEntry.CreateError"));
139153
return;
@@ -273,21 +287,18 @@ export default function DataPanel() {
273287
}}
274288
deleteRuleMutation={deleteDataMutation}
275289
component={(item: any) => {
276-
return (
277-
<JSONViewer
278-
colorMode={colorMode}
279-
code={JSON.stringify(item, null, 2)}
280-
className="dataList"
281-
/>
282-
);
290+
const code = JSON.stringify(item, null, 2);
291+
return <JSONViewer colorMode={colorMode} code={code} className="dataList" />;
283292
}}
284293
toolComponent={(item: any) => {
285294
const newData = { ...item };
286295
delete newData._id;
296+
297+
const text = stringifyEJSONAndFormat(newData);
287298
return (
288299
<CopyText
289300
hideToolTip
290-
text={JSON.stringify(newData, null, 2)}
301+
text={text}
291302
tip={String(t("Copied"))}
292303
className="ml-2 hover:bg-gray-200"
293304
>
@@ -312,7 +323,7 @@ export default function DataPanel() {
312323
<div className="mb-4 flex-1 rounded">
313324
<JSONEditor
314325
colorMode={colorMode}
315-
value={JSON.stringify(currentData.data || {}, null, 2)}
326+
value={stringifyEJSONAndFormat(currentData.data || {})}
316327
onChange={(values) => {
317328
setCurrentData((pre: any) => {
318329
return {

0 commit comments

Comments
 (0)