Database no longer supports callbacks

Breaking change notice

Itential Platform 2023.2 introduces a breaking change in which the MongoDB Node Driver version no longer supports callbacks.

Global database change

Itential has upgraded the MongoDB Node Driver version used in the global database variable available to custom applications and adapters. The new version no longer supports callbacks — it uses a Promise-only API. All other arguments (filters, updates, options, etc.) remain unchanged. Previously, the last argument of all database operations accepted a callback function that was invoked when the operation completed.

What should I do?

The following example shows a callback-based operation using the global database variable. Callback functions used an “error-first” syntax, where any error was returned as the first argument and the result of a successful operation as the second:

1class myCog {
2 myMethod(name, callback) {
3 const filter = { name };
4
5 database.collection('my-collection').findOne(filter, (err, doc) => {
6 if (err) {
7 return callback(null, err);
8 }
9 return callback(doc);
10 });
11 }
12}

There are two approaches for migrating to the Promise-based API:

Option 1: Use then() and catch()

Chain .then() and .catch() methods on the returned Promise. Each takes a function executed on success or failure:

1class myCog {
2 myMethod(name, callback) {
3 const filter = { name };
4
5 database.collection('my-collection').findOne(filter)
6 .then((doc) => {
7 return callback(doc);
8 })
9 .catch((err) => {
10 return callback(null, err);
11 });
12 }
13}

Option 2: Use async/await

Define the method as async and await the Promise. This produces a flatter, simpler syntax. If a Promise is awaited, it returns the successful result or throws an error if the Promise rejects:

1class myCog {
2 async myMethod(name, callback) {
3 const filter = { name };
4
5 try {
6 const doc = await database.collection('my-collection').findOne(filter);
7 return callback(doc);
8 } catch (err) {
9 return callback(null, err);
10 }
11 }
12}

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