Repository navigation
fix(mongodb): use cursor.transform for doc to entity transformation and skip load broadcast in next if toArray - #11926
Conversation
b08d6ba to
d8136f2
Compare
commit: |
Code Review by Qodo
1. Null transform truncates cursor
|
applyEntityTransformationToCursor and remove redundant monkey patch on cursor.next()applyEntityTransformationToCursor method with correct monkey patch for toArray and next
81ff8ca to
3898694
Compare
34cf0c3 to
6d98759
Compare
|
/review |
6d98759 to
a0b14ae
Compare
a0b14ae to
77f23fb
Compare
ce8f646 to
df5da01
Compare
applyEntityTransformationToCursor method with correct monkey patch for toArray and nexttoArray and next methods with proxy pattern
df5da01 to
7d637db
Compare
eda1d57 to
d49d514
Compare
toArray and next methods with proxy patternnext in monkey-patched toArray method
d49d514 to
79ff557
Compare
|
next in monkey-patched toArray method|
When will it be merged? |
| if (entities.length > 0) { | ||
| await broadcaster.broadcast("Load", metadata, entities) | ||
| } | ||
| return entities |
There was a problem hiding this comment.
any errors that should bubble up to user
There was a problem hiding this comment.
cursor.toArray = async () => {
isInToArray = true
try {
const entities = await originalToArray()
if (entities.length > 0) {
await broadcaster.broadcast("Load", metadata, entities)
}
return entities
} catch (e) {
// if broadcast fails, still return entities?
// or re-throw?
} finally {
isInToArray = false
}
}trying to understand - need to spin up a mongodb instance to give this a try, but given the tests is a scenario covered where we show how that will work?
smith-xyz
left a comment
There was a problem hiding this comment.
just a minor note - but it seems to be an existing issue from before, let me know if I'm missing something in that comment
| if (entities.length > 0) { | ||
| await broadcaster.broadcast("Load", metadata, entities) | ||
| } | ||
| return entities |
There was a problem hiding this comment.
cursor.toArray = async () => {
isInToArray = true
try {
const entities = await originalToArray()
if (entities.length > 0) {
await broadcaster.broadcast("Load", metadata, entities)
}
return entities
} catch (e) {
// if broadcast fails, still return entities?
// or re-throw?
} finally {
isInToArray = false
}
}trying to understand - need to spin up a mongodb instance to give this a try, but given the tests is a scenario covered where we show how that will work?



Closes #11091
The Problem
In the current MongoDB implementation,
applyEntityTransformationToCursormodifies the driver'snext()andtoArray()methods. Because the MongoDB driver may internally call both methods on the same cursor instance (for example, duringfindoperations), this leads to issue:EntitySubscriberInterface.afterLoadmethod is triggered multiple times for the same database record because each patched method independently transforms the document and emits a "Load" event.Description of Change
cursor.transformAPI instead of callingDocumentToEntityTransformer.transform/transformAllin overriddentoArray/next— handlesnext()andtoArray(). AstransformDocument()is called internally by the toArray and next methodsnextastoArraycalls it internally (Ifnextis called alone, broadcast the load events.)transformintypings.tsPull Request Checklist
masterbranchFixes #11091tests/**.test.ts)docs/docs/**.md) (N/A)