2022-11-15 19:33:24 +01:00
|
|
|
import newid from "./newid"
|
|
|
|
import { db as dbCore } from "@budibase/backend-core"
|
|
|
|
|
|
|
|
type Optional = string | null
|
|
|
|
|
|
|
|
export const AppStatus = {
|
2021-05-21 11:32:16 +02:00
|
|
|
DEV: "development",
|
2021-05-19 16:09:57 +02:00
|
|
|
ALL: "all",
|
2021-05-21 11:32:16 +02:00
|
|
|
DEPLOYED: "published",
|
2021-05-13 12:06:08 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export const SearchIndexes = {
|
2021-03-26 00:42:50 +01:00
|
|
|
ROWS: "rows",
|
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export const BudibaseInternalDB = {
|
2021-06-15 20:48:05 +02:00
|
|
|
_id: "bb_internal",
|
2022-11-21 12:10:29 +01:00
|
|
|
type: dbCore.BUDIBASE_DATASOURCE_TYPE,
|
2021-06-17 12:40:01 +02:00
|
|
|
name: "Budibase DB",
|
2021-06-15 20:48:05 +02:00
|
|
|
source: "BUDIBASE",
|
|
|
|
config: {},
|
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export const SEPARATOR = dbCore.SEPARATOR
|
|
|
|
export const StaticDatabases = dbCore.StaticDatabases
|
|
|
|
export const DocumentType = dbCore.DocumentType
|
|
|
|
export const APP_PREFIX = dbCore.APP_PREFIX
|
|
|
|
export const APP_DEV_PREFIX = dbCore.APP_DEV_PREFIX
|
|
|
|
export const isDevAppID = dbCore.isDevAppID
|
|
|
|
export const isProdAppID = dbCore.isProdAppID
|
|
|
|
export const USER_METDATA_PREFIX = `${DocumentType.ROW}${SEPARATOR}${dbCore.InternalTable.USER_METADATA}${SEPARATOR}`
|
|
|
|
export const LINK_USER_METADATA_PREFIX = `${DocumentType.LINK}${SEPARATOR}${dbCore.InternalTable.USER_METADATA}${SEPARATOR}`
|
|
|
|
export const TABLE_ROW_PREFIX = `${DocumentType.ROW}${SEPARATOR}${DocumentType.TABLE}`
|
|
|
|
export const ViewName = dbCore.ViewName
|
|
|
|
export const InternalTables = dbCore.InternalTable
|
|
|
|
export const UNICODE_MAX = dbCore.UNICODE_MAX
|
|
|
|
export const generateAppID = dbCore.generateAppID
|
|
|
|
export const generateDevAppID = dbCore.getDevelopmentAppID
|
|
|
|
export const generateRoleID = dbCore.generateRoleID
|
|
|
|
export const getRoleParams = dbCore.getRoleParams
|
|
|
|
export const getQueryIndex = dbCore.getQueryIndex
|
|
|
|
export const getDocParams = dbCore.getDocParams
|
|
|
|
export const getRowParams = dbCore.getRowParams
|
|
|
|
export const generateRowID = dbCore.generateRowID
|
|
|
|
export const getUserMetadataParams = dbCore.getUserMetadataParams
|
|
|
|
export const generateUserMetadataID = dbCore.generateUserMetadataID
|
|
|
|
export const getGlobalIDFromUserMetadataID =
|
|
|
|
dbCore.getGlobalIDFromUserMetadataID
|
2021-09-29 18:43:16 +02:00
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-09 19:49:23 +02:00
|
|
|
* Gets parameters for retrieving tables, this is a utility function for the getDocParams function.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getTableParams(tableId?: Optional, otherProps = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.TABLE, tableId, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-09 19:49:23 +02:00
|
|
|
* Generates a new table ID.
|
|
|
|
* @returns {string} The new table ID which the table doc can be stored under.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateTableID() {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.TABLE}${SEPARATOR}${newid()}`
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:54:32 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getTableIDFromRowID(rowId: string) {
|
2022-02-28 13:54:32 +01:00
|
|
|
const components = rowId
|
2022-09-28 09:56:45 +02:00
|
|
|
.split(DocumentType.TABLE + SEPARATOR)[1]
|
2022-02-28 13:54:32 +01:00
|
|
|
.split(SEPARATOR)
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.TABLE}${SEPARATOR}${components[0]}`
|
2022-02-28 13:54:32 +01:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-02 15:19:39 +02:00
|
|
|
* Gets parameters for retrieving automations, this is a utility function for the getDocParams function.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getAutomationParams(
|
|
|
|
automationId?: Optional,
|
|
|
|
otherProps: any = {}
|
|
|
|
) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.AUTOMATION, automationId, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
|
|
|
* Generates a new automation ID.
|
|
|
|
* @returns {string} The new automation ID which the automation doc can be stored under.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateAutomationID() {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.AUTOMATION}${SEPARATOR}${newid()}`
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2020-10-09 19:49:23 +02:00
|
|
|
* @param {string} tableId1 The ID of the linker table.
|
|
|
|
* @param {string} tableId2 The ID of the linked table.
|
2020-10-09 20:10:28 +02:00
|
|
|
* @param {string} rowId1 The ID of the linker row.
|
|
|
|
* @param {string} rowId2 The ID of the linked row.
|
2021-02-15 18:47:14 +01:00
|
|
|
* @param {string} fieldName1 The name of the field in the linker row.
|
|
|
|
* @param {string} fieldName2 the name of the field in the linked row.
|
2020-10-02 13:37:46 +02:00
|
|
|
* @returns {string} The new link doc ID which the automation doc can be stored under.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateLinkID(
|
|
|
|
tableId1: string,
|
|
|
|
tableId2: string,
|
|
|
|
rowId1: string,
|
|
|
|
rowId2: string,
|
|
|
|
fieldName1: string,
|
|
|
|
fieldName2: string
|
|
|
|
) {
|
2021-02-15 18:47:14 +01:00
|
|
|
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}`
|
2020-10-12 15:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving link docs, this is a utility function for the getDocParams function.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getLinkParams(otherProps: any = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.LINK, null, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 15:53:51 +01:00
|
|
|
/**
|
2020-11-23 15:07:18 +01:00
|
|
|
* Generates a new layout ID.
|
|
|
|
* @returns {string} The new layout ID which the layout doc can be stored under.
|
2020-11-02 15:53:51 +01:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateLayoutID(id: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.LAYOUT}${SEPARATOR}${id || newid()}`
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-23 15:07:18 +01:00
|
|
|
* Gets parameters for retrieving layout, this is a utility function for the getDocParams function.
|
2020-11-02 15:53:51 +01:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getLayoutParams(layoutId?: Optional, otherProps: any = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.LAYOUT, layoutId, otherProps)
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
/**
|
|
|
|
* Generates a new screen ID.
|
|
|
|
* @returns {string} The new screen ID which the screen doc can be stored under.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateScreenID() {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.SCREEN}${SEPARATOR}${newid()}`
|
2020-11-03 18:42:54 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getScreenParams(screenId?: Optional, otherProps: any = {}) {
|
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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateWebhookID() {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.WEBHOOK}${SEPARATOR}${newid()}`
|
2020-11-03 17:27:28 +01:00
|
|
|
}
|
|
|
|
|
2020-10-22 18:48:32 +02:00
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving a webhook, this is a utility function for the getDocParams function.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getWebhookParams(webhookId?: Optional, otherProps: any = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.WEBHOOK, webhookId, otherProps)
|
2020-10-22 18:48:32 +02:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateDatasourceID({ plus = false } = {}) {
|
2021-06-04 11:50:02 +02:00
|
|
|
return `${
|
2022-09-28 09:56:45 +02:00
|
|
|
plus ? DocumentType.DATASOURCE_PLUS : DocumentType.DATASOURCE
|
2021-06-04 11:50:02 +02:00
|
|
|
}${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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getDatasourceParams(
|
|
|
|
datasourceId?: Optional,
|
|
|
|
otherProps: any = {}
|
|
|
|
) {
|
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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateQueryID(datasourceId: string) {
|
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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateAutomationMetadataID(automationId: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.AUTOMATION_METADATA}${SEPARATOR}${automationId}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve all automation metadata in an app database.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getAutomationMetadataParams(otherProps: any = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
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.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getQueryParams(datasourceId?: Optional, otherProps: any = {}) {
|
2021-01-12 17:49:11 +01:00
|
|
|
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
|
|
|
}
|
2021-02-17 19:04:21 +01:00
|
|
|
|
2021-12-07 19:24:10 +01:00
|
|
|
/**
|
|
|
|
* Generates a new flag document ID.
|
|
|
|
* @returns {string} The ID of the flag document that was generated.
|
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateUserFlagID(userId: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.USER_FLAG}${SEPARATOR}${userId}`
|
2021-12-07 19:24:10 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateMetadataID(type: string, entityId: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.METADATA}${SEPARATOR}${type}${SEPARATOR}${entityId}`
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getMetadataParams(
|
|
|
|
type: string,
|
|
|
|
entityId?: Optional,
|
|
|
|
otherProps: any = {}
|
|
|
|
) {
|
2021-09-08 20:29:28 +02:00
|
|
|
let docId = `${type}${SEPARATOR}`
|
|
|
|
if (entityId != null) {
|
|
|
|
docId += entityId
|
|
|
|
}
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.METADATA, docId, otherProps)
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generateMemoryViewID(viewName: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.MEM_VIEW}${SEPARATOR}${viewName}`
|
2021-09-20 19:24:09 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getMemoryViewParams(otherProps: any = {}) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return getDocParams(DocumentType.MEM_VIEW, null, otherProps)
|
|
|
|
}
|
|
|
|
|
2022-11-15 19:33:24 +01:00
|
|
|
export function generatePluginID(name: string) {
|
2022-09-28 09:56:45 +02:00
|
|
|
return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
|
2021-09-20 19:24:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 19:04:21 +01:00
|
|
|
/**
|
2021-02-18 14:38:57 +01:00
|
|
|
* This can be used with the db.allDocs to get a list of IDs
|
2021-02-17 19:04:21 +01:00
|
|
|
*/
|
2022-11-15 19:33:24 +01:00
|
|
|
export function getMultiIDParams(ids: string[]) {
|
2021-02-18 14:38:57 +01:00
|
|
|
return {
|
|
|
|
keys: ids,
|
|
|
|
include_docs: true,
|
2021-02-17 19:04:21 +01:00
|
|
|
}
|
|
|
|
}
|