Help: Option pkFactory must be an object with a createPk function (since version 4.x)

Hi guys,

I’m using a custom pkFactory since years:

const CustomPKFactory = function() {};
CustomPKFactory.prototype = new Object();
CustomPKFactory.createPk = function () {
    return Random.id();
};

// connect to db
const connectToDatabase = async function (uri) {
    //Performance optimization Step 3: test that database connection exists and is valid
    //before re-using it
    if (cachedDb && cachedDb.serverConfig.isConnected()) {
        console.log('=> using cached database instance');
        return cachedDb;
    }
    const mongoClient = await MongoClient.connect(uri, {
        useUnifiedTopology: true, pkFactory: CustomPKFactory, maxPoolSize: 200,
    });
    cachedClient = mongoClient;
    cachedDb = mongoClient.db('worker');
    return cachedDb;
};

The above code works fine until version 3.7.3 of the MongoDb NodeJS driver. However, as soon as I upgrade to version 4.x I get the following error message:

Option pkFactory must be an object with a createPk function

I can’t find anything about this in the release notes (Releases · mongodb/node-mongodb-native · GitHub), nor have I found a solution on the internet.

Can anyone help me how to get around this error? I need to update the driver.

Thanks in advance!