@budibase/server
Version:
Budibase Web Server
144 lines (135 loc) • 4.58 kB
text/typescript
import postgres from "./postgres"
import dynamodb from "./dynamodb"
import mongodb from "./mongodb"
import elasticsearch from "./elasticsearch"
import couchdb from "./couchdb"
import sqlServer from "./microsoftSqlServer"
import s3 from "./s3"
import airtable from "./airtable"
import mysql from "./mysql"
import arangodb from "./arangodb"
import rest from "./rest"
import googlesheets from "./googlesheets"
import firebase from "./firebase"
import redis from "./redis"
import snowflake from "./snowflake"
import oracle from "./oracle"
import {
SourceName,
Integration,
PluginType,
IntegrationBase,
DatasourcePlus,
} from "@budibase/types"
import { getDatasourcePlugin } from "../utilities/fileSystem"
import env from "../environment"
import cloneDeep from "lodash/cloneDeep"
import sdk from "../sdk"
const DEFINITIONS: Record<SourceName, Integration | undefined> = {
[]: postgres.schema,
[]: dynamodb.schema,
[]: mongodb.schema,
[]: elasticsearch.schema,
[]: couchdb.schema,
[]: sqlServer.schema,
[]: s3.schema,
[]: mysql.schema,
[]: rest.schema,
[]: firebase.schema,
[]: googlesheets.schema,
[]: redis.schema,
[]: snowflake.schema,
[]: oracle.schema,
/* deprecated - not available through UI */
[]: arangodb.schema,
[]: airtable.schema,
/* un-used */
[]: undefined,
}
type IntegrationBaseConstructor = new (...args: any[]) => IntegrationBase
type DatasourcePlusConstructor = new (...args: any[]) => DatasourcePlus
export function isDatasourcePlusConstructor(
integration: IntegrationBaseConstructor
): integration is DatasourcePlusConstructor {
return !!integration.prototype.query
}
const INTEGRATIONS: Record<SourceName, IntegrationBaseConstructor | undefined> =
{
[]: postgres.integration,
[]: dynamodb.integration,
[]: mongodb.integration,
[]: elasticsearch.integration,
[]: couchdb.integration,
[]: sqlServer.integration,
[]: s3.integration,
[]: mysql.integration,
[]: rest.integration,
[]: firebase.integration,
[]: googlesheets.integration,
[]: redis.integration,
[]: snowflake.integration,
[]: oracle.integration,
/* deprecated - not available through UI */
[]: arangodb.integration,
[]: airtable.integration,
/* un-used */
[]: undefined,
}
export async function getDefinition(
source: SourceName
): Promise<Integration | undefined> {
// check if its integrated, faster
const definition = DEFINITIONS[source]
if (definition) {
return definition
}
const allDefinitions = await getDefinitions()
return allDefinitions[source]
}
export async function getDefinitions() {
const pluginSchemas: { [key: string]: Integration } = {}
if (env.SELF_HOSTED) {
const plugins = await sdk.plugins.fetch(PluginType.DATASOURCE)
// extract the actual schema from each custom
for (let plugin of plugins) {
const sourceId = plugin.name
pluginSchemas[sourceId] = {
...plugin.schema["schema"],
custom: true,
}
if (plugin.iconUrl) {
pluginSchemas[sourceId].iconUrl = plugin.iconUrl
}
}
}
return {
...cloneDeep(DEFINITIONS),
...pluginSchemas,
}
}
export async function getIntegration(
integration: SourceName
): Promise<IntegrationBaseConstructor> {
if (INTEGRATIONS[integration]) {
return INTEGRATIONS[integration]
}
if (env.SELF_HOSTED) {
const plugins = await sdk.plugins.fetch(PluginType.DATASOURCE)
for (let plugin of plugins) {
if (plugin.name === integration) {
// need to use commonJS require due to its dynamic runtime nature
const retrieved = await getDatasourcePlugin(plugin)
if (retrieved.integration) {
return retrieved.integration
} else {
return retrieved
}
}
}
}
throw new Error(`No datasource implementation found called: "${integration}"`)
}
export default {
getDefinitions,
getIntegration,
}