Finally removing all usages of backend-core/ type imports from server, including some further typescript conversions.

This commit is contained in:
mike12345567 2022-11-22 19:49:59 +00:00
parent ed6a6ab8ce
commit 6f6d000cb8
53 changed files with 333 additions and 356 deletions

View File

@ -1,10 +1,13 @@
import { DEFAULT_TENANT_ID } from "../constants" import { DEFAULT_TENANT_ID } from "../constants"
import { doWithDB } from "../db" import {
import { DocumentType, StaticDatabases } from "../db/constants" DocumentType,
import { getAllApps } from "../db/utils" StaticDatabases,
getAllApps,
getGlobalDBName,
doWithDB,
} from "../db"
import environment from "../environment" import environment from "../environment"
import { doInTenant, getTenantIds, getTenantId } from "../tenancy" import { doInTenant, getTenantIds, getTenantId } from "../tenancy"
import { getGlobalDBName } from "../db/tenancy"
import * as context from "../context" import * as context from "../context"
import { DEFINITIONS } from "." import { DEFINITIONS } from "."
import { import {

View File

@ -3,7 +3,7 @@ const yargs = require("yargs")
const fs = require("fs") const fs = require("fs")
const { join } = require("path") const { join } = require("path")
require("../src/db").init() require("../src/db").init()
const { doWithDB } = require("@budibase/backend-core/db") const { db: dbCore } = require("@budibase/backend-core")
// load environment // load environment
const env = require("../src/environment") const env = require("../src/environment")
const { const {
@ -48,7 +48,7 @@ yargs
const writeStream = fs.createWriteStream(join(exportPath, "dump.text")) const writeStream = fs.createWriteStream(join(exportPath, "dump.text"))
// perform couch dump // perform couch dump
await doWithDB(appId, async db => { await dbCore.doWithDB(appId, async db => {
return db.dump(writeStream, { return db.dump(writeStream, {
filter: doc => filter: doc =>
!( !(

View File

@ -3,7 +3,7 @@ import { InternalTables } from "../../db/utils"
import { getFullUser } from "../../utilities/users" import { getFullUser } from "../../utilities/users"
import { roles, context } from "@budibase/backend-core" import { roles, context } from "@budibase/backend-core"
import { groups } from "@budibase/pro" import { groups } from "@budibase/pro"
import { ContextUser, User } from "@budibase/types" import { ContextUser, User, Row } from "@budibase/types"
const PUBLIC_ROLE = roles.BUILTIN_ROLE_IDS.PUBLIC const PUBLIC_ROLE = roles.BUILTIN_ROLE_IDS.PUBLIC
@ -43,7 +43,7 @@ export async function fetchSelf(ctx: any) {
try { try {
const userTable = await db.get(InternalTables.USER_METADATA) const userTable = await db.get(InternalTables.USER_METADATA)
// specifically needs to make sure is enriched // specifically needs to make sure is enriched
ctx.body = await outputProcessing(userTable, user) ctx.body = await outputProcessing(userTable, user as Row)
} catch (err: any) { } catch (err: any) {
let response let response
// user didn't exist in app, don't pretend they do // user didn't exist in app, don't pretend they do

View File

@ -7,7 +7,6 @@ import { create } from "./application"
import { join } from "path" import { join } from "path"
import { App, BBContext, Database } from "@budibase/types" import { App, BBContext, Database } from "@budibase/types"
import sdk from "../../sdk" import sdk from "../../sdk"
import { getAllApps } from "@budibase/backend-core/src/db"
async function createApp(appName: string, appDirectory: string) { async function createApp(appName: string, appDirectory: string) {
const ctx = { const ctx = {
@ -39,7 +38,7 @@ export async function exportApps(ctx: BBContext) {
if (env.SELF_HOSTED || !env.MULTI_TENANCY) { if (env.SELF_HOSTED || !env.MULTI_TENANCY) {
ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.") ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.")
} }
const apps = (await getAllApps({ all: true })) as App[] const apps = (await dbCore.getAllApps({ all: true })) as App[]
const globalDBString = await sdk.backups.exportDB(dbCore.getGlobalDBName(), { const globalDBString = await sdk.backups.exportDB(dbCore.getGlobalDBName(), {
filter: (doc: any) => !doc._id.startsWith(DocumentType.USER), filter: (doc: any) => !doc._id.startsWith(DocumentType.USER),
}) })
@ -61,7 +60,7 @@ async function checkHasBeenImported() {
if (!env.SELF_HOSTED || env.MULTI_TENANCY) { if (!env.SELF_HOSTED || env.MULTI_TENANCY) {
return true return true
} }
const apps = await getAllApps({ all: true }) const apps = await dbCore.getAllApps({ all: true })
return apps.length !== 0 return apps.length !== 0
} }

View File

@ -1,30 +1,29 @@
const { import {
generateDatasourceID, generateDatasourceID,
getDatasourceParams, getDatasourceParams,
getQueryParams, getQueryParams,
DocumentType, DocumentType,
BudibaseInternalDB, BudibaseInternalDB,
getTableParams, getTableParams,
} = require("../../db/utils") } from "../../db/utils"
const { destroy: tableDestroy } = require("./table/internal") import { destroy as tableDestroy } from "./table/internal"
const { BuildSchemaErrors, InvalidColumns } = require("../../constants") import { BuildSchemaErrors, InvalidColumns } from "../../constants"
const { getIntegration } = require("../../integrations") import { getIntegration } from "../../integrations"
const { getDatasourceAndQuery } = require("./row/utils") import { getDatasourceAndQuery } from "./row/utils"
const { invalidateDynamicVariables } = require("../../threads/utils") import { invalidateDynamicVariables } from "../../threads/utils"
const { getAppDB } = require("@budibase/backend-core/context") import { db as dbCore, context, events } from "@budibase/backend-core"
const { events } = require("@budibase/backend-core") import { BBContext, Datasource, Row } from "@budibase/types"
const { db: dbCore } = require("@budibase/backend-core")
exports.fetch = async function (ctx) { export async function fetch(ctx: BBContext) {
// Get internal tables // Get internal tables
const db = getAppDB() const db = context.getAppDB()
const internalTables = await db.allDocs( const internalTables = await db.allDocs(
getTableParams(null, { getTableParams(null, {
include_docs: true, include_docs: true,
}) })
) )
const internal = internalTables.rows.reduce((acc, row) => { const internal = internalTables.rows.reduce((acc: any, row: Row) => {
const sourceId = row.doc.sourceId || "bb_internal" const sourceId = row.doc.sourceId || "bb_internal"
acc[sourceId] = acc[sourceId] || [] acc[sourceId] = acc[sourceId] || []
acc[sourceId].push(row.doc) acc[sourceId].push(row.doc)
@ -60,8 +59,8 @@ exports.fetch = async function (ctx) {
ctx.body = [bbInternalDb, ...datasources] ctx.body = [bbInternalDb, ...datasources]
} }
exports.buildSchemaFromDb = async function (ctx) { export async function buildSchemaFromDb(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const datasource = await db.get(ctx.params.datasourceId) const datasource = await db.get(ctx.params.datasourceId)
const tablesFilter = ctx.request.body.tablesFilter const tablesFilter = ctx.request.body.tablesFilter
@ -72,7 +71,9 @@ exports.buildSchemaFromDb = async function (ctx) {
} }
for (let key in tables) { for (let key in tables) {
if ( if (
tablesFilter.some(filter => filter.toLowerCase() === key.toLowerCase()) tablesFilter.some(
(filter: any) => filter.toLowerCase() === key.toLowerCase()
)
) { ) {
datasource.entities[key] = tables[key] datasource.entities[key] = tables[key]
} }
@ -85,7 +86,7 @@ exports.buildSchemaFromDb = async function (ctx) {
const dbResp = await db.put(datasource) const dbResp = await db.put(datasource)
datasource._rev = dbResp.rev datasource._rev = dbResp.rev
const response = { datasource } const response: any = { datasource }
if (error) { if (error) {
response.error = error response.error = error
} }
@ -95,9 +96,9 @@ exports.buildSchemaFromDb = async function (ctx) {
/** /**
* Make sure all datasource entities have a display name selected * Make sure all datasource entities have a display name selected
*/ */
const setDefaultDisplayColumns = datasource => { function setDefaultDisplayColumns(datasource: Datasource) {
// //
for (let entity of Object.values(datasource.entities)) { for (let entity of Object.values(datasource.entities || {})) {
if (entity.primaryDisplay) { if (entity.primaryDisplay) {
continue continue
} }
@ -113,9 +114,12 @@ const setDefaultDisplayColumns = datasource => {
/** /**
* Check for variables that have been updated or removed and invalidate them. * Check for variables that have been updated or removed and invalidate them.
*/ */
const invalidateVariables = async (existingDatasource, updatedDatasource) => { async function invalidateVariables(
const existingVariables = existingDatasource.config.dynamicVariables existingDatasource: Datasource,
const updatedVariables = updatedDatasource.config.dynamicVariables updatedDatasource: Datasource
) {
const existingVariables: any = existingDatasource.config?.dynamicVariables
const updatedVariables: any = updatedDatasource.config?.dynamicVariables
const toInvalidate = [] const toInvalidate = []
if (!existingVariables) { if (!existingVariables) {
@ -127,9 +131,9 @@ const invalidateVariables = async (existingDatasource, updatedDatasource) => {
toInvalidate.push(...existingVariables) toInvalidate.push(...existingVariables)
} else { } else {
// invaldate changed / removed // invaldate changed / removed
existingVariables.forEach(existing => { existingVariables.forEach((existing: any) => {
const unchanged = updatedVariables.find( const unchanged = updatedVariables.find(
updated => (updated: any) =>
existing.name === updated.name && existing.name === updated.name &&
existing.queryId === updated.queryId && existing.queryId === updated.queryId &&
existing.value === updated.value existing.value === updated.value
@ -142,8 +146,8 @@ const invalidateVariables = async (existingDatasource, updatedDatasource) => {
await invalidateDynamicVariables(toInvalidate) await invalidateDynamicVariables(toInvalidate)
} }
exports.update = async function (ctx) { export async function update(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const datasourceId = ctx.params.datasourceId const datasourceId = ctx.params.datasourceId
let datasource = await db.get(datasourceId) let datasource = await db.get(datasourceId)
const auth = datasource.config.auth const auth = datasource.config.auth
@ -171,8 +175,8 @@ exports.update = async function (ctx) {
ctx.body = { datasource } ctx.body = { datasource }
} }
exports.save = async function (ctx) { export async function save(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const plus = ctx.request.body.datasource.plus const plus = ctx.request.body.datasource.plus
const fetchSchema = ctx.request.body.fetchSchema const fetchSchema = ctx.request.body.fetchSchema
@ -202,15 +206,15 @@ exports.save = async function (ctx) {
} }
} }
const response = { datasource } const response: any = { datasource }
if (schemaError) { if (schemaError) {
response.error = schemaError response.error = schemaError
} }
ctx.body = response ctx.body = response
} }
const destroyInternalTablesBySourceId = async datasourceId => { async function destroyInternalTablesBySourceId(datasourceId: string) {
const db = getAppDB() const db = context.getAppDB()
// Get all internal tables // Get all internal tables
const internalTables = await db.allDocs( const internalTables = await db.allDocs(
@ -220,12 +224,15 @@ const destroyInternalTablesBySourceId = async datasourceId => {
) )
// Filter by datasource and return the docs. // Filter by datasource and return the docs.
const datasourceTableDocs = internalTables.rows.reduce((acc, table) => { const datasourceTableDocs = internalTables.rows.reduce(
(acc: any, table: any) => {
if (table.doc.sourceId == datasourceId) { if (table.doc.sourceId == datasourceId) {
acc.push(table.doc) acc.push(table.doc)
} }
return acc return acc
}, []) },
[]
)
// Destroy the tables. // Destroy the tables.
for (const table of datasourceTableDocs) { for (const table of datasourceTableDocs) {
@ -237,8 +244,8 @@ const destroyInternalTablesBySourceId = async datasourceId => {
} }
} }
exports.destroy = async function (ctx) { export async function destroy(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const datasourceId = ctx.params.datasourceId const datasourceId = ctx.params.datasourceId
const datasource = await db.get(datasourceId) const datasource = await db.get(datasourceId)
@ -249,7 +256,7 @@ exports.destroy = async function (ctx) {
} else { } else {
const queries = await db.allDocs(getQueryParams(datasourceId, null)) const queries = await db.allDocs(getQueryParams(datasourceId, null))
await db.bulkDocs( await db.bulkDocs(
queries.rows.map(row => ({ queries.rows.map((row: any) => ({
_id: row.id, _id: row.id,
_rev: row.value.rev, _rev: row.value.rev,
_deleted: true, _deleted: true,
@ -265,28 +272,28 @@ exports.destroy = async function (ctx) {
ctx.status = 200 ctx.status = 200
} }
exports.find = async function (ctx) { export async function find(ctx: BBContext) {
const database = getAppDB() const database = context.getAppDB()
ctx.body = await database.get(ctx.params.datasourceId) ctx.body = await database.get(ctx.params.datasourceId)
} }
// dynamic query functionality // dynamic query functionality
exports.query = async function (ctx) { export async function query(ctx: BBContext) {
const queryJson = ctx.request.body const queryJson = ctx.request.body
try { try {
ctx.body = await getDatasourceAndQuery(queryJson) ctx.body = await getDatasourceAndQuery(queryJson)
} catch (err) { } catch (err: any) {
ctx.throw(400, err) ctx.throw(400, err)
} }
} }
function getErrorTables(errors, errorType) { function getErrorTables(errors: any, errorType: string) {
return Object.entries(errors) return Object.entries(errors)
.filter(entry => entry[1] === errorType) .filter(entry => entry[1] === errorType)
.map(([name]) => name) .map(([name]) => name)
} }
function updateError(error, newError, tables) { function updateError(error: any, newError: any, tables: string[]) {
if (!error) { if (!error) {
error = "" error = ""
} }
@ -297,7 +304,7 @@ function updateError(error, newError, tables) {
return error return error
} }
const buildSchemaHelper = async datasource => { async function buildSchemaHelper(datasource: Datasource) {
const Connector = await getIntegration(datasource.source) const Connector = await getIntegration(datasource.source)
// Connect to the DB and build the schema // Connect to the DB and build the schema

View File

@ -4,7 +4,7 @@ import { context } from "@budibase/backend-core"
/** /**
* This is used to pass around information about the deployment that is occurring * This is used to pass around information about the deployment that is occurring
*/ */
export class Deployment { export default class Deployment {
_id: string _id: string
verification: any verification: any
status?: string status?: string

View File

@ -1,10 +1,10 @@
const { EMPTY_LAYOUT } = require("../../constants/layouts") import { EMPTY_LAYOUT } from "../../constants/layouts"
const { generateLayoutID, getScreenParams } = require("../../db/utils") import { generateLayoutID, getScreenParams } from "../../db/utils"
const { getAppDB } = require("@budibase/backend-core/context") import { events, context } from "@budibase/backend-core"
const { events } = require("@budibase/backend-core") import { BBContext } from "@budibase/types"
exports.save = async function (ctx) { export async function save(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
let layout = ctx.request.body let layout = ctx.request.body
if (!layout.props) { if (!layout.props) {
@ -24,8 +24,8 @@ exports.save = async function (ctx) {
ctx.status = 200 ctx.status = 200
} }
exports.destroy = async function (ctx) { export async function destroy(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const layoutId = ctx.params.layoutId, const layoutId = ctx.params.layoutId,
layoutRev = ctx.params.layoutRev layoutRev = ctx.params.layoutRev

View File

@ -1,6 +1,6 @@
import { search as stringSearch, addRev } from "./utils" import { search as stringSearch, addRev } from "./utils"
import { default as controller } from "../table" import * as controller from "../table"
import { Table } from "../../../definitions/common" import { Table } from "@budibase/types"
function fixTable(table: Table, params: any) { function fixTable(table: Table, params: any) {
if (!params || !table) { if (!params || !table) {

View File

@ -9,7 +9,7 @@ const {
breakRowIdField, breakRowIdField,
} = require("../../../integrations/utils") } = require("../../../integrations/utils")
const ExternalRequest = require("./ExternalRequest") const ExternalRequest = require("./ExternalRequest")
const { getAppDB } = require("@budibase/backend-core/context") const { context } = require("@budibase/backend-core")
const exporters = require("../view/exporters") const exporters = require("../view/exporters")
const { apiFileReturn } = require("../../../utilities/fileSystem") const { apiFileReturn } = require("../../../utilities/fileSystem")
@ -166,7 +166,7 @@ exports.validate = async () => {
exports.exportRows = async ctx => { exports.exportRows = async ctx => {
const { datasourceId } = breakExternalTableId(ctx.params.tableId) const { datasourceId } = breakExternalTableId(ctx.params.tableId)
const db = getAppDB() const db = context.getAppDB()
const format = ctx.query.format const format = ctx.query.format
const { columns } = ctx.request.body const { columns } = ctx.request.body
const datasource = await db.get(datasourceId) const datasource = await db.get(datasourceId)
@ -209,7 +209,7 @@ exports.fetchEnrichedRow = async ctx => {
const id = ctx.params.rowId const id = ctx.params.rowId
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const { datasourceId, tableName } = breakExternalTableId(tableId) const { datasourceId, tableName } = breakExternalTableId(tableId)
const db = getAppDB() const db = context.getAppDB()
const datasource = await db.get(datasourceId) const datasource = await db.get(datasourceId)
if (!datasource || !datasource.entities) { if (!datasource || !datasource.entities) {
ctx.throw(400, "Datasource has not been configured for plus API.") ctx.throw(400, "Datasource has not been configured for plus API.")

View File

@ -6,7 +6,6 @@ const {
DocumentType, DocumentType,
InternalTables, InternalTables,
} = require("../../../db/utils") } = require("../../../db/utils")
const { getDB } = require("@budibase/backend-core/db")
const userController = require("../user") const userController = require("../user")
const { const {
inputProcessing, inputProcessing,
@ -26,7 +25,7 @@ const {
getFromMemoryDoc, getFromMemoryDoc,
} = require("../view/utils") } = require("../view/utils")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
const { getAppDB } = require("@budibase/backend-core/context") const { context, db: dbCore } = require("@budibase/backend-core")
const { finaliseRow, updateRelatedFormula } = require("./staticFormula") const { finaliseRow, updateRelatedFormula } = require("./staticFormula")
const exporters = require("../view/exporters") const exporters = require("../view/exporters")
const { apiFileReturn } = require("../../../utilities/fileSystem") const { apiFileReturn } = require("../../../utilities/fileSystem")
@ -80,7 +79,7 @@ async function getRawTableData(ctx, db, tableId) {
} }
exports.patch = async ctx => { exports.patch = async ctx => {
const db = getAppDB() const db = context.getAppDB()
const inputs = ctx.request.body const inputs = ctx.request.body
const tableId = inputs.tableId const tableId = inputs.tableId
const isUserTable = tableId === InternalTables.USER_METADATA const isUserTable = tableId === InternalTables.USER_METADATA
@ -145,7 +144,7 @@ exports.patch = async ctx => {
} }
exports.save = async function (ctx) { exports.save = async function (ctx) {
const db = getAppDB() const db = context.getAppDB()
let inputs = ctx.request.body let inputs = ctx.request.body
inputs.tableId = ctx.params.tableId inputs.tableId = ctx.params.tableId
@ -188,7 +187,7 @@ exports.fetchView = async ctx => {
return exports.fetch(ctx) return exports.fetch(ctx)
} }
const db = getAppDB() const db = context.getAppDB()
const { calculation, group, field } = ctx.query const { calculation, group, field } = ctx.query
const viewInfo = await getView(db, viewName) const viewInfo = await getView(db, viewName)
let response let response
@ -242,7 +241,7 @@ exports.fetchView = async ctx => {
} }
exports.fetch = async ctx => { exports.fetch = async ctx => {
const db = getAppDB() const db = context.getAppDB()
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
let table = await db.get(tableId) let table = await db.get(tableId)
@ -251,7 +250,7 @@ exports.fetch = async ctx => {
} }
exports.find = async ctx => { exports.find = async ctx => {
const db = getDB(ctx.appId) const db = dbCore.getDB(ctx.appId)
const table = await db.get(ctx.params.tableId) const table = await db.get(ctx.params.tableId)
let row = await findRow(ctx, ctx.params.tableId, ctx.params.rowId) let row = await findRow(ctx, ctx.params.tableId, ctx.params.rowId)
row = await outputProcessing(table, row) row = await outputProcessing(table, row)
@ -259,7 +258,7 @@ exports.find = async ctx => {
} }
exports.destroy = async function (ctx) { exports.destroy = async function (ctx) {
const db = getAppDB() const db = context.getAppDB()
const { _id } = ctx.request.body 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 let _rev = ctx.request.body._rev || row._rev
@ -295,7 +294,7 @@ exports.destroy = async function (ctx) {
} }
exports.bulkDestroy = async ctx => { exports.bulkDestroy = async ctx => {
const db = getAppDB() const db = context.getAppDB()
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const table = await db.get(tableId) const table = await db.get(tableId)
let { rows } = ctx.request.body let { rows } = ctx.request.body
@ -338,7 +337,7 @@ exports.search = async ctx => {
} }
const { tableId } = ctx.params const { tableId } = ctx.params
const db = getAppDB() const db = context.getAppDB()
const { paginate, query, ...params } = ctx.request.body const { paginate, query, ...params } = ctx.request.body
params.version = ctx.version params.version = ctx.version
params.tableId = tableId params.tableId = tableId
@ -371,7 +370,7 @@ exports.validate = async ctx => {
} }
exports.exportRows = async ctx => { exports.exportRows = async ctx => {
const db = getAppDB() const db = context.getAppDB()
const table = await db.get(ctx.params.tableId) const table = await db.get(ctx.params.tableId)
const rowIds = ctx.request.body.rows const rowIds = ctx.request.body.rows
let format = ctx.query.format let format = ctx.query.format
@ -408,7 +407,7 @@ exports.exportRows = async ctx => {
} }
exports.fetchEnrichedRow = async ctx => { exports.fetchEnrichedRow = async ctx => {
const db = getAppDB() const db = context.getAppDB()
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const rowId = ctx.params.rowId const rowId = ctx.params.rowId
// need table to work out where links go in row // need table to work out where links go in row

View File

@ -1,5 +1,5 @@
import { InternalTables } from "../../../db/utils" import { InternalTables } from "../../../db/utils"
import userController from "../user" import * as userController from "../user"
import { FieldTypes } from "../../../constants" import { FieldTypes } from "../../../constants"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { makeExternalQuery } from "../../../integrations/base/query" import { makeExternalQuery } from "../../../integrations/base/query"

View File

@ -133,7 +133,7 @@ export async function save(ctx: any) {
tableToSave._rev = result.rev tableToSave._rev = result.rev
} }
// has to run after, make sure it has _id // has to run after, make sure it has _id
await runStaticFormulaChecks(tableToSave, { oldTable, deletion: null }) await runStaticFormulaChecks(tableToSave, { oldTable, deletion: false })
return tableToSave return tableToSave
} }
@ -176,7 +176,6 @@ export async function destroy(ctx: any) {
// has to run after, make sure it has _id // has to run after, make sure it has _id
await runStaticFormulaChecks(tableToDelete, { await runStaticFormulaChecks(tableToDelete, {
oldTable: null,
deletion: true, deletion: true,
}) })
await cleanupAttachments(tableToDelete, { await cleanupAttachments(tableToDelete, {

View File

@ -1,23 +1,22 @@
const { import {
generateUserMetadataID, generateUserMetadataID,
getUserMetadataParams, getUserMetadataParams,
generateUserFlagID, generateUserFlagID,
} = require("../../db/utils") } from "../../db/utils"
const { InternalTables } = require("../../db/utils") import { InternalTables } from "../../db/utils"
const { getGlobalUsers, getRawGlobalUser } = require("../../utilities/global") import { getGlobalUsers, getRawGlobalUser } from "../../utilities/global"
const { getFullUser } = require("../../utilities/users") import { getFullUser } from "../../utilities/users"
const { isEqual } = require("lodash") import { isEqual } from "lodash"
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") import {
const { context,
getDevelopmentAppID, constants,
getProdAppIDs, roles as rolesCore,
dbExists, db as dbCore,
} = require("@budibase/backend-core/db") } from "@budibase/backend-core"
const { UserStatus } = require("@budibase/backend-core/constants") import { BBContext, User } from "@budibase/types"
const { getAppDB, doInAppContext } = require("@budibase/backend-core/context")
async function rawMetadata() { async function rawMetadata() {
const db = getAppDB() const db = context.getAppDB()
return ( return (
await db.allDocs( await db.allDocs(
getUserMetadataParams(null, { getUserMetadataParams(null, {
@ -27,9 +26,9 @@ async function rawMetadata() {
).rows.map(row => row.doc) ).rows.map(row => row.doc)
} }
function combineMetadataAndUser(user, metadata) { function combineMetadataAndUser(user: any, metadata: any) {
// skip users with no access // skip users with no access
if (user.roleId === BUILTIN_ROLE_IDS.PUBLIC) { if (user.roleId === rolesCore.BUILTIN_ROLE_IDS.PUBLIC) {
return null return null
} }
delete user._rev delete user._rev
@ -55,9 +54,9 @@ function combineMetadataAndUser(user, metadata) {
return null return null
} }
exports.syncGlobalUsers = async () => { export async function syncGlobalUsers() {
// sync user metadata // sync user metadata
const db = getAppDB() const db = context.getAppDB()
const [users, metadata] = await Promise.all([getGlobalUsers(), rawMetadata()]) const [users, metadata] = await Promise.all([getGlobalUsers(), rawMetadata()])
const toWrite = [] const toWrite = []
for (let user of users) { for (let user of users) {
@ -69,13 +68,13 @@ exports.syncGlobalUsers = async () => {
await db.bulkDocs(toWrite) await db.bulkDocs(toWrite)
} }
exports.syncUser = async function (ctx) { export async function syncUser(ctx: BBContext) {
let deleting = false, let deleting = false,
user user: User | any
const userId = ctx.params.id const userId = ctx.params.id
try { try {
user = await getRawGlobalUser(userId) user = await getRawGlobalUser(userId)
} catch (err) { } catch (err: any) {
if (err && err.status === 404) { if (err && err.status === 404) {
user = {} user = {}
deleting = true deleting = true
@ -92,21 +91,21 @@ exports.syncUser = async function (ctx) {
let prodAppIds let prodAppIds
// if they are a builder then get all production app IDs // if they are a builder then get all production app IDs
if ((user.builder && user.builder.global) || deleting) { if ((user.builder && user.builder.global) || deleting) {
prodAppIds = await getProdAppIDs() prodAppIds = await dbCore.getProdAppIDs()
} else { } else {
prodAppIds = Object.entries(roles) prodAppIds = Object.entries(roles)
.filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC) .filter(entry => entry[1] !== rolesCore.BUILTIN_ROLE_IDS.PUBLIC)
.map(([appId]) => appId) .map(([appId]) => appId)
} }
for (let prodAppId of prodAppIds) { for (let prodAppId of prodAppIds) {
const roleId = roles[prodAppId] const roleId = roles[prodAppId]
const devAppId = getDevelopmentAppID(prodAppId) const devAppId = dbCore.getDevelopmentAppID(prodAppId)
for (let appId of [prodAppId, devAppId]) { for (let appId of [prodAppId, devAppId]) {
if (!(await dbExists(appId))) { if (!(await dbCore.dbExists(appId))) {
continue continue
} }
await doInAppContext(appId, async () => { await context.doInAppContext(appId, async () => {
const db = getAppDB() const db = context.getAppDB()
const metadataId = generateUserMetadataID(userId) const metadataId = generateUserMetadataID(userId)
let metadata let metadata
try { try {
@ -127,8 +126,8 @@ exports.syncUser = async function (ctx) {
? combineMetadataAndUser(user, metadata) ? combineMetadataAndUser(user, metadata)
: { : {
...metadata, ...metadata,
status: UserStatus.INACTIVE, status: constants.UserStatus.INACTIVE,
metadata: BUILTIN_ROLE_IDS.PUBLIC, metadata: rolesCore.BUILTIN_ROLE_IDS.PUBLIC,
} }
// if its null then there was no updates required // if its null then there was no updates required
if (combined) { if (combined) {
@ -142,10 +141,9 @@ exports.syncUser = async function (ctx) {
} }
} }
exports.fetchMetadata = async function (ctx) { export async function fetchMetadata(ctx: BBContext) {
const database = getAppDB()
const global = await getGlobalUsers() const global = await getGlobalUsers()
const metadata = await rawMetadata(database) const metadata = await rawMetadata()
const users = [] const users = []
for (let user of global) { for (let user of global) {
// find the metadata that matches up to the global ID // find the metadata that matches up to the global ID
@ -162,18 +160,18 @@ exports.fetchMetadata = async function (ctx) {
ctx.body = users ctx.body = users
} }
exports.updateSelfMetadata = async function (ctx) { export async function updateSelfMetadata(ctx: BBContext) {
// overwrite the ID with current users // overwrite the ID with current users
ctx.request.body._id = ctx.user._id ctx.request.body._id = ctx.user?._id
// make sure no stale rev // make sure no stale rev
delete ctx.request.body._rev delete ctx.request.body._rev
// make sure no csrf token // make sure no csrf token
delete ctx.request.body.csrfToken delete ctx.request.body.csrfToken
await exports.updateMetadata(ctx) await updateMetadata(ctx)
} }
exports.updateMetadata = async function (ctx) { export async function updateMetadata(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
const user = ctx.request.body const user = ctx.request.body
// this isn't applicable to the user // this isn't applicable to the user
delete user.roles delete user.roles
@ -184,8 +182,8 @@ exports.updateMetadata = async function (ctx) {
ctx.body = await db.put(metadata) ctx.body = await db.put(metadata)
} }
exports.destroyMetadata = async function (ctx) { export async function destroyMetadata(ctx: BBContext) {
const db = getAppDB() const db = context.getAppDB()
try { try {
const dbUser = await db.get(ctx.params.id) const dbUser = await db.get(ctx.params.id)
await db.remove(dbUser._id, dbUser._rev) await db.remove(dbUser._id, dbUser._rev)
@ -197,18 +195,18 @@ exports.destroyMetadata = async function (ctx) {
} }
} }
exports.findMetadata = async function (ctx) { export async function findMetadata(ctx: BBContext) {
ctx.body = await getFullUser(ctx, ctx.params.id) ctx.body = await getFullUser(ctx, ctx.params.id)
} }
exports.setFlag = async function (ctx) { export async function setFlag(ctx: BBContext) {
const userId = ctx.user._id const userId = ctx.user?._id
const { flag, value } = ctx.request.body const { flag, value } = ctx.request.body
if (!flag) { if (!flag) {
ctx.throw(400, "Must supply a 'flag' field in request body.") ctx.throw(400, "Must supply a 'flag' field in request body.")
} }
const flagDocId = generateUserFlagID(userId) const flagDocId = generateUserFlagID(userId!)
const db = getAppDB() const db = context.getAppDB()
let doc let doc
try { try {
doc = await db.get(flagDocId) doc = await db.get(flagDocId)
@ -220,10 +218,10 @@ exports.setFlag = async function (ctx) {
ctx.body = { message: "Flag set successfully" } ctx.body = { message: "Flag set successfully" }
} }
exports.getFlags = async function (ctx) { export async function getFlags(ctx: BBContext) {
const userId = ctx.user._id const userId = ctx.user?._id
const docId = generateUserFlagID(userId) const docId = generateUserFlagID(userId!)
const db = getAppDB() const db = context.getAppDB()
let doc let doc
try { try {
doc = await db.get(docId) doc = await db.get(docId)

View File

@ -70,7 +70,7 @@ export async function getViews() {
} }
export async function saveView( export async function saveView(
originalName: string, originalName: string | null,
viewName: string, viewName: string,
viewTemplate: any viewTemplate: any
) { ) {

View File

@ -1,20 +1,15 @@
const Router = require("@koa/router") import Router from "@koa/router"
const { import { errors, auth } from "@budibase/backend-core"
buildAuthMiddleware, import currentApp from "../middleware/currentapp"
auditLog, import zlib from "zlib"
buildTenancyMiddleware, import { mainRoutes, staticRoutes, publicRoutes } from "./routes"
} = require("@budibase/backend-core/auth") import pkg from "../../package.json"
const { errors } = require("@budibase/backend-core") import env from "../environment"
const currentApp = require("../middleware/currentapp") import { middleware as pro } from "@budibase/pro"
export { shutdown } from "./routes/public"
const compress = require("koa-compress") const compress = require("koa-compress")
const zlib = require("zlib")
const { mainRoutes, staticRoutes, publicRoutes } = require("./routes")
const pkg = require("../../package.json")
const env = require("../environment")
const { middleware: pro } = require("@budibase/pro")
const { shutdown } = require("./routes/public")
const router = new Router() export const router: Router = new Router()
router.get("/health", ctx => (ctx.status = 200)) router.get("/health", ctx => (ctx.status = 200))
router.get("/version", ctx => (ctx.body = pkg.version)) router.get("/version", ctx => (ctx.body = pkg.version))
@ -42,7 +37,7 @@ router
// re-direct before any middlewares occur // re-direct before any middlewares occur
.redirect("/", "/builder") .redirect("/", "/builder")
.use( .use(
buildAuthMiddleware(null, { auth.buildAuthMiddleware(null, {
publicAllowed: true, publicAllowed: true,
}) })
) )
@ -50,19 +45,20 @@ router
// the server can be public anywhere, so nowhere should throw errors // the server can be public anywhere, so nowhere should throw errors
// if the tenancy has not been set, it'll have to be discovered at application layer // if the tenancy has not been set, it'll have to be discovered at application layer
.use( .use(
buildTenancyMiddleware(null, null, { auth.buildTenancyMiddleware(null, null, {
noTenancyRequired: true, noTenancyRequired: true,
}) })
) )
.use(pro.licensing()) .use(pro.licensing())
// @ts-ignore
.use(currentApp) .use(currentApp)
.use(auditLog) .use(auth.auditLog)
// error handling middleware // error handling middleware
router.use(async (ctx, next) => { router.use(async (ctx, next) => {
try { try {
await next() await next()
} catch (err) { } catch (err: any) {
ctx.status = err.status || err.statusCode || 500 ctx.status = err.status || err.statusCode || 500
const error = errors.getPublicError(err) const error = errors.getPublicError(err)
ctx.body = { ctx.body = {
@ -91,6 +87,3 @@ router.use(publicRoutes.allowedMethods())
// WARNING - static routes will catch everything else after them this must be last // WARNING - static routes will catch everything else after them this must be last
router.use(staticRoutes.routes()) router.use(staticRoutes.routes())
router.use(staticRoutes.allowedMethods()) router.use(staticRoutes.allowedMethods())
module.exports.router = router
module.exports.shutdown = shutdown

View File

@ -1,7 +1,7 @@
import Router from "@koa/router" import Router from "@koa/router"
import authorized from "../../middleware/authorized" import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
import controller from "../controllers/layout" import * as controller from "../controllers/layout"
const router: Router = new Router() const router: Router = new Router()

View File

@ -1,7 +1,7 @@
import Router from "@koa/router" import Router from "@koa/router"
import * as controller from "../controllers/script" import * as controller from "../controllers/script"
import authorized from "../../middleware/authorized" import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core/permissions" import { permissions } from "@budibase/backend-core"
const router: Router = new Router() const router: Router = new Router()

View File

@ -1,6 +1,7 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { roles } = require("@budibase/backend-core")
const setup = require("./utilities") const setup = require("./utilities")
const { basicRow } = setup.structures const { basicRow } = setup.structures
const { BUILTIN_ROLE_IDS } = roles
const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC
const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC

View File

@ -1,10 +1,8 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { roles, events, permissions } = require("@budibase/backend-core")
const {
BuiltinPermissionID,
} = require("@budibase/backend-core/permissions")
const setup = require("./utilities") const setup = require("./utilities")
const { basicRole } = setup.structures const { basicRole } = setup.structures
const { events } = require("@budibase/backend-core") const { BUILTIN_ROLE_IDS } = roles
const { BuiltinPermissionID } = permissions
describe("/roles", () => { describe("/roles", () => {
let request = setup.getRequest() let request = setup.getRequest()

View File

@ -1,7 +1,8 @@
const setup = require("./utilities") const setup = require("./utilities")
const { basicScreen } = setup.structures const { basicScreen } = setup.structures
const { checkBuilderEndpoint, runInProd } = require("./utilities/TestFunctions") const { checkBuilderEndpoint, runInProd } = require("./utilities/TestFunctions")
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { roles } = require("@budibase/backend-core")
const { BUILTIN_ROLE_IDS } = roles
const route = "/test" const route = "/test"

View File

@ -1,8 +1,7 @@
const { outputProcessing } = require("../../../utilities/rowProcessor") const { outputProcessing } = require("../../../utilities/rowProcessor")
const setup = require("./utilities") const setup = require("./utilities")
const { basicRow } = setup.structures const { basicRow } = setup.structures
const { doInAppContext } = require("@budibase/backend-core/context") const { context, tenancy } = require("@budibase/backend-core")
const { doInTenant } = require("@budibase/backend-core/tenancy")
const { const {
quotas, quotas,
} = require("@budibase/pro") } = require("@budibase/pro")
@ -447,7 +446,7 @@ describe("/rows", () => {
describe("fetchEnrichedRows", () => { describe("fetchEnrichedRows", () => {
it("should allow enriching some linked rows", async () => { it("should allow enriching some linked rows", async () => {
const { table, firstRow, secondRow } = await doInTenant( const { table, firstRow, secondRow } = await tenancy.doInTenant(
setup.structures.TENANT_ID, setup.structures.TENANT_ID,
async () => { async () => {
const table = await config.createLinkedTable() const table = await config.createLinkedTable()
@ -507,7 +506,7 @@ describe("/rows", () => {
}) })
// the environment needs configured for this // the environment needs configured for this
await setup.switchToSelfHosted(async () => { await setup.switchToSelfHosted(async () => {
doInAppContext(config.getAppId(), async () => { context.doInAppContext(config.getAppId(), async () => {
const enriched = await outputProcessing(table, [row]) const enriched = await outputProcessing(table, [row])
expect(enriched[0].attachment[0].url).toBe( expect(enriched[0].attachment[0].url).toBe(
`/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv` `/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`

View File

@ -1,6 +1,7 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { roles } = require("@budibase/backend-core")
const { checkPermissionsEndpoint } = require("./utilities/TestFunctions") const { checkPermissionsEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities") const setup = require("./utilities")
const { BUILTIN_ROLE_IDS } = roles
jest.mock("../../../utilities/workerRequests", () => ({ jest.mock("../../../utilities/workerRequests", () => ({
getGlobalUsers: jest.fn(() => { getGlobalUsers: jest.fn(() => {

View File

@ -1,10 +1,8 @@
import * as rowController from "../../../controllers/row" import * as rowController from "../../../controllers/row"
import * as appController from "../../../controllers/application" import * as appController from "../../../controllers/application"
import { AppStatus } from "../../../../db/utils" import { AppStatus } from "../../../../db/utils"
import { BUILTIN_ROLE_IDS } from "@budibase/backend-core/roles" import { roles, tenancy, context } from "@budibase/backend-core"
import { doInTenant } from "@budibase/backend-core/tenancy"
import { TENANT_ID } from "../../../../tests/utilities/structures" import { TENANT_ID } from "../../../../tests/utilities/structures"
import { getAppDB, doInAppContext } from "@budibase/backend-core/context"
import * as env from "../../../../environment" import * as env from "../../../../environment"
class Request { class Request {
@ -21,7 +19,7 @@ class Request {
} }
function runRequest(appId: any, controlFunc: any, request?: any) { function runRequest(appId: any, controlFunc: any, request?: any) {
return doInAppContext(appId, async () => { return context.doInAppContext(appId, async () => {
return controlFunc(request) return controlFunc(request)
}) })
} }
@ -33,7 +31,7 @@ export const getAllTableRows = async (config: any) => {
} }
export const clearAllApps = async (tenantId = TENANT_ID) => { export const clearAllApps = async (tenantId = TENANT_ID) => {
await doInTenant(tenantId, async () => { await tenancy.doInTenant(tenantId, async () => {
const req: any = { query: { status: AppStatus.DEV }, user: { tenantId } } const req: any = { query: { status: AppStatus.DEV }, user: { tenantId } }
await appController.fetch(req) await appController.fetch(req)
const apps = req.body const apps = req.body
@ -51,7 +49,7 @@ export const clearAllApps = async (tenantId = TENANT_ID) => {
export const clearAllAutomations = async (config: any) => { export const clearAllAutomations = async (config: any) => {
const automations = await config.getAllAutomations() const automations = await config.getAllAutomations()
for (let auto of automations) { for (let auto of automations) {
await doInAppContext(config.appId, async () => { await context.doInAppContext(config.appId, async () => {
await config.deleteAutomation(auto) await config.deleteAutomation(auto)
}) })
} }
@ -110,7 +108,7 @@ export const checkPermissionsEndpoint = async ({
.expect(200) .expect(200)
let failHeader let failHeader
if (failRole === BUILTIN_ROLE_IDS.PUBLIC) { if (failRole === roles.BUILTIN_ROLE_IDS.PUBLIC) {
failHeader = config.publicHeaders({ prodApp: true }) failHeader = config.publicHeaders({ prodApp: true })
} else { } else {
failHeader = await config.login({ failHeader = await config.login({
@ -127,7 +125,7 @@ export const checkPermissionsEndpoint = async ({
} }
export const getDB = () => { export const getDB = () => {
return getAppDB() return context.getAppDB()
} }
export const testAutomation = async (config: any, automation: any) => { export const testAutomation = async (config: any, automation: any) => {

View File

@ -1,5 +1,5 @@
import Router from "@koa/router" import Router from "@koa/router"
import controller from "../controllers/user" import * as controller from "../controllers/user"
import authorized from "../../middleware/authorized" import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
const { PermissionType, PermissionLevel } = permissions const { PermissionType, PermissionLevel } = permissions

View File

@ -19,10 +19,9 @@ const http = require("http")
const api = require("./api") const api = require("./api")
const automations = require("./automations/index") const automations = require("./automations/index")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const { logAlert } = require("@budibase/backend-core/logging")
const { Thread } = require("./threads") const { Thread } = require("./threads")
import redis from "./utilities/redis" import * as redis from "./utilities/redis"
import { events } from "@budibase/backend-core" import { events, logging } from "@budibase/backend-core"
import { initialise as initialiseWebsockets } from "./websocket" import { initialise as initialiseWebsockets } from "./websocket"
import { startup } from "./startup" import { startup } from "./startup"
@ -93,7 +92,7 @@ process.on("uncaughtException", err => {
return return
} }
errCode = -1 errCode = -1
logAlert("Uncaught exception.", err) logging.logAlert("Uncaught exception.", err)
shutdown() shutdown()
}) })

View File

@ -8,8 +8,7 @@ const { automationQueue } = require("./bullboard")
const { checkTestFlag } = require("../utilities/redis") const { checkTestFlag } = require("../utilities/redis")
const utils = require("./utils") const utils = require("./utils")
const env = require("../environment") const env = require("../environment")
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { context, db: dbCore } = require("@budibase/backend-core")
const { getAllApps } = require("@budibase/backend-core/db")
const TRIGGER_DEFINITIONS = definitions const TRIGGER_DEFINITIONS = definitions
const JOB_OPTS = { const JOB_OPTS = {
@ -18,7 +17,7 @@ const JOB_OPTS = {
} }
async function getAllAutomations() { async function getAllAutomations() {
const db = getAppDB() const db = context.getAppDB()
let automations = await db.allDocs( let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true }) getAutomationParams(null, { include_docs: true })
) )
@ -30,7 +29,7 @@ async function queueRelevantRowAutomations(event, eventType) {
throw `No appId specified for ${eventType} - check event emitters.` throw `No appId specified for ${eventType} - check event emitters.`
} }
doInAppContext(event.appId, async () => { context.doInAppContext(event.appId, async () => {
let automations = await getAllAutomations() let automations = await getAllAutomations()
// filter down to the correct event type // filter down to the correct event type
@ -122,9 +121,9 @@ exports.rebootTrigger = async () => {
} }
// iterate through all production apps, find the reboot crons // iterate through all production apps, find the reboot crons
// and trigger events for them // and trigger events for them
const appIds = await getAllApps({ dev: false, idsOnly: true }) const appIds = await dbCore.getAllApps({ dev: false, idsOnly: true })
for (let prodAppId of appIds) { for (let prodAppId of appIds) {
await doInAppContext(prodAppId, async () => { await context.doInAppContext(prodAppId, async () => {
let automations = await getAllAutomations() let automations = await getAllAutomations()
let rebootEvents = [] let rebootEvents = []
for (let automation of automations) { for (let automation of automations) {

View File

@ -1,6 +1,4 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { objectStore, roles, constants } = require("@budibase/backend-core")
const { UserStatus } = require("@budibase/backend-core/constants")
const { objectStore } = require("@budibase/backend-core")
const FilterTypes = { const FilterTypes = {
STRING: "string", STRING: "string",
@ -139,7 +137,7 @@ exports.USERS_TABLE_SCHEMA = {
constraints: { constraints: {
type: exports.FieldTypes.STRING, type: exports.FieldTypes.STRING,
presence: false, presence: false,
inclusion: Object.values(BUILTIN_ROLE_IDS), inclusion: Object.values(roles.BUILTIN_ROLE_IDS),
}, },
}, },
status: { status: {
@ -149,7 +147,7 @@ exports.USERS_TABLE_SCHEMA = {
constraints: { constraints: {
type: exports.FieldTypes.STRING, type: exports.FieldTypes.STRING,
presence: false, presence: false,
inclusion: Object.values(UserStatus), inclusion: Object.values(constants.UserStatus),
}, },
}, },
}, },

View File

@ -1,4 +1,4 @@
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") const { roles } = require("@budibase/backend-core")
const { BASE_LAYOUT_PROP_IDS } = require("./layouts") const { BASE_LAYOUT_PROP_IDS } = require("./layouts")
exports.createHomeScreen = () => ({ exports.createHomeScreen = () => ({
@ -40,7 +40,7 @@ exports.createHomeScreen = () => ({
}, },
routing: { routing: {
route: "/", route: "/",
roleId: BUILTIN_ROLE_IDS.BASIC, roleId: roles.BUILTIN_ROLE_IDS.BASIC,
}, },
name: "home-screen", name: "home-screen",
}) })

View File

@ -2,8 +2,8 @@ const newid = require("./newid")
// bypass the main application db config // bypass the main application db config
// use in memory pouchdb directly // use in memory pouchdb directly
const { getPouch, closePouchDB } = require("@budibase/backend-core/db") const { db: dbCore } = require("@budibase/backend-core")
const Pouch = getPouch({ inMemory: true }) const Pouch = dbCore.getPouch({ inMemory: true })
exports.runView = async (view, calculation, group, data) => { exports.runView = async (view, calculation, group, data) => {
// use a different ID each time for the DB, make sure they // use a different ID each time for the DB, make sure they
@ -44,6 +44,6 @@ exports.runView = async (view, calculation, group, data) => {
return response return response
} finally { } finally {
await db.destroy() await db.destroy()
await closePouchDB(db) await dbCore.closePouchDB(db)
} }
} }

View File

@ -2,12 +2,12 @@ const { IncludeDocs, getLinkDocuments } = require("./linkUtils")
const { InternalTables, getUserMetadataParams } = require("../utils") const { InternalTables, getUserMetadataParams } = require("../utils")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const { FieldTypes, RelationshipTypes } = require("../../constants") const { FieldTypes, RelationshipTypes } = require("../../constants")
const { getAppDB } = require("@budibase/backend-core/context") const { context } = require("@budibase/backend-core")
const LinkDocument = require("./LinkDocument") const LinkDocument = require("./LinkDocument")
class LinkController { class LinkController {
constructor({ tableId, row, table, oldTable }) { constructor({ tableId, row, table, oldTable }) {
this._db = getAppDB() this._db = context.getAppDB()
this._tableId = tableId this._tableId = tableId
this._row = row this._row = row
this._table = table this._table = table

View File

@ -14,7 +14,7 @@ const { getMultiIDParams, USER_METDATA_PREFIX } = require("../../db/utils")
const { partition } = require("lodash") const { partition } = require("lodash")
const { getGlobalUsersFromMetadata } = require("../../utilities/global") const { getGlobalUsersFromMetadata } = require("../../utilities/global")
const { processFormulas } = require("../../utilities/rowProcessor/utils") const { processFormulas } = require("../../utilities/rowProcessor/utils")
const { getAppDB } = require("@budibase/backend-core/context") const { context } = require("@budibase/backend-core")
/** /**
* This functionality makes sure that when rows with links are created, updated or deleted they are processed * This functionality makes sure that when rows with links are created, updated or deleted they are processed
@ -74,7 +74,7 @@ async function getLinksForRows(rows) {
async function getFullLinkedDocs(links) { async function getFullLinkedDocs(links) {
// create DBs // create DBs
const db = getAppDB() const db = context.getAppDB()
const linkedRowIds = links.map(link => link.id) const linkedRowIds = links.map(link => link.id)
const uniqueRowIds = [...new Set(linkedRowIds)] const uniqueRowIds = [...new Set(linkedRowIds)]
let dbRows = (await db.allDocs(getMultiIDParams(uniqueRowIds))).rows.map( let dbRows = (await db.allDocs(getMultiIDParams(uniqueRowIds))).rows.map(

View File

@ -2,7 +2,7 @@ const Sentry = require("@sentry/node")
const { ViewName, getQueryIndex } = require("../utils") const { ViewName, getQueryIndex } = require("../utils")
const { FieldTypes } = require("../../constants") const { FieldTypes } = require("../../constants")
const { createLinkView } = require("../views/staticViews") const { createLinkView } = require("../views/staticViews")
const { getAppDB } = require("@budibase/backend-core/context") const { context } = require("@budibase/backend-core")
/** /**
* Only needed so that boolean parameters are being used for includeDocs * Only needed so that boolean parameters are being used for includeDocs
@ -30,7 +30,7 @@ exports.createLinkView = createLinkView
*/ */
exports.getLinkDocuments = async function (args) { exports.getLinkDocuments = async function (args) {
const { tableId, rowId, includeDocs } = args const { tableId, rowId, includeDocs } = args
const db = getAppDB() const db = context.getAppDB()
let params let params
if (rowId != null) { if (rowId != null) {
params = { key: [tableId, rowId] } params = { key: [tableId, rowId] }
@ -89,7 +89,7 @@ exports.getLinkedTableIDs = table => {
} }
exports.getLinkedTable = async (id, tables) => { exports.getLinkedTable = async (id, tables) => {
const db = getAppDB() const db = context.getAppDB()
let linkedTable = tables.find(table => table._id === id) let linkedTable = tables.find(table => table._id === id)
if (linkedTable) { if (linkedTable) {
return linkedTable return linkedTable

View File

@ -128,7 +128,7 @@ export function getLinkParams(otherProps: any = {}) {
* Generates a new layout ID. * Generates a new layout ID.
* @returns {string} The new layout ID which the layout doc can be stored under. * @returns {string} The new layout ID which the layout doc can be stored under.
*/ */
export function generateLayoutID(id: string) { export function generateLayoutID(id?: string) {
return `${DocumentType.LAYOUT}${SEPARATOR}${id || newid()}` return `${DocumentType.LAYOUT}${SEPARATOR}${id || newid()}`
} }

View File

@ -1,4 +1,4 @@
const { getAppDB } = require("@budibase/backend-core/context") const { context } = require("@budibase/backend-core")
const { DocumentType, SEPARATOR, ViewName, SearchIndexes } = require("../utils") const { DocumentType, SEPARATOR, ViewName, SearchIndexes } = require("../utils")
const SCREEN_PREFIX = DocumentType.SCREEN + SEPARATOR const SCREEN_PREFIX = DocumentType.SCREEN + SEPARATOR
@ -20,7 +20,7 @@ const SCREEN_PREFIX = DocumentType.SCREEN + SEPARATOR
* so it may be slow. * so it may be slow.
*/ */
exports.createLinkView = async () => { exports.createLinkView = async () => {
const db = getAppDB() const db = context.getAppDB()
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
const view = { const view = {
map: function (doc) { map: function (doc) {
@ -54,7 +54,7 @@ exports.createLinkView = async () => {
} }
exports.createRoutingView = async () => { exports.createRoutingView = async () => {
const db = getAppDB() const db = context.getAppDB()
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
const view = { const view = {
// if using variables in a map function need to inject them before use // if using variables in a map function need to inject them before use
@ -75,7 +75,7 @@ exports.createRoutingView = async () => {
} }
async function searchIndex(indexName, fnString) { async function searchIndex(indexName, fnString) {
const db = getAppDB() const db = context.getAppDB()
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
designDoc.indexes = { designDoc.indexes = {
[indexName]: { [indexName]: {

View File

@ -96,7 +96,7 @@ function generateSelectStatement(
): (string | Knex.Raw)[] { ): (string | Knex.Raw)[] {
const { resource, meta } = json const { resource, meta } = json
const schema = meta?.table?.schema const schema = meta?.table?.schema
return resource.fields.map(field => { return resource!.fields.map(field => {
const fieldNames = field.split(/\./g) const fieldNames = field.split(/\./g)
const tableName = fieldNames[0] const tableName = fieldNames[0]
const columnName = fieldNames[1] const columnName = fieldNames[1]

View File

@ -185,7 +185,7 @@ class SqlTableQueryBuilder {
json.table, json.table,
json.meta.tables, json.meta.tables,
json.meta.table, json.meta.table,
json.meta.renamed json.meta.renamed!
) )
break break
case Operation.DELETE_TABLE: case Operation.DELETE_TABLE:

View File

@ -67,8 +67,7 @@ if (
INTEGRATIONS[SourceName.ORACLE] = oracle.integration INTEGRATIONS[SourceName.ORACLE] = oracle.integration
} }
module.exports = { export async function getDefinitions() {
getDefinitions: async () => {
const pluginSchemas: { [key: string]: Integration } = {} const pluginSchemas: { [key: string]: Integration } = {}
if (environment.SELF_HOSTED) { if (environment.SELF_HOSTED) {
const plugins = await getPlugins(PluginType.DATASOURCE) const plugins = await getPlugins(PluginType.DATASOURCE)
@ -88,8 +87,9 @@ module.exports = {
...cloneDeep(DEFINITIONS), ...cloneDeep(DEFINITIONS),
...pluginSchemas, ...pluginSchemas,
} }
}, }
getIntegration: async (integration: string) => {
export async function getIntegration(integration: string) {
if (INTEGRATIONS[integration]) { if (INTEGRATIONS[integration]) {
return INTEGRATIONS[integration] return INTEGRATIONS[integration]
} }
@ -112,5 +112,9 @@ module.exports = {
} }
} }
throw new Error("No datasource implementation found.") throw new Error("No datasource implementation found.")
}, }
export default {
getDefinitions,
getIntegration,
} }

View File

@ -1,4 +1,4 @@
const { DocumentType } = require("@budibase/backend-core/db") import { db as dbCore } from "@budibase/backend-core"
import { getAppUrl } from "../../api/controllers/application" import { getAppUrl } from "../../api/controllers/application"
/** /**
@ -11,7 +11,7 @@ import { getAppUrl } from "../../api/controllers/application"
export const run = async (appDb: any) => { export const run = async (appDb: any) => {
let metadata let metadata
try { try {
metadata = await appDb.get(DocumentType.APP_METADATA) metadata = await appDb.get(dbCore.DocumentType.APP_METADATA)
} catch (e) { } catch (e) {
// sometimes the metadata document doesn't exist // sometimes the metadata document doesn't exist
// exit early instead of failing the migration // exit early instead of failing the migration

View File

@ -1,4 +1,4 @@
const { DocumentType, doWithDB } = require("@budibase/backend-core/db") const { db: dbCore } = require("@budibase/backend-core")
const TestConfig = require("../../../tests/utilities/TestConfiguration") const TestConfig = require("../../../tests/utilities/TestConfiguration")
const migration = require("../appUrls") const migration = require("../appUrls")
@ -14,12 +14,12 @@ describe("run", () => {
it("runs successfully", async () => { it("runs successfully", async () => {
const app = await config.createApp("testApp") const app = await config.createApp("testApp")
const metadata = await doWithDB(app.appId, async db => { const metadata = await dbCore.doWithDB(app.appId, async db => {
const metadataDoc = await db.get( DocumentType.APP_METADATA) const metadataDoc = await db.get(dbCore.DocumentType.APP_METADATA)
delete metadataDoc.url delete metadataDoc.url
await db.put(metadataDoc) await db.put(metadataDoc)
await migration.run(db) await migration.run(db)
return await db.get( DocumentType.APP_METADATA) return await db.get(dbCore.DocumentType.APP_METADATA)
}) })
expect(metadata.url).toEqual("/testapp") expect(metadata.url).toEqual("/testapp")
}) })

View File

@ -1,11 +1,16 @@
jest.mock("@budibase/backend-core/db", () => ({ jest.mock("@budibase/backend-core", () => {
...jest.requireActual("@budibase/backend-core/db"), const core = jest.requireActual("@budibase/backend-core")
createNewUserEmailView: jest.fn(), return {
})) ...core,
const coreDb = require("@budibase/backend-core/db") db: {
...core.db,
createNewUserEmailView: jest.fn()
}
}
})
const { tenancy, db: dbCore } = require("@budibase/backend-core")
const TestConfig = require("../../../tests/utilities/TestConfiguration") const TestConfig = require("../../../tests/utilities/TestConfiguration")
const { TENANT_ID } = require("../../../tests/utilities/structures") const { TENANT_ID } = require("../../../tests/utilities/structures")
const { getGlobalDB, doInTenant } = require("@budibase/backend-core/tenancy")
// mock email view creation // mock email view creation
@ -21,10 +26,10 @@ describe("run", () => {
afterAll(config.end) afterAll(config.end)
it("runs successfully", async () => { it("runs successfully", async () => {
await doInTenant(TENANT_ID, async () => { await tenancy.doInTenant(TENANT_ID, async () => {
const globalDb = getGlobalDB() const globalDb = tenancy.getGlobalDB()
await migration.run(globalDb) await migration.run(globalDb)
expect(coreDb.createNewUserEmailView).toHaveBeenCalledTimes(1) expect(dbCore.createNewUserEmailView).toHaveBeenCalledTimes(1)
}) })
}) })
}) })

View File

@ -1,15 +1,14 @@
import { getTenantId } from "@budibase/backend-core/tenancy" import { tenancy, db as dbCore } from "@budibase/backend-core"
import { getAllApps } from "@budibase/backend-core/db"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { QuotaUsageType, StaticQuotaName } from "@budibase/types" import { QuotaUsageType, StaticQuotaName } from "@budibase/types"
export const run = async () => { export const run = async () => {
// get app count // get app count
const devApps = await getAllApps({ dev: true }) const devApps = await dbCore.getAllApps({ dev: true })
const appCount = devApps ? devApps.length : 0 const appCount = devApps ? devApps.length : 0
// sync app count // sync app count
const tenantId = getTenantId() const tenantId = tenancy.getTenantId()
console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`) console.log(`[Tenant: ${tenantId}] Syncing app count: ${appCount}`)
await quotas.setUsage(appCount, StaticQuotaName.APPS, QuotaUsageType.STATIC) await quotas.setUsage(appCount, StaticQuotaName.APPS, QuotaUsageType.STATIC)
} }

View File

@ -1,12 +1,11 @@
import { getTenantId } from "@budibase/backend-core/tenancy" import { tenancy, db as dbCore } from "@budibase/backend-core"
import { getAllApps } from "@budibase/backend-core/db"
import { getUniqueRows } from "../../../utilities/usageQuota/rows" import { getUniqueRows } from "../../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { StaticQuotaName, QuotaUsageType, App } from "@budibase/types" import { StaticQuotaName, QuotaUsageType, App } from "@budibase/types"
export const run = async () => { export const run = async () => {
// get all rows in all apps // get all rows in all apps
const allApps = (await getAllApps({ all: true })) as App[] const allApps = (await dbCore.getAllApps({ all: true })) as App[]
const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : [] const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : []
const { appRows } = await getUniqueRows(appIds) const { appRows } = await getUniqueRows(appIds)
@ -19,7 +18,7 @@ export const run = async () => {
}) })
// sync row count // sync row count
const tenantId = getTenantId() const tenantId = tenancy.getTenantId()
console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`) console.log(`[Tenant: ${tenantId}] Syncing row count: ${rowCount}`)
await quotas.setUsagePerApp( await quotas.setUsagePerApp(
counts, counts,

View File

@ -2,7 +2,7 @@ import TestConfig from "../../../../tests/utilities/TestConfiguration"
import * as syncRows from "../syncRows" import * as syncRows from "../syncRows"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { QuotaUsageType, StaticQuotaName } from "@budibase/types" import { QuotaUsageType, StaticQuotaName } from "@budibase/types"
const { getProdAppID } = require("@budibase/backend-core/db") const { db: dbCore } = require("@budibase/backend-core")
describe("syncRows", () => { describe("syncRows", () => {
let config = new TestConfig(false) let config = new TestConfig(false)
@ -38,12 +38,12 @@ describe("syncRows", () => {
// assert the migration worked // assert the migration worked
usageDoc = await quotas.getQuotaUsage() usageDoc = await quotas.getQuotaUsage()
expect(usageDoc.usageQuota.rows).toEqual(3) expect(usageDoc.usageQuota.rows).toEqual(3)
expect(usageDoc.apps?.[getProdAppID(app1.appId)].usageQuota.rows).toEqual( expect(
1 usageDoc.apps?.[dbCore.getProdAppID(app1.appId)].usageQuota.rows
) ).toEqual(1)
expect(usageDoc.apps?.[getProdAppID(app2.appId)].usageQuota.rows).toEqual( expect(
2 usageDoc.apps?.[dbCore.getProdAppID(app2.appId)].usageQuota.rows
) ).toEqual(2)
}) })
}) })
}) })

View File

@ -1,4 +1,4 @@
const { createNewUserEmailView } = require("@budibase/backend-core/db") import { db as dbCore } from "@budibase/backend-core"
/** /**
* Date: * Date:
@ -8,6 +8,6 @@ const { createNewUserEmailView } = require("@budibase/backend-core/db")
* Recreate the user email view to include latest changes i.e. lower casing the email address * Recreate the user email view to include latest changes i.e. lower casing the email address
*/ */
export const run = async (db: any) => { export const run = async () => {
await createNewUserEmailView(db) await dbCore.createNewUserEmailView()
} }

View File

@ -1,14 +0,0 @@
declare module "@budibase/backend-core/tenancy"
declare module "@budibase/backend-core/db"
declare module "@budibase/backend-core/context"
declare module "@budibase/backend-core/cache"
declare module "@budibase/backend-core/permissions"
declare module "@budibase/backend-core/roles"
declare module "@budibase/backend-core/constants"
declare module "@budibase/backend-core/auth"
declare module "@budibase/backend-core/sessions"
declare module "@budibase/backend-core/encryption"
declare module "@budibase/backend-core/utils"
declare module "@budibase/backend-core/redis"
declare module "@budibase/backend-core/objectStore"
declare module "@budibase/backend-core/plugins"

View File

@ -1,5 +1,5 @@
import * as env from "./environment" import * as env from "./environment"
import redis from "./utilities/redis" import * as redis from "./utilities/redis"
import { import {
createAdminUser, createAdminUser,
generateApiKey, generateApiKey,
@ -19,7 +19,7 @@ import eventEmitter from "./events"
import * as migrations from "./migrations" import * as migrations from "./migrations"
import bullboard from "./automations/bullboard" import bullboard from "./automations/bullboard"
import * as pro from "@budibase/pro" import * as pro from "@budibase/pro"
import api from "./api" import * as api from "./api"
import sdk from "./sdk" import sdk from "./sdk"
const pino = require("koa-pino-logger") const pino = require("koa-pino-logger")

View File

@ -23,15 +23,14 @@ import {
AutomationMetadata, AutomationMetadata,
} from "../definitions/automations" } from "../definitions/automations"
import { WorkerCallback } from "./definitions" import { WorkerCallback } from "./definitions"
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") import { context, logging } from "@budibase/backend-core"
const { logAlertWithInfo, logWarn } = require("@budibase/backend-core/logging") import { processObject } from "@budibase/string-templates"
const { processObject } = require("@budibase/string-templates") import { cloneDeep } from "lodash/fp"
import env from "../environment"
const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId
const CRON_STEP_ID = triggerDefs.CRON.stepId const CRON_STEP_ID = triggerDefs.CRON.stepId
const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED } const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED }
const { cloneDeep } = require("lodash/fp")
const env = require("../environment")
function typecastForLooping(loopStep: LoopStep, input: LoopInput) { function typecastForLooping(loopStep: LoopStep, input: LoopInput) {
if (!input || !input.binding) { if (!input || !input.binding) {
@ -121,7 +120,7 @@ class Orchestrator {
async getMetadata(): Promise<AutomationMetadata> { async getMetadata(): Promise<AutomationMetadata> {
const metadataId = generateAutomationMetadataID(this._automation._id!) const metadataId = generateAutomationMetadataID(this._automation._id!)
const db = getAppDB() const db = context.getAppDB()
let metadata: AutomationMetadata let metadata: AutomationMetadata
try { try {
metadata = await db.get(metadataId) metadata = await db.get(metadataId)
@ -138,7 +137,7 @@ class Orchestrator {
if (!this._job.opts.repeat) { if (!this._job.opts.repeat) {
return return
} }
logWarn( logging.logWarn(
`CRON disabled reason=${reason} - ${this._appId}/${this._automation._id}` `CRON disabled reason=${reason} - ${this._appId}/${this._automation._id}`
) )
const automation = this._automation const automation = this._automation
@ -184,14 +183,14 @@ class Orchestrator {
} else { } else {
metadata.errorCount = 0 metadata.errorCount = 0
} }
const db = getAppDB() const db = context.getAppDB()
try { try {
await db.put(metadata) await db.put(metadata)
} catch (err) { } catch (err) {
logAlertWithInfo( logging.logAlertWithInfo(
"Failed to write automation metadata", "Failed to write automation metadata",
db.name, db.name,
automation._id, automation._id!,
err err
) )
} }
@ -265,7 +264,7 @@ class Orchestrator {
for (let step of automation.definition.steps) { for (let step of automation.definition.steps) {
stepCount++ stepCount++
let input, let input: any,
iterations = 1, iterations = 1,
iterationCount = 0 iterationCount = 0
@ -283,7 +282,7 @@ class Orchestrator {
let originalStepInput = cloneDeep(step.inputs) let originalStepInput = cloneDeep(step.inputs)
// Handle if the user has set a max iteration count or if it reaches the max limit set by us // Handle if the user has set a max iteration count or if it reaches the max limit set by us
if (loopStep && input.binding) { if (loopStep && input.binding) {
let newInput = await processObject( let newInput: any = await processObject(
loopStep.inputs, loopStep.inputs,
cloneDeep(this._context) cloneDeep(this._context)
) )
@ -352,7 +351,7 @@ class Orchestrator {
} }
} }
if ( if (
index === env.AUTOMATION_MAX_ITERATION || index === env.AUTOMATION_MAX_ITERATIONS ||
index === parseInt(loopStep.inputs.iterations) index === parseInt(loopStep.inputs.iterations)
) { ) {
this.updateContextAndOutput(loopStepNumber, step, tempOutput, { this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
@ -481,7 +480,7 @@ export function execute(job: Job, callback: WorkerCallback) {
if (!appId) { if (!appId) {
throw new Error("Unable to execute, event doesn't contain app ID.") throw new Error("Unable to execute, event doesn't contain app ID.")
} }
doInAppContext(appId, async () => { return context.doInAppContext(appId, async () => {
const automationOrchestrator = new Orchestrator(job) const automationOrchestrator = new Orchestrator(job)
try { try {
const response = await automationOrchestrator.execute() const response = await automationOrchestrator.execute()
@ -497,7 +496,7 @@ export const removeStalled = async (job: Job) => {
if (!appId) { if (!appId) {
throw new Error("Unable to execute, event doesn't contain app ID.") throw new Error("Unable to execute, event doesn't contain app ID.")
} }
await doInAppContext(appId, async () => { await context.doInAppContext(appId, async () => {
const automationOrchestrator = new Orchestrator(job) const automationOrchestrator = new Orchestrator(job)
await automationOrchestrator.stopCron("stalled") await automationOrchestrator.stopCron("stalled")
}) })

View File

@ -1,17 +1,12 @@
import { default as threadUtils } from "./utils" import { default as threadUtils } from "./utils"
threadUtils.threadSetup() threadUtils.threadSetup()
import { WorkerCallback, QueryEvent, QueryVariable } from "./definitions" import { WorkerCallback, QueryEvent, QueryVariable } from "./definitions"
const ScriptRunner = require("../utilities/scriptRunner") import ScriptRunner from "../utilities/scriptRunner"
const { getIntegration } = require("../integrations") import { getIntegration } from "../integrations"
const { processStringSync } = require("@budibase/string-templates") import { processStringSync } from "@budibase/string-templates"
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") import { context, cache, auth } from "@budibase/backend-core"
const { import { getGlobalIDFromUserMetadataID } from "../db/utils"
refreshOAuthToken, import { cloneDeep } from "lodash/fp"
updateUserOAuth,
} = require("@budibase/backend-core/auth")
const { user: userCache } = require("@budibase/backend-core/cache")
const { getGlobalIDFromUserMetadataID } = require("../db/utils")
const { cloneDeep } = require("lodash/fp")
const { isSQL } = require("../integrations/utils") const { isSQL } = require("../integrations/utils")
const { const {
@ -170,7 +165,7 @@ class QueryRunner {
} }
async runAnotherQuery(queryId: string, parameters: any) { async runAnotherQuery(queryId: string, parameters: any) {
const db = getAppDB() const db = context.getAppDB()
const query = await db.get(queryId) const query = await db.get(queryId)
const datasource = await db.get(query.datasourceId) const datasource = await db.get(query.datasourceId)
return new QueryRunner( return new QueryRunner(
@ -194,7 +189,7 @@ class QueryRunner {
throw new Error("No refresh token found for authenticated user") throw new Error("No refresh token found for authenticated user")
} }
const resp = await refreshOAuthToken( const resp: any = await auth.refreshOAuthToken(
oauth2.refreshToken, oauth2.refreshToken,
providerType, providerType,
configId configId
@ -204,8 +199,8 @@ class QueryRunner {
// There are several other properties available in 'resp' // There are several other properties available in 'resp'
if (!resp.err) { if (!resp.err) {
const globalUserId = getGlobalIDFromUserMetadataID(_id) const globalUserId = getGlobalIDFromUserMetadataID(_id)
await updateUserOAuth(globalUserId, resp) await auth.updateUserOAuth(globalUserId, resp)
this.ctx.user = await userCache.getUser(globalUserId) this.ctx.user = await cache.user.getUser(globalUserId)
} else { } else {
// In this event the user may have oAuth issues that // In this event the user may have oAuth issues that
// could require re-authenticating with their provider. // could require re-authenticating with their provider.
@ -285,7 +280,7 @@ class QueryRunner {
} }
export function execute(input: QueryEvent, callback: WorkerCallback) { export function execute(input: QueryEvent, callback: WorkerCallback) {
doInAppContext(input.appId, async () => { context.doInAppContext(input.appId!, async () => {
const Runner = new QueryRunner(input) const Runner = new QueryRunner(input)
try { try {
const response = await Runner.execute() const response = await Runner.execute()

View File

@ -1,8 +1,7 @@
import { QueryVariable } from "./definitions" import { QueryVariable } from "./definitions"
const env = require("../environment") import env from "../environment"
const db = require("../db") import db from "../db"
const redis = require("@budibase/backend-core/redis") import { redis, db as dbCore } from "@budibase/backend-core"
const { SEPARATOR } = require("@budibase/backend-core/db")
const VARIABLE_TTL_SECONDS = 3600 const VARIABLE_TTL_SECONDS = 3600
let client: any let client: any
@ -21,7 +20,7 @@ process.on("exit", async () => {
}) })
function makeVariableKey(queryId: string, variable: string) { function makeVariableKey(queryId: string, variable: string) {
return `${queryId}${SEPARATOR}${variable}` return `${queryId}${dbCore.SEPARATOR}${variable}`
} }
export function threadSetup() { export function threadSetup() {

View File

@ -1,6 +1,6 @@
import { redis } from "@budibase/backend-core" import { redis } from "@budibase/backend-core"
import { getGlobalIDFromUserMetadataID } from "../db/utils" import { getGlobalIDFromUserMetadataID } from "../db/utils"
import { User } from "@budibase/types" import { ContextUser } from "@budibase/types"
const APP_DEV_LOCK_SECONDS = 600 const APP_DEV_LOCK_SECONDS = 600
const AUTOMATION_TEST_FLAG_SECONDS = 60 const AUTOMATION_TEST_FLAG_SECONDS = 60
@ -24,7 +24,7 @@ export async function shutdown() {
console.log("Redis shutdown") console.log("Redis shutdown")
} }
export async function doesUserHaveLock(devAppId: string, user: User) { export async function doesUserHaveLock(devAppId: string, user: ContextUser) {
const value = await devAppClient.get(devAppId) const value = await devAppClient.get(devAppId)
if (!value) { if (!value) {
return true return true
@ -39,7 +39,7 @@ export async function getLocksById(appIds: string[]) {
return await devAppClient.bulkGet(appIds) return await devAppClient.bulkGet(appIds)
} }
export async function updateLock(devAppId: string, user: User) { export async function updateLock(devAppId: string, user: ContextUser) {
// make sure always global user ID // make sure always global user ID
const globalId = getGlobalIDFromUserMetadataID(user._id!) const globalId = getGlobalIDFromUserMetadataID(user._id!)
const inputUser = { const inputUser = {
@ -52,7 +52,7 @@ export async function updateLock(devAppId: string, user: User) {
await devAppClient.store(devAppId, inputUser, APP_DEV_LOCK_SECONDS) await devAppClient.store(devAppId, inputUser, APP_DEV_LOCK_SECONDS)
} }
export async function clearLock(devAppId: string, user: User) { export async function clearLock(devAppId: string, user: ContextUser) {
const value = await devAppClient.get(devAppId) const value = await devAppClient.get(devAppId)
if (!value) { if (!value) {
return return

View File

@ -180,7 +180,7 @@ export function inputProcessing(
*/ */
export async function outputProcessing( export async function outputProcessing(
table: Table, table: Table,
rows: Row[], rows: Row[] | Row,
opts = { squash: true } opts = { squash: true }
) { ) {
let wasArray = true let wasArray = true

View File

@ -2,8 +2,7 @@ import path from "path"
import * as env from "./environment" import * as env from "./environment"
import chokidar from "chokidar" import chokidar from "chokidar"
import fs from "fs" import fs from "fs"
import { tenancy } from "@budibase/backend-core" import { constants, tenancy } from "@budibase/backend-core"
import { DEFAULT_TENANT_ID } from "@budibase/backend-core/constants"
import { processUploadedPlugin } from "./api/controllers/plugin" import { processUploadedPlugin } from "./api/controllers/plugin"
export function watch() { export function watch() {
@ -23,7 +22,7 @@ export function watch() {
if (!path?.endsWith(".tar.gz") || !fs.existsSync(path)) { if (!path?.endsWith(".tar.gz") || !fs.existsSync(path)) {
return return
} }
await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => { await tenancy.doInTenant(constants.DEFAULT_TENANT_ID, async () => {
try { try {
const split = path.split("/") const split = path.split("/")
const name = split[split.length - 1] const name = split[split.length - 1]

View File

@ -3,19 +3,19 @@ import { BaseEvent } from "./event"
export interface RoleCreatedEvent extends BaseEvent { export interface RoleCreatedEvent extends BaseEvent {
roleId: string roleId: string
permissionId: string permissionId: string
inherits: string inherits?: string
} }
export interface RoleUpdatedEvent extends BaseEvent { export interface RoleUpdatedEvent extends BaseEvent {
roleId: string roleId: string
permissionId: string permissionId: string
inherits: string inherits?: string
} }
export interface RoleDeletedEvent extends BaseEvent { export interface RoleDeletedEvent extends BaseEvent {
roleId: string roleId: string
permissionId: string permissionId: string
inherits: string inherits?: string
} }
export interface RoleAssignedEvent extends BaseEvent { export interface RoleAssignedEvent extends BaseEvent {