1
0
Fork 0
mirror of synced 2024-06-01 18:20:18 +12:00
budibase/packages/auth/src/db/utils.js

66 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-04-19 22:34:07 +12:00
const { newid } = require("../hashing")
exports.ViewNames = {
USER_BY_EMAIL: "by_email",
}
2021-04-07 22:33:16 +12:00
exports.StaticDatabases = {
GLOBAL: {
name: "global-db",
2021-04-07 22:33:16 +12:00
},
}
const DocumentTypes = {
USER: "us",
APP: "app",
2021-04-19 22:34:07 +12:00
GROUP: "group",
2021-04-07 22:33:16 +12:00
}
exports.DocumentTypes = DocumentTypes
2021-04-07 22:33:16 +12:00
const UNICODE_MAX = "\ufff0"
const SEPARATOR = "_"
exports.SEPARATOR = SEPARATOR
2021-04-07 22:33:16 +12:00
/**
* Generates a new global user ID.
2021-04-07 22:33:16 +12:00
* @returns {string} The new user ID which the user doc can be stored under.
*/
exports.generateUserID = () => {
return `${DocumentTypes.USER}${SEPARATOR}${newid()}`
}
2021-04-07 22:33:16 +12:00
/**
2021-04-19 22:34:07 +12: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 22:33:16 +12:00
/**
2021-04-20 03:16:46 +12:00
* Gets parameters for retrieving groups.
*/
exports.getGroupParams = (id = "", otherProps = {}) => {
return {
...otherProps,
startkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}`,
endkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}${UNICODE_MAX}`,
}
}
/**
* Gets parameters for retrieving users.
2021-04-07 22:33:16 +12:00
*/
exports.getUserParams = (globalId = "", otherProps = {}) => {
if (!globalId) {
globalId = ""
}
2021-04-07 22:33:16 +12:00
return {
...otherProps,
startkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}`,
endkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}${UNICODE_MAX}`,
2021-04-07 22:33:16 +12:00
}
}