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-09-09 13:32:16 +02:00
|
|
|
VERIFICATIONS: "verification",
|
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-07-06 19:10:04 +02:00
|
|
|
SESSIONS: "session",
|
|
|
|
USER_CACHE: "users",
|
2021-09-10 15:37:34 +02:00
|
|
|
FLAGS: "flags",
|
2021-11-15 18:40:45 +01:00
|
|
|
APP_METADATA: "appMetadata",
|
2021-12-16 17:58:15 +01:00
|
|
|
QUERY_VARS: "queryVars",
|
2022-03-03 14:37:04 +01:00
|
|
|
LICENSES: "license",
|
2022-05-23 16:03:52 +02:00
|
|
|
GENERIC_CACHE: "data_cache",
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
exports.SEPARATOR = SEPARATOR
|
|
|
|
|
2021-05-04 19:13:44 +02:00
|
|
|
exports.getRedisOptions = (clustered = false) => {
|
2022-03-10 16:53:23 +01:00
|
|
|
let password = REDIS_PASSWORD
|
|
|
|
let url = REDIS_URL.split("//")
|
|
|
|
// get rid of the protocol
|
|
|
|
url = url.length > 1 ? url[1] : url[0]
|
|
|
|
// check for a password etc
|
|
|
|
url = url.split("@")
|
|
|
|
if (url.length > 1) {
|
|
|
|
// get the password
|
|
|
|
password = url[0].split(":")[1]
|
|
|
|
url = url[1]
|
|
|
|
} else {
|
|
|
|
url = url[0]
|
|
|
|
}
|
|
|
|
const [host, port] = url.split(":")
|
2021-09-09 18:14:24 +02:00
|
|
|
|
|
|
|
let redisProtocolUrl
|
|
|
|
|
|
|
|
// fully qualified redis URL
|
2022-03-14 17:50:01 +01:00
|
|
|
if (/rediss?:\/\//.test(REDIS_URL)) {
|
2021-09-09 18:14:24 +02:00
|
|
|
redisProtocolUrl = REDIS_URL
|
|
|
|
}
|
|
|
|
|
2021-05-04 19:13:44 +02:00
|
|
|
const opts = {
|
|
|
|
connectTimeout: CONNECT_TIMEOUT_MS,
|
|
|
|
}
|
|
|
|
if (clustered) {
|
|
|
|
opts.redisOptions = {}
|
|
|
|
opts.redisOptions.tls = {}
|
2022-03-10 16:53:23 +01:00
|
|
|
opts.redisOptions.password = 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
|
2022-03-10 16:53:23 +01:00
|
|
|
opts.password = password
|
2021-05-04 19:13:44 +02:00
|
|
|
}
|
2021-09-09 18:14:24 +02:00
|
|
|
return { opts, host, port, redisProtocolUrl }
|
2021-05-04 19:13:44 +02:00
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|