2020-10-02 13:37:46 +02:00
|
|
|
const newid = require("./newid")
|
2021-05-13 14:29:53 +02:00
|
|
|
const {
|
|
|
|
DocumentTypes: CoreDocTypes,
|
2021-05-14 16:43:41 +02:00
|
|
|
getRoleParams,
|
|
|
|
generateRoleID,
|
2021-05-13 14:29:53 +02:00
|
|
|
APP_DEV_PREFIX,
|
|
|
|
APP_PREFIX,
|
|
|
|
SEPARATOR,
|
2021-05-14 16:43:41 +02:00
|
|
|
} = require("@budibase/auth/db")
|
2020-10-02 13:37:46 +02:00
|
|
|
|
2020-10-09 10:47:37 +02:00
|
|
|
const UNICODE_MAX = "\ufff0"
|
|
|
|
|
2021-03-22 17:39:11 +01:00
|
|
|
const StaticDatabases = {
|
|
|
|
BUILDER: {
|
|
|
|
name: "builder-db",
|
|
|
|
baseDoc: "builder-doc",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:06:08 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-01 18:22:08 +02:00
|
|
|
const DocumentTypes = {
|
2021-05-13 14:29:53 +02:00
|
|
|
APP: CoreDocTypes.APP,
|
|
|
|
APP_DEV: CoreDocTypes.APP_DEV,
|
2021-05-16 22:25:37 +02:00
|
|
|
APP_METADATA: CoreDocTypes.APP_METADATA,
|
2021-05-14 16:43:41 +02:00
|
|
|
ROLE: CoreDocTypes.ROLE,
|
2020-10-09 19:49:23 +02:00
|
|
|
TABLE: "ta",
|
2020-10-09 20:16:33 +02:00
|
|
|
ROW: "ro",
|
2020-10-09 10:47:37 +02:00
|
|
|
USER: "us",
|
|
|
|
AUTOMATION: "au",
|
|
|
|
LINK: "li",
|
2020-10-22 18:48:32 +02:00
|
|
|
WEBHOOK: "wh",
|
2020-10-28 21:35:06 +01:00
|
|
|
INSTANCE: "inst",
|
2020-11-23 15:07:18 +01:00
|
|
|
LAYOUT: "layout",
|
2020-11-02 15:53:51 +01:00
|
|
|
SCREEN: "screen",
|
2020-12-18 19:19:43 +01:00
|
|
|
DATASOURCE: "datasource",
|
|
|
|
QUERY: "query",
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:22:41 +01:00
|
|
|
const ViewNames = {
|
|
|
|
LINK: "by_link",
|
|
|
|
ROUTING: "screen_routes",
|
2021-04-08 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const InternalTables = {
|
|
|
|
USER_METADATA: "ta_users",
|
2020-11-10 18:22:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 00:42:50 +01:00
|
|
|
const SearchIndexes = {
|
|
|
|
ROWS: "rows",
|
|
|
|
}
|
|
|
|
|
2021-05-13 14:29:53 +02:00
|
|
|
exports.APP_PREFIX = APP_PREFIX
|
|
|
|
exports.APP_DEV_PREFIX = APP_DEV_PREFIX
|
2021-03-22 17:39:11 +01:00
|
|
|
exports.StaticDatabases = StaticDatabases
|
2020-11-10 18:22:41 +01:00
|
|
|
exports.ViewNames = ViewNames
|
2021-04-08 17:58:33 +02:00
|
|
|
exports.InternalTables = InternalTables
|
2020-10-01 18:22:08 +02:00
|
|
|
exports.DocumentTypes = DocumentTypes
|
2020-10-09 10:47:37 +02:00
|
|
|
exports.SEPARATOR = SEPARATOR
|
2020-10-09 11:46:38 +02:00
|
|
|
exports.UNICODE_MAX = UNICODE_MAX
|
2021-03-26 00:42:50 +01:00
|
|
|
exports.SearchIndexes = SearchIndexes
|
2021-05-13 12:06:08 +02:00
|
|
|
exports.AppStatus = AppStatus
|
2020-10-01 18:22:08 +02:00
|
|
|
|
2021-05-14 16:43:41 +02:00
|
|
|
exports.generateRoleID = generateRoleID
|
|
|
|
exports.getRoleParams = getRoleParams
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getQueryIndex = viewName => {
|
2020-11-10 18:22:41 +01:00
|
|
|
return `database/${viewName}`
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-02 15:19:39 +02:00
|
|
|
* If creating DB allDocs/query params with only a single top level ID this can be used, this
|
2020-10-09 19:49:23 +02:00
|
|
|
* is usually the case as most of our docs are top level e.g. tables, automations, users and so on.
|
2020-10-09 20:10:28 +02:00
|
|
|
* More complex cases such as link docs and rows which have multiple levels of IDs that their
|
2020-10-02 15:19:39 +02:00
|
|
|
* ID consists of need their own functions to build the allDocs parameters.
|
2020-10-02 13:37:46 +02:00
|
|
|
* @param {string} docType The type of document which input params are being built for, e.g. user,
|
2020-10-09 19:49:23 +02:00
|
|
|
* link, app, table and so on.
|
2020-10-02 15:19:39 +02:00
|
|
|
* @param {string|null} docId The ID of the document minus its type - this is only needed if looking
|
|
|
|
* for a singular document.
|
2020-10-02 13:37:46 +02:00
|
|
|
* @param {object} otherProps Add any other properties onto the request, e.g. include_docs.
|
|
|
|
* @returns {object} Parameters which can then be used with an allDocs request.
|
|
|
|
*/
|
2020-10-02 15:19:39 +02:00
|
|
|
function getDocParams(docType, docId = null, otherProps = {}) {
|
|
|
|
if (docId == null) {
|
|
|
|
docId = ""
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
...otherProps,
|
2020-10-09 10:47:37 +02:00
|
|
|
startkey: `${docType}${SEPARATOR}${docId}`,
|
|
|
|
endkey: `${docType}${SEPARATOR}${docId}${UNICODE_MAX}`,
|
2020-10-01 18:22:08 +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
|
|
|
*/
|
2020-10-09 19:49:23 +02:00
|
|
|
exports.getTableParams = (tableId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.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
|
|
|
*/
|
2020-10-09 19:49:23 +02:00
|
|
|
exports.generateTableID = () => {
|
|
|
|
return `${DocumentTypes.TABLE}${SEPARATOR}${newid()}`
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-09 20:10:28 +02:00
|
|
|
* Gets the DB allDocs/query params for retrieving a row.
|
2020-10-12 18:45:11 +02:00
|
|
|
* @param {string|null} tableId The table in which the rows have been stored.
|
2020-10-09 20:10:28 +02:00
|
|
|
* @param {string|null} rowId The ID of the row which is being specifically queried for. This can be
|
|
|
|
* left null to get all the rows in the table.
|
2020-10-02 13:37:46 +02:00
|
|
|
* @param {object} otherProps Any other properties to add to the request.
|
|
|
|
* @returns {object} Parameters which can then be used with an allDocs request.
|
|
|
|
*/
|
2020-10-12 18:56:40 +02:00
|
|
|
exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => {
|
2020-10-09 19:49:23 +02:00
|
|
|
if (tableId == null) {
|
2020-10-12 18:45:11 +02:00
|
|
|
return getDocParams(DocumentTypes.ROW, null, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
2020-11-24 15:04:14 +01:00
|
|
|
|
2021-04-21 19:15:57 +02:00
|
|
|
const endOfKey = rowId == null ? `${tableId}${SEPARATOR}` : rowId
|
2020-11-24 15:04:14 +01:00
|
|
|
|
|
|
|
return getDocParams(DocumentTypes.ROW, endOfKey, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-09 20:10:28 +02:00
|
|
|
* Gets a new row ID for the specified table.
|
|
|
|
* @param {string} tableId The table which the row is being created for.
|
2021-04-09 16:11:49 +02:00
|
|
|
* @param {string|null} id If an ID is to be used then the UUID can be substituted for this.
|
2020-10-09 20:10:28 +02:00
|
|
|
* @returns {string} The new ID which a row doc can be stored under.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2021-04-09 16:11:49 +02:00
|
|
|
exports.generateRowID = (tableId, id = null) => {
|
|
|
|
id = id || newid()
|
|
|
|
return `${DocumentTypes.ROW}${SEPARATOR}${tableId}${SEPARATOR}${id}`
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2020-10-02 15:19:39 +02:00
|
|
|
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2021-04-19 17:26:33 +02:00
|
|
|
exports.getUserMetadataParams = (userId = null, otherProps = {}) => {
|
|
|
|
return exports.getRowParams(InternalTables.USER_METADATA, userId, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2021-04-19 17:26:33 +02:00
|
|
|
* Generates a new user ID based on the passed in global ID.
|
|
|
|
* @param {string} globalId The ID of the global user.
|
2020-10-02 13:37:46 +02:00
|
|
|
* @returns {string} The new user ID which the user doc can be stored under.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.generateUserMetadataID = globalId => {
|
2021-04-19 17:26:33 +02:00
|
|
|
return exports.generateRowID(InternalTables.USER_METADATA, globalId)
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-19 17:26:33 +02:00
|
|
|
* Breaks up the ID to get the global ID.
|
2021-04-09 16:11:49 +02:00
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getGlobalIDFromUserMetadataID = id => {
|
2021-05-12 18:37:09 +02:00
|
|
|
const prefix = `${DocumentTypes.ROW}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}`
|
2021-05-20 21:48:24 +02:00
|
|
|
if (!id || !id.includes(prefix)) {
|
2021-05-12 18:37:09 +02:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
return id.split(prefix)[1]
|
2020-10-01 18:22:08 +02: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
|
|
|
*/
|
|
|
|
exports.getAutomationParams = (automationId = null, otherProps = {}) => {
|
2020-10-02 15:19:39 +02:00
|
|
|
return getDocParams(DocumentTypes.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.
|
|
|
|
*/
|
|
|
|
exports.generateAutomationID = () => {
|
2020-10-09 10:47:37 +02:00
|
|
|
return `${DocumentTypes.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.
|
|
|
|
*/
|
2021-02-15 18:47:14 +01:00
|
|
|
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}`
|
|
|
|
return `${DocumentTypes.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.
|
|
|
|
*/
|
|
|
|
exports.getLinkParams = (otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.LINK, null, otherProps)
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
|
|
|
* Generates a new app ID.
|
|
|
|
* @returns {string} The new app ID which the app doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateAppID = () => {
|
2020-10-09 10:47:37 +02:00
|
|
|
return `${DocumentTypes.APP}${SEPARATOR}${newid()}`
|
2020-10-01 18:22:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
/**
|
2021-05-12 18:37:09 +02:00
|
|
|
* Generates a development app ID from a real app ID.
|
|
|
|
* @returns {string} the dev app ID which can be used for dev database.
|
2020-10-02 13:37:46 +02:00
|
|
|
*/
|
2021-05-12 18:37:09 +02:00
|
|
|
exports.generateDevAppID = appId => {
|
|
|
|
const prefix = `${DocumentTypes.APP}${SEPARATOR}`
|
|
|
|
const uuid = appId.split(prefix)[1]
|
|
|
|
return `${DocumentTypes.APP_DEV}${SEPARATOR}${uuid}`
|
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
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.generateLayoutID = id => {
|
2020-12-03 18:31:38 +01:00
|
|
|
return `${DocumentTypes.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
|
|
|
*/
|
2020-11-23 15:07:18 +01:00
|
|
|
exports.getLayoutParams = (layoutId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.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.
|
|
|
|
*/
|
2020-11-20 18:47:13 +01:00
|
|
|
exports.generateScreenID = () => {
|
|
|
|
return `${DocumentTypes.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
|
|
|
*/
|
2020-11-20 18:47:13 +01:00
|
|
|
exports.getScreenParams = (screenId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.SCREEN, screenId, otherProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new webhook ID.
|
|
|
|
* @returns {string} The new webhook ID which the webhook doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateWebhookID = () => {
|
|
|
|
return `${DocumentTypes.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.
|
|
|
|
*/
|
|
|
|
exports.getWebhookParams = (webhookId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.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 = () => {
|
|
|
|
return `${DocumentTypes.DATASOURCE}${SEPARATOR}${newid()}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving a datasource, this is a utility function for the getDocParams function.
|
|
|
|
*/
|
|
|
|
exports.getDatasourceParams = (datasourceId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.DATASOURCE, datasourceId, otherProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 `${
|
|
|
|
DocumentTypes.QUERY
|
|
|
|
}${SEPARATOR}${datasourceId}${SEPARATOR}${newid()}`
|
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) {
|
|
|
|
return getDocParams(DocumentTypes.QUERY, null, otherProps)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getDocParams(
|
|
|
|
DocumentTypes.QUERY,
|
|
|
|
`${datasourceId}${SEPARATOR}`,
|
|
|
|
otherProps
|
|
|
|
)
|
2020-12-18 19:19:43 +01: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
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.getMultiIDParams = ids => {
|
2021-02-18 14:38:57 +01:00
|
|
|
return {
|
|
|
|
keys: ids,
|
|
|
|
include_docs: true,
|
2021-02-17 19:04:21 +01:00
|
|
|
}
|
|
|
|
}
|