Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5614

add support for explicit resource management in the Node driver

    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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

      1. Two new well known symbols (Symbol.dispose and Symbol.asyncDispose) are added
      2. 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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: