-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: internal to MongoDB $jsonSchema conversion COMPASS-8701 #218
feat: internal to MongoDB $jsonSchema conversion COMPASS-8701 #218
Conversation
a035b4b
to
94626d3
Compare
const required = []; | ||
const properties: MongoDBJSONSchema['properties'] = {}; | ||
for (const field of fields) { | ||
if (signal?.aborted) throw new Error('Operation aborted'); |
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 is synchronous code, so there's no way for an abort to happen between multiple iterations of this loop, right?
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, the code is synchronous. I'm not super sure about this abort, the concern was that there might be some extremely nested documents, but most likely that kind of thing would become a problem already at the analysis, so having the abort there is probably enough. What do you think @Anemy ?
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 Anna is getting at is that once this code starts, we're not releasing the javascript thread to do any other work, so we can't have the signal abort except for it already being aborted. To have the functions be cancellable we'll want to yield occasionally, so wrapping these in async and awaiting a promise somewhere inside.
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.
To have the functions be cancellable we'll want to yield occasionally, so wrapping these in async and awaiting a promise somewhere inside.
Fwiw that still wouldn't end up being interruptible by anything other than microtasks – e.g. a user clicking an "abort" button would still never have any effect
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.
Would a solution where we occasionally run a set timeout work nicely? Or do you feel we should consider moving this to a worker worker? I feel a worker will add some complexity to this code to something which typically runs pretty quick most of the time, but does sound like the more elegant solution.
if (yieldCounter > 100) {
yieldCounter = 0;
await new Promise((resolve) => setTimeout(resolve));
}
yieldCounter++;
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.
Yeah, worker would be nice, but we'd have to be careful about not breaking webpack in downstream packages or running in browser environments, which is probably where a lot of the complexity would lie
setTimeout
without a timeout sounds like a decent approach that's a lot simpler, yeah
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.
Thank you both, turns out I didn't actually understand how this works. I've tried the approach with setTimeout
Co-authored-by: Anna Henningsen <[email protected]>
] | ||
} | ||
] | ||
}; |
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.
Not something for this PR, but do we plan to have integration tests at some point, e.g. ones that take documents, generate schemas for them, then add a validator to a real MongoDB database + try to insert those documents?
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.
Great idea! I was planning something similar in the E2E tests in Compass, but having such integration tests in this package definitely makes sense. I will add a ticket.
530563f
into
COMPASS-6862-schema-export-multiple-formats
…S-8702 COMPASS-8709 (#222) * feat: add analyzeDocuments + SchemaAccessor COMPASS-8799 (#216) --------- Co-authored-by: Anna Henningsen <[email protected]> * feat: internal to MongoDB $jsonSchema conversion COMPASS-8701 (#218) --------- Co-authored-by: Anna Henningsen <[email protected]> * feat: internal to Standard JSON Schema conversion COMPASS-8700 (#219) * feat: internal to Expanded JSON Schema conversion COMPASS-8702 (#220) --------- Co-authored-by: Anna Henningsen <[email protected]>
While the internal format is very verbose, for $jsonSchema we keep the basics:
bsonType
(converted from the internal bsonType - the map mostly taken from @mcasimir's POC https://github.com/mongodb-js/compass/pull/5690/files)required
+properties
for objectsitems
for arrayanyOf