Fix conflicting columns issue
This commit is contained in:
parent
bc0208bc83
commit
3d917bfe22
|
@ -45,6 +45,7 @@ export interface RunConfig {
|
||||||
row?: Row
|
row?: Row
|
||||||
rows?: Row[]
|
rows?: Row[]
|
||||||
tables?: Record<string, Table>
|
tables?: Record<string, Table>
|
||||||
|
includeSqlRelationships?: IncludeRelationship
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildFilters(
|
function buildFilters(
|
||||||
|
@ -707,7 +708,9 @@ export class ExternalRequest {
|
||||||
},
|
},
|
||||||
resource: {
|
resource: {
|
||||||
// have to specify the fields to avoid column overlap (for SQL)
|
// have to specify the fields to avoid column overlap (for SQL)
|
||||||
fields: isSql ? this.buildFields(table) : [],
|
fields: isSql
|
||||||
|
? this.buildFields(table, config.includeSqlRelationships)
|
||||||
|
: [],
|
||||||
},
|
},
|
||||||
filters,
|
filters,
|
||||||
sort,
|
sort,
|
||||||
|
|
|
@ -18,6 +18,7 @@ import {
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
Table,
|
Table,
|
||||||
Datasource,
|
Datasource,
|
||||||
|
IncludeRelationship,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ export async function patch(ctx: BBContext) {
|
||||||
return handleRequest(Operation.UPDATE, tableId, {
|
return handleRequest(Operation.UPDATE, tableId, {
|
||||||
id: breakRowIdField(id),
|
id: breakRowIdField(id),
|
||||||
row: inputs,
|
row: inputs,
|
||||||
|
includeSqlRelationships: IncludeRelationship.EXCLUDE,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +67,7 @@ export async function save(ctx: BBContext) {
|
||||||
const tableId = ctx.params.tableId
|
const tableId = ctx.params.tableId
|
||||||
return handleRequest(Operation.CREATE, tableId, {
|
return handleRequest(Operation.CREATE, tableId, {
|
||||||
row: inputs,
|
row: inputs,
|
||||||
|
includeSqlRelationships: IncludeRelationship.EXCLUDE,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +81,9 @@ export async function fetchView(ctx: BBContext) {
|
||||||
|
|
||||||
export async function fetch(ctx: BBContext) {
|
export async function fetch(ctx: BBContext) {
|
||||||
const tableId = ctx.params.tableId
|
const tableId = ctx.params.tableId
|
||||||
return handleRequest(Operation.READ, tableId)
|
return handleRequest(Operation.READ, tableId, {
|
||||||
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: BBContext) {
|
export async function find(ctx: BBContext) {
|
||||||
|
@ -86,6 +91,7 @@ export async function find(ctx: BBContext) {
|
||||||
const tableId = ctx.params.tableId
|
const tableId = ctx.params.tableId
|
||||||
const response = (await handleRequest(Operation.READ, tableId, {
|
const response = (await handleRequest(Operation.READ, tableId, {
|
||||||
id: breakRowIdField(id),
|
id: breakRowIdField(id),
|
||||||
|
includeSqlRelationships: IncludeRelationship.EXCLUDE,
|
||||||
})) as Row[]
|
})) as Row[]
|
||||||
return response ? response[0] : response
|
return response ? response[0] : response
|
||||||
}
|
}
|
||||||
|
@ -95,6 +101,7 @@ export async function destroy(ctx: BBContext) {
|
||||||
const id = ctx.request.body._id
|
const id = ctx.request.body._id
|
||||||
const { row } = (await handleRequest(Operation.DELETE, tableId, {
|
const { row } = (await handleRequest(Operation.DELETE, tableId, {
|
||||||
id: breakRowIdField(id),
|
id: breakRowIdField(id),
|
||||||
|
includeSqlRelationships: IncludeRelationship.EXCLUDE,
|
||||||
})) as { row: Row }
|
})) as { row: Row }
|
||||||
return { response: { ok: true }, row }
|
return { response: { ok: true }, row }
|
||||||
}
|
}
|
||||||
|
@ -107,6 +114,7 @@ export async function bulkDestroy(ctx: BBContext) {
|
||||||
promises.push(
|
promises.push(
|
||||||
handleRequest(Operation.DELETE, tableId, {
|
handleRequest(Operation.DELETE, tableId, {
|
||||||
id: breakRowIdField(row._id),
|
id: breakRowIdField(row._id),
|
||||||
|
includeSqlRelationships: IncludeRelationship.EXCLUDE,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -149,6 +157,7 @@ export async function search(ctx: BBContext) {
|
||||||
filters: query,
|
filters: query,
|
||||||
sort,
|
sort,
|
||||||
paginate: paginateObj as PaginationJson,
|
paginate: paginateObj as PaginationJson,
|
||||||
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})) as Row[]
|
})) as Row[]
|
||||||
let hasNextPage = false
|
let hasNextPage = false
|
||||||
if (paginate && rows.length === limit) {
|
if (paginate && rows.length === limit) {
|
||||||
|
@ -159,6 +168,7 @@ export async function search(ctx: BBContext) {
|
||||||
limit: 1,
|
limit: 1,
|
||||||
page: bookmark * limit + 1,
|
page: bookmark * limit + 1,
|
||||||
},
|
},
|
||||||
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})) as Row[]
|
})) as Row[]
|
||||||
hasNextPage = nextRows.length > 0
|
hasNextPage = nextRows.length > 0
|
||||||
}
|
}
|
||||||
|
@ -247,6 +257,7 @@ export async function fetchEnrichedRow(ctx: BBContext) {
|
||||||
const response = (await handleRequest(Operation.READ, tableId, {
|
const response = (await handleRequest(Operation.READ, tableId, {
|
||||||
id,
|
id,
|
||||||
datasource,
|
datasource,
|
||||||
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})) as Row[]
|
})) as Row[]
|
||||||
const table: Table = tables[tableName]
|
const table: Table = tables[tableName]
|
||||||
const row = response[0]
|
const row = response[0]
|
||||||
|
@ -274,6 +285,7 @@ export async function fetchEnrichedRow(ctx: BBContext) {
|
||||||
[primaryLink]: linkedIds,
|
[primaryLink]: linkedIds,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return row
|
return row
|
||||||
|
|
|
@ -89,26 +89,13 @@ function parseFilters(filters: SearchFilters | undefined): SearchFilters {
|
||||||
|
|
||||||
function generateSelectStatement(
|
function generateSelectStatement(
|
||||||
json: QueryJson,
|
json: QueryJson,
|
||||||
knex: Knex,
|
knex: Knex
|
||||||
opts?: { excludeJoinColumns: boolean }
|
): (string | Knex.Raw)[] {
|
||||||
): (string | Knex.Raw)[] | "*" {
|
|
||||||
const { resource, meta } = json
|
const { resource, meta } = json
|
||||||
const schema = meta?.table?.schema
|
const schema = meta?.table?.schema
|
||||||
|
return resource!.fields.map(field => {
|
||||||
if (!resource) {
|
|
||||||
return "*"
|
|
||||||
}
|
|
||||||
|
|
||||||
return resource.fields.reduce<(string | Knex.Raw)[]>((p, field) => {
|
|
||||||
const fieldNames = field.split(/\./g)
|
const fieldNames = field.split(/\./g)
|
||||||
const tableName = fieldNames[0]
|
const tableName = fieldNames[0]
|
||||||
if (
|
|
||||||
meta?.table?.name &&
|
|
||||||
opts?.excludeJoinColumns &&
|
|
||||||
tableName !== meta.table.name
|
|
||||||
) {
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
const columnName = fieldNames[1]
|
const columnName = fieldNames[1]
|
||||||
if (
|
if (
|
||||||
columnName &&
|
columnName &&
|
||||||
|
@ -117,18 +104,13 @@ function generateSelectStatement(
|
||||||
) {
|
) {
|
||||||
const externalType = schema[columnName].externalType
|
const externalType = schema[columnName].externalType
|
||||||
if (externalType?.includes("money")) {
|
if (externalType?.includes("money")) {
|
||||||
p.push(
|
return knex.raw(
|
||||||
knex.raw(
|
`"${tableName}"."${columnName}"::money::numeric as "${field}"`
|
||||||
`"${tableName}"."${columnName}"::money::numeric as "${field}"`
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return p
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return `${field} as ${field}`
|
||||||
p.push(`${field} as ${field}`)
|
})
|
||||||
return p
|
|
||||||
}, [])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class InternalBuilder {
|
class InternalBuilder {
|
||||||
|
@ -417,9 +399,7 @@ class InternalBuilder {
|
||||||
} else {
|
} else {
|
||||||
return query
|
return query
|
||||||
.insert(parsedBody)
|
.insert(parsedBody)
|
||||||
.returning(
|
.returning(generateSelectStatement(json, knex))
|
||||||
generateSelectStatement(json, knex, { excludeJoinColumns: true })
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,9 +428,7 @@ class InternalBuilder {
|
||||||
if (resource.fields && resource.fields.length > 0) {
|
if (resource.fields && resource.fields.length > 0) {
|
||||||
// select the resources as the format "table.columnName" - this is what is provided
|
// select the resources as the format "table.columnName" - this is what is provided
|
||||||
// by the resource builder further up
|
// by the resource builder further up
|
||||||
selectStatement = generateSelectStatement(json, knex, {
|
selectStatement = generateSelectStatement(json, knex)
|
||||||
excludeJoinColumns: false,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
let foundLimit = limit || BASE_LIMIT
|
let foundLimit = limit || BASE_LIMIT
|
||||||
// handle pagination
|
// handle pagination
|
||||||
|
@ -508,9 +486,7 @@ class InternalBuilder {
|
||||||
} else {
|
} else {
|
||||||
return query
|
return query
|
||||||
.update(parsedBody)
|
.update(parsedBody)
|
||||||
.returning(
|
.returning(generateSelectStatement(json, knex))
|
||||||
generateSelectStatement(json, knex, { excludeJoinColumns: true })
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -525,11 +501,7 @@ class InternalBuilder {
|
||||||
if (opts.disableReturning) {
|
if (opts.disableReturning) {
|
||||||
return query.delete()
|
return query.delete()
|
||||||
} else {
|
} else {
|
||||||
return query
|
return query.delete().returning(generateSelectStatement(json, knex))
|
||||||
.delete()
|
|
||||||
.returning(
|
|
||||||
generateSelectStatement(json, knex, { excludeJoinColumns: true })
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue