|
|
@ -5,7 +5,7 @@ import { |
|
|
PaginationJson, |
|
|
PaginationJson, |
|
|
RelationshipsJson, |
|
|
RelationshipsJson, |
|
|
} from "../../../definitions/datasource" |
|
|
} from "../../../definitions/datasource" |
|
|
import { Row, Table, FieldSchema } from "../../../definitions/common" |
|
|
import {Row, Table, FieldSchema, Datasource} from "../../../definitions/common" |
|
|
import { |
|
|
import { |
|
|
breakRowIdField, |
|
|
breakRowIdField, |
|
|
generateRowIdField, |
|
|
generateRowIdField, |
|
|
@ -29,11 +29,11 @@ interface RunConfig { |
|
|
module External { |
|
|
module External { |
|
|
const { makeExternalQuery } = require("./utils") |
|
|
const { makeExternalQuery } = require("./utils") |
|
|
const { DataSourceOperation, FieldTypes } = require("../../../constants") |
|
|
const { DataSourceOperation, FieldTypes } = require("../../../constants") |
|
|
const { getAllExternalTables } = require("../table/utils") |
|
|
const { breakExternalTableId, isSQL } = require("../../../integrations/utils") |
|
|
const { breakExternalTableId } = require("../../../integrations/utils") |
|
|
|
|
|
const { processObjectSync } = require("@budibase/string-templates") |
|
|
const { processObjectSync } = require("@budibase/string-templates") |
|
|
const { cloneDeep } = require("lodash/fp") |
|
|
const { cloneDeep } = require("lodash/fp") |
|
|
const { isEqual } = require("lodash") |
|
|
const { isEqual } = require("lodash") |
|
|
|
|
|
const CouchDB = require("../../../db") |
|
|
|
|
|
|
|
|
function buildFilters( |
|
|
function buildFilters( |
|
|
id: string | undefined, |
|
|
id: string | undefined, |
|
|
@ -128,18 +128,22 @@ module External { |
|
|
private readonly appId: string |
|
|
private readonly appId: string |
|
|
private operation: Operation |
|
|
private operation: Operation |
|
|
private tableId: string |
|
|
private tableId: string |
|
|
private tables: { [key: string]: Table } |
|
|
private datasource: Datasource |
|
|
|
|
|
private tables: { [key: string]: Table } = {} |
|
|
|
|
|
|
|
|
constructor( |
|
|
constructor( |
|
|
appId: string, |
|
|
appId: string, |
|
|
operation: Operation, |
|
|
operation: Operation, |
|
|
tableId: string, |
|
|
tableId: string, |
|
|
tables: { [key: string]: Table } |
|
|
datasource: Datasource |
|
|
) { |
|
|
) { |
|
|
this.appId = appId |
|
|
this.appId = appId |
|
|
this.operation = operation |
|
|
this.operation = operation |
|
|
this.tableId = tableId |
|
|
this.tableId = tableId |
|
|
this.tables = tables |
|
|
this.datasource = datasource |
|
|
|
|
|
if (datasource && datasource.entities) { |
|
|
|
|
|
this.tables = datasource.entities |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
inputProcessing(row: Row, table: Table) { |
|
|
inputProcessing(row: Row, table: Table) { |
|
|
@ -451,10 +455,16 @@ module External { |
|
|
async run({ id, row, filters, sort, paginate }: RunConfig) { |
|
|
async run({ id, row, filters, sort, paginate }: RunConfig) { |
|
|
const { appId, operation, tableId } = this |
|
|
const { appId, operation, tableId } = this |
|
|
let { datasourceId, tableName } = breakExternalTableId(tableId) |
|
|
let { datasourceId, tableName } = breakExternalTableId(tableId) |
|
|
if (!this.tables) { |
|
|
if (!this.datasource) { |
|
|
this.tables = await getAllExternalTables(appId, datasourceId) |
|
|
const db = new CouchDB(appId) |
|
|
|
|
|
this.datasource = await db.get(datasourceId) |
|
|
|
|
|
if (!this.datasource || !this.datasource.entities) { |
|
|
|
|
|
throw "No tables found, fetch tables before query." |
|
|
|
|
|
} |
|
|
|
|
|
this.tables = this.datasource.entities |
|
|
} |
|
|
} |
|
|
const table = this.tables[tableName] |
|
|
const table = this.tables[tableName] |
|
|
|
|
|
let isSql = isSQL(this.datasource) |
|
|
if (!table) { |
|
|
if (!table) { |
|
|
throw `Unable to process query, table "${tableName}" not defined.` |
|
|
throw `Unable to process query, table "${tableName}" not defined.` |
|
|
} |
|
|
} |
|
|
@ -476,8 +486,8 @@ module External { |
|
|
operation, |
|
|
operation, |
|
|
}, |
|
|
}, |
|
|
resource: { |
|
|
resource: { |
|
|
// have to specify the fields to avoid column overlap
|
|
|
// have to specify the fields to avoid column overlap (for SQL)
|
|
|
fields: this.buildFields(table), |
|
|
fields: isSql ? this.buildFields(table) : [], |
|
|
}, |
|
|
}, |
|
|
filters, |
|
|
filters, |
|
|
sort, |
|
|
sort, |
|
|
|