Merge branch 'master' into v3-ui
This commit is contained in:
commit
baa55489d4
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||||
"version": "2.32.4",
|
"version": "2.32.5",
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"packages": [
|
"packages": [
|
||||||
"packages/*",
|
"packages/*",
|
||||||
|
|
|
@ -40,7 +40,6 @@ import { dataFilters, helpers } from "@budibase/shared-core"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
|
|
||||||
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
|
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
|
||||||
const MAX_SQS_RELATIONSHIP_FIELDS = 63
|
|
||||||
|
|
||||||
function getBaseLimit() {
|
function getBaseLimit() {
|
||||||
const envLimit = environment.SQL_MAX_ROWS
|
const envLimit = environment.SQL_MAX_ROWS
|
||||||
|
@ -56,6 +55,20 @@ function getRelationshipLimit() {
|
||||||
return envLimit || 500
|
return envLimit || 500
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function prioritisedArraySort(toSort: string[], priorities: string[]) {
|
||||||
|
return toSort.sort((a, b) => {
|
||||||
|
const aPriority = priorities.find(field => field && a.endsWith(field))
|
||||||
|
const bPriority = priorities.find(field => field && b.endsWith(field))
|
||||||
|
if (aPriority && !bPriority) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if (!aPriority && bPriority) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return a.localeCompare(b)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function getTableName(table?: Table): string | undefined {
|
function getTableName(table?: Table): string | undefined {
|
||||||
// SQS uses the table ID rather than the table name
|
// SQS uses the table ID rather than the table name
|
||||||
if (
|
if (
|
||||||
|
@ -877,6 +890,22 @@ class InternalBuilder {
|
||||||
return `'${unaliased}'${separator}${tableField}`
|
return `'${unaliased}'${separator}${tableField}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maxFunctionParameters() {
|
||||||
|
// functions like say json_build_object() in SQL have a limit as to how many can be performed
|
||||||
|
// before a limit is met, this limit exists in Postgres/SQLite. This can be very important, such as
|
||||||
|
// for JSON column building as part of relationships. We also have a default limit to avoid very complex
|
||||||
|
// functions being built - it is likely this is not necessary or the best way to do it.
|
||||||
|
switch (this.client) {
|
||||||
|
case SqlClient.SQL_LITE:
|
||||||
|
return 127
|
||||||
|
case SqlClient.POSTGRES:
|
||||||
|
return 100
|
||||||
|
// other DBs don't have a limit, but set some sort of limit
|
||||||
|
default:
|
||||||
|
return 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addJsonRelationships(
|
addJsonRelationships(
|
||||||
query: Knex.QueryBuilder,
|
query: Knex.QueryBuilder,
|
||||||
fromTable: string,
|
fromTable: string,
|
||||||
|
@ -884,7 +913,7 @@ class InternalBuilder {
|
||||||
): Knex.QueryBuilder {
|
): Knex.QueryBuilder {
|
||||||
const sqlClient = this.client
|
const sqlClient = this.client
|
||||||
const knex = this.knex
|
const knex = this.knex
|
||||||
const { resource, tableAliases: aliases, endpoint } = this.query
|
const { resource, tableAliases: aliases, endpoint, meta } = this.query
|
||||||
const fields = resource?.fields || []
|
const fields = resource?.fields || []
|
||||||
for (let relationship of relationships) {
|
for (let relationship of relationships) {
|
||||||
const {
|
const {
|
||||||
|
@ -899,21 +928,27 @@ class InternalBuilder {
|
||||||
if (!toTable || !fromTable) {
|
if (!toTable || !fromTable) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
const relatedTable = meta.tables?.[toTable]
|
||||||
const toAlias = aliases?.[toTable] || toTable,
|
const toAlias = aliases?.[toTable] || toTable,
|
||||||
fromAlias = aliases?.[fromTable] || fromTable
|
fromAlias = aliases?.[fromTable] || fromTable
|
||||||
let toTableWithSchema = this.tableNameWithSchema(toTable, {
|
let toTableWithSchema = this.tableNameWithSchema(toTable, {
|
||||||
alias: toAlias,
|
alias: toAlias,
|
||||||
schema: endpoint.schema,
|
schema: endpoint.schema,
|
||||||
})
|
})
|
||||||
let relationshipFields = fields.filter(
|
const requiredFields = [
|
||||||
field => field.split(".")[0] === toAlias
|
...(relatedTable?.primary || []),
|
||||||
|
relatedTable?.primaryDisplay,
|
||||||
|
].filter(field => field) as string[]
|
||||||
|
// sort the required fields to first in the list, so they don't get sliced out
|
||||||
|
let relationshipFields = prioritisedArraySort(
|
||||||
|
fields.filter(field => field.split(".")[0] === toAlias),
|
||||||
|
requiredFields
|
||||||
|
)
|
||||||
|
|
||||||
|
relationshipFields = relationshipFields.slice(
|
||||||
|
0,
|
||||||
|
Math.floor(this.maxFunctionParameters() / 2)
|
||||||
)
|
)
|
||||||
if (this.client === SqlClient.SQL_LITE) {
|
|
||||||
relationshipFields = relationshipFields.slice(
|
|
||||||
0,
|
|
||||||
MAX_SQS_RELATIONSHIP_FIELDS
|
|
||||||
)
|
|
||||||
}
|
|
||||||
const fieldList: string = relationshipFields
|
const fieldList: string = relationshipFields
|
||||||
.map(field => this.buildJsonField(field))
|
.map(field => this.buildJsonField(field))
|
||||||
.join(",")
|
.join(",")
|
||||||
|
|
|
@ -12,6 +12,7 @@ import {
|
||||||
OneToManyRelationshipFieldMetadata,
|
OneToManyRelationshipFieldMetadata,
|
||||||
Operation,
|
Operation,
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
|
QueryJson,
|
||||||
RelationshipFieldMetadata,
|
RelationshipFieldMetadata,
|
||||||
Row,
|
Row,
|
||||||
SearchFilters,
|
SearchFilters,
|
||||||
|
@ -161,7 +162,6 @@ export class ExternalRequest<T extends Operation> {
|
||||||
private readonly tableId: string
|
private readonly tableId: string
|
||||||
private datasource?: Datasource
|
private datasource?: Datasource
|
||||||
private tables: { [key: string]: Table } = {}
|
private tables: { [key: string]: Table } = {}
|
||||||
private tableList: Table[]
|
|
||||||
|
|
||||||
constructor(operation: T, tableId: string, datasource?: Datasource) {
|
constructor(operation: T, tableId: string, datasource?: Datasource) {
|
||||||
this.operation = operation
|
this.operation = operation
|
||||||
|
@ -170,7 +170,6 @@ export class ExternalRequest<T extends Operation> {
|
||||||
if (datasource && datasource.entities) {
|
if (datasource && datasource.entities) {
|
||||||
this.tables = datasource.entities
|
this.tables = datasource.entities
|
||||||
}
|
}
|
||||||
this.tableList = Object.values(this.tables)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private prepareFilters(
|
private prepareFilters(
|
||||||
|
@ -301,7 +300,6 @@ export class ExternalRequest<T extends Operation> {
|
||||||
throw "No tables found, fetch tables before query."
|
throw "No tables found, fetch tables before query."
|
||||||
}
|
}
|
||||||
this.tables = this.datasource.entities
|
this.tables = this.datasource.entities
|
||||||
this.tableList = Object.values(this.tables)
|
|
||||||
}
|
}
|
||||||
return { tables: this.tables, datasource: this.datasource }
|
return { tables: this.tables, datasource: this.datasource }
|
||||||
}
|
}
|
||||||
|
@ -463,7 +461,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
breakExternalTableId(relatedTableId)
|
breakExternalTableId(relatedTableId)
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
||||||
if (!lookupField || !row[lookupField]) {
|
if (!lookupField || !row?.[lookupField] == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const endpoint = getEndpoint(relatedTableId, Operation.READ)
|
const endpoint = getEndpoint(relatedTableId, Operation.READ)
|
||||||
|
@ -631,7 +629,8 @@ export class ExternalRequest<T extends Operation> {
|
||||||
const { datasource: ds } = await this.retrieveMetadata(datasourceId)
|
const { datasource: ds } = await this.retrieveMetadata(datasourceId)
|
||||||
datasource = ds
|
datasource = ds
|
||||||
}
|
}
|
||||||
const table = this.tables[tableName]
|
const tables = this.tables
|
||||||
|
const table = tables[tableName]
|
||||||
let isSql = isSQL(datasource)
|
let isSql = isSQL(datasource)
|
||||||
if (!table) {
|
if (!table) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -686,7 +685,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
) {
|
) {
|
||||||
throw "Deletion must be filtered"
|
throw "Deletion must be filtered"
|
||||||
}
|
}
|
||||||
let json = {
|
let json: QueryJson = {
|
||||||
endpoint: {
|
endpoint: {
|
||||||
datasourceId: datasourceId!,
|
datasourceId: datasourceId!,
|
||||||
entityId: tableName,
|
entityId: tableName,
|
||||||
|
@ -715,7 +714,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
table,
|
table,
|
||||||
id: config.id,
|
tables: tables,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3080,4 +3080,46 @@ describe.each([
|
||||||
}).toHaveLength(4)
|
}).toHaveLength(4)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
isSql &&
|
||||||
|
describe("max related columns", () => {
|
||||||
|
let relatedRows: Row[]
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const relatedSchema: TableSchema = {}
|
||||||
|
const row: Row = {}
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
const name = `column${i}`
|
||||||
|
relatedSchema[name] = { name, type: FieldType.NUMBER }
|
||||||
|
row[name] = i
|
||||||
|
}
|
||||||
|
const relatedTable = await createTable(relatedSchema)
|
||||||
|
table = await createTable({
|
||||||
|
name: { name: "name", type: FieldType.STRING },
|
||||||
|
related1: {
|
||||||
|
type: FieldType.LINK,
|
||||||
|
name: "related1",
|
||||||
|
fieldName: "main1",
|
||||||
|
tableId: relatedTable._id!,
|
||||||
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
relatedRows = await Promise.all([
|
||||||
|
config.api.row.save(relatedTable._id!, row),
|
||||||
|
])
|
||||||
|
await config.api.row.save(table._id!, {
|
||||||
|
name: "foo",
|
||||||
|
related1: [relatedRows[0]._id],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("retrieve the row with relationships", async () => {
|
||||||
|
await expectQuery({}).toContainExactly([
|
||||||
|
{
|
||||||
|
name: "foo",
|
||||||
|
related1: [{ _id: relatedRows[0]._id }],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -551,11 +551,16 @@ export class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const hasFilters = dataFilters.hasFilters(query.filters)
|
const hasFilters = dataFilters.hasFilters(query.filters)
|
||||||
const limit = query.paginate?.limit || 100
|
const limit = query.paginate?.limit || 100
|
||||||
const page: number =
|
let offset = query.paginate?.offset || 0
|
||||||
typeof query.paginate?.page === "number"
|
|
||||||
? query.paginate.page
|
let page = query.paginate?.page
|
||||||
: parseInt(query.paginate?.page || "1")
|
if (typeof page === "string") {
|
||||||
const offset = (page - 1) * limit
|
page = parseInt(page)
|
||||||
|
}
|
||||||
|
if (page !== undefined) {
|
||||||
|
offset = page * limit
|
||||||
|
}
|
||||||
|
|
||||||
const sheet = this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
let rows: GoogleSpreadsheetRow[] = []
|
let rows: GoogleSpreadsheetRow[] = []
|
||||||
if (query.paginate && !hasFilters) {
|
if (query.paginate && !hasFilters) {
|
||||||
|
|
|
@ -208,6 +208,42 @@ describe("Google Sheets Integration", () => {
|
||||||
expect(row2.name).toEqual("Test Contact 2")
|
expect(row2.name).toEqual("Test Contact 2")
|
||||||
expect(row2.description).toEqual("original description 2")
|
expect(row2.description).toEqual("original description 2")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("can paginate correctly", async () => {
|
||||||
|
await config.api.row.bulkImport(table._id!, {
|
||||||
|
rows: Array.from({ length: 248 }, (_, i) => ({
|
||||||
|
name: `${i}`,
|
||||||
|
description: "",
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
|
||||||
|
let resp = await config.api.row.search(table._id!, {
|
||||||
|
tableId: table._id!,
|
||||||
|
query: {},
|
||||||
|
paginate: true,
|
||||||
|
limit: 10,
|
||||||
|
})
|
||||||
|
let rows = resp.rows
|
||||||
|
|
||||||
|
while (resp.hasNextPage) {
|
||||||
|
resp = await config.api.row.search(table._id!, {
|
||||||
|
tableId: table._id!,
|
||||||
|
query: {},
|
||||||
|
paginate: true,
|
||||||
|
limit: 10,
|
||||||
|
bookmark: resp.bookmark,
|
||||||
|
})
|
||||||
|
rows = rows.concat(resp.rows)
|
||||||
|
if (rows.length > 250) {
|
||||||
|
throw new Error("Too many rows returned")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(rows.length).toEqual(250)
|
||||||
|
expect(rows.map(row => row.name)).toEqual(
|
||||||
|
expect.arrayContaining(Array.from({ length: 248 }, (_, i) => `${i}`))
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("update", () => {
|
describe("update", () => {
|
||||||
|
|
|
@ -162,7 +162,7 @@ describe("SQL query builder", () => {
|
||||||
const query = sql._query(generateRelationshipJson({ schema: "production" }))
|
const query = sql._query(generateRelationshipJson({ schema: "production" }))
|
||||||
expect(query).toEqual({
|
expect(query).toEqual({
|
||||||
bindings: [limit, relationshipLimit],
|
bindings: [limit, relationshipLimit],
|
||||||
sql: `with "paginated" as (select "brands".* from "production"."brands" order by "test"."id" asc limit $1) select "brands".*, (select json_agg(json_build_object('product_id',"products"."product_id",'product_name',"products"."product_name",'brand_id',"products"."brand_id")) from (select "products".* from "production"."products" as "products" where "products"."brand_id" = "brands"."brand_id" order by "products"."brand_id" asc limit $2) as "products") as "products" from "paginated" as "brands" order by "test"."id" asc`,
|
sql: `with "paginated" as (select "brands".* from "production"."brands" order by "test"."id" asc limit $1) select "brands".*, (select json_agg(json_build_object('brand_id',"products"."brand_id",'product_id',"products"."product_id",'product_name',"products"."product_name")) from (select "products".* from "production"."products" as "products" where "products"."brand_id" = "brands"."brand_id" order by "products"."brand_id" asc limit $2) as "products") as "products" from "paginated" as "brands" order by "test"."id" asc`,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -170,7 +170,7 @@ describe("SQL query builder", () => {
|
||||||
const query = sql._query(generateRelationshipJson())
|
const query = sql._query(generateRelationshipJson())
|
||||||
expect(query).toEqual({
|
expect(query).toEqual({
|
||||||
bindings: [limit, relationshipLimit],
|
bindings: [limit, relationshipLimit],
|
||||||
sql: `with "paginated" as (select "brands".* from "brands" order by "test"."id" asc limit $1) select "brands".*, (select json_agg(json_build_object('product_id',"products"."product_id",'product_name',"products"."product_name",'brand_id',"products"."brand_id")) from (select "products".* from "products" as "products" where "products"."brand_id" = "brands"."brand_id" order by "products"."brand_id" asc limit $2) as "products") as "products" from "paginated" as "brands" order by "test"."id" asc`,
|
sql: `with "paginated" as (select "brands".* from "brands" order by "test"."id" asc limit $1) select "brands".*, (select json_agg(json_build_object('brand_id',"products"."brand_id",'product_id',"products"."product_id",'product_name',"products"."product_name")) from (select "products".* from "products" as "products" where "products"."brand_id" = "brands"."brand_id" order by "products"."brand_id" asc limit $2) as "products") as "products" from "paginated" as "brands" order by "test"."id" asc`,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ describe("Captures of real examples", () => {
|
||||||
bindings: [primaryLimit, relationshipLimit, relationshipLimit],
|
bindings: [primaryLimit, relationshipLimit, relationshipLimit],
|
||||||
sql: expect.stringContaining(
|
sql: expect.stringContaining(
|
||||||
multiline(
|
multiline(
|
||||||
`select json_agg(json_build_object('executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid",'executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid")`
|
`select json_agg(json_build_object('completed',"b"."completed",'completed',"b"."completed",'executorid',"b"."executorid",'executorid',"b"."executorid",'qaid',"b"."qaid",'qaid',"b"."qaid",'taskid',"b"."taskid",'taskid',"b"."taskid",'taskname',"b"."taskname",'taskname',"b"."taskname")`
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
@ -95,7 +95,7 @@ describe("Captures of real examples", () => {
|
||||||
sql: expect.stringContaining(
|
sql: expect.stringContaining(
|
||||||
multiline(
|
multiline(
|
||||||
`with "paginated" as (select "a".* from "products" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc limit $1)
|
`with "paginated" as (select "a".* from "products" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc limit $1)
|
||||||
select "a".*, (select json_agg(json_build_object('executorid',"b"."executorid",'taskname',"b"."taskname",'taskid',"b"."taskid",'completed',"b"."completed",'qaid',"b"."qaid"))
|
select "a".*, (select json_agg(json_build_object('completed',"b"."completed",'executorid',"b"."executorid",'qaid',"b"."qaid",'taskid',"b"."taskid",'taskname',"b"."taskname"))
|
||||||
from (select "b".* from "tasks" as "b" inner join "products_tasks" as "c" on "b"."taskid" = "c"."taskid" where "c"."productid" = "a"."productid" order by "b"."taskid" asc limit $2) as "b") as "tasks"
|
from (select "b".* from "tasks" as "b" inner join "products_tasks" as "c" on "b"."taskid" = "c"."taskid" where "c"."productid" = "a"."productid" order by "b"."taskid" asc limit $2) as "b") as "tasks"
|
||||||
from "paginated" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc`
|
from "paginated" as "a" order by "a"."productname" asc nulls first, "a"."productid" asc`
|
||||||
)
|
)
|
||||||
|
@ -113,7 +113,7 @@ describe("Captures of real examples", () => {
|
||||||
bindings: [...filters, relationshipLimit, relationshipLimit],
|
bindings: [...filters, relationshipLimit, relationshipLimit],
|
||||||
sql: multiline(
|
sql: multiline(
|
||||||
`with "paginated" as (select "a".* from "tasks" as "a" where "a"."taskid" in ($1, $2) order by "a"."taskid" asc limit $3)
|
`with "paginated" as (select "a".* from "tasks" as "a" where "a"."taskid" in ($1, $2) order by "a"."taskid" asc limit $3)
|
||||||
select "a".*, (select json_agg(json_build_object('productname',"b"."productname",'productid',"b"."productid"))
|
select "a".*, (select json_agg(json_build_object('productid',"b"."productid",'productname',"b"."productname"))
|
||||||
from (select "b".* from "products" as "b" inner join "products_tasks" as "c" on "b"."productid" = "c"."productid"
|
from (select "b".* from "products" as "b" inner join "products_tasks" as "c" on "b"."productid" = "c"."productid"
|
||||||
where "c"."taskid" = "a"."taskid" order by "b"."productid" asc limit $4) as "b") as "products" from "paginated" as "a" order by "a"."taskid" asc`
|
where "c"."taskid" = "a"."taskid" order by "b"."productid" asc limit $4) as "b") as "products" from "paginated" as "a" order by "a"."taskid" asc`
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue