Move code to sdk
This commit is contained in:
parent
6df4bf84df
commit
dd77779bc1
|
@ -14,10 +14,7 @@ import {
|
||||||
DatasourcePlus,
|
DatasourcePlus,
|
||||||
FetchDatasourceInfoRequest,
|
FetchDatasourceInfoRequest,
|
||||||
FetchDatasourceInfoResponse,
|
FetchDatasourceInfoResponse,
|
||||||
IntegrationBase,
|
|
||||||
Schema,
|
|
||||||
SourceName,
|
SourceName,
|
||||||
Table,
|
|
||||||
UpdateDatasourceResponse,
|
UpdateDatasourceResponse,
|
||||||
UserCtx,
|
UserCtx,
|
||||||
VerifyDatasourceRequest,
|
VerifyDatasourceRequest,
|
||||||
|
@ -28,62 +25,6 @@ import { builderSocket } from "../../websockets"
|
||||||
import { setupCreationAuth as googleSetupCreationAuth } from "../../integrations/googlesheets"
|
import { setupCreationAuth as googleSetupCreationAuth } from "../../integrations/googlesheets"
|
||||||
import { isEqual } from "lodash"
|
import { isEqual } from "lodash"
|
||||||
|
|
||||||
async function getConnector(
|
|
||||||
datasource: Datasource
|
|
||||||
): Promise<IntegrationBase | DatasourcePlus> {
|
|
||||||
const Connector = await getIntegration(datasource.source)
|
|
||||||
// can't enrich if it doesn't have an ID yet
|
|
||||||
if (datasource._id) {
|
|
||||||
datasource = await sdk.datasources.enrich(datasource)
|
|
||||||
}
|
|
||||||
// Connect to the DB and build the schema
|
|
||||||
return new Connector(datasource.config)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getAndMergeDatasource(datasource: Datasource) {
|
|
||||||
let existingDatasource: undefined | Datasource
|
|
||||||
if (datasource._id) {
|
|
||||||
existingDatasource = await sdk.datasources.get(datasource._id)
|
|
||||||
}
|
|
||||||
let enrichedDatasource = datasource
|
|
||||||
if (existingDatasource) {
|
|
||||||
enrichedDatasource = sdk.datasources.mergeConfigs(
|
|
||||||
datasource,
|
|
||||||
existingDatasource
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return await sdk.datasources.enrich(enrichedDatasource)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function buildSchemaHelper(datasource: Datasource): Promise<Schema> {
|
|
||||||
const connector = (await getConnector(datasource)) as DatasourcePlus
|
|
||||||
return await connector.buildSchema(datasource._id!, datasource.entities!)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function buildFilteredSchema(
|
|
||||||
datasource: Datasource,
|
|
||||||
filter?: string[]
|
|
||||||
): Promise<Schema> {
|
|
||||||
const schema = await buildSchemaHelper(datasource)
|
|
||||||
if (!filter) {
|
|
||||||
return schema
|
|
||||||
}
|
|
||||||
|
|
||||||
let filteredSchema: Schema = { tables: {}, errors: {} }
|
|
||||||
for (let key in schema.tables) {
|
|
||||||
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
|
|
||||||
filteredSchema.tables[key] = schema.tables[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let key in schema.errors) {
|
|
||||||
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
|
|
||||||
filteredSchema.errors[key] = schema.errors[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filteredSchema
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetch(ctx: UserCtx) {
|
export async function fetch(ctx: UserCtx) {
|
||||||
ctx.body = await sdk.datasources.fetch()
|
ctx.body = await sdk.datasources.fetch()
|
||||||
}
|
}
|
||||||
|
@ -92,8 +33,7 @@ export async function verify(
|
||||||
ctx: UserCtx<VerifyDatasourceRequest, VerifyDatasourceResponse>
|
ctx: UserCtx<VerifyDatasourceRequest, VerifyDatasourceResponse>
|
||||||
) {
|
) {
|
||||||
const { datasource } = ctx.request.body
|
const { datasource } = ctx.request.body
|
||||||
const enrichedDatasource = await getAndMergeDatasource(datasource)
|
const connector = await sdk.datasources.getConnector(datasource)
|
||||||
const connector = await getConnector(enrichedDatasource)
|
|
||||||
if (!connector.testConnection) {
|
if (!connector.testConnection) {
|
||||||
ctx.throw(400, "Connection information verification not supported")
|
ctx.throw(400, "Connection information verification not supported")
|
||||||
}
|
}
|
||||||
|
@ -109,8 +49,9 @@ export async function information(
|
||||||
ctx: UserCtx<FetchDatasourceInfoRequest, FetchDatasourceInfoResponse>
|
ctx: UserCtx<FetchDatasourceInfoRequest, FetchDatasourceInfoResponse>
|
||||||
) {
|
) {
|
||||||
const { datasource } = ctx.request.body
|
const { datasource } = ctx.request.body
|
||||||
const enrichedDatasource = await getAndMergeDatasource(datasource)
|
const connector = (await sdk.datasources.getConnector(
|
||||||
const connector = (await getConnector(enrichedDatasource)) as DatasourcePlus
|
datasource
|
||||||
|
)) as DatasourcePlus
|
||||||
if (!connector.getTableNames) {
|
if (!connector.getTableNames) {
|
||||||
ctx.throw(400, "Table name fetching not supported by datasource")
|
ctx.throw(400, "Table name fetching not supported by datasource")
|
||||||
}
|
}
|
||||||
|
@ -125,7 +66,10 @@ export async function buildSchemaFromDb(ctx: UserCtx) {
|
||||||
const tablesFilter = ctx.request.body.tablesFilter
|
const tablesFilter = ctx.request.body.tablesFilter
|
||||||
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
||||||
|
|
||||||
const { tables, errors } = await buildFilteredSchema(datasource, tablesFilter)
|
const { tables, errors } = await sdk.datasources.buildFilteredSchema(
|
||||||
|
datasource,
|
||||||
|
tablesFilter
|
||||||
|
)
|
||||||
datasource.entities = tables
|
datasource.entities = tables
|
||||||
|
|
||||||
setDefaultDisplayColumns(datasource)
|
setDefaultDisplayColumns(datasource)
|
||||||
|
@ -277,7 +221,10 @@ export async function save(
|
||||||
|
|
||||||
let errors: Record<string, string> = {}
|
let errors: Record<string, string> = {}
|
||||||
if (fetchSchema) {
|
if (fetchSchema) {
|
||||||
const schema = await buildFilteredSchema(datasource, tablesFilter)
|
const schema = await sdk.datasources.buildFilteredSchema(
|
||||||
|
datasource,
|
||||||
|
tablesFilter
|
||||||
|
)
|
||||||
datasource.entities = schema.tables
|
datasource.entities = schema.tables
|
||||||
setDefaultDisplayColumns(datasource)
|
setDefaultDisplayColumns(datasource)
|
||||||
errors = schema.errors
|
errors = schema.errors
|
||||||
|
@ -381,8 +328,7 @@ export async function query(ctx: UserCtx) {
|
||||||
|
|
||||||
export async function getExternalSchema(ctx: UserCtx) {
|
export async function getExternalSchema(ctx: UserCtx) {
|
||||||
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
||||||
const enrichedDatasource = await getAndMergeDatasource(datasource)
|
const connector = await sdk.datasources.getConnector(datasource)
|
||||||
const connector = await getConnector(enrichedDatasource)
|
|
||||||
|
|
||||||
if (!connector.getExternalSchema) {
|
if (!connector.getExternalSchema) {
|
||||||
ctx.throw(400, "Datasource does not support exporting external schema")
|
ctx.throw(400, "Datasource does not support exporting external schema")
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import * as datasources from "./datasources"
|
import * as datasources from "./datasources"
|
||||||
|
import * as plus from "./plus"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
...datasources,
|
...datasources,
|
||||||
|
...plus,
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
import {
|
||||||
|
Datasource,
|
||||||
|
DatasourcePlus,
|
||||||
|
IntegrationBase,
|
||||||
|
Schema,
|
||||||
|
} from "@budibase/types"
|
||||||
|
import * as datasources from "./datasources"
|
||||||
|
import { getIntegration } from "../../../integrations"
|
||||||
|
|
||||||
|
export async function buildFilteredSchema(
|
||||||
|
datasource: Datasource,
|
||||||
|
filter?: string[]
|
||||||
|
): Promise<Schema> {
|
||||||
|
const schema = await buildSchemaHelper(datasource)
|
||||||
|
if (!filter) {
|
||||||
|
return schema
|
||||||
|
}
|
||||||
|
|
||||||
|
let filteredSchema: Schema = { tables: {}, errors: {} }
|
||||||
|
for (let key in schema.tables) {
|
||||||
|
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
|
||||||
|
filteredSchema.tables[key] = schema.tables[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in schema.errors) {
|
||||||
|
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
|
||||||
|
filteredSchema.errors[key] = schema.errors[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredSchema
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildSchemaHelper(datasource: Datasource): Promise<Schema> {
|
||||||
|
const connector = (await getConnector(datasource)) as DatasourcePlus
|
||||||
|
const externalSchema = await connector.buildSchema(
|
||||||
|
datasource._id!,
|
||||||
|
datasource.entities!
|
||||||
|
)
|
||||||
|
return externalSchema
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getConnector(
|
||||||
|
datasource: Datasource
|
||||||
|
): Promise<IntegrationBase | DatasourcePlus> {
|
||||||
|
const Connector = await getIntegration(datasource.source)
|
||||||
|
|
||||||
|
datasource = await getAndMergeDatasource(datasource)
|
||||||
|
|
||||||
|
// can't enrich if it doesn't have an ID yet
|
||||||
|
if (datasource._id) {
|
||||||
|
datasource = await datasources.enrich(datasource)
|
||||||
|
}
|
||||||
|
// Connect to the DB and build the schema
|
||||||
|
return new Connector(datasource.config)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAndMergeDatasource(datasource: Datasource) {
|
||||||
|
let existingDatasource: undefined | Datasource
|
||||||
|
if (datasource._id) {
|
||||||
|
existingDatasource = await datasources.get(datasource._id)
|
||||||
|
}
|
||||||
|
let enrichedDatasource = datasource
|
||||||
|
if (existingDatasource) {
|
||||||
|
enrichedDatasource = datasources.mergeConfigs(
|
||||||
|
datasource,
|
||||||
|
existingDatasource
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return await datasources.enrich(enrichedDatasource)
|
||||||
|
}
|
Loading…
Reference in New Issue