-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: TypeScript
-
Labels:
Use Case
As a user of the Node driver,
I want the Node driver to support async resource management,
So that so that I can use the explicit resource management proposal without workarounds.
Examples of the code users can write if we support explicit resource management in our API:
async function foo() { using client = await MongoClient.connect(..); } foo(); // `client` is automatically closed when we leave the scope of `foo` // interesting example, can we use this for transactions? async function bar(){ using session = client.startSession().withTransaction(); await collection.insertOne({name: 'yay resource management'}, { session }); } // transaction automatically commits when we leave the scope of `bar()`
Background
Ecmascript has moved explicit resource management to stage 3, and now major runtimes and bundlers are adding support for the helpers (examples include Nodejs, webpack, and Typescript).
The interesting part of this proposal is that there are two parts
- Two new well known symbols (Symbol.dispose and Symbol.asyncDispose) are added
- New syntax (and tslib helpers) have been added that use the new well known symbols to provide scope-based resource management
Practically, this means that while we can't use 2) in the Node driver (without modifications to our build process), we can support 1) for public facing APIs.
A list of APIs that can be made disposable:
- MongoClient
- Sessions (maybe)
- Cursors
- ChangeStreams
- ClientEncryption
Typescript provides a suggested implementation to polyfill the new symbols in the TS5.2 release notes.
If/when we pick this up, we'll want a design that outlines 1) which parts of the driver can/should be disposable and 2) what disposal semantics mean for that part of the driver.
We will need to consider TS support on TS versions < 5.2. Defining `Symbol.asyncDispose` on the MongoClient may cause TS issues for users on TS versions where `Symbol.asyncDispose` is not defined.
- related to
-
NODE-5221 Design client close() behavior
- Blocked
- links to