NestJS TypeORM config for MongoDB Replica Set


File ormconfig.json

Method 1: Same ports

{
  "type": "mongodb",
  "host": "mongodb-repl-1,mongodb-repl-2",
  "port": 27017,
  "database": "test",
  "replicaSet": "rs0",
  "synchronize": true,
  "logging": false,
  "entities": [
    "dist/entity/*.js"
  ],
  "subscribers": [
    "dist/subscriber/*.js"
  ],
  "migrations": [
    "dist/migration/*.js"
  ],
  "cli": {
    "entitiesDir": "dist/entity",
    "migrationsDir": "dist/migration",
    "subscribersDir": "dist/subscriber"
  }
}

Method 2: Different ports

{
  "type": "mongodb",
  "url": "mongodb://localhost:27017,localhost:27018/test?replicaSet=rs0",  
  "synchronize": true,
  "logging": false,
  "entities": [
    "dist/entity/*.js"
  ],
  "subscribers": [
    "dist/subscriber/*.js"
  ],
  "migrations": [
    "dist/migration/*.js"
  ],
  "cli": {
    "entitiesDir": "dist/entity",
    "migrationsDir": "dist/migration",
    "subscribersDir": "dist/subscriber"
  }
}

Leave a Reply