Database Will No Longer Support Callbacks
  • 19 Dec 2024
  • Dark
    Light
  • PDF

Database Will No Longer Support Callbacks

  • Dark
    Light
  • PDF

Article summary

Breaking Change Notice

IAP 2023.2 introduces a breaking change in which the MongoDB Node Driver version will no longer support callbacks.

Global Database Change

Itential has upgraded the MongoDB Node Driver version that is used in the global database variable available to custom applications and adapters. The new version of this driver no longer supports callbacks, instead using a Promise-only API. The rest of the arguments (i.e., filters, updates, options, etc) remain unchanged. Previously, the last argument of all database operations took in a callback function, invoked when the operation completed.

Itential understands that a callback to a Promise-based migration can be a significant refactor. To help support your migration strategy, we have outlined two different approaches below.

What should I do?

Below is an example of a callback-based operation using the global database variable. The callback functions operated in an "error-first" syntax, where any error would be returned as the first argument and the result of a successful operation would be second. Note that in this example an arrow function is used, but traditional function signatures were also allowed.

class myCog {
  myMethod(name, callback) {
    const filter = { name };

    database.collection('my-collection').findOne(filter, (err, doc) => {
      if (err) {
        // Error occurred while performing the operation
        return callback(null, err);
      }

      // Success
      return callback(doc);
    });
  }
}

Now that all operations return a Promise, the first option is to modify the callback function to use then() and catch() methods. Each of these methods takes in a function that is executed in the event of a success or failure and can be chained together.

class myCog {
  myMethod(name, callback) {
    const filter = { name };

    database.collection('my-collection').findOne(filter)
      .then((doc) => {
        // Success
        return callback(doc);
      })
      .catch((err) => {
        // Error occurred while performing the operation
        return callback(null, err)
      });
  }
}

The last option is to use async/await. Defining a function or method as async allows it to await Promises using a synchronously executing code style. This syntax enables a much flatter and simpler syntax. If a Promise is "awaited", it will return the successful result and throw an error if the Promise rejects. Note the async statement added to myMethod and the await statement added to the findOne operation.

class myCog {
  async myMethod(name, callback) {
    const filter = { name };

    try {
      const doc = await database.collection('my-collection').findOne(filter);

      // Success
      return callback(doc);
    } catch (err) {
      // Error occurred while performing the operation
      return callback(null, err);
    }
  }
}

If you experience any issues or need assistance with your migration strategy, please contact the Itential Product Support Team.


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.