1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00
budibase/packages/backend-core/src/docIds/ids.ts

119 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-03-31 04:07:59 +13:00
import {
APP_PREFIX,
DocumentType,
InternalTable,
SEPARATOR,
} from "../constants"
import { newid } from "./newid"
2023-03-31 04:07:59 +13:00
/**
* Generates a new app ID.
* @returns The new app ID which the app doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export const generateAppID = (tenantId?: string | null) => {
let id = APP_PREFIX
if (tenantId) {
id += `${tenantId}${SEPARATOR}`
}
return `${id}${newid()}`
}
/**
* Gets a new row ID for the specified table.
* @param tableId The table which the row is being created for.
* @param id If an ID is to be used then the UUID can be substituted for this.
* @returns The new ID which a row doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export function generateRowID(tableId: string, id?: string) {
id = id || newid()
return `${DocumentType.ROW}${SEPARATOR}${tableId}${SEPARATOR}${id}`
}
/**
* Generates a new workspace ID.
* @returns The new workspace ID which the workspace doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export function generateWorkspaceID() {
return `${DocumentType.WORKSPACE}${SEPARATOR}${newid()}`
}
/**
* Generates a new global user ID.
* @returns The new user ID which the user doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export function generateGlobalUserID(id?: any) {
return `${DocumentType.USER}${SEPARATOR}${id || newid()}`
}
2023-10-10 05:02:21 +13:00
const isGlobalUserIDRegex = new RegExp(`^${DocumentType.USER}${SEPARATOR}.+`)
export function isGlobalUserID(id: string) {
return isGlobalUserIDRegex.test(id)
}
2023-03-31 04:07:59 +13:00
/**
* Generates a new user ID based on the passed in global ID.
* @param globalId The ID of the global user.
* @returns The new user ID which the user doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export function generateUserMetadataID(globalId: string) {
return generateRowID(InternalTable.USER_METADATA, globalId)
}
/**
* Breaks up the ID to get the global ID.
*/
export function getGlobalIDFromUserMetadataID(id: string) {
const prefix = `${DocumentType.ROW}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}`
if (!id || !id.includes(prefix)) {
return id
}
return id.split(prefix)[1]
}
/**
* Generates a template ID.
* @param ownerId The owner/user of the template, this could be global or a workspace level.
*/
2024-02-29 01:19:08 +13:00
export function generateTemplateID(ownerId: string) {
2023-03-31 04:07:59 +13:00
return `${DocumentType.TEMPLATE}${SEPARATOR}${ownerId}${SEPARATOR}${newid()}`
}
export function generateAppUserID(prodAppId: string, userId: string) {
return `${prodAppId}${SEPARATOR}${userId}`
}
/**
* Generates a new role ID.
* @returns The new role ID which the role doc can be stored under.
2023-03-31 04:07:59 +13:00
*/
export function generateRoleID(name: string) {
const prefix = `${DocumentType.ROLE}${SEPARATOR}`
if (name.startsWith(prefix)) {
return name
}
return `${prefix}${name}`
2023-03-31 04:07:59 +13:00
}
2023-06-28 01:56:24 +12:00
/**
* Utility function to be more verbose.
*/
export function prefixRoleID(name: string) {
return generateRoleID(name)
}
2023-03-31 04:07:59 +13:00
/**
* Generates a new dev info document ID - this is scoped to a user.
* @returns The new dev info ID which info for dev (like api key) can be stored under.
2023-03-31 04:07:59 +13:00
*/
2024-02-29 01:19:08 +13:00
export const generateDevInfoID = (userId: string) => {
2023-03-31 04:07:59 +13:00
return `${DocumentType.DEV_INFO}${SEPARATOR}${userId}`
}
/**
* Generates a new plugin ID - to be used in the global DB.
* @returns The new plugin ID which a plugin metadata document can be stored under.
2023-03-31 04:07:59 +13:00
*/
export const generatePluginID = (name: string) => {
return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
}