From f5557fd8059fd613618fd96bce125d72eee89224 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 20 Oct 2022 15:05:50 +0100 Subject: [PATCH] Fetching a list of backup/restore events includes the full user object. --- packages/backend-core/src/db/constants.ts | 19 ++- packages/backend-core/src/db/utils.ts | 74 ++++++++++- .../server/src/api/controllers/table/utils.ts | 13 +- packages/server/src/db/utils.js | 125 +++--------------- packages/types/src/documents/app/backup.ts | 3 +- 5 files changed, 108 insertions(+), 126 deletions(-) diff --git a/packages/backend-core/src/db/constants.ts b/packages/backend-core/src/db/constants.ts index 8f8f45638b..446f1f7d01 100644 --- a/packages/backend-core/src/db/constants.ts +++ b/packages/backend-core/src/db/constants.ts @@ -31,6 +31,10 @@ export const DeprecatedViews = { ], } +export enum InternalTable { + USER_METADATA = "ta_users", +} + export enum DocumentType { USER = "us", GROUP = "gr", @@ -47,10 +51,23 @@ export enum DocumentType { AUTOMATION_LOG = "log_au", ACCOUNT_METADATA = "acc_metadata", PLUGIN = "plg", - TABLE = "ta", DATASOURCE = "datasource", DATASOURCE_PLUS = "datasource_plus", APP_BACKUP = "backup", + TABLE = "ta", + ROW = "ro", + AUTOMATION = "au", + LINK = "li", + WEBHOOK = "wh", + INSTANCE = "inst", + LAYOUT = "layout", + SCREEN = "screen", + QUERY = "query", + DEPLOYMENTS = "deployments", + METADATA = "metadata", + MEM_VIEW = "view", + USER_FLAG = "flag", + AUTOMATION_METADATA = "meta_au", } export const StaticDatabases = { diff --git a/packages/backend-core/src/db/utils.ts b/packages/backend-core/src/db/utils.ts index 8d824d60bb..c04da5da4f 100644 --- a/packages/backend-core/src/db/utils.ts +++ b/packages/backend-core/src/db/utils.ts @@ -1,10 +1,16 @@ import { newid } from "../hashing" import { DEFAULT_TENANT_ID, Configs } from "../constants" import env from "../environment" -import { SEPARATOR, DocumentType, UNICODE_MAX, ViewName } from "./constants" +import { + SEPARATOR, + DocumentType, + UNICODE_MAX, + ViewName, + InternalTable, +} from "./constants" import { getTenantId, getGlobalDB } from "../context" import { getGlobalDBName } from "./tenancy" -import { doWithDB, allDbs, directCouchQuery, directCouchAllDbs } from "./index" +import { doWithDB, allDbs, directCouchAllDbs } from "./index" import { getAppMetadata } from "../cache/appMetadata" import { isDevApp, isDevAppID, getProdAppID } from "./conversions" import { APP_PREFIX } from "./constants" @@ -40,8 +46,8 @@ export const generateAppID = (tenantId = null) => { * @returns {object} Parameters which can then be used with an allDocs request. */ export function getDocParams( - docType: any, - docId: any = null, + docType: string, + docId?: string | null, otherProps: any = {} ) { if (docId == null) { @@ -54,6 +60,28 @@ export function getDocParams( } } +/** + * Gets the DB allDocs/query params for retrieving a row. + * @param {string|null} tableId The table in which the rows have been stored. + * @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. + * @param {object} otherProps Any other properties to add to the request. + * @returns {object} Parameters which can then be used with an allDocs request. + */ +export function getRowParams( + tableId?: string | null, + rowId?: string | null, + otherProps = {} +) { + if (tableId == null) { + return getDocParams(DocumentType.ROW, null, otherProps) + } + + const endOfKey = rowId == null ? `${tableId}${SEPARATOR}` : rowId + + return getDocParams(DocumentType.ROW, endOfKey, otherProps) +} + /** * Retrieve the correct index for a view based on default design DB. */ @@ -61,6 +89,17 @@ export function getQueryIndex(viewName: ViewName) { return `database/${viewName}` } +/** + * Gets a new row ID for the specified table. + * @param {string} tableId The table which the row is being created for. + * @param {string|null} id If an ID is to be used then the UUID can be substituted for this. + * @returns {string} The new ID which a row doc can be stored under. + */ +export function generateRowID(tableId: string, id?: string) { + id = id || newid() + return `${DocumentType.ROW}${SEPARATOR}${tableId}${SEPARATOR}${id}` +} + /** * Check if a given ID is that of a table. * @returns {boolean} @@ -128,6 +167,33 @@ export function getGlobalUserParams(globalId: any, otherProps: any = {}) { } } +/** + * Gets parameters for retrieving users, this is a utility function for the getDocParams function. + */ +export function getUserMetadataParams(userId?: string, otherProps = {}) { + return getRowParams(InternalTable.USER_METADATA, userId, otherProps) +} + +/** + * Generates a new user ID based on the passed in global ID. + * @param {string} globalId The ID of the global user. + * @returns {string} The new user ID which the user doc can be stored under. + */ +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] +} + export function getUsersByAppParams(appId: any, otherProps: any = {}) { const prodAppId = getProdAppID(appId) return { diff --git a/packages/server/src/api/controllers/table/utils.ts b/packages/server/src/api/controllers/table/utils.ts index 79c551b63a..6d17d12f3d 100644 --- a/packages/server/src/api/controllers/table/utils.ts +++ b/packages/server/src/api/controllers/table/utils.ts @@ -1,11 +1,5 @@ import { transform } from "../../../utilities/csvParser" -import { - getRowParams, - generateRowID, - InternalTables, - getTableParams, - BudibaseInternalDB, -} from "../../../db/utils" +import { getRowParams, generateRowID, InternalTables } from "../../../db/utils" import { isEqual } from "lodash" import { AutoFieldSubTypes, FieldTypes } from "../../../constants" import { @@ -17,11 +11,6 @@ import { SwitchableTypes, CanSwitchTypes, } from "../../../constants" -import { - isExternalTable, - breakExternalTableId, - isSQL, -} from "../../../integrations/utils" import { getViews, saveView } from "../view/utils" import viewTemplate from "../view/viewBuilder" const { getAppDB } = require("@budibase/backend-core/context") diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index 046172df73..acd72cbf66 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -1,6 +1,7 @@ const newid = require("./newid") const { - DocumentType: CoreDocTypes, + DocumentType: CoreDocType, + InternalTable, getRoleParams, generateRoleID, APP_DEV_PREFIX, @@ -13,6 +14,12 @@ const { generateAppID, getQueryIndex, ViewName, + getDocParams, + getRowParams, + generateRowID, + getUserMetadataParams, + generateUserMetadataID, + getGlobalIDFromUserMetadataID, } = require("@budibase/backend-core/db") const UNICODE_MAX = "\ufff0" @@ -23,28 +30,7 @@ const AppStatus = { DEPLOYED: "published", } -const DocumentType = { - ...CoreDocTypes, - TABLE: "ta", - ROW: "ro", - USER: "us", - AUTOMATION: "au", - LINK: "li", - WEBHOOK: "wh", - INSTANCE: "inst", - LAYOUT: "layout", - SCREEN: "screen", - QUERY: "query", - DEPLOYMENTS: "deployments", - METADATA: "metadata", - MEM_VIEW: "view", - USER_FLAG: "flag", - AUTOMATION_METADATA: "meta_au", -} - -const InternalTables = { - USER_METADATA: "ta_users", -} +const DocumentType = CoreDocType const SearchIndexes = { ROWS: "rows", @@ -64,11 +50,11 @@ exports.APP_PREFIX = APP_PREFIX exports.APP_DEV_PREFIX = APP_DEV_PREFIX exports.isDevAppID = isDevAppID exports.isProdAppID = isProdAppID -exports.USER_METDATA_PREFIX = `${DocumentType.ROW}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}` -exports.LINK_USER_METADATA_PREFIX = `${DocumentType.LINK}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}` +exports.USER_METDATA_PREFIX = `${DocumentType.ROW}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}` +exports.LINK_USER_METADATA_PREFIX = `${DocumentType.LINK}${SEPARATOR}${InternalTable.USER_METADATA}${SEPARATOR}` exports.TABLE_ROW_PREFIX = `${DocumentType.ROW}${SEPARATOR}${DocumentType.TABLE}` exports.ViewName = ViewName -exports.InternalTables = InternalTables +exports.InternalTables = InternalTable exports.DocumentType = DocumentType exports.SEPARATOR = SEPARATOR exports.UNICODE_MAX = UNICODE_MAX @@ -77,36 +63,15 @@ exports.AppStatus = AppStatus exports.BudibaseInternalDB = BudibaseInternalDB exports.generateAppID = generateAppID exports.generateDevAppID = getDevelopmentAppID - exports.generateRoleID = generateRoleID exports.getRoleParams = getRoleParams - exports.getQueryIndex = getQueryIndex - -/** - * If creating DB allDocs/query params with only a single top level ID this can be used, this - * is usually the case as most of our docs are top level e.g. tables, automations, users and so on. - * More complex cases such as link docs and rows which have multiple levels of IDs that their - * ID consists of need their own functions to build the allDocs parameters. - * @param {string} docType The type of document which input params are being built for, e.g. user, - * link, app, table and so on. - * @param {string|null} docId The ID of the document minus its type - this is only needed if looking - * for a singular document. - * @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. - */ -function getDocParams(docType, docId = null, otherProps = {}) { - if (docId == null) { - docId = "" - } - return { - ...otherProps, - startkey: `${docType}${SEPARATOR}${docId}`, - endkey: `${docType}${SEPARATOR}${docId}${UNICODE_MAX}`, - } -} - 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. @@ -123,24 +88,6 @@ exports.generateTableID = () => { return `${DocumentType.TABLE}${SEPARATOR}${newid()}` } -/** - * Gets the DB allDocs/query params for retrieving a row. - * @param {string|null} tableId The table in which the rows have been stored. - * @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. - * @param {object} otherProps Any other properties to add to the request. - * @returns {object} Parameters which can then be used with an allDocs request. - */ -exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => { - if (tableId == null) { - return getDocParams(DocumentType.ROW, null, otherProps) - } - - const endOfKey = rowId == null ? `${tableId}${SEPARATOR}` : rowId - - return getDocParams(DocumentType.ROW, endOfKey, otherProps) -} - /** * 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. @@ -153,44 +100,6 @@ exports.getTableIDFromRowID = rowId => { return `${DocumentType.TABLE}${SEPARATOR}${components[0]}` } -/** - * Gets a new row ID for the specified table. - * @param {string} tableId The table which the row is being created for. - * @param {string|null} id If an ID is to be used then the UUID can be substituted for this. - * @returns {string} The new ID which a row doc can be stored under. - */ -exports.generateRowID = (tableId, id = null) => { - id = id || newid() - return `${DocumentType.ROW}${SEPARATOR}${tableId}${SEPARATOR}${id}` -} - -/** - * Gets parameters for retrieving users, this is a utility function for the getDocParams function. - */ -exports.getUserMetadataParams = (userId = null, otherProps = {}) => { - return exports.getRowParams(InternalTables.USER_METADATA, userId, otherProps) -} - -/** - * Generates a new user ID based on the passed in global ID. - * @param {string} globalId The ID of the global user. - * @returns {string} The new user ID which the user doc can be stored under. - */ -exports.generateUserMetadataID = globalId => { - return exports.generateRowID(InternalTables.USER_METADATA, globalId) -} - -/** - * Breaks up the ID to get the global ID. - */ -exports.getGlobalIDFromUserMetadataID = id => { - const prefix = `${DocumentType.ROW}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}` - if (!id || !id.includes(prefix)) { - return id - } - return id.split(prefix)[1] -} - /** * Gets parameters for retrieving automations, this is a utility function for the getDocParams function. */ diff --git a/packages/types/src/documents/app/backup.ts b/packages/types/src/documents/app/backup.ts index a6d75efcff..520013a8bb 100644 --- a/packages/types/src/documents/app/backup.ts +++ b/packages/types/src/documents/app/backup.ts @@ -1,4 +1,5 @@ import { Document } from "../document" +import { User } from "../../" export enum AppBackupType { BACKUP = "backup", @@ -29,7 +30,7 @@ export interface AppBackupMetadata { type: AppBackupType status: AppBackupStatus name?: string - createdBy?: string + createdBy?: string | User timestamp: string contents?: AppBackupContents }