Skip to content

Commit 1dce3d3

Browse files
committed
replace undefined with null before stringifying the value and sort object keys according the columns order
1 parent fd37709 commit 1dce3d3

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/devtools/components/main-content/object-store-view/AddObjectsButton.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
createSignal,
77
onMount,
88
Show,
9-
untrack,
109
} from "solid-js";
1110

1211
import UnstyledButton from "@/devtools/components/buttons/UnstyledButton";
@@ -103,17 +102,16 @@ export default function AddObjectsButton() {
103102
editor = createJSONEditor(editorRef, "");
104103
});
105104
const editorData = createMemo(() => {
105+
const columns = query.data?.columns;
106106
const activeStore = query.data?.activeStore;
107-
if (tableMutationStore.selectedObjects.length > 0) {
108-
return JSON.stringify(tableMutationStore.selectedObjects, null, 2);
109-
} else {
110-
if (activeStore) {
111-
const columns = untrack(() => query.data?.columns || []);
112-
return getSampleValue(columns);
113-
} else {
114-
return "";
115-
}
107+
108+
if (columns && tableMutationStore.selectedObjects.length > 0) {
109+
return stringifyData(tableMutationStore.selectedObjects, columns);
110+
} else if (columns && activeStore) {
111+
const data = getSampleValue(columns);
112+
return stringifyData(data, columns);
116113
}
114+
return "";
117115
});
118116
createEffect(() => {
119117
editor.setOptions({ value: editorData() });
@@ -203,7 +201,22 @@ function getSampleValue(columns: TableColumn[]) {
203201
return [column.name, value] as [string, TableColumnValue];
204202
});
205203
const obj = Object.fromEntries(keyValuePairs);
206-
return JSON.stringify([obj], null, 2);
204+
return [obj];
205+
}
206+
207+
function stringifyData(data: TableRow[], columns: TableColumn[]) {
208+
// sort the data keys according to columns' order
209+
const objects = data.map((row) => {
210+
const keyValuePairs = columns.map((col) => [col.name, row[col.name]]);
211+
return Object.fromEntries(keyValuePairs);
212+
});
213+
214+
return JSON.stringify(
215+
objects,
216+
// replace undefined with null
217+
(_, val) => (val === undefined ? null : val),
218+
2,
219+
);
207220
}
208221

209222
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)