1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Show BSON types correctly

This commit is contained in:
Romein van Buren 2023-01-24 20:55:53 +01:00
parent b58a2ca84f
commit ae811bc0d5
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
7 changed files with 183 additions and 13 deletions

View File

@ -7,6 +7,9 @@
"": {
"name": "frontend",
"version": "0.0.0",
"dependencies": {
"bson": "^4.7.2"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.1",
"eslint": "^8.31.0",
@ -409,6 +412,25 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dev": true
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -419,6 +441,40 @@
"concat-map": "0.0.1"
}
},
"node_modules/bson": {
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/bson/-/bson-4.7.2.tgz",
"integrity": "sha512-Ry9wCtIZ5kGqkJoi6aD8KjxFZEx78guTQDnpXWiNthsxzrxAK/i8E6pCHAIZTbaEFWcOCvbecMukfK7XUvyLpQ==",
"dependencies": {
"buffer": "^5.6.0"
},
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/buffer": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"dependencies": {
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"node_modules/call-bind": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
@ -1888,6 +1944,25 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/ignore": {
"version": "5.2.4",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz",
@ -3496,6 +3571,11 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dev": true
},
"base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -3506,6 +3586,23 @@
"concat-map": "0.0.1"
}
},
"bson": {
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/bson/-/bson-4.7.2.tgz",
"integrity": "sha512-Ry9wCtIZ5kGqkJoi6aD8KjxFZEx78guTQDnpXWiNthsxzrxAK/i8E6pCHAIZTbaEFWcOCvbecMukfK7XUvyLpQ==",
"requires": {
"buffer": "^5.6.0"
}
},
"buffer": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
"requires": {
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"call-bind": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
@ -4523,6 +4620,11 @@
"has-symbols": "^1.0.2"
}
},
"ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="
},
"ignore": {
"version": "5.2.4",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz",

View File

@ -21,5 +21,8 @@
"nodejs",
"svelte3"
]
},
"dependencies": {
"bson": "^4.7.2"
}
}

View File

@ -1,4 +1,5 @@
<script>
import { isBsonBuiltin, isDate } from '../utils';
import Grid from './grid.svelte';
export let data = [];
@ -16,12 +17,13 @@
let items = [];
$: if (data) {
// items = dissectObject(data).map(item => ({ ...item, menu: getRootMenu(item.key, item) }));
items = [];
if (Array.isArray(data)) {
for (const item of data) {
const newItem = {};
newItem.key = item[key];
newItem.key = stringifyValue(item[key]);
newItem.type = getType(item[key]);
newItem.children = dissectObject(item);
newItem.menu = getRootMenu(key, item[key]);
@ -34,7 +36,13 @@
}
function getType(value) {
if (Array.isArray(value)) {
if (isBsonBuiltin(value)) {
return value._bsontype;
}
else if (isDate(value)) {
return 'Date';
}
else if (Array.isArray(value)) {
return `array (${value.length} item${value.length === 1 ? '' : 's'})`;
}
else if (typeof value === 'number') {
@ -43,9 +51,6 @@
}
return 'integer';
}
// else if (new Date(value).toString() !== 'Invalid Date') {
// return 'date';
// }
else if (value === null) {
return 'null';
}
@ -58,12 +63,40 @@
}
}
function stringifyValue(value) {
if (isBsonBuiltin(value)) {
value = value.inspect?.();
if (value.startsWith('new ')) {
value = value.slice(4);
}
if (value.startsWith('Int32(')) {
value = value.slice(6, -1);
}
else if (value.startsWith('Double(')) {
value = value.slice(7, -1);
}
else if (value.startsWith('Binary(Buffer.from(')) {
value = `BinData(${JSON.stringify(value.sub_type || 0)}, ${value.slice(18, -1)}`;
}
}
else if (isDate(value)) {
value = value.toString();
}
return value;
}
function dissectObject(object) {
const entries = [ ...Array.isArray(object) ? object.entries() : Object.entries(object) ];
return entries.map(([ key, value ]) => {
key = key + '';
const type = getType(value);
const child = { key, value, type, menu: value?.menu };
key = key + '';
const child = {
key,
type,
value: stringifyValue(value),
menu: value?.menu,
};
if (type.startsWith('object') || type.startsWith('array')) {
child.children = dissectObject(value);

View File

@ -9,6 +9,7 @@
import { onMount } from 'svelte';
import Grid from '../../../components/grid.svelte';
import { applicationSettings, views } from '../../../stores';
import { EJSON } from 'bson';
export let collection;
@ -36,8 +37,10 @@
async function submitQuery() {
activePath = [];
result = await FindItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
if (result) {
const newResult = await FindItems(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(form));
if (newResult) {
newResult.results = newResult.results?.map(s => EJSON.parse(s, { relaxed: false }));
result = newResult;
submittedForm = JSON.parse(JSON.stringify(form));
}
resetFocus();

View File

@ -48,3 +48,16 @@ export function randomString(length = 12) {
return output;
}
export function isBsonBuiltin(value) {
return (
(typeof value === 'object') &&
(value !== null) &&
(typeof value._bsontype === 'string') &&
(typeof value.inspect === 'function')
);
}
export function isDate(value) {
return (value instanceof Date) && !isNaN(value.getTime());
}

View File

@ -18,7 +18,7 @@ export namespace app {
}
export class findResult {
total: number;
results: any;
results: string[];
static createFrom(source: any = {}) {
return new findResult(source);

View File

@ -11,8 +11,8 @@ import (
)
type findResult struct {
Total int64 `json:"total"`
Results interface{} `json:"results"`
Total int64 `json:"total"`
Results []string `json:"results"`
}
func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findResult {
@ -123,7 +123,23 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes
return out
}
out.Results = results
out.Results = make([]string, 0)
out.Total = total
for _, r := range results {
marshalled, err := bson.MarshalExtJSON(r, true, true)
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Failed to marshal BSON",
Message: err.Error(),
})
return out
}
out.Results = append(out.Results, string(marshalled))
}
fmt.Println(out.Results)
return out
}