Move datasource.save to sdk
This commit is contained in:
parent
92eb185390
commit
8e038e61b0
|
@ -1,9 +1,4 @@
|
||||||
import {
|
import { getQueryParams, getTableParams } from "../../db/utils"
|
||||||
DocumentType,
|
|
||||||
generateDatasourceID,
|
|
||||||
getQueryParams,
|
|
||||||
getTableParams,
|
|
||||||
} from "../../db/utils"
|
|
||||||
import { getIntegration } from "../../integrations"
|
import { getIntegration } from "../../integrations"
|
||||||
import { invalidateDynamicVariables } from "../../threads/utils"
|
import { invalidateDynamicVariables } from "../../threads/utils"
|
||||||
import { context, db as dbCore, events } from "@budibase/backend-core"
|
import { context, db as dbCore, events } from "@budibase/backend-core"
|
||||||
|
@ -24,7 +19,6 @@ import {
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
import { builderSocket } from "../../websockets"
|
import { builderSocket } from "../../websockets"
|
||||||
import { setupCreationAuth as googleSetupCreationAuth } from "../../integrations/googlesheets"
|
|
||||||
import { isEqual } from "lodash"
|
import { isEqual } from "lodash"
|
||||||
|
|
||||||
export async function fetch(ctx: UserCtx) {
|
export async function fetch(ctx: UserCtx) {
|
||||||
|
@ -209,54 +203,18 @@ export async function update(ctx: UserCtx<any, UpdateDatasourceResponse>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const preSaveAction: Partial<Record<SourceName, any>> = {
|
|
||||||
[SourceName.GOOGLE_SHEETS]: async (datasource: Datasource) => {
|
|
||||||
await googleSetupCreationAuth(datasource.config as any)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
ctx: UserCtx<CreateDatasourceRequest, CreateDatasourceResponse>
|
ctx: UserCtx<CreateDatasourceRequest, CreateDatasourceResponse>
|
||||||
) {
|
) {
|
||||||
const db = context.getAppDB()
|
const {
|
||||||
const plus = ctx.request.body.datasource.plus
|
datasource: datasourceData,
|
||||||
const fetchSchema = ctx.request.body.fetchSchema
|
fetchSchema,
|
||||||
const tablesFilter = ctx.request.body.tablesFilter
|
tablesFilter,
|
||||||
|
} = ctx.request.body
|
||||||
const datasource = {
|
const { datasource, errors } = await sdk.datasources.save(datasourceData, {
|
||||||
_id: generateDatasourceID({ plus }),
|
fetchSchema,
|
||||||
...ctx.request.body.datasource,
|
tablesFilter,
|
||||||
type: plus ? DocumentType.DATASOURCE_PLUS : DocumentType.DATASOURCE,
|
})
|
||||||
}
|
|
||||||
|
|
||||||
let errors: Record<string, string> = {}
|
|
||||||
if (fetchSchema) {
|
|
||||||
const schema = await sdk.datasources.buildFilteredSchema(
|
|
||||||
datasource,
|
|
||||||
tablesFilter
|
|
||||||
)
|
|
||||||
datasource.entities = schema.tables
|
|
||||||
setDefaultDisplayColumns(datasource)
|
|
||||||
errors = schema.errors
|
|
||||||
}
|
|
||||||
|
|
||||||
if (preSaveAction[datasource.source]) {
|
|
||||||
await preSaveAction[datasource.source](datasource)
|
|
||||||
}
|
|
||||||
|
|
||||||
const dbResp = await db.put(
|
|
||||||
sdk.tables.populateExternalTableSchemas(datasource)
|
|
||||||
)
|
|
||||||
await events.datasource.created(datasource)
|
|
||||||
datasource._rev = dbResp.rev
|
|
||||||
|
|
||||||
// Drain connection pools when configuration is changed
|
|
||||||
if (datasource.source) {
|
|
||||||
const source = await getIntegration(datasource.source)
|
|
||||||
if (source && source.pool) {
|
|
||||||
await source.pool.end()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
datasource: await sdk.datasources.removeSecretSingle(datasource),
|
datasource: await sdk.datasources.removeSecretSingle(datasource),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { context, db as dbCore } from "@budibase/backend-core"
|
import { context, db as dbCore, events } from "@budibase/backend-core"
|
||||||
import { findHBSBlocks, processObjectSync } from "@budibase/string-templates"
|
import { findHBSBlocks, processObjectSync } from "@budibase/string-templates"
|
||||||
import {
|
import {
|
||||||
Datasource,
|
Datasource,
|
||||||
|
@ -14,16 +14,22 @@ import {
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { getEnvironmentVariables } from "../../utils"
|
import { getEnvironmentVariables } from "../../utils"
|
||||||
import { getDefinitions, getDefinition } from "../../../integrations"
|
import {
|
||||||
|
getDefinitions,
|
||||||
|
getDefinition,
|
||||||
|
getIntegration,
|
||||||
|
} from "../../../integrations"
|
||||||
import merge from "lodash/merge"
|
import merge from "lodash/merge"
|
||||||
import {
|
import {
|
||||||
BudibaseInternalDB,
|
BudibaseInternalDB,
|
||||||
|
generateDatasourceID,
|
||||||
getDatasourceParams,
|
getDatasourceParams,
|
||||||
getDatasourcePlusParams,
|
getDatasourcePlusParams,
|
||||||
getTableParams,
|
getTableParams,
|
||||||
|
DocumentType,
|
||||||
} from "../../../db/utils"
|
} from "../../../db/utils"
|
||||||
import sdk from "../../index"
|
import sdk from "../../index"
|
||||||
import datasource from "../../../api/routes/datasource"
|
import { setupCreationAuth as googleSetupCreationAuth } from "../../../integrations/googlesheets"
|
||||||
|
|
||||||
const ENV_VAR_PREFIX = "env."
|
const ENV_VAR_PREFIX = "env."
|
||||||
|
|
||||||
|
@ -273,3 +279,75 @@ export async function getExternalDatasources(): Promise<Datasource[]> {
|
||||||
|
|
||||||
return externalDatasources.rows.map(r => r.doc!)
|
return externalDatasources.rows.map(r => r.doc!)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function save(
|
||||||
|
datasource: Datasource,
|
||||||
|
opts?: { fetchSchema?: boolean; tablesFilter?: string[] }
|
||||||
|
): Promise<{ datasource: Datasource; errors: Record<string, string> }> {
|
||||||
|
const db = context.getAppDB()
|
||||||
|
const plus = datasource.plus
|
||||||
|
|
||||||
|
const fetchSchema = opts?.fetchSchema || false
|
||||||
|
const tablesFilter = opts?.tablesFilter || []
|
||||||
|
|
||||||
|
datasource = {
|
||||||
|
_id: generateDatasourceID({ plus }),
|
||||||
|
...datasource,
|
||||||
|
type: plus ? DocumentType.DATASOURCE_PLUS : DocumentType.DATASOURCE,
|
||||||
|
}
|
||||||
|
|
||||||
|
let errors: Record<string, string> = {}
|
||||||
|
if (fetchSchema) {
|
||||||
|
const schema = await sdk.datasources.buildFilteredSchema(
|
||||||
|
datasource,
|
||||||
|
tablesFilter
|
||||||
|
)
|
||||||
|
datasource.entities = schema.tables
|
||||||
|
setDefaultDisplayColumns(datasource)
|
||||||
|
errors = schema.errors
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preSaveAction[datasource.source]) {
|
||||||
|
await preSaveAction[datasource.source](datasource)
|
||||||
|
}
|
||||||
|
|
||||||
|
const dbResp = await db.put(
|
||||||
|
sdk.tables.populateExternalTableSchemas(datasource)
|
||||||
|
)
|
||||||
|
await events.datasource.created(datasource)
|
||||||
|
datasource._rev = dbResp.rev
|
||||||
|
|
||||||
|
// Drain connection pools when configuration is changed
|
||||||
|
if (datasource.source) {
|
||||||
|
const source = await getIntegration(datasource.source)
|
||||||
|
if (source && source.pool) {
|
||||||
|
await source.pool.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { datasource, errors }
|
||||||
|
}
|
||||||
|
|
||||||
|
const preSaveAction: Partial<Record<SourceName, any>> = {
|
||||||
|
[SourceName.GOOGLE_SHEETS]: async (datasource: Datasource) => {
|
||||||
|
await googleSetupCreationAuth(datasource.config as any)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure all datasource entities have a display name selected
|
||||||
|
*/
|
||||||
|
function setDefaultDisplayColumns(datasource: Datasource) {
|
||||||
|
//
|
||||||
|
for (let entity of Object.values(datasource.entities || {})) {
|
||||||
|
if (entity.primaryDisplay) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const notAutoColumn = Object.values(entity.schema).find(
|
||||||
|
schema => !schema.autocolumn
|
||||||
|
)
|
||||||
|
if (notAutoColumn) {
|
||||||
|
entity.primaryDisplay = notAutoColumn.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue