2021-05-04 19:13:44 +02:00
|
|
|
const env = require("../environment")
|
|
|
|
|
|
|
|
const SLOT_REFRESH_MS = 2000
|
|
|
|
const CONNECT_TIMEOUT_MS = 10000
|
2021-04-27 18:29:05 +02:00
|
|
|
const SEPARATOR = "-"
|
2021-05-05 18:49:34 +02:00
|
|
|
const REDIS_URL = !env.REDIS_URL ? "localhost:6379" : env.REDIS_URL
|
|
|
|
const REDIS_PASSWORD = !env.REDIS_PASSWORD ? "budibase" : env.REDIS_PASSWORD
|
2021-04-27 18:29:05 +02:00
|
|
|
|
|
|
|
exports.Databases = {
|
|
|
|
PW_RESETS: "pwReset",
|
2021-05-05 13:11:06 +02:00
|
|
|
INVITATIONS: "invitation",
|
2021-05-12 18:37:09 +02:00
|
|
|
DEV_LOCKS: "devLocks",
|
2021-05-21 14:07:10 +02:00
|
|
|
DEBOUNCE: "debounce",
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:13:44 +02:00
|
|
|
exports.getRedisOptions = (clustered = false) => {
|
2021-05-05 18:49:34 +02:00
|
|
|
const [host, port] = REDIS_URL.split(":")
|
2021-05-04 19:13:44 +02:00
|
|
|
const opts = {
|
|
|
|
connectTimeout: CONNECT_TIMEOUT_MS,
|
|
|
|
}
|
|
|
|
if (clustered) {
|
|
|
|
opts.redisOptions = {}
|
|
|
|
opts.redisOptions.tls = {}
|
2021-05-05 18:49:34 +02:00
|
|
|
opts.redisOptions.password = REDIS_PASSWORD
|
2021-05-04 19:13:44 +02:00
|
|
|
opts.slotsRefreshTimeout = SLOT_REFRESH_MS
|
|
|
|
opts.dnsLookup = (address, callback) => callback(null, address)
|
|
|
|
} else {
|
|
|
|
opts.host = host
|
|
|
|
opts.port = port
|
2021-05-05 18:49:34 +02:00
|
|
|
opts.password = REDIS_PASSWORD
|
2021-05-04 19:13:44 +02:00
|
|
|
}
|
|
|
|
return { opts, host, port }
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:29:05 +02:00
|
|
|
exports.addDbPrefix = (db, key) => {
|
2021-05-13 16:32:03 +02:00
|
|
|
if (key.includes(db)) {
|
|
|
|
return key
|
|
|
|
}
|
2021-04-27 18:29:05 +02:00
|
|
|
return `${db}${SEPARATOR}${key}`
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.removeDbPrefix = key => {
|
|
|
|
let parts = key.split(SEPARATOR)
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
parts.shift()
|
|
|
|
return parts.join(SEPARATOR)
|
|
|
|
} else {
|
|
|
|
// return the only part
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
}
|