-
Notifications
You must be signed in to change notification settings - Fork 85
[WIP] output full bson objects MONGOSH-1285 #2584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| // doesn't have readBufferedDocuments or toArray. We can try cast things to | ||
| // ServiceProviderAbstractCursor, but then that's not assignable to | ||
| // ServiceProviderAnyCursor. And that's why there's so much casting below. | ||
| const cursor = (this._sp[key] as any)(...args) as any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just had the thought that one obvious way around this is to just have cursorMethod() for regular cursors and changeCursorMethod() for change stream cursors. And just replace ReturnType<ServiceProvider[K]> with something a lot more specific. Then we can probably have much better type safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, although I think either way is fine here
| const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom'); | ||
|
|
||
| function cursorNext( | ||
| original: () => Promise<Document | null> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can probably use some typescript to automatically pick this type off abstract cursor. Something like that. Same for the other cursor methods below.
| return async function (): Promise<Document | null> { | ||
| const result = await original(); | ||
| if (result) { | ||
| replaceWithCustomInspect(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just suddenly had the realisation that this is a terrible function name. addCustomInspect() rather?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's that bad, but yes, addCustomInspect() sounds good 🙂
| this: DeepInspectServiceProviderWrapper, | ||
| ...args: Parameters<Required<ServiceProvider>[K]> | ||
| ): // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-ignore The returntype already contains a promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A way around all this is to just make one function similar to bsonMethod() for each unique return type, kinda like what I did for the cursor methods.
| this: DeepInspectServiceProviderWrapper, | ||
| ...args: Parameters<Required<ServiceProvider>[K]> | ||
| ): ReturnType<Required<ServiceProvider>[K]> { | ||
| // not wrapping the result at all because forwardedMethod() is for simple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the decision here to leave everything that's not definitely a bson document or array alone. Make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! 🙂
| maxStringLength: Infinity, | ||
| }; | ||
|
|
||
| // reuse the standard inpect logic for an object without causing infinite |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels very hacky, but I haven't thought of a better way to do it yet. I'm trying to avoid having to write my own function that will format an array or object exactly the way util.inspect() already does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We know it's BSON, so it should be fine to use a shallow copy of the document/array, right?
| for (const item of obj) { | ||
| replaceWithCustomInspect(item); | ||
| } | ||
| } else if (obj && typeof obj === 'object' && obj !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, again, I made the decision to only touch arrays and documents/sub-documents and leave simple values alone. BUT this currently also installs our inspect function on BSON types like long, decimal, etc. Which we probably don't want?
What's the best way to do that? Look for obj._bsontype?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for some reason this code doesn't mess up BSON object formatting 🤔 I suppose the prototype still has the BSONValue one? https://github.com/mongodb/js-bson/blob/main/src/bson_value.ts#L38
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the best way to do that? Look for
obj._bsontype?
We're pretty close to adopting BSON 7.x, where we have obj[bsonType] as a more reliable symbol property (but yeah, that's the best way to check for existing BSON objects)
2428a91 to
4b2991c
Compare
| undefined, | ||
| undefined, | ||
| initialServiceProvider | ||
| this.initialServiceProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw it is VERY easy to accidentally pass initialServiceProvider (ie. the unwrapped value) to something in place of this.initialServiceProvider. Ask me how I know..
| return result; | ||
| } | ||
|
|
||
| function replaceWithCustomInspect(obj: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something obvious to note here: I'm mutating the obj in place rather than creating new objects. I did it that way for simplicity and because I'm worried about the impact on performance making a gazillion new objects, but admittedly I haven't profiled that yet. Let me know if you think there's a better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should be fine 👍
| // doesn't have readBufferedDocuments or toArray. We can try cast things to | ||
| // ServiceProviderAbstractCursor, but then that's not assignable to | ||
| // ServiceProviderAnyCursor. And that's why there's so much casting below. | ||
| const cursor = (this._sp[key] as any)(...args) as any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, although I think either way is fine here
| // ServiceProviderAnyCursor. And that's why there's so much casting below. | ||
| const cursor = (this._sp[key] as any)(...args) as any; | ||
|
|
||
| cursor.next = cursorNext( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd consider returning a wrapped cursor instead of modifying the existing one, if that's possible – like the wrapping for the ServiceProvider itself, this would make sure that we actually wrap all methods that we should and aren't forgetting about any
|
|
||
| return results; | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ All of these methods are essentially the same, no?
| return async function (): Promise<Document | null> { | ||
| const result = await original(); | ||
| if (result) { | ||
| replaceWithCustomInspect(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's that bad, but yes, addCustomInspect() sounds good 🙂
| this: DeepInspectServiceProviderWrapper, | ||
| ...args: Parameters<Required<ServiceProvider>[K]> | ||
| ): ReturnType<Required<ServiceProvider>[K]> { | ||
| // not wrapping the result at all because forwardedMethod() is for simple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! 🙂
| maxStringLength: Infinity, | ||
| }; | ||
|
|
||
| // reuse the standard inpect logic for an object without causing infinite |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We know it's BSON, so it should be fine to use a shallow copy of the document/array, right?
| return result; | ||
| } | ||
|
|
||
| function replaceWithCustomInspect(obj: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should be fine 👍
|
|
||
| function replaceWithCustomInspect(obj: any) { | ||
| if (Array.isArray(obj)) { | ||
| (obj as any)[customInspectSymbol] = customDocumentInspect; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (obj as any)[customInspectSymbol] = customDocumentInspect; | |
| (obj as any)[customInspectSymbol] ??= customDocumentInspect; |
maybe unnecessarily defensive, but if something already has a custom inspect function for some reason, we can probably leave that in place
| for (const item of obj) { | ||
| replaceWithCustomInspect(item); | ||
| } | ||
| } else if (obj && typeof obj === 'object' && obj !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the best way to do that? Look for
obj._bsontype?
We're pretty close to adopting BSON 7.x, where we have obj[bsonType] as a more reliable symbol property (but yeah, that's the best way to check for existing BSON objects)
MONGOSH-1285
I built this on top of @addaleax's wip branch, so the types are still mostly from there.
The short version of what we're trying to do: For BSON that we get from the database we want to print all of it, untruncated, when it gets evaluated or otherwise inspected with util.inspect().
This solution wraps the ServiceProvider with another class that implements all the same methods. Then:
I have been testing it with this document which should be affected by the inspectOptions
depth,maxArrayLengthandmaxStringLength:And this doc has every BSON type which is useful for testing that we're not messing up existing BSON formatting:
You can test that the find cursor's tryNext was replaced by running:
(Notice that the whole array, string and object all printed.)
This will exercise the inspect function on the top-level array that was returned.
You can test that it recursively installed it on the documents inside that array by running:
or even
This needs a lot of tests. There are almost certainly some cases left that I've missed. And I'm unsure about some details. Just opening to have a discussion. Oh and we might want a way for users to opt out of it.