Repository navigation
Expand file tree
/
Copy pathmigrate-mongo-config.js
More file actions
77 lines (63 loc) · 2.03 KB
/
Copy pathmigrate-mongo-config.js
File metadata and controls
77 lines (63 loc) · 2.03 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require('dotenv').config()
const config = require('config')
const appEnv = process.env.NODE_ENV || 'development'
function getConfiguredValue (key) {
const configPath = `${appEnv}.${key}`
return config.has(configPath) ? config.get(configPath) : null
}
function getMongoConnectionString () {
if (process.env.MONGO_CONN_STRING) {
return process.env.MONGO_CONN_STRING
}
const dbUser = process.env.MONGO_USER || getConfiguredValue('username')
const dbPassword = process.env.MONGO_PASSWORD || getConfiguredValue('password')
const dbHost = process.env.MONGO_HOST || getConfiguredValue('host')
const dbPort = process.env.MONGO_PORT || getConfiguredValue('port')
const dbName = getMongoDatabaseName()
const dbLoginPrepend = dbUser && dbPassword ? `${dbUser}:${dbPassword}@` : ''
if (process.env.useAWS) {
return `mongodb://${dbLoginPrepend}${dbHost}:${dbPort}/${dbName}?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false`
}
return `mongodb://${dbLoginPrepend}${dbHost}:${dbPort}/${dbName}`
}
function getDatabaseNameFromUri () {
if (!process.env.MONGO_CONN_STRING) {
return null
}
try {
const uri = new URL(process.env.MONGO_CONN_STRING)
const databaseName = uri.pathname.replace(/^\//, '')
return databaseName || null
} catch (err) {
return null
}
}
function getMongoDatabaseName () {
return process.env.MONGO_DB_NAME || getDatabaseNameFromUri() || getConfiguredValue('database')
}
function getMongoConnectionOptions () {
if (process.env.useAWS) {
return {
authMechanism: 'SCRAM-SHA-1',
tls: false
}
}
return {
tls: false
}
}
const migrationConfig = {
mongodb: {
url: getMongoConnectionString(),
databaseName: getMongoDatabaseName(),
options: getMongoConnectionOptions()
},
migrationsDir: 'migrations',
changelogCollectionName: 'migrations_changelog',
lockCollectionName: 'migrations_changelog_lock',
lockTtl: 0,
migrationFileExtension: '.js',
useFileHash: false,
moduleSystem: 'commonjs'
}
module.exports = migrationConfig