budibase/packages/auth/src/db/utils.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-04-19 12:34:07 +02:00
const { newid } = require("../hashing")
2021-04-07 12:33:16 +02:00
exports.StaticDatabases = {
GLOBAL: {
name: "global-db",
2021-04-07 12:33:16 +02:00
},
}
const DocumentTypes = {
USER: "us",
APP: "app",
2021-04-19 12:34:07 +02:00
GROUP: "group",
2021-04-07 12:33:16 +02:00
}
exports.DocumentTypes = DocumentTypes
2021-04-07 12:33:16 +02:00
const UNICODE_MAX = "\ufff0"
const SEPARATOR = "_"
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}`
}
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
/**
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
*/
exports.getUserParams = (email = "", otherProps = {}) => {
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}`,
}
}