鹏云 李
鹏云 李

Reputation: 27

how can i dynamic update Datasource.groovy file

in a grails project, there will a file named DataSource.groovy. Such as follows:

dataSource {
  pooled = true
  driverClassName = "org.h2.Driver"
  username = "sa"
  password = ""
}

mongodb {
  host = 127.0.0.1// adjust this according to your settings
  port = 27017
  databaseName = 'test'
  username = 'user'  // database user and password, if server requires authentication
  password = 's3cret' 
}

My question is that how can I set for example mongodb.host dynamically at run time.

Upvotes: 1

Views: 529

Answers (1)

Philip Tenn
Philip Tenn

Reputation: 6073

If you have different MongoDB Hosts, you can set up different environments for development, test, and production using the environments closure in your DataSource.groovy.

In your example above, let's say that you are using localhost 127.0.0.1 for development and mongo-prodserver for production

environments {
   development {
       grails {
           mongo {
               host = "127.0.0.1"
               port = 27017
               username = "user"
               password= "s3cret"
               databaseName = "test"
           }
       }
   }
   production {
       grails {
           mongo {
               host = "mongo-prodserver"
               port = 27017
               username = "user"
               password= "s3cret"
               databaseName = "prod"
           }
       }
   }
   ...
}

Here is the link to Grails Doc on DataSources and Environments.

Upvotes: 1

Related Questions