2021-04-19 12:34:07 +02:00
|
|
|
const { newid } = require("../hashing")
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
exports.StaticDatabases = {
|
2021-04-16 19:09:34 +02:00
|
|
|
GLOBAL: {
|
|
|
|
name: "global-db",
|
2021-04-07 12:33:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const DocumentTypes = {
|
|
|
|
USER: "us",
|
2021-04-13 14:26:13 +02:00
|
|
|
APP: "app",
|
2021-04-19 12:34:07 +02:00
|
|
|
GROUP: "group",
|
2021-04-20 19:14:36 +02:00
|
|
|
CONFIG: "config",
|
2021-04-07 12:33:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
exports.DocumentTypes = DocumentTypes
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
const UNICODE_MAX = "\ufff0"
|
|
|
|
const SEPARATOR = "_"
|
|
|
|
|
2021-04-13 14:26:13 +02:00
|
|
|
exports.SEPARATOR = SEPARATOR
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
/**
|
|
|
|
* Generates a new user ID based on the passed in email.
|
|
|
|
* @param {string} email The email which the ID is going to be built up of.
|
|
|
|
* @returns {string} The new user ID which the user doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateUserID = email => {
|
|
|
|
return `${DocumentTypes.USER}${SEPARATOR}${email}`
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:31:58 +02:00
|
|
|
exports.getEmailFromUserID = userId => {
|
|
|
|
return userId.split(`${DocumentTypes.USER}${SEPARATOR}`)[1]
|
|
|
|
}
|
|
|
|
|
2021-04-19 12:34:07 +02:00
|
|
|
/**
|
|
|
|
* Generates a new group ID.
|
|
|
|
* @returns {string} The new group ID which the group doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateGroupID = () => {
|
|
|
|
return `${DocumentTypes.GROUP}${SEPARATOR}${newid()}`
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
/**
|
2021-04-19 17:16:46 +02:00
|
|
|
* Gets parameters for retrieving groups.
|
|
|
|
*/
|
|
|
|
exports.getGroupParams = (id = "", otherProps = {}) => {
|
|
|
|
return {
|
|
|
|
...otherProps,
|
|
|
|
startkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}`,
|
|
|
|
endkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}${UNICODE_MAX}`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-20 19:14:36 +02:00
|
|
|
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
|
2021-04-07 12:33:16 +02:00
|
|
|
*/
|
|
|
|
exports.getUserParams = (email = "", otherProps = {}) => {
|
2021-04-09 16:11:49 +02:00
|
|
|
if (!email) {
|
|
|
|
email = ""
|
|
|
|
}
|
2021-04-07 12:33:16 +02:00
|
|
|
return {
|
|
|
|
...otherProps,
|
|
|
|
startkey: `${DocumentTypes.USER}${SEPARATOR}${email}`,
|
|
|
|
endkey: `${DocumentTypes.USER}${SEPARATOR}${email}${UNICODE_MAX}`,
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 19:14:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new configuration ID.
|
|
|
|
* @returns {string} The new configuration ID which the config doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateConfigID = (type = "", group = "") => {
|
|
|
|
group += SEPARATOR
|
|
|
|
|
|
|
|
return `${
|
|
|
|
DocumentTypes.CONFIG
|
|
|
|
}${SEPARATOR}${type}${SEPARATOR}${group}${newid()}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving configurations.
|
|
|
|
*/
|
|
|
|
exports.getConfigParams = (type = "", group = "", otherProps = {}) => {
|
|
|
|
return {
|
|
|
|
...otherProps,
|
|
|
|
startkey: `${DocumentTypes.CONFIG}${SEPARATOR}${type}${SEPARATOR}${group}`,
|
|
|
|
endkey: `${DocumentTypes.CONFIG}${SEPARATOR}${type}${SEPARATOR}${group}${UNICODE_MAX}`,
|
|
|
|
}
|
|
|
|
}
|