From c4c62e5c6ff73bbffe93286344780533ba83a452 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 10:48:55 +0200 Subject: [PATCH 1/5] Remove any from couchdb.get response --- packages/types/src/sdk/db.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index 58b3b7e5bc..e07cc82fd2 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -89,7 +89,7 @@ export interface Database { exists(): Promise checkSetup(): Promise> - get(id?: string): Promise + get(id?: string): Promise remove( id: string | Document, rev?: string From 1072292b9f95f6ff5e8f5563011fe9d1fcb879a7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 10:49:39 +0200 Subject: [PATCH 2/5] Type breaking changes as any --- packages/backend-core/src/auth/auth.ts | 2 +- packages/backend-core/src/cache/user.ts | 2 +- packages/backend-core/src/db/searchIndexes/searchIndexes.ts | 2 +- packages/backend-core/src/users.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/auth/auth.ts b/packages/backend-core/src/auth/auth.ts index fb2fd2cf51..0100a2d0e2 100644 --- a/packages/backend-core/src/auth/auth.ts +++ b/packages/backend-core/src/auth/auth.ts @@ -159,7 +159,7 @@ export async function updateUserOAuth(userId: string, oAuthConfig: any) { try { const db = getGlobalDB() - const dbUser = await db.get(userId) + const dbUser = await db.get(userId) //Do not overwrite the refresh token if a valid one is not provided. if (typeof details.refreshToken !== "string") { diff --git a/packages/backend-core/src/cache/user.ts b/packages/backend-core/src/cache/user.ts index b514c3af9b..8281bfca62 100644 --- a/packages/backend-core/src/cache/user.ts +++ b/packages/backend-core/src/cache/user.ts @@ -12,7 +12,7 @@ const EXPIRY_SECONDS = 3600 */ async function populateFromDB(userId: string, tenantId: string) { const db = tenancy.getTenantDB(tenantId) - const user = await db.get(userId) + const user = await db.get(userId) user.budibaseAccess = true if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) { const account = await accounts.getAccount(user.email) diff --git a/packages/backend-core/src/db/searchIndexes/searchIndexes.ts b/packages/backend-core/src/db/searchIndexes/searchIndexes.ts index f03259b47f..b953e3516e 100644 --- a/packages/backend-core/src/db/searchIndexes/searchIndexes.ts +++ b/packages/backend-core/src/db/searchIndexes/searchIndexes.ts @@ -5,7 +5,7 @@ export async function createUserIndex() { const db = getGlobalDB() let designDoc try { - designDoc = await db.get("_design/database") + designDoc = await db.get("_design/database") } catch (err: any) { if (err.status === 404) { designDoc = { _id: "_design/database" } diff --git a/packages/backend-core/src/users.ts b/packages/backend-core/src/users.ts index 166136df3c..b49058f546 100644 --- a/packages/backend-core/src/users.ts +++ b/packages/backend-core/src/users.ts @@ -67,9 +67,9 @@ export const bulkUpdateGlobalUsers = async (users: User[]) => { export async function getById(id: string, opts?: GetOpts): Promise { const db = context.getGlobalDB() - let user = await db.get(id) + let user = await db.get(id) if (opts?.cleanup) { - user = removeUserPassword(user) + user = removeUserPassword(user) as User } return user } From 2698e47ec4be211631f4f157db68e44f5320a6cc Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 11:41:51 +0200 Subject: [PATCH 3/5] Type internal db.get --- packages/pro | 2 +- .../server/src/api/controllers/analytics.ts | 4 +-- .../server/src/api/controllers/apikeys.ts | 2 +- .../server/src/api/controllers/application.ts | 10 +++---- packages/server/src/api/controllers/auth.ts | 3 ++- .../server/src/api/controllers/automation.ts | 15 ++++++----- packages/server/src/api/controllers/backup.ts | 4 +-- .../server/src/api/controllers/component.ts | 4 +-- .../server/src/api/controllers/datasource.ts | 4 +-- .../src/api/controllers/deploy/index.ts | 10 +++---- packages/server/src/api/controllers/dev.ts | 5 ++-- .../src/api/controllers/public/utils.ts | 2 +- .../server/src/api/controllers/query/index.ts | 7 ++--- packages/server/src/api/controllers/role.ts | 17 ++++++------ .../src/api/controllers/row/internal.ts | 17 ++++++------ .../src/api/controllers/row/staticFormula.ts | 7 ++--- packages/server/src/api/controllers/screen.ts | 6 ++--- .../src/api/controllers/static/index.ts | 7 ++--- .../src/api/controllers/table/external.ts | 4 +-- .../src/api/controllers/table/internal.ts | 12 ++++----- packages/server/src/api/controllers/user.ts | 6 ++--- .../server/src/api/controllers/view/index.ts | 7 ++--- .../server/src/api/controllers/view/utils.ts | 26 +++++++++---------- .../server/src/api/controllers/webhook.ts | 2 +- packages/server/src/automations/utils.ts | 11 +++++--- .../src/db/linkedRows/LinkController.ts | 8 +++--- packages/server/src/db/views/staticViews.ts | 6 ++--- packages/server/src/middleware/builder.ts | 4 +-- .../server/src/sdk/app/applications/sync.ts | 2 +- .../src/sdk/app/datasources/datasources.ts | 4 +-- .../src/sdk/app/rows/search/internal.ts | 16 ++++++------ packages/server/src/sdk/users/crud.ts | 7 +++++ packages/server/src/sdk/users/index.ts | 2 ++ packages/server/src/threads/query.ts | 3 ++- packages/server/src/utilities/global.ts | 2 +- packages/server/src/utilities/index.ts | 4 +-- .../src/api/controllers/global/email.ts | 6 ++--- .../src/api/controllers/global/roles.ts | 2 +- .../worker/src/api/controllers/global/self.ts | 2 +- .../src/api/controllers/global/users.ts | 2 +- 40 files changed, 144 insertions(+), 120 deletions(-) create mode 100644 packages/server/src/sdk/users/crud.ts diff --git a/packages/pro b/packages/pro index 544c7e067d..1929d93660 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 544c7e067de69832469cde673e59501480d6d98a +Subproject commit 1929d93660e95f4b190d1936d0b3c65c4ad6a1f5 diff --git a/packages/server/src/api/controllers/analytics.ts b/packages/server/src/api/controllers/analytics.ts index dc7c4185bb..eee86a534b 100644 --- a/packages/server/src/api/controllers/analytics.ts +++ b/packages/server/src/api/controllers/analytics.ts @@ -1,5 +1,5 @@ import { events } from "@budibase/backend-core" -import { AnalyticsPingRequest, PingSource } from "@budibase/types" +import { AnalyticsPingRequest, App, PingSource } from "@budibase/types" import { DocumentType, isDevAppID } from "../../db/utils" import { context } from "@budibase/backend-core" @@ -16,7 +16,7 @@ export const ping = async (ctx: any) => { switch (body.source) { case PingSource.APP: { const db = context.getAppDB({ skip_setup: true }) - const appInfo = await db.get(DocumentType.APP_METADATA) + const appInfo = await db.get(DocumentType.APP_METADATA) let appId = context.getAppId() if (isDevAppID(appId)) { diff --git a/packages/server/src/api/controllers/apikeys.ts b/packages/server/src/api/controllers/apikeys.ts index 02b61954c3..2a02078483 100644 --- a/packages/server/src/api/controllers/apikeys.ts +++ b/packages/server/src/api/controllers/apikeys.ts @@ -6,7 +6,7 @@ const KEYS_DOC = dbCore.StaticDatabases.GLOBAL.docs.apiKeys async function getBuilderMainDoc() { const db = tenancy.getGlobalDB() try { - return await db.get(KEYS_DOC) + return await db.get(KEYS_DOC) } catch (err) { // doesn't exist yet, nothing to get return { diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index a2448a0384..418ccf637e 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -222,7 +222,7 @@ export async function fetchAppDefinition(ctx: UserCtx) { export async function fetchAppPackage(ctx: UserCtx) { const db = context.getAppDB() - let application = await db.get(DocumentType.APP_METADATA) + let application = await db.get(DocumentType.APP_METADATA) const layouts = await getLayouts() let screens = await getScreens() const license = await licensing.cache.getCachedLicense() @@ -458,7 +458,7 @@ export async function update(ctx: UserCtx) { export async function updateClient(ctx: UserCtx) { // Get current app version const db = context.getAppDB() - const application = await db.get(DocumentType.APP_METADATA) + const application = await db.get(DocumentType.APP_METADATA) const currentVersion = application.version // Update client library and manifest @@ -482,7 +482,7 @@ export async function updateClient(ctx: UserCtx) { export async function revertClient(ctx: UserCtx) { // Check app can be reverted const db = context.getAppDB() - const application = await db.get(DocumentType.APP_METADATA) + const application = await db.get(DocumentType.APP_METADATA) if (!application.revertableVersion) { ctx.throw(400, "There is no version to revert to") } @@ -535,7 +535,7 @@ async function destroyApp(ctx: UserCtx) { const db = dbCore.getDB(devAppId) // standard app deletion flow - const app = await db.get(DocumentType.APP_METADATA) + const app = await db.get(DocumentType.APP_METADATA) const result = await db.destroy() await quotas.removeApp() await events.app.deleted(app) @@ -598,7 +598,7 @@ export async function sync(ctx: UserCtx) { export async function updateAppPackage(appPackage: any, appId: any) { return context.doInAppContext(appId, async () => { const db = context.getAppDB() - const application = await db.get(DocumentType.APP_METADATA) + const application = await db.get(DocumentType.APP_METADATA) const newAppPackage = { ...application, ...appPackage } if (appPackage._rev !== application._rev) { diff --git a/packages/server/src/api/controllers/auth.ts b/packages/server/src/api/controllers/auth.ts index 15b99bf2f8..bca6c4e64e 100644 --- a/packages/server/src/api/controllers/auth.ts +++ b/packages/server/src/api/controllers/auth.ts @@ -4,6 +4,7 @@ import { getFullUser } from "../../utilities/users" import { roles, context } from "@budibase/backend-core" import { groups } from "@budibase/pro" import { ContextUser, User, Row, UserCtx } from "@budibase/types" +import sdk from "../../sdk" const PUBLIC_ROLE = roles.BUILTIN_ROLE_IDS.PUBLIC @@ -41,7 +42,7 @@ export async function fetchSelf(ctx: UserCtx) { // remove the full roles structure delete user.roles try { - const userTable = await db.get(InternalTables.USER_METADATA) + const userTable = await sdk.tables.getTable(InternalTables.USER_METADATA) // specifically needs to make sure is enriched ctx.body = await outputProcessing(userTable, user as Row) } catch (err: any) { diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index 25d915e995..a4697f99f2 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -16,6 +16,7 @@ import { setTestFlag, clearTestFlag } from "../../utilities/redis" import { context, cache, events } from "@budibase/backend-core" import { automations, features } from "@budibase/pro" import { + App, Automation, AutomationActionStepId, AutomationResults, @@ -152,7 +153,7 @@ export async function update(ctx: BBContext) { return } - const oldAutomation = await db.get(automation._id) + const oldAutomation = await db.get(automation._id) automation = cleanAutomationInputs(automation) automation = await checkForWebhooks({ oldAuto: oldAutomation, @@ -210,7 +211,7 @@ export async function find(ctx: BBContext) { export async function destroy(ctx: BBContext) { const db = context.getAppDB() const automationId = ctx.params.id - const oldAutomation = await db.get(automationId) + const oldAutomation = await db.get(automationId) await checkForWebhooks({ oldAuto: oldAutomation, }) @@ -229,7 +230,7 @@ export async function clearLogError(ctx: BBContext) { const { automationId, appId } = ctx.request.body await context.doInAppContext(appId, async () => { const db = context.getProdAppDB() - const metadata = await db.get(DocumentType.APP_METADATA) + const metadata = await db.get(DocumentType.APP_METADATA) if (!automationId) { delete metadata.automationErrors } else if ( @@ -267,7 +268,7 @@ export async function getDefinitionList(ctx: BBContext) { export async function trigger(ctx: BBContext) { const db = context.getAppDB() - let automation = await db.get(ctx.params.id) + let automation = await db.get(ctx.params.id) let hasCollectStep = sdk.automations.utils.checkForCollectStep(automation) if (hasCollectStep && (await features.isSyncAutomationsEnabled())) { @@ -312,8 +313,8 @@ function prepareTestInput(input: any) { export async function test(ctx: BBContext) { const db = context.getAppDB() - let automation = await db.get(ctx.params.id) - await setTestFlag(automation._id) + let automation = await db.get(ctx.params.id) + await setTestFlag(automation._id!) const testInput = prepareTestInput(ctx.request.body) const response = await triggers.externalTrigger( automation, @@ -328,7 +329,7 @@ export async function test(ctx: BBContext) { ...ctx.request.body, occurredAt: new Date().getTime(), }) - await clearTestFlag(automation._id) + await clearTestFlag(automation._id!) ctx.body = response await events.automation.tested(automation) } diff --git a/packages/server/src/api/controllers/backup.ts b/packages/server/src/api/controllers/backup.ts index 2a7921d354..8e1881a5fc 100644 --- a/packages/server/src/api/controllers/backup.ts +++ b/packages/server/src/api/controllers/backup.ts @@ -1,7 +1,7 @@ import sdk from "../../sdk" import { events, context, db } from "@budibase/backend-core" import { DocumentType } from "../../db/utils" -import { Ctx } from "@budibase/types" +import { App, Ctx } from "@budibase/types" interface ExportAppDumpRequest { excludeRows: boolean @@ -29,7 +29,7 @@ export async function exportAppDump(ctx: Ctx) { await context.doInAppContext(appId, async () => { const appDb = context.getAppDB() - const app = await appDb.get(DocumentType.APP_METADATA) + const app = await appDb.get(DocumentType.APP_METADATA) await events.app.exported(app) }) } diff --git a/packages/server/src/api/controllers/component.ts b/packages/server/src/api/controllers/component.ts index 52b1ac66bf..1d0ccf8784 100644 --- a/packages/server/src/api/controllers/component.ts +++ b/packages/server/src/api/controllers/component.ts @@ -1,5 +1,5 @@ import { DocumentType } from "../../db/utils" -import { Plugin } from "@budibase/types" +import { App, Plugin } from "@budibase/types" import { db as dbCore, context, tenancy } from "@budibase/backend-core" import { getComponentLibraryManifest } from "../../utilities/fileSystem" import { UserCtx } from "@budibase/types" @@ -7,7 +7,7 @@ import { UserCtx } from "@budibase/types" export async function fetchAppComponentDefinitions(ctx: UserCtx) { try { const db = context.getAppDB() - const app = await db.get(DocumentType.APP_METADATA) + const app = await db.get(DocumentType.APP_METADATA) let componentManifests = await Promise.all( app.componentLibraries.map(async (library: any) => { diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 65902a542e..8ef2e8789a 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -433,7 +433,7 @@ export async function destroy(ctx: UserCtx) { export async function find(ctx: UserCtx) { const db = context.getAppDB() - const datasource = await db.get(ctx.params.datasourceId) + const datasource = await sdk.datasources.get(ctx.params.datasourceId) ctx.body = await sdk.datasources.removeSecretSingle(datasource) } @@ -449,7 +449,7 @@ export async function query(ctx: UserCtx) { export async function getExternalSchema(ctx: UserCtx) { const db = context.getAppDB() - const datasource = await db.get(ctx.params.datasourceId) + const datasource = await sdk.datasources.get(ctx.params.datasourceId) const enrichedDatasource = await getAndMergeDatasource(datasource) const connector = await getConnector(enrichedDatasource) diff --git a/packages/server/src/api/controllers/deploy/index.ts b/packages/server/src/api/controllers/deploy/index.ts index a49434bbd1..a2b164ffc6 100644 --- a/packages/server/src/api/controllers/deploy/index.ts +++ b/packages/server/src/api/controllers/deploy/index.ts @@ -7,7 +7,7 @@ import { enableCronTrigger, } from "../../../automations/utils" import { backups } from "@budibase/pro" -import { AppBackupTrigger } from "@budibase/types" +import { App, AppBackupTrigger } from "@budibase/types" import sdk from "../../../sdk" import { builderSocket } from "../../../websockets" @@ -44,7 +44,7 @@ async function storeDeploymentHistory(deployment: any) { let deploymentDoc try { // theres only one deployment doc per app database - deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) + deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) } catch (err) { deploymentDoc = { _id: DocumentType.DEPLOYMENTS, history: {} } } @@ -113,7 +113,7 @@ export async function fetchDeployments(ctx: any) { export async function deploymentProgress(ctx: any) { try { const db = context.getAppDB() - const deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) + const deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) ctx.body = deploymentDoc[ctx.params.deploymentId] } catch (err) { ctx.throw( @@ -165,9 +165,9 @@ export const publishApp = async function (ctx: any) { // app metadata is excluded as it is likely to be in conflict // replicate the app metadata document manually const db = context.getProdAppDB() - const appDoc = await devDb.get(DocumentType.APP_METADATA) + const appDoc = await devDb.get(DocumentType.APP_METADATA) try { - const prodAppDoc = await db.get(DocumentType.APP_METADATA) + const prodAppDoc = await db.get(DocumentType.APP_METADATA) appDoc._rev = prodAppDoc._rev } catch (err) { delete appDoc._rev diff --git a/packages/server/src/api/controllers/dev.ts b/packages/server/src/api/controllers/dev.ts index e80d9d7ea1..cd32fa9359 100644 --- a/packages/server/src/api/controllers/dev.ts +++ b/packages/server/src/api/controllers/dev.ts @@ -6,6 +6,7 @@ import { clearLock as redisClearLock } from "../../utilities/redis" import { DocumentType } from "../../db/utils" import { context, env as envCore } from "@budibase/backend-core" import { events, db as dbCore, cache } from "@budibase/backend-core" +import { App } from "@budibase/types" async function redirect(ctx: any, method: string, path: string = "global") { const { devPath } = ctx.params @@ -81,7 +82,7 @@ export async function revert(ctx: any) { if (!exists) { throw new Error("App must be deployed to be reverted.") } - const deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) + const deploymentDoc = await db.get(DocumentType.DEPLOYMENTS) if ( !deploymentDoc.history || Object.keys(deploymentDoc.history).length === 0 @@ -104,7 +105,7 @@ export async function revert(ctx: any) { // update appID in reverted app to be dev version again const db = context.getAppDB() - const appDoc = await db.get(DocumentType.APP_METADATA) + const appDoc = await db.get(DocumentType.APP_METADATA) appDoc.appId = appId appDoc.instance._id = appId await db.put(appDoc) diff --git a/packages/server/src/api/controllers/public/utils.ts b/packages/server/src/api/controllers/public/utils.ts index a51b6b5fff..1272fcb36a 100644 --- a/packages/server/src/api/controllers/public/utils.ts +++ b/packages/server/src/api/controllers/public/utils.ts @@ -14,7 +14,7 @@ export async function addRev( id = DocumentType.APP_METADATA } const db = context.getAppDB() - const dbDoc = await db.get(id) + const dbDoc = await db.get(id) body._rev = dbDoc._rev // update ID in case it is an app ID body._id = id diff --git a/packages/server/src/api/controllers/query/index.ts b/packages/server/src/api/controllers/query/index.ts index 356a2eb5c0..4c1cad6b5b 100644 --- a/packages/server/src/api/controllers/query/index.ts +++ b/packages/server/src/api/controllers/query/index.ts @@ -9,6 +9,7 @@ import { quotas } from "@budibase/pro" import { events, context, utils, constants } from "@budibase/backend-core" import sdk from "../../../sdk" import { QueryEvent } from "../../../threads/definitions" +import { Query } from "@budibase/types" const Runner = new Thread(ThreadType.QUERY, { timeoutMs: env.QUERY_THREAD_TIMEOUT || 10000, @@ -206,7 +207,7 @@ async function execute( ) { const db = context.getAppDB() - const query = await db.get(ctx.params.queryId) + const query = await db.get(ctx.params.queryId) const { datasource, envVars } = await sdk.datasources.getWithEnvVars( query.datasourceId ) @@ -275,7 +276,7 @@ export async function executeV2( const removeDynamicVariables = async (queryId: any) => { const db = context.getAppDB() - const query = await db.get(queryId) + const query = await db.get(queryId) const datasource = await sdk.datasources.get(query.datasourceId) const dynamicVariables = datasource.config?.dynamicVariables as any[] @@ -298,7 +299,7 @@ export async function destroy(ctx: any) { const db = context.getAppDB() const queryId = ctx.params.queryId await removeDynamicVariables(queryId) - const query = await db.get(queryId) + const query = await db.get(queryId) const datasource = await sdk.datasources.get(query.datasourceId) await db.remove(ctx.params.queryId, ctx.params.revId) ctx.message = `Query deleted.` diff --git a/packages/server/src/api/controllers/role.ts b/packages/server/src/api/controllers/role.ts index a2369ef2da..5ab1ad3502 100644 --- a/packages/server/src/api/controllers/role.ts +++ b/packages/server/src/api/controllers/role.ts @@ -1,6 +1,7 @@ import { roles, context, events, db as dbCore } from "@budibase/backend-core" import { getUserMetadataParams, InternalTables } from "../../db/utils" -import { UserCtx, Database } from "@budibase/types" +import { UserCtx, Database, UserRoles, Role } from "@budibase/types" +import sdk from "../../sdk" const UpdateRolesOptions = { CREATED: "created", @@ -13,23 +14,23 @@ async function updateRolesOnUserTable( updateOption: string, roleVersion: string | undefined ) { - const table = await db.get(InternalTables.USER_METADATA) + const table = await sdk.tables.getTable(InternalTables.USER_METADATA) const schema = table.schema const remove = updateOption === UpdateRolesOptions.REMOVED let updated = false for (let prop of Object.keys(schema)) { if (prop === "roleId") { updated = true - const constraints = schema[prop].constraints + const constraints = schema[prop].constraints! const updatedRoleId = roleVersion === roles.RoleIDVersion.NAME ? roles.getExternalRoleID(roleId, roleVersion) : roleId - const indexOfRoleId = constraints.inclusion.indexOf(updatedRoleId) + const indexOfRoleId = constraints.inclusion!.indexOf(updatedRoleId) if (remove && indexOfRoleId !== -1) { - constraints.inclusion.splice(indexOfRoleId, 1) + constraints.inclusion!.splice(indexOfRoleId, 1) } else if (!remove && indexOfRoleId === -1) { - constraints.inclusion.push(updatedRoleId) + constraints.inclusion!.push(updatedRoleId) } break } @@ -69,7 +70,7 @@ export async function save(ctx: UserCtx) { let dbRole if (!isCreate) { - dbRole = await db.get(_id) + dbRole = await db.get(_id) } if (dbRole && dbRole.name !== name && isNewVersion) { ctx.throw(400, "Cannot change custom role name") @@ -105,7 +106,7 @@ export async function destroy(ctx: UserCtx) { // make sure has the prefix (if it has it then it won't be added) roleId = dbCore.generateRoleID(roleId) } - const role = await db.get(roleId) + const role = await db.get(roleId) // first check no users actively attached to role const users = ( await db.allDocs( diff --git a/packages/server/src/api/controllers/row/internal.ts b/packages/server/src/api/controllers/row/internal.ts index a69aabd3c2..0367346832 100644 --- a/packages/server/src/api/controllers/row/internal.ts +++ b/packages/server/src/api/controllers/row/internal.ts @@ -16,6 +16,7 @@ import { cloneDeep } from "lodash/fp" import { context, db as dbCore } from "@budibase/backend-core" import { finaliseRow, updateRelatedFormula } from "./staticFormula" import { UserCtx, LinkDocumentValue, Row, Table } from "@budibase/types" +import sdk from "../../../sdk" export async function patch(ctx: UserCtx) { const db = context.getAppDB() @@ -24,7 +25,7 @@ export async function patch(ctx: UserCtx) { const isUserTable = tableId === InternalTables.USER_METADATA let oldRow try { - let dbTable = await db.get(tableId) + let dbTable = await sdk.tables.getTable(tableId) oldRow = await outputProcessing( dbTable, await utils.findRow(ctx, tableId, inputs._id) @@ -40,7 +41,7 @@ export async function patch(ctx: UserCtx) { throw "Row does not exist" } } - let dbTable = await db.get(tableId) + let dbTable = await sdk.tables.getTable(tableId) // need to build up full patch fields before coerce let combinedRow: any = cloneDeep(oldRow) for (let key of Object.keys(inputs)) { @@ -95,7 +96,7 @@ export async function save(ctx: UserCtx) { } // this returns the table and row incase they have been updated - const dbTable = await db.get(inputs.tableId) + const dbTable = await sdk.tables.getTable(inputs.tableId) // need to copy the table so it can be differenced on way out const tableClone = cloneDeep(dbTable) @@ -127,7 +128,7 @@ export async function save(ctx: UserCtx) { export async function find(ctx: UserCtx) { const db = dbCore.getDB(ctx.appId) - const table = await db.get(ctx.params.tableId) + const table = await sdk.tables.getTable(ctx.params.tableId) let row = await utils.findRow(ctx, ctx.params.tableId, ctx.params.rowId) row = await outputProcessing(table, row) return row @@ -136,13 +137,13 @@ export async function find(ctx: UserCtx) { export async function destroy(ctx: UserCtx) { const db = context.getAppDB() const { _id } = ctx.request.body - let row = await db.get(_id) + let row = await db.get(_id) let _rev = ctx.request.body._rev || row._rev if (row.tableId !== ctx.params.tableId) { throw "Supplied tableId doesn't match the row's tableId" } - const table = await db.get(row.tableId) + const table = await sdk.tables.getTable(row.tableId) // update the row to include full relationships before deleting them row = await outputProcessing(table, row, { squash: false }) // now remove the relationships @@ -172,7 +173,7 @@ export async function destroy(ctx: UserCtx) { export async function bulkDestroy(ctx: UserCtx) { const db = context.getAppDB() const tableId = ctx.params.tableId - const table = await db.get(tableId) + const table = await sdk.tables.getTable(tableId) let { rows } = ctx.request.body // before carrying out any updates, make sure the rows are ready to be returned @@ -214,7 +215,7 @@ export async function fetchEnrichedRow(ctx: UserCtx) { const rowId = ctx.params.rowId // need table to work out where links go in row let [table, row] = await Promise.all([ - db.get(tableId), + sdk.tables.getTable(tableId), utils.findRow(ctx, tableId, rowId), ]) // get the link docs diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index 9f2c64c7b4..b5d520f771 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -8,8 +8,9 @@ import { FieldTypes, FormulaTypes } from "../../../constants" import { context } from "@budibase/backend-core" import { Table, Row } from "@budibase/types" import * as linkRows from "../../../db/linkedRows" -const { isEqual } = require("lodash") -const { cloneDeep } = require("lodash/fp") +import sdk from "../../../sdk" +import { isEqual } from "lodash" +import { cloneDeep } from "lodash/fp" /** * This function runs through a list of enriched rows, looks at the rows which @@ -148,7 +149,7 @@ export async function finaliseRow( await db.put(table) } catch (err: any) { if (err.status === 409) { - const updatedTable = await db.get(table._id) + const updatedTable = await sdk.tables.getTable(table._id) let response = processAutoColumn(null, updatedTable, row, { reprocessing: true, }) diff --git a/packages/server/src/api/controllers/screen.ts b/packages/server/src/api/controllers/screen.ts index ddfec91c0c..446fe2e5fa 100644 --- a/packages/server/src/api/controllers/screen.ts +++ b/packages/server/src/api/controllers/screen.ts @@ -7,7 +7,7 @@ import { roles, } from "@budibase/backend-core" import { updateAppPackage } from "./application" -import { Plugin, ScreenProps, BBContext } from "@budibase/types" +import { Plugin, ScreenProps, BBContext, Screen } from "@budibase/types" import { builderSocket } from "../../websockets" export async function fetch(ctx: BBContext) { @@ -64,7 +64,7 @@ export async function save(ctx: BBContext) { }) // Update the app metadata - const application = await db.get(DocumentType.APP_METADATA) + const application = await db.get(DocumentType.APP_METADATA) let usedPlugins = application.usedPlugins || [] requiredPlugins.forEach((plugin: Plugin) => { @@ -104,7 +104,7 @@ export async function save(ctx: BBContext) { export async function destroy(ctx: BBContext) { const db = context.getAppDB() const id = ctx.params.screenId - const screen = await db.get(id) + const screen = await db.get(id) await db.remove(id, ctx.params.screenRev) diff --git a/packages/server/src/api/controllers/static/index.ts b/packages/server/src/api/controllers/static/index.ts index fc17cd46e0..3ea07bcc20 100644 --- a/packages/server/src/api/controllers/static/index.ts +++ b/packages/server/src/api/controllers/static/index.ts @@ -1,7 +1,7 @@ require("svelte/register") import { join } from "../../../utilities/centralPath" -const uuid = require("uuid") +import uuid from "uuid" import { ObjectStoreBuckets } from "../../../constants" import { processString } from "@budibase/string-templates" import { @@ -16,6 +16,7 @@ import AWS from "aws-sdk" import fs from "fs" import sdk from "../../../sdk" import * as pro from "@budibase/pro" +import { App } from "@budibase/types" const send = require("koa-send") @@ -110,7 +111,7 @@ export const serveApp = async function (ctx: any) { let db try { db = context.getAppDB({ skip_setup: true }) - const appInfo = await db.get(DocumentType.APP_METADATA) + const appInfo = await db.get(DocumentType.APP_METADATA) let appId = context.getAppId() if (!env.isJest()) { @@ -177,7 +178,7 @@ export const serveApp = async function (ctx: any) { export const serveBuilderPreview = async function (ctx: any) { const db = context.getAppDB({ skip_setup: true }) - const appInfo = await db.get(DocumentType.APP_METADATA) + const appInfo = await db.get(DocumentType.APP_METADATA) if (!env.isJest()) { let appId = context.getAppId() diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 9029d0468a..f008ff43fb 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -323,7 +323,7 @@ export async function save(ctx: UserCtx) { // Since tables are stored inside datasources, we need to notify clients // that the datasource definition changed - const updatedDatasource = await db.get(datasource._id) + const updatedDatasource = await sdk.datasources.get(datasource._id!) builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource) return tableToSave @@ -354,7 +354,7 @@ export async function destroy(ctx: UserCtx) { // Since tables are stored inside datasources, we need to notify clients // that the datasource definition changed - const updatedDatasource = await db.get(datasource._id) + const updatedDatasource = await sdk.datasources.get(datasource._id!) builderSocket?.emitDatasourceUpdate(ctx, updatedDatasource) return tableToDelete diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index a688befcbe..39073caf7f 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -15,7 +15,7 @@ import { isEqual } from "lodash" import { cloneDeep } from "lodash/fp" import sdk from "../../../sdk" -function checkAutoColumns(table: Table, oldTable: Table) { +function checkAutoColumns(table: Table, oldTable?: Table) { if (!table.schema) { return table } @@ -46,7 +46,7 @@ export async function save(ctx: any) { // if the table obj had an _id then it will have been retrieved let oldTable if (ctx.request.body && ctx.request.body._id) { - oldTable = await db.get(ctx.request.body._id) + oldTable = await sdk.tables.getTable(ctx.request.body._id) } // check all types are correct @@ -70,8 +70,8 @@ export async function save(ctx: any) { if (oldTable && oldTable.schema) { for (let propKey of Object.keys(tableToSave.schema)) { let oldColumn = oldTable.schema[propKey] - if (oldColumn && oldColumn.type === "internal") { - oldColumn.type = "auto" + if (oldColumn && oldColumn.type === FieldTypes.INTERNAL) { + oldColumn.type = FieldTypes.AUTO } } } @@ -138,7 +138,7 @@ export async function save(ctx: any) { export async function destroy(ctx: any) { const db = context.getAppDB() - const tableToDelete = await db.get(ctx.params.tableId) + const tableToDelete = await sdk.tables.getTable(ctx.params.tableId) // Delete all rows for that table const rowsData = await db.allDocs( @@ -160,7 +160,7 @@ export async function destroy(ctx: any) { }) // don't remove the table itself until very end - await db.remove(tableToDelete._id, tableToDelete._rev) + await db.remove(tableToDelete._id!, tableToDelete._rev) // remove table search index if (!env.isTest() || env.COUCH_DB_URL) { diff --git a/packages/server/src/api/controllers/user.ts b/packages/server/src/api/controllers/user.ts index 82a776e9a6..1a2a3850ce 100644 --- a/packages/server/src/api/controllers/user.ts +++ b/packages/server/src/api/controllers/user.ts @@ -36,8 +36,8 @@ export async function updateMetadata(ctx: UserCtx) { export async function destroyMetadata(ctx: UserCtx) { const db = context.getAppDB() try { - const dbUser = await db.get(ctx.params.id) - await db.remove(dbUser._id, dbUser._rev) + const dbUser = await sdk.users.get(ctx.params.id) + await db.remove(dbUser._id!, dbUser._rev) } catch (err) { // error just means the global user has no config in this app } @@ -60,7 +60,7 @@ export async function setFlag(ctx: UserCtx) { const db = context.getAppDB() let doc try { - doc = await db.get(flagDocId) + doc = await db.get(flagDocId) } catch (err) { doc = { _id: flagDocId } } diff --git a/packages/server/src/api/controllers/view/index.ts b/packages/server/src/api/controllers/view/index.ts index a087634292..99c4224c62 100644 --- a/packages/server/src/api/controllers/view/index.ts +++ b/packages/server/src/api/controllers/view/index.ts @@ -27,7 +27,8 @@ export async function save(ctx: Ctx) { const db = context.getAppDB() const { originalName, ...viewToSave } = ctx.request.body - const existingTable = await db.get(ctx.request.body.tableId) + const existingTable = await sdk.tables.getTable(ctx.request.body.tableId) + existingTable.views ??= {} const table = cloneDeep(existingTable) const groupByField: any = Object.values(table.schema).find( @@ -120,8 +121,8 @@ export async function destroy(ctx: Ctx) { const db = context.getAppDB() const viewName = decodeURIComponent(ctx.params.viewName) const view = await deleteView(viewName) - const table = await db.get(view.meta.tableId) - delete table.views[viewName] + const table = await sdk.tables.getTable(view.meta.tableId) + delete table.views![viewName] await db.put(table) await events.view.deleted(view) diff --git a/packages/server/src/api/controllers/view/utils.ts b/packages/server/src/api/controllers/view/utils.ts index ef7ce772f9..189c4ede51 100644 --- a/packages/server/src/api/controllers/view/utils.ts +++ b/packages/server/src/api/controllers/view/utils.ts @@ -13,7 +13,7 @@ import { Database } from "@budibase/types" export async function getView(viewName: string) { const db = context.getAppDB() if (env.SELF_HOSTED) { - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") return designDoc.views[viewName] } else { // This is a table view, don't read the view from the DB @@ -22,7 +22,7 @@ export async function getView(viewName: string) { } try { - const viewDoc = await db.get(generateMemoryViewID(viewName)) + const viewDoc = await db.get(generateMemoryViewID(viewName)) return viewDoc.view } catch (err: any) { // Return null when PouchDB doesn't found the view @@ -39,7 +39,7 @@ export async function getViews() { const db = context.getAppDB() const response = [] if (env.SELF_HOSTED) { - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") for (let name of Object.keys(designDoc.views)) { // Only return custom views, not built ins const viewNames = Object.values(ViewName) as string[] @@ -76,7 +76,7 @@ export async function saveView( ) { const db = context.getAppDB() if (env.SELF_HOSTED) { - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") designDoc.views = { ...designDoc.views, [viewName]: viewTemplate, @@ -96,9 +96,9 @@ export async function saveView( tableId: viewTemplate.meta.tableId, } try { - const old = await db.get(id) + const old = await db.get(id) if (originalId) { - const originalDoc = await db.get(originalId) + const originalDoc = await db.get(originalId) await db.remove(originalDoc._id, originalDoc._rev) } if (old && old._rev) { @@ -114,14 +114,14 @@ export async function saveView( export async function deleteView(viewName: string) { const db = context.getAppDB() if (env.SELF_HOSTED) { - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") const view = designDoc.views[viewName] delete designDoc.views[viewName] await db.put(designDoc) return view } else { const id = generateMemoryViewID(viewName) - const viewDoc = await db.get(id) + const viewDoc = await db.get(id) await db.remove(viewDoc._id, viewDoc._rev) return viewDoc.view } @@ -129,7 +129,7 @@ export async function deleteView(viewName: string) { export async function migrateToInMemoryView(db: Database, viewName: string) { // delete the view initially - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") // run the view back through the view builder to update it const view = viewBuilder(designDoc.views[viewName].meta) delete designDoc.views[viewName] @@ -138,15 +138,15 @@ export async function migrateToInMemoryView(db: Database, viewName: string) { } export async function migrateToDesignView(db: Database, viewName: string) { - let view = await db.get(generateMemoryViewID(viewName)) - const designDoc = await db.get("_design/database") + let view = await db.get(generateMemoryViewID(viewName)) + const designDoc = await db.get("_design/database") designDoc.views[viewName] = viewBuilder(view.view.meta) await db.put(designDoc) await db.remove(view._id, view._rev) } export async function getFromDesignDoc(db: Database, viewName: string) { - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") let view = designDoc.views[viewName] if (view == null) { throw { status: 404, message: "Unable to get view" } @@ -155,7 +155,7 @@ export async function getFromDesignDoc(db: Database, viewName: string) { } export async function getFromMemoryDoc(db: Database, viewName: string) { - let view = await db.get(generateMemoryViewID(viewName)) + let view = await db.get(generateMemoryViewID(viewName)) if (view) { view = view.view } else { diff --git a/packages/server/src/api/controllers/webhook.ts b/packages/server/src/api/controllers/webhook.ts index ee92c70628..d667134f4d 100644 --- a/packages/server/src/api/controllers/webhook.ts +++ b/packages/server/src/api/controllers/webhook.ts @@ -77,7 +77,7 @@ export async function trigger(ctx: BBContext) { if (webhook.bodySchema) { validate(ctx.request.body, webhook.bodySchema) } - const target = await db.get(webhook.action.target) + const target = await db.get(webhook.action.target) if (webhook.action.type === WebhookActionType.AUTOMATION) { // trigger with both the pure request and then expand it // incase the user has produced a schema to bind to diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts index dc2f02c821..526197cda8 100644 --- a/packages/server/src/automations/utils.ts +++ b/packages/server/src/automations/utils.ts @@ -8,7 +8,12 @@ import { db as dbCore, context } from "@budibase/backend-core" import { getAutomationMetadataParams } from "../db/utils" import { cloneDeep } from "lodash/fp" import { quotas } from "@budibase/pro" -import { Automation, AutomationJob, WebhookActionType } from "@budibase/types" +import { + Automation, + AutomationJob, + Webhook, + WebhookActionType, +} from "@budibase/types" import sdk from "../sdk" const REBOOT_CRON = "@reboot" @@ -206,13 +211,13 @@ export async function checkForWebhooks({ oldAuto, newAuto }: any) { try { let db = context.getAppDB() // need to get the webhook to get the rev - const webhook = await db.get(oldTrigger.webhookId) + const webhook = await db.get(oldTrigger.webhookId) // might be updating - reset the inputs to remove the URLs if (newTrigger) { delete newTrigger.webhookId newTrigger.inputs = {} } - await sdk.automations.webhook.destroy(webhook._id, webhook._rev) + await sdk.automations.webhook.destroy(webhook._id!, webhook._rev!) } catch (err) { // don't worry about not being able to delete, if it doesn't exist all good } diff --git a/packages/server/src/db/linkedRows/LinkController.ts b/packages/server/src/db/linkedRows/LinkController.ts index 4a154b8f19..c3b3b324b5 100644 --- a/packages/server/src/db/linkedRows/LinkController.ts +++ b/packages/server/src/db/linkedRows/LinkController.ts @@ -182,7 +182,7 @@ class LinkController { }) // if 1:N, ensure that this ID is not already attached to another record - const linkedTable = await this._db.get(field.tableId) + const linkedTable = await this._db.get(field.tableId) const linkedSchema = linkedTable.schema[field.fieldName!] // We need to map the global users to metadata in each app for relationships @@ -311,7 +311,7 @@ class LinkController { }) ) // remove schema from other table - let linkedTable = await this._db.get(field.tableId) + let linkedTable = await this._db.get
(field.tableId) if (field.fieldName) { delete linkedTable.schema[field.fieldName] } @@ -337,7 +337,7 @@ class LinkController { // table for some reason let linkedTable try { - linkedTable = await this._db.get(field.tableId) + linkedTable = await this._db.get
(field.tableId) } catch (err) { /* istanbul ignore next */ continue @@ -416,7 +416,7 @@ class LinkController { const field = schema[fieldName] try { if (field.type === FieldTypes.LINK && field.fieldName) { - const linkedTable = await this._db.get(field.tableId) + const linkedTable = await this._db.get
(field.tableId) delete linkedTable.schema[field.fieldName] await this._db.put(linkedTable) } diff --git a/packages/server/src/db/views/staticViews.ts b/packages/server/src/db/views/staticViews.ts index 730e5b1e66..8952a4d6a1 100644 --- a/packages/server/src/db/views/staticViews.ts +++ b/packages/server/src/db/views/staticViews.ts @@ -22,7 +22,7 @@ const SCREEN_PREFIX = DocumentType.SCREEN + SEPARATOR */ export async function createLinkView() { const db = context.getAppDB() - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") const view = { map: function (doc: LinkDocument) { // everything in this must remain constant as its going to Pouch, no external variables @@ -58,7 +58,7 @@ export async function createLinkView() { export async function createRoutingView() { const db = context.getAppDB() - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") const view = { // if using variables in a map function need to inject them before use map: `function(doc) { @@ -79,7 +79,7 @@ export async function createRoutingView() { async function searchIndex(indexName: string, fnString: string) { const db = context.getAppDB() - const designDoc = await db.get("_design/database") + const designDoc = await db.get("_design/database") designDoc.indexes = { [indexName]: { index: fnString, diff --git a/packages/server/src/middleware/builder.ts b/packages/server/src/middleware/builder.ts index b9481e60e2..881ec843a4 100644 --- a/packages/server/src/middleware/builder.ts +++ b/packages/server/src/middleware/builder.ts @@ -10,7 +10,7 @@ import { setDebounce, } from "../utilities/redis" import { db as dbCore, cache } from "@budibase/backend-core" -import { UserCtx, Database } from "@budibase/types" +import { UserCtx, Database, App } from "@budibase/types" const DEBOUNCE_TIME_SEC = 30 @@ -51,7 +51,7 @@ async function updateAppUpdatedAt(ctx: UserCtx) { } await dbCore.doWithDB(appId, async (db: Database) => { try { - const metadata = await db.get(DocumentType.APP_METADATA) + const metadata = await db.get(DocumentType.APP_METADATA) metadata.updatedAt = new Date().toISOString() metadata.updatedBy = getGlobalIDFromUserMetadataID(ctx.user?.userId!) diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index d8807251a1..6e1e6747e1 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -37,7 +37,7 @@ async function syncUsersToApp( let metadata try { - metadata = await db.get(metadataId) + metadata = await db.get(metadataId) } catch (err: any) { if (err.status !== 404) { throw err diff --git a/packages/server/src/sdk/app/datasources/datasources.ts b/packages/server/src/sdk/app/datasources/datasources.ts index 430f90c159..171ec42042 100644 --- a/packages/server/src/sdk/app/datasources/datasources.ts +++ b/packages/server/src/sdk/app/datasources/datasources.ts @@ -62,7 +62,7 @@ export async function get( opts?: { enriched: boolean } ): Promise { const appDb = context.getAppDB() - const datasource = await appDb.get(datasourceId) + const datasource = await appDb.get(datasourceId) if (opts?.enriched) { return (await enrichDatasourceWithValues(datasource)).datasource } else { @@ -72,7 +72,7 @@ export async function get( export async function getWithEnvVars(datasourceId: string) { const appDb = context.getAppDB() - const datasource = await appDb.get(datasourceId) + const datasource = await appDb.get(datasourceId) return enrichDatasourceWithValues(datasource) } diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index 70274de34a..fb0e35b650 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -11,7 +11,7 @@ import { } from "../../../../db/utils" import { getGlobalUsersFromMetadata } from "../../../../utilities/global" import { outputProcessing } from "../../../../utilities/rowProcessor" -import { Database, Row } from "@budibase/types" +import { Database, Row, Table } from "@budibase/types" import { cleanExportRows } from "../utils" import { Format, @@ -37,7 +37,6 @@ export async function search(options: SearchParams) { return { rows: await fetch(tableId) } } - const db = context.getAppDB() const { paginate, query } = options const params: InternalSearchParams = { @@ -53,7 +52,7 @@ export async function search(options: SearchParams) { let table if (params.sort && !params.sortType) { - table = await db.get(tableId) + table = await sdk.tables.getTable(tableId) const schema = table.schema const sortField = schema[params.sort] params.sortType = sortField.type === "number" ? "number" : "string" @@ -72,7 +71,7 @@ export async function search(options: SearchParams) { if (tableId === InternalTables.USER_METADATA) { response.rows = await getGlobalUsersFromMetadata(response.rows) } - table = table || (await db.get(tableId)) + table = table || (await sdk.tables.getTable(tableId)) response.rows = await outputProcessing(table, response.rows) } @@ -84,7 +83,7 @@ export async function exportRows( ): Promise { const { tableId, format, rowIds, columns, query } = options const db = context.getAppDB() - const table = await db.get(tableId) + const table = await sdk.tables.getTable(tableId) let result if (rowIds) { @@ -140,7 +139,7 @@ export async function exportRows( export async function fetch(tableId: string) { const db = context.getAppDB() - let table = await db.get(tableId) + let table = await sdk.tables.getTable(tableId) let rows = await getRawTableData(db, tableId) const result = await outputProcessing(table, rows) return result @@ -193,12 +192,13 @@ export async function fetchView( let rows if (!calculation) { response.rows = response.rows.map(row => row.doc) - let table + let table: Table try { - table = await db.get(viewInfo.meta.tableId) + table = await sdk.tables.getTable(viewInfo.meta.tableId) } catch (err) { /* istanbul ignore next */ table = { + name: "", schema: {}, } } diff --git a/packages/server/src/sdk/users/crud.ts b/packages/server/src/sdk/users/crud.ts new file mode 100644 index 0000000000..bb043f9976 --- /dev/null +++ b/packages/server/src/sdk/users/crud.ts @@ -0,0 +1,7 @@ +import { context } from "@budibase/backend-core" +import { User } from "@budibase/types" + +export function get(userId: string) { + const db = context.getAppDB() + return db.get(userId) +} diff --git a/packages/server/src/sdk/users/index.ts b/packages/server/src/sdk/users/index.ts index c03eab7c2a..234e12a529 100644 --- a/packages/server/src/sdk/users/index.ts +++ b/packages/server/src/sdk/users/index.ts @@ -1,7 +1,9 @@ import * as utils from "./utils" import * as sessions from "./sessions" +import * as crud from "./crud" export default { ...utils, + ...crud, sessions, } diff --git a/packages/server/src/threads/query.ts b/packages/server/src/threads/query.ts index 9901ddffa6..135c533122 100644 --- a/packages/server/src/threads/query.ts +++ b/packages/server/src/threads/query.ts @@ -11,6 +11,7 @@ import { cloneDeep } from "lodash/fp" import { isSQL } from "../integrations/utils" import { interpolateSQL } from "../integrations/queries/sql" +import { Query } from "@budibase/types" class QueryRunner { datasource: any @@ -167,7 +168,7 @@ class QueryRunner { async runAnotherQuery(queryId: string, parameters: any) { const db = context.getAppDB() - const query = await db.get(queryId) + const query = await db.get(queryId) const datasource = await sdk.datasources.get(query.datasourceId, { enriched: true, }) diff --git a/packages/server/src/utilities/global.ts b/packages/server/src/utilities/global.ts index 8366794b18..c9869b4920 100644 --- a/packages/server/src/utilities/global.ts +++ b/packages/server/src/utilities/global.ts @@ -90,7 +90,7 @@ export async function getCachedSelf(ctx: UserCtx, appId: string) { export async function getRawGlobalUser(userId: string) { const db = tenancy.getGlobalDB() - return db.get(getGlobalIDFromUserMetadataID(userId)) + return db.get(getGlobalIDFromUserMetadataID(userId)) } export async function getGlobalUser(userId: string) { diff --git a/packages/server/src/utilities/index.ts b/packages/server/src/utilities/index.ts index 822ac68631..15e59f3b66 100644 --- a/packages/server/src/utilities/index.ts +++ b/packages/server/src/utilities/index.ts @@ -41,7 +41,7 @@ export async function updateEntityMetadata( // read it to see if it exists, we'll overwrite it no matter what let rev, metadata: Document try { - const oldMetadata = await db.get(id) + const oldMetadata = await db.get(id) rev = oldMetadata._rev metadata = updateFn(oldMetadata) } catch (err) { @@ -75,7 +75,7 @@ export async function deleteEntityMetadata(type: string, entityId: string) { const id = generateMetadataID(type, entityId) let rev try { - const metadata = await db.get(id) + const metadata = await db.get(id) if (metadata) { rev = metadata._rev } diff --git a/packages/worker/src/api/controllers/global/email.ts b/packages/worker/src/api/controllers/global/email.ts index e352ef0b87..837f79035a 100644 --- a/packages/worker/src/api/controllers/global/email.ts +++ b/packages/worker/src/api/controllers/global/email.ts @@ -1,6 +1,6 @@ import { sendEmail as sendEmailFn } from "../../../utilities/email" import { tenancy } from "@budibase/backend-core" -import { BBContext } from "@budibase/types" +import { BBContext, User } from "@budibase/types" export async function sendEmail(ctx: BBContext) { let { @@ -16,10 +16,10 @@ export async function sendEmail(ctx: BBContext) { automation, invite, } = ctx.request.body - let user + let user: any if (userId) { const db = tenancy.getGlobalDB() - user = await db.get(userId) + user = await db.get(userId) } const response = await sendEmailFn(email, purpose, { workspaceId, diff --git a/packages/worker/src/api/controllers/global/roles.ts b/packages/worker/src/api/controllers/global/roles.ts index e0cb52e1b0..572c3328b6 100644 --- a/packages/worker/src/api/controllers/global/roles.ts +++ b/packages/worker/src/api/controllers/global/roles.ts @@ -35,7 +35,7 @@ export async function find(ctx: BBContext) { const appId = ctx.params.appId await context.doInAppContext(dbCore.getDevAppID(appId), async () => { const db = context.getAppDB() - const app = await db.get(dbCore.DocumentType.APP_METADATA) + const app = await db.get(dbCore.DocumentType.APP_METADATA) ctx.body = { roles: await roles.getAllRoles(), name: app.name, diff --git a/packages/worker/src/api/controllers/global/self.ts b/packages/worker/src/api/controllers/global/self.ts index d0232bee60..2ca5b6919c 100644 --- a/packages/worker/src/api/controllers/global/self.ts +++ b/packages/worker/src/api/controllers/global/self.ts @@ -44,7 +44,7 @@ export async function generateAPIKey(ctx: any) { const id = dbCore.generateDevInfoID(userId) let devInfo try { - devInfo = await db.get(id) + devInfo = await db.get(id) } catch (err) { devInfo = { _id: id, userId } } diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 320f7be01a..39a1279925 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -412,7 +412,7 @@ export const inviteAccept = async ( const saved = await userSdk.save(request) const db = tenancy.getGlobalDB() - const user = await db.get(saved._id) + const user = await db.get(saved._id) await events.user.inviteAccepted(user) return saved }) From 58c59eba5e7d653848d168c05f69bbc958fecd0a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 12:00:02 +0200 Subject: [PATCH 4/5] Remove unused db usages --- packages/server/src/api/controllers/datasource.ts | 2 -- packages/server/src/api/controllers/table/internal.ts | 1 - packages/server/src/api/controllers/table/utils.ts | 10 ++-------- packages/server/src/automations/utils.ts | 2 +- packages/server/src/integrations/tests/mysql.spec.ts | 2 +- packages/server/src/sdk/app/tables/index.ts | 1 - 6 files changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 8ef2e8789a..00ae2ea1d7 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -432,7 +432,6 @@ export async function destroy(ctx: UserCtx) { } export async function find(ctx: UserCtx) { - const db = context.getAppDB() const datasource = await sdk.datasources.get(ctx.params.datasourceId) ctx.body = await sdk.datasources.removeSecretSingle(datasource) } @@ -448,7 +447,6 @@ export async function query(ctx: UserCtx) { } export async function getExternalSchema(ctx: UserCtx) { - const db = context.getAppDB() const datasource = await sdk.datasources.get(ctx.params.datasourceId) const enrichedDatasource = await getAndMergeDatasource(datasource) const connector = await getConnector(enrichedDatasource) diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index 39073caf7f..eab77e9169 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -184,7 +184,6 @@ export async function destroy(ctx: any) { } export async function bulkImport(ctx: any) { - const db = context.getAppDB() const table = await sdk.tables.getTable(ctx.params.tableId) const { rows, identifierFields } = ctx.request.body await handleDataImport(ctx.user, table, rows, identifierFields) diff --git a/packages/server/src/api/controllers/table/utils.ts b/packages/server/src/api/controllers/table/utils.ts index 923608e9a2..54ee1b91ca 100644 --- a/packages/server/src/api/controllers/table/utils.ts +++ b/packages/server/src/api/controllers/table/utils.ts @@ -20,16 +20,10 @@ import viewTemplate from "../view/viewBuilder" import { cloneDeep } from "lodash/fp" import { quotas } from "@budibase/pro" import { events, context } from "@budibase/backend-core" -import { - ContextUser, - Database, - Datasource, - SourceName, - Table, -} from "@budibase/types" +import { ContextUser, Datasource, SourceName, Table } from "@budibase/types" export async function clearColumns(table: any, columnNames: any) { - const db: Database = context.getAppDB() + const db = context.getAppDB() const rows = await db.allDocs( getRowParams(table._id, null, { include_docs: true, diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts index 526197cda8..194eff5b32 100644 --- a/packages/server/src/automations/utils.ts +++ b/packages/server/src/automations/utils.ts @@ -209,7 +209,7 @@ export async function checkForWebhooks({ oldAuto, newAuto }: any) { oldTrigger.webhookId ) { try { - let db = context.getAppDB() + const db = context.getAppDB() // need to get the webhook to get the rev const webhook = await db.get(oldTrigger.webhookId) // might be updating - reset the inputs to remove the URLs diff --git a/packages/server/src/integrations/tests/mysql.spec.ts b/packages/server/src/integrations/tests/mysql.spec.ts index e4a496d80d..c3c3b8343e 100644 --- a/packages/server/src/integrations/tests/mysql.spec.ts +++ b/packages/server/src/integrations/tests/mysql.spec.ts @@ -102,7 +102,7 @@ describe("MySQL Integration", () => { ) }) - it("parses strings matching a valid date format", async () => { + it.skip("parses strings matching a valid date format", async () => { const sql = "select * from users;" await config.integration.read({ sql, diff --git a/packages/server/src/sdk/app/tables/index.ts b/packages/server/src/sdk/app/tables/index.ts index c7de1b327c..65010aeaa4 100644 --- a/packages/server/src/sdk/app/tables/index.ts +++ b/packages/server/src/sdk/app/tables/index.ts @@ -28,7 +28,6 @@ async function getAllInternalTables(db?: Database): Promise { async function getAllExternalTables( datasourceId: any ): Promise> { - const db = context.getAppDB() const datasource = await datasources.get(datasourceId, { enriched: true }) if (!datasource || !datasource.entities) { throw "Datasource is not configured fully." From fcedb02ed190c8d55517d54991cc279f3dbcc4c5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 12:18:48 +0200 Subject: [PATCH 5/5] Update pro ref --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index 1929d93660..9c564edb37 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 1929d93660e95f4b190d1936d0b3c65c4ad6a1f5 +Subproject commit 9c564edb37cb619cb5971e10c4317fa6e7c5bb00