45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import { datasources, tables } from "../stores/backend"
|
|
import { IntegrationNames } from "../constants/backend"
|
|
import analytics, { Events } from "../analytics"
|
|
import { get } from "svelte/store"
|
|
import cloneDeep from "lodash/cloneDeepWith"
|
|
|
|
function prepareData(config) {
|
|
let datasource = {}
|
|
let existingTypeCount = get(datasources).list.filter(
|
|
ds => ds.source === config.type
|
|
).length
|
|
|
|
let baseName = IntegrationNames[config.type]
|
|
let name =
|
|
existingTypeCount === 0 ? baseName : `${baseName}-${existingTypeCount + 1}`
|
|
|
|
datasource.type = "datasource"
|
|
datasource.source = config.type
|
|
datasource.config = config.config
|
|
datasource.name = name
|
|
datasource.plus = config.plus
|
|
|
|
return datasource
|
|
}
|
|
|
|
export async function saveDatasource(config, skipFetch = false) {
|
|
const datasource = prepareData(config)
|
|
// Create datasource
|
|
const resp = await datasources.save(datasource, !skipFetch && datasource.plus)
|
|
|
|
// update the tables incase data source plus
|
|
await tables.fetch()
|
|
await datasources.select(resp._id)
|
|
analytics.captureEvent(Events.DATASOURCE.CREATED, {
|
|
name: resp.name,
|
|
source: resp.source,
|
|
})
|
|
return resp
|
|
}
|
|
|
|
export async function createRestDatasource(integration) {
|
|
const config = cloneDeep(integration)
|
|
return saveDatasource(config)
|
|
}
|