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

Pass through results of given lambda in withTransaction and withSession

    • 3
    • Needed
    • Needed
    • 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?

      The provided withTransaction and withSession helpers make running transactions very simple, but it's difficult to get the results of the operation back.

      Both functions take a lambda as an argument that must return a Promise, but the functions don't pass through the value that the promise resolves to. This means that I need to create a wrapper function like this in order to get the return value:

      runTransaction(writeFunc) {
      
      	let writeResults = null;
      
      	const txnFunc = session => (
      
      		writeFunc(session)
      			.then((results) => { writeResults = results; })
      	);
      
      	return this.client.withSession(session => session.withTransaction(txnFunc)))
      		.then(() => {
      
      			return writeResults;
      		});
      }
      

      It's not the end of the world, but it would be nice if these functions could just pass through the results.


      Use Case

      As a Node.js user
      I want a meaningful result from the withTransaction/withSession API
      So that I can write robust handling code for the total result of the operations in the function passed to those APIs

      User Impact

      • Breaking change The withTransaction API has returned the command result or "undefined" depending on the transaction outcome. It will now return the result of the function passed in by the user.

      Dependencies

      • None

      Unknowns

      • None

      Acceptance Criteria

      Implementation Requirements

      • Capture the result of the resolved promise from the user's withTransaction function.
        • Return the captured value from the withTransaction API
      • "" - for withSession
      • Update typescript to automatically defined the return type from the type of the given function

      Testing Requirements

      • withTransaction should return the result of the function regardless of transaction outcome
      • Both withSession/withTransaction should throw/reject if the function passed in throws/rejects
      • Add typescript tests that demonstrate automatic return type calculation

      Documentation Requirements

      Follow Up Requirements

      • None

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            dominicp Dominic
            Bailey Pearson
            Votes:
            6 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: