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

46 lines
927 B
JavaScript
Raw Normal View History

const uuid = require("uuid/v4")
exports.ViewNames = {
USER_BY_EMAIL: "by_email",
}
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-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 global user ID.
2021-04-07 12:33:16 +02:00
* @returns {string} The new user ID which the user doc can be stored under.
*/
exports.generateUserID = () => {
return `${DocumentTypes.USER}${SEPARATOR}${uuid()}`
}
2021-04-07 12:33:16 +02:00
/**
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
*/
exports.getUserParams = (globalId = "", otherProps = {}) => {
if (!globalId) {
globalId = ""
}
2021-04-07 12:33:16 +02:00
return {
...otherProps,
startkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}`,
endkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}${UNICODE_MAX}`,
2021-04-07 12:33:16 +02:00
}
}