Database managementΒΆ

Carbond makes it easy to manage connections to multiple databases in your application. The Service class has two properties for specifying database URIs:

  • dbUri: A connection string specified as a MongoDB URI (e.g. "mongodb://username:password@localhost:27017/mydb"). The Service will connect to this database on startup. The application can then reference a connection to this database via the db property on the Service.
  • dbUris: A mapping of names to MongoDB URIs . The Service will connect to these databases on startup. The application can reference a connection to these databases via the Service as dbs[<name>] or dbs.<name>.

Examples

A Service with a single db connection:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
__(function() {
  module.exports = o.main({
    _type: carbon.carbond.Service,
    port: 8888,
    dbUri: "mongodb://localhost:27017/mydb",
    endpoints: {
      hello: o({
        _type: carbon.carbond.Endpoint,
        get: function(req) {
          return this.getService().db
                                  .getCollection('messages')
                                  .find()
                                  .toArray()
        }
      })
    }
  })
})

A Service that connects to multiple databases:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
__(function() {
  module.exports = o.main({
    _type: carbon.carbond.Service,
    port: 8888,
    dbUris: {
      main: 'mongodb://localhost:27017/mydb',
      reporting: 'mongodb://localhost:27017/reporting'
    },
    endpoints: {
      messages: o({
        _type: carbon.carbond.Endpoint,
        get: function(req) {
          return this.getService().dbs['main']
                                  .getCollection('messages')
                                  .find()
                                  .toArray()
        }
      }),
      dashboards: o({
        _type: carbon.carbond.Endpoint,
        get: function(req) {
          return this.getService().dbs['reporting']
                                  .getCollection('dashboards')
                                  .find()
                                  .toArray()
        }
      })
    }
  })
})