fix(mongodb): close client connection on dispose#792
Conversation
📝 WalkthroughWalkthroughThe MongoDB driver now holds a reference to the created ChangesMongoDB Driver Lifecycle
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/drivers/mongodb.ts`:
- Line 32: Declare the shared mongoClient in mongodb.ts as optional/possibly
undefined instead of a definitely assigned MongoClient, since dispose() may run
before getMongoCollection() initializes it. Update the mongoClient declaration
and keep the optional close call in dispose() aligned with that type so strict
definite-assignment checks pass.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
|
|
||
| const driver: DriverFactory<MongoDbOptions, Collection> = (opts) => { | ||
| let collection: Collection; | ||
| let mongoClient: MongoClient; |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== tsconfig strictness =="
fd -HI '^tsconfig.*\.json$' . -x sh -c '
echo "--- $1"
jq "{strict:.compilerOptions.strict, strictNullChecks:.compilerOptions.strictNullChecks}" "$1" 2>/dev/null || cat "$1"
' sh {}
echo
echo "== mongodb driver snippet =="
sed -n '30,40p;117,119p' src/drivers/mongodb.tsRepository: unjs/unstorage
Length of output: 1542
Declare mongoClient as optional. dispose() can run before getMongoCollection() assigns the client, and let mongoClient: MongoClient; can fail strict definite-assignment checks at mongoClient?.close().
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/drivers/mongodb.ts` at line 32, Declare the shared mongoClient in
mongodb.ts as optional/possibly undefined instead of a definitely assigned
MongoClient, since dispose() may run before getMongoCollection() initializes it.
Update the mongoClient declaration and keep the optional close call in dispose()
aligned with that type so strict definite-assignment checks pass.
The mongodb driver creates a
MongoClienton first access but never closes it, so the connection pool stays open afterstorage.dispose(). Theredis,deno-kvanddb0(#693) drivers already close their connection on dispose; this brings mongodb in line.It keeps a reference to the client and closes it in
dispose(). The optional chain covers the case where dispose runs before any connection was opened, andMongoClient.close()is safe to call more than once, so thedriver.dispose()thenstorage.dispose()path in the test harness is fine.Summary by CodeRabbit