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

295 lines
8.5 KiB
JavaScript
Raw Normal View History

const newid = require("./newid")
const {
DocumentType: CoreDocType,
InternalTable,
getRoleParams,
generateRoleID,
APP_DEV_PREFIX,
APP_PREFIX,
SEPARATOR,
StaticDatabases,
2021-09-07 14:58:53 +02:00
isDevAppID,
isProdAppID,
getDevelopmentAppID,
generateAppID,
2022-09-28 09:56:45 +02:00
getQueryIndex,
ViewName,
getDocParams,
getRowParams,
generateRowID,
getUserMetadataParams,
generateUserMetadataID,
getGlobalIDFromUserMetadataID,
} = require("@budibase/backend-core/db")
const { BUDIBASE_DATASOURCE_TYPE } = require("@budibase/backend-core/constants")
2022-11-15 18:03:35 +01:00
const UNICODE_MAX = "\ufff0"
2021-05-13 12:06:08 +02:00
const AppStatus = {
DEV: "development",
ALL: "all",
DEPLOYED: "published",
2021-05-13 12:06:08 +02:00
}
const DocumentType = CoreDocType
const SearchIndexes = {
ROWS: "rows",
}
exports.StaticDatabases = StaticDatabases
const BudibaseInternalDB = {
_id: "bb_internal",
2022-11-15 18:03:35 +01:00
type: BUDIBASE_DATASOURCE_TYPE,
name: "Budibase DB",
source: "BUDIBASE",
config: {},
}
exports.APP_PREFIX = APP_PREFIX
exports.APP_DEV_PREFIX = APP_DEV_PREFIX
2021-09-07 14:58:53 +02:00
exports.isDevAppID = isDevAppID
exports.isProdAppID = isProdAppID
exports.USER_METDATA_PREFIX = `${DocumentType.ROW}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}`
exports.LINK_USER_METADATA_PREFIX = `${DocumentType.LINK}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}`
2022-09-28 09:56:45 +02:00
exports.TABLE_ROW_PREFIX = `${DocumentType.ROW}${SEPARATOR}${DocumentType.TABLE}`
exports.ViewName = ViewName
exports.InternalTables = InternalTable
2022-09-28 09:56:45 +02:00
exports.DocumentType = DocumentType
exports.SEPARATOR = SEPARATOR
2020-10-09 11:46:38 +02:00
exports.UNICODE_MAX = UNICODE_MAX
exports.SearchIndexes = SearchIndexes
2021-05-13 12:06:08 +02:00
exports.AppStatus = AppStatus
exports.BudibaseInternalDB = BudibaseInternalDB
exports.generateAppID = generateAppID
exports.generateDevAppID = getDevelopmentAppID
exports.generateRoleID = generateRoleID
exports.getRoleParams = getRoleParams
2022-09-28 09:56:45 +02:00
exports.getQueryIndex = getQueryIndex
exports.getDocParams = getDocParams
exports.getRowParams = getRowParams
exports.generateRowID = generateRowID
exports.getUserMetadataParams = getUserMetadataParams
exports.generateUserMetadataID = generateUserMetadataID
exports.getGlobalIDFromUserMetadataID = getGlobalIDFromUserMetadataID
/**
* Gets parameters for retrieving tables, this is a utility function for the getDocParams function.
*/
exports.getTableParams = (tableId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.TABLE, tableId, otherProps)
}
/**
* Generates a new table ID.
* @returns {string} The new table ID which the table doc can be stored under.
*/
exports.generateTableID = () => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.TABLE}${SEPARATOR}${newid()}`
}
/**
* Given a row ID this will find the table ID within it (only works for internal tables).
* @param {string} rowId The ID of the row.
* @returns {string} The table ID.
*/
exports.getTableIDFromRowID = rowId => {
const components = rowId
2022-09-28 09:56:45 +02:00
.split(DocumentType.TABLE + SEPARATOR)[1]
.split(SEPARATOR)
2022-09-28 09:56:45 +02:00
return `${DocumentType.TABLE}${SEPARATOR}${components[0]}`
}
/**
* Gets parameters for retrieving automations, this is a utility function for the getDocParams function.
*/
exports.getAutomationParams = (automationId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.AUTOMATION, automationId, otherProps)
}
/**
* Generates a new automation ID.
* @returns {string} The new automation ID which the automation doc can be stored under.
*/
exports.generateAutomationID = () => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.AUTOMATION}${SEPARATOR}${newid()}`
}
/**
* Generates a new link doc ID. This is currently not usable with the alldocs call,
* instead a view is built to make walking to tree easier.
* @param {string} tableId1 The ID of the linker table.
* @param {string} tableId2 The ID of the linked table.
* @param {string} rowId1 The ID of the linker row.
* @param {string} rowId2 The ID of the linked row.
* @param {string} fieldName1 The name of the field in the linker row.
* @param {string} fieldName2 the name of the field in the linked row.
* @returns {string} The new link doc ID which the automation doc can be stored under.
*/
exports.generateLinkID = (
tableId1,
tableId2,
rowId1,
rowId2,
fieldName1,
fieldName2
) => {
const tables = `${SEPARATOR}${tableId1}${SEPARATOR}${tableId2}`
const rows = `${SEPARATOR}${rowId1}${SEPARATOR}${rowId2}`
const fields = `${SEPARATOR}${fieldName1}${SEPARATOR}${fieldName2}`
2022-09-28 09:56:45 +02:00
return `${DocumentType.LINK}${tables}${rows}${fields}`
}
/**
* Gets parameters for retrieving link docs, this is a utility function for the getDocParams function.
*/
exports.getLinkParams = (otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.LINK, null, otherProps)
}
/**
* Generates a new layout ID.
* @returns {string} The new layout ID which the layout doc can be stored under.
*/
2021-05-04 12:32:22 +02:00
exports.generateLayoutID = id => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.LAYOUT}${SEPARATOR}${id || newid()}`
}
/**
* Gets parameters for retrieving layout, this is a utility function for the getDocParams function.
*/
exports.getLayoutParams = (layoutId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.LAYOUT, layoutId, otherProps)
}
/**
* Generates a new screen ID.
* @returns {string} The new screen ID which the screen doc can be stored under.
*/
2020-11-20 18:47:13 +01:00
exports.generateScreenID = () => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.SCREEN}${SEPARATOR}${newid()}`
}
2020-11-03 17:27:28 +01:00
/**
2020-11-20 18:47:13 +01:00
* Gets parameters for retrieving screens, this is a utility function for the getDocParams function.
2020-11-03 17:27:28 +01:00
*/
2020-11-20 18:47:13 +01:00
exports.getScreenParams = (screenId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.SCREEN, screenId, otherProps)
2020-11-20 18:47:13 +01:00
}
/**
* Generates a new webhook ID.
* @returns {string} The new webhook ID which the webhook doc can be stored under.
*/
exports.generateWebhookID = () => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.WEBHOOK}${SEPARATOR}${newid()}`
2020-11-03 17:27:28 +01:00
}
/**
* Gets parameters for retrieving a webhook, this is a utility function for the getDocParams function.
*/
exports.getWebhookParams = (webhookId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.WEBHOOK, webhookId, otherProps)
}
2020-12-18 19:19:43 +01:00
/**
* Generates a new datasource ID.
* @returns {string} The new datasource ID which the webhook doc can be stored under.
*/
exports.generateDatasourceID = ({ plus = false } = {}) => {
return `${
2022-09-28 09:56:45 +02:00
plus ? DocumentType.DATASOURCE_PLUS : DocumentType.DATASOURCE
}${SEPARATOR}${newid()}`
2020-12-18 19:19:43 +01:00
}
/**
* Gets parameters for retrieving a datasource, this is a utility function for the getDocParams function.
*/
exports.getDatasourceParams = (datasourceId = null, otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.DATASOURCE, datasourceId, otherProps)
2020-12-18 19:19:43 +01:00
}
/**
* Generates a new query ID.
* @returns {string} The new query ID which the query doc can be stored under.
*/
2021-05-04 12:32:22 +02:00
exports.generateQueryID = datasourceId => {
2021-01-06 13:28:51 +01:00
return `${
2022-09-28 09:56:45 +02:00
DocumentType.QUERY
2021-01-06 13:28:51 +01:00
}${SEPARATOR}${datasourceId}${SEPARATOR}${newid()}`
2020-12-18 19:19:43 +01:00
}
2022-09-28 09:56:45 +02:00
/**
* Generates a metadata ID for automations, used to track errors in recurring
* automations etc.
*/
exports.generateAutomationMetadataID = automationId => {
return `${DocumentType.AUTOMATION_METADATA}${SEPARATOR}${automationId}`
}
/**
* Retrieve all automation metadata in an app database.
*/
exports.getAutomationMetadataParams = (otherProps = {}) => {
return getDocParams(DocumentType.AUTOMATION_METADATA, null, otherProps)
}
2020-12-18 19:19:43 +01:00
/**
* Gets parameters for retrieving a query, this is a utility function for the getDocParams function.
*/
2021-01-12 17:49:11 +01:00
exports.getQueryParams = (datasourceId = null, otherProps = {}) => {
if (datasourceId == null) {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.QUERY, null, otherProps)
2021-01-12 17:49:11 +01:00
}
return getDocParams(
2022-09-28 09:56:45 +02:00
DocumentType.QUERY,
2021-01-12 17:49:11 +01:00
`${datasourceId}${SEPARATOR}`,
otherProps
)
2020-12-18 19:19:43 +01:00
}
/**
* Generates a new flag document ID.
* @returns {string} The ID of the flag document that was generated.
*/
exports.generateUserFlagID = userId => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.USER_FLAG}${SEPARATOR}${userId}`
}
exports.generateMetadataID = (type, entityId) => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.METADATA}${SEPARATOR}${type}${SEPARATOR}${entityId}`
}
exports.getMetadataParams = (type, entityId = null, otherProps = {}) => {
let docId = `${type}${SEPARATOR}`
if (entityId != null) {
docId += entityId
}
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.METADATA, docId, otherProps)
}
exports.generateMemoryViewID = viewName => {
2022-09-28 09:56:45 +02:00
return `${DocumentType.MEM_VIEW}${SEPARATOR}${viewName}`
}
exports.getMemoryViewParams = (otherProps = {}) => {
2022-09-28 09:56:45 +02:00
return getDocParams(DocumentType.MEM_VIEW, null, otherProps)
}
exports.generatePluginID = name => {
return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
}
/**
* This can be used with the db.allDocs to get a list of IDs
*/
2021-05-04 12:32:22 +02:00
exports.getMultiIDParams = ids => {
return {
keys: ids,
include_docs: true,
}
}