/
connect.coffee
42 lines (39 loc) · 1.55 KB
/
connect.coffee
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
31
32
33
34
35
36
37
38
39
40
41
42
# Database configurations. We use this to configure database access and then
# get the driver instance (Db object).
databases = {}
# Configure a database.
#
# You can call this with one argument, a database name, and it will apply the
# default configuration.
#
# You can call this with database name and an object containing configuration
# options. Supported options are:
# - host -- Database host name (defaults to 127.0.0.1)
# - port -- Database port number (defaults to 27017)
# - pool -- Connection pool size (defaults to 10)
# - name -- Actual database name if different from name argument
#
# You can also call this with an object, where each key is a database name, and
# the corresponding value the database configuration.
exports.configure = configure = (name, options)->
{ Configuration } = require("./database")
if name.constructor == Object
configs = name
for name, options of configs
configure name, options
else
throw "Already have configuration for #{name}" if databases[name]
options ||= {}
config = new Configuration(options.name || name, options)
databases[name] = config
configure.default ||= name
# Default database name. If not set, pick the first database.
configure.default = null
configure.DEFAULT = "development"
# Provides access to the specified database (null for default database).
exports.connect = connect = (name)->
{ Database } = require("./database")
name ||= configure.default || process.env.NODE_ENV || configure.DEFAULT
unless databases[name]
configure name
return new Database(databases[name])