2022-11-26 08:57:07 +13:00
|
|
|
import * as rowController from "../../api/controllers/row"
|
|
|
|
import * as tableController from "../../api/controllers/table"
|
|
|
|
import { FieldTypes } from "../../constants"
|
|
|
|
import { buildCtx } from "./utils"
|
2022-11-27 04:10:41 +13:00
|
|
|
import * as automationUtils from "../automationUtils"
|
2022-11-26 08:57:07 +13:00
|
|
|
import {
|
|
|
|
AutomationActionStepId,
|
2023-04-07 05:11:44 +12:00
|
|
|
AutomationCustomIOType,
|
2023-05-23 02:46:42 +12:00
|
|
|
AutomationFeature,
|
2023-04-07 05:11:44 +12:00
|
|
|
AutomationIOType,
|
2022-11-26 08:57:07 +13:00
|
|
|
AutomationStepInput,
|
2023-04-07 05:11:44 +12:00
|
|
|
AutomationStepSchema,
|
|
|
|
AutomationStepType,
|
2022-11-26 08:57:07 +13:00
|
|
|
SearchFilters,
|
|
|
|
Table,
|
|
|
|
} from "@budibase/types"
|
2021-09-15 04:49:01 +12:00
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
enum SortOrder {
|
|
|
|
ASCENDING = "ascending",
|
|
|
|
DESCENDING = "descending",
|
2021-09-15 04:49:01 +12:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
const SortOrderPretty = {
|
|
|
|
[SortOrder.ASCENDING]: "Ascending",
|
|
|
|
[SortOrder.DESCENDING]: "Descending",
|
2021-09-15 04:49:01 +12:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
enum EmptyFilterOption {
|
|
|
|
RETURN_ALL = "all",
|
|
|
|
RETURN_NONE = "none",
|
2022-08-11 02:38:08 +12:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
const EmptyFilterOptionPretty = {
|
|
|
|
[EmptyFilterOption.RETURN_ALL]: "Return all table rows",
|
|
|
|
[EmptyFilterOption.RETURN_NONE]: "Return no rows",
|
2022-08-11 02:38:08 +12:00
|
|
|
}
|
|
|
|
|
2022-11-27 04:10:41 +13:00
|
|
|
export const definition: AutomationStepSchema = {
|
2021-09-15 04:49:01 +12:00
|
|
|
description: "Query rows from the database",
|
2021-09-16 06:02:44 +12:00
|
|
|
icon: "Search",
|
2021-09-15 04:49:01 +12:00
|
|
|
name: "Query rows",
|
|
|
|
tagline: "Query rows from {{inputs.enriched.table.name}} table",
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationStepType.ACTION,
|
2022-11-26 08:57:07 +13:00
|
|
|
stepId: AutomationActionStepId.QUERY_ROWS,
|
2021-09-15 04:49:01 +12:00
|
|
|
internal: true,
|
2023-05-23 02:46:42 +12:00
|
|
|
features: [AutomationFeature.LOOPING],
|
2021-09-15 04:49:01 +12:00
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
tableId: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.STRING,
|
|
|
|
customType: AutomationCustomIOType.TABLE,
|
2021-09-15 04:49:01 +12:00
|
|
|
title: "Table",
|
|
|
|
},
|
|
|
|
filters: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.OBJECT,
|
|
|
|
customType: AutomationCustomIOType.FILTERS,
|
2021-09-15 04:49:01 +12:00
|
|
|
title: "Filtering",
|
|
|
|
},
|
|
|
|
sortColumn: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.STRING,
|
2021-09-15 04:49:01 +12:00
|
|
|
title: "Sort Column",
|
2023-04-07 05:11:44 +12:00
|
|
|
customType: AutomationCustomIOType.COLUMN,
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
|
|
|
sortOrder: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.STRING,
|
2021-09-15 04:49:01 +12:00
|
|
|
title: "Sort Order",
|
2022-11-26 08:57:07 +13:00
|
|
|
enum: Object.values(SortOrder),
|
|
|
|
pretty: Object.values(SortOrderPretty),
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
|
|
|
limit: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.NUMBER,
|
2021-09-15 04:49:01 +12:00
|
|
|
title: "Limit",
|
2023-04-07 05:11:44 +12:00
|
|
|
customType: AutomationCustomIOType.QUERY_LIMIT,
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
2022-08-11 02:38:08 +12:00
|
|
|
onEmptyFilter: {
|
2022-11-26 08:57:07 +13:00
|
|
|
pretty: Object.values(EmptyFilterOptionPretty),
|
|
|
|
enum: Object.values(EmptyFilterOption),
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.STRING,
|
2022-08-11 02:38:08 +12:00
|
|
|
title: "When Filter Empty",
|
|
|
|
},
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
2021-09-16 06:02:44 +12:00
|
|
|
required: ["tableId"],
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
2021-09-16 06:02:44 +12:00
|
|
|
rows: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.ARRAY,
|
|
|
|
customType: AutomationCustomIOType.ROWS,
|
2021-09-15 04:49:01 +12:00
|
|
|
description: "The rows that were found",
|
|
|
|
},
|
|
|
|
success: {
|
2023-04-07 05:11:44 +12:00
|
|
|
type: AutomationIOType.BOOLEAN,
|
2022-09-24 00:35:27 +12:00
|
|
|
description: "Whether the query was successful",
|
2021-09-15 04:49:01 +12:00
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["rows", "success"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
async function getTable(appId: string, tableId: string) {
|
|
|
|
const ctx: any = buildCtx(appId, null, {
|
2021-09-16 06:02:44 +12:00
|
|
|
params: {
|
2022-03-01 08:27:37 +13:00
|
|
|
tableId,
|
2021-09-16 06:02:44 +12:00
|
|
|
},
|
2021-09-21 06:21:04 +12:00
|
|
|
})
|
2021-09-16 06:02:44 +12:00
|
|
|
await tableController.find(ctx)
|
|
|
|
return ctx.body
|
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
function typeCoercion(filters: SearchFilters, table: Table) {
|
2022-07-29 00:39:11 +12:00
|
|
|
if (!filters || !table) {
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
for (let key of Object.keys(filters)) {
|
2022-11-26 08:57:07 +13:00
|
|
|
// @ts-ignore
|
|
|
|
const searchParam = filters[key]
|
|
|
|
if (typeof searchParam === "object") {
|
|
|
|
for (let [property, value] of Object.entries(searchParam)) {
|
2022-07-29 00:39:11 +12:00
|
|
|
const column = table.schema[property]
|
|
|
|
// convert string inputs
|
|
|
|
if (!column || typeof value !== "string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (column.type === FieldTypes.NUMBER) {
|
2022-11-26 08:57:07 +13:00
|
|
|
searchParam[property] = parseFloat(value)
|
2022-07-29 00:39:11 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
function hasNullFilters(filters: any[]) {
|
|
|
|
return (
|
|
|
|
filters.length === 0 ||
|
|
|
|
filters.some(filter => filter.value === null || filter.value === "")
|
|
|
|
)
|
|
|
|
}
|
2022-08-11 02:38:08 +12:00
|
|
|
|
2022-11-26 08:57:07 +13:00
|
|
|
export async function run({ inputs, appId }: AutomationStepInput) {
|
2021-09-16 06:02:44 +12:00
|
|
|
const { tableId, filters, sortColumn, sortOrder, limit } = inputs
|
2022-08-20 01:57:15 +12:00
|
|
|
if (!tableId) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: {
|
|
|
|
message: "You must select a table to query.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 06:02:44 +12:00
|
|
|
const table = await getTable(appId, tableId)
|
|
|
|
let sortType = FieldTypes.STRING
|
2022-03-22 01:32:04 +13:00
|
|
|
if (table && table.schema && table.schema[sortColumn] && sortColumn) {
|
2021-09-16 06:02:44 +12:00
|
|
|
const fieldType = table.schema[sortColumn].type
|
|
|
|
sortType =
|
|
|
|
fieldType === FieldTypes.NUMBER ? FieldTypes.NUMBER : FieldTypes.STRING
|
|
|
|
}
|
2022-11-26 08:57:07 +13:00
|
|
|
const ctx: any = buildCtx(appId, null, {
|
2021-09-16 06:02:44 +12:00
|
|
|
params: {
|
|
|
|
tableId,
|
|
|
|
},
|
2021-09-21 06:21:04 +12:00
|
|
|
body: {
|
|
|
|
sortType,
|
2022-07-28 03:18:12 +12:00
|
|
|
limit,
|
2021-09-21 06:21:04 +12:00
|
|
|
sort: sortColumn,
|
2022-07-29 00:39:11 +12:00
|
|
|
query: typeCoercion(filters || {}, table),
|
2022-07-28 03:18:12 +12:00
|
|
|
// default to ascending, like data tab
|
2022-11-26 08:57:07 +13:00
|
|
|
sortOrder: sortOrder || SortOrder.ASCENDING,
|
2021-09-16 06:02:44 +12:00
|
|
|
},
|
2022-07-28 03:18:12 +12:00
|
|
|
version: "1",
|
2021-09-21 06:21:04 +12:00
|
|
|
})
|
2021-09-16 06:02:44 +12:00
|
|
|
try {
|
2022-08-11 02:38:08 +12:00
|
|
|
let rows
|
|
|
|
|
|
|
|
if (
|
2022-11-26 08:57:07 +13:00
|
|
|
inputs.onEmptyFilter === EmptyFilterOption.RETURN_NONE &&
|
2022-08-11 02:38:08 +12:00
|
|
|
inputs["filters-def"] &&
|
|
|
|
hasNullFilters(inputs["filters-def"])
|
|
|
|
) {
|
|
|
|
rows = []
|
|
|
|
} else {
|
|
|
|
await rowController.search(ctx)
|
|
|
|
rows = ctx.body ? ctx.body.rows : []
|
|
|
|
}
|
|
|
|
|
2021-09-16 06:02:44 +12:00
|
|
|
return {
|
2022-08-11 02:38:08 +12:00
|
|
|
rows,
|
2021-09-16 06:02:44 +12:00
|
|
|
success: ctx.status === 200,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
success: false,
|
2021-10-20 05:00:54 +13:00
|
|
|
response: automationUtils.getError(err),
|
2021-09-16 06:02:44 +12:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 04:49:01 +12:00
|
|
|
}
|