MCPcopy Create free account
hub / github.com/bytebase/bytebase / flattenNoSQLColumn

Function flattenNoSQLColumn

frontend/src/utils/sqlResult.ts:50–116  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

48};
49
50const flattenNoSQLColumn = (value: unknown): unknown => {
51 if (typeof value !== "object") {
52 return value;
53 }
54 if (value === null) {
55 return value;
56 }
57 if (Array.isArray(value)) {
58 return value.map(flattenNoSQLColumn);
59 }
60
61 const dict = value as { [key: string]: unknown };
62 if (Object.keys(dict).length === 1 && Object.keys(dict)[0].startsWith("$")) {
63 // Used by the MongoDB response.
64 // https://www.mongodb.com/zh-cn/docs/manual/reference/mongodb-extended-json/#bson-data-types-and-associated-representations
65 const key = Object.keys(dict)[0];
66 switch (key) {
67 case "$oid":
68 return dict[key];
69 case "$date":
70 if (typeof dict[key] !== "object" || dict[key] === null) {
71 return dict[key];
72 }
73 const dateObj = dict[key] as Record<string, unknown>;
74 if (!dateObj["$numberLong"]) {
75 return dict[key];
76 }
77 return new Date(parseInt(dateObj["$numberLong"] as string));
78 case "$numberLong":
79 // Return as string to preserve precision for large integers (> 2^53-1)
80 return dict[key] as string;
81 case "$numberDouble":
82 return parseFloat(dict[key] as string);
83 case "$numberInt":
84 return parseInt(dict[key] as string);
85 case "$numberDecimal":
86 return Number(dict[key]);
87 case "$timestamp":
88 return (dict[key] as { t: number; i: number }).t;
89 case "$binary": {
90 // https://www.mongodb.com/zh-cn/docs/manual/reference/bson-types/#binary-data
91 const { base64, subType } = dict[key] as {
92 base64: string;
93 subType: string;
94 };
95 switch (subType) {
96 case "03":
97 case "04":
98 try {
99 return decodeBase64ToUUID(base64);
100 } catch {
101 return dict[key];
102 }
103 default:
104 return dict[key];
105 }
106 }
107 default:

Callers 1

convertAnyToRowValueFunction · 0.85

Calls 3

parseIntFunction · 0.85
decodeBase64ToUUIDFunction · 0.85
keysMethod · 0.80

Tested by

no test coverage detected