MongoParseError: options poolsize, minsize are not supported in typeorm

MongoParseError: options poolsize, minsize are not supported in typeorm

If you are using TypeORM with MongoDB and you encounter the error message MongoParseError: options poolsize, minsize are not supported in typeorm, don’t worry, there is a solution. This error typically occurs when you try to configure the poolSize or minSize options in your TypeORM connection options for MongoDB, but these options are not supported by TypeORM.

Fortunately, there are a couple of solutions to overcome this issue:

Solution 1: Use the native MongoDB driver options

In this solution, we will bypass the limitations of TypeORM and directly use the native MongoDB driver options to configure the connection pool size and minimum size.

Here’s an example of how you can modify your TypeORM connection options:


import { createConnection } from 'typeorm';

createConnection({
  type: 'mongodb',
  host: 'localhost',
  port: 27017,
  database: 'mydatabase',
  entities: [__dirname + '/entities/*.js'],
  synchronize: true,
  useUnifiedTopology: true,
  extra: {
    poolSize: 10,
    minSize: 5,
  },
}).then(connection => {
  // Connection successful
}).catch(error => {
  // Handle connection error
});

By using the extra option, you can pass any additional options directly to the MongoDB driver. In this case, we set the poolSize to 10 and minSize to 5.

Solution 2: Use a custom connection factory

If you prefer a more modular approach, you can create a custom connection factory that handles the connection options and sets the desired pool size and minimum size.

Here’s an example of how you can create a custom connection factory:


import { createConnection, getConnectionOptions } from 'typeorm';

async function createCustomConnection() {
  const connectionOptions = await getConnectionOptions();
  connectionOptions.extra = {
    poolSize: 10,
    minSize: 5,
  };

  return createConnection(connectionOptions);
}

createCustomConnection().then(connection => {
  // Connection successful
}).catch(error => {
  // Handle connection error
});

In this solution, we first retrieve the default connection options using getConnectionOptions(). Then, we modify the extra property to set the desired pool size and minimum size. Finally, we create the connection using the modified options.

With these solutions, you should be able to overcome the MongoParseError: options poolsize, minsize are not supported in typeorm error and configure the pool size and minimum size for your MongoDB connection in TypeORM.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *