Getting functions in place which make it easy to update pats of a filter list by their keys - getting this to work for SQS and external.
This commit is contained in:
parent
6812c21076
commit
28d0d627ce
|
@ -3,12 +3,14 @@ import * as rows from "./rows"
|
||||||
import * as search from "./search"
|
import * as search from "./search"
|
||||||
import * as utils from "./utils"
|
import * as utils from "./utils"
|
||||||
import * as external from "./external"
|
import * as external from "./external"
|
||||||
|
import * as filters from "./search/filters"
|
||||||
import AliasTables from "./sqlAlias"
|
import AliasTables from "./sqlAlias"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
...attachments,
|
...attachments,
|
||||||
...rows,
|
...rows,
|
||||||
...search,
|
...search,
|
||||||
|
filters,
|
||||||
utils,
|
utils,
|
||||||
external,
|
external,
|
||||||
AliasTables,
|
AliasTables,
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
import {
|
||||||
|
FieldType,
|
||||||
|
RelationshipFieldMetadata,
|
||||||
|
SearchFilters,
|
||||||
|
Table,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
|
export function getRelationshipColumns(table: Table): {
|
||||||
|
name: string
|
||||||
|
definition: RelationshipFieldMetadata
|
||||||
|
}[] {
|
||||||
|
return Object.entries(table.schema)
|
||||||
|
.filter(entry => entry[1].type === FieldType.LINK)
|
||||||
|
.map(entry => ({
|
||||||
|
name: entry[0],
|
||||||
|
definition: entry[1] as RelationshipFieldMetadata,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTableIDList(
|
||||||
|
tables: Table[]
|
||||||
|
): { name: string; id: string }[] {
|
||||||
|
return tables
|
||||||
|
.filter(table => table.originalName)
|
||||||
|
.map(table => ({ id: table._id!, name: table.originalName! }))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateFilterKeys(
|
||||||
|
filters: SearchFilters,
|
||||||
|
updates: { original: string; updated: string }[]
|
||||||
|
): SearchFilters {
|
||||||
|
// sort the updates by length first - this is necessary to avoid replacing sub-strings
|
||||||
|
updates = updates.sort((a, b) => b.original.length - a.original.length)
|
||||||
|
const makeFilterKeyRegex = (str: string) =>
|
||||||
|
new RegExp(`^${str}.|:${str}.`, "g")
|
||||||
|
for (let filter of Object.values(filters)) {
|
||||||
|
if (typeof filter !== "object") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for (let [key, keyFilter] of Object.entries(filter)) {
|
||||||
|
if (keyFilter === "") {
|
||||||
|
delete filter[key]
|
||||||
|
}
|
||||||
|
const possibleKey = updates.find(({ original }) =>
|
||||||
|
key.match(makeFilterKeyRegex(original))
|
||||||
|
)
|
||||||
|
if (possibleKey && possibleKey.original !== possibleKey.updated) {
|
||||||
|
// only replace the first, not replaceAll
|
||||||
|
filter[key.replace(possibleKey.original, possibleKey.updated)] =
|
||||||
|
filter[key]
|
||||||
|
delete filter[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filters
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
Datasource,
|
||||||
DocumentType,
|
DocumentType,
|
||||||
FieldType,
|
FieldType,
|
||||||
Operation,
|
Operation,
|
||||||
|
@ -12,7 +13,6 @@ import {
|
||||||
SortType,
|
SortType,
|
||||||
SqlClient,
|
SqlClient,
|
||||||
Table,
|
Table,
|
||||||
Datasource,
|
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
buildInternalRelationships,
|
buildInternalRelationships,
|
||||||
|
@ -30,6 +30,11 @@ import AliasTables from "../sqlAlias"
|
||||||
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
import { outputProcessing } from "../../../../utilities/rowProcessor"
|
||||||
import pick from "lodash/pick"
|
import pick from "lodash/pick"
|
||||||
import { processRowCountResponse } from "../utils"
|
import { processRowCountResponse } from "../utils"
|
||||||
|
import {
|
||||||
|
updateFilterKeys,
|
||||||
|
getRelationshipColumns,
|
||||||
|
getTableIDList,
|
||||||
|
} from "./filters"
|
||||||
|
|
||||||
const builder = new sql.Sql(SqlClient.SQL_LITE)
|
const builder = new sql.Sql(SqlClient.SQL_LITE)
|
||||||
|
|
||||||
|
@ -60,34 +65,31 @@ function buildInternalFieldList(
|
||||||
return fieldList
|
return fieldList
|
||||||
}
|
}
|
||||||
|
|
||||||
function tableNameInFieldRegex(tableName: string) {
|
function cleanupFilters(
|
||||||
return new RegExp(`^${tableName}.|:${tableName}.`, "g")
|
filters: SearchFilters,
|
||||||
}
|
table: Table,
|
||||||
|
allTables: Table[]
|
||||||
function cleanupFilters(filters: SearchFilters, tables: Table[]) {
|
) {
|
||||||
for (let filter of Object.values(filters)) {
|
// get a list of all relationship columns in the table for updating
|
||||||
if (typeof filter !== "object") {
|
const relationshipColumns = getRelationshipColumns(table)
|
||||||
continue
|
// get table names to ID map for relationships
|
||||||
}
|
const tableNameToID = getTableIDList(allTables)
|
||||||
for (let [key, keyFilter] of Object.entries(filter)) {
|
// all should be applied at once
|
||||||
if (keyFilter === "") {
|
filters = updateFilterKeys(
|
||||||
delete filter[key]
|
filters,
|
||||||
}
|
relationshipColumns
|
||||||
|
.map(({ name, definition }) => ({
|
||||||
// relationship, switch to table ID
|
original: name,
|
||||||
const tableRelated = tables.find(
|
updated: definition.tableId,
|
||||||
table =>
|
}))
|
||||||
table.originalName &&
|
.concat(
|
||||||
key.match(tableNameInFieldRegex(table.originalName))
|
tableNameToID.map(({ name, id }) => ({
|
||||||
|
original: name,
|
||||||
|
updated: id,
|
||||||
|
}))
|
||||||
)
|
)
|
||||||
if (tableRelated && tableRelated.originalName) {
|
)
|
||||||
// only replace the first, not replaceAll
|
|
||||||
filter[key.replace(tableRelated.originalName, tableRelated._id!)] =
|
|
||||||
filter[key]
|
|
||||||
delete filter[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filters
|
return filters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +178,7 @@ export async function search(
|
||||||
operation: Operation.READ,
|
operation: Operation.READ,
|
||||||
},
|
},
|
||||||
filters: {
|
filters: {
|
||||||
...cleanupFilters(query, allTables),
|
...cleanupFilters(query, table, allTables),
|
||||||
documentType: DocumentType.ROW,
|
documentType: DocumentType.ROW,
|
||||||
},
|
},
|
||||||
table,
|
table,
|
||||||
|
|
Loading…
Reference in New Issue