2024-10-25 18:32:42 +02:00
import { object } from "./utils"
import Resource from "./utils/Resource"
2024-11-15 18:24:21 +01:00
import {
ArrayOperator ,
BasicOperator ,
CalculationType ,
RangeOperator ,
SortOrder ,
SortType ,
} from "@budibase/types"
import { cloneDeep } from "lodash"
2024-10-25 18:32:42 +02:00
const view = {
2024-10-25 18:51:52 +02:00
name : "peopleView" ,
tableId : "ta_896a325f7e8147d2a2cda93c5d236511" ,
2024-10-25 18:32:42 +02:00
schema : {
name : {
2024-10-25 18:51:52 +02:00
visible : true ,
readonly : false ,
order : 1 ,
width : 300 ,
2024-10-25 18:32:42 +02:00
} ,
age : {
2024-10-25 18:51:52 +02:00
visible : true ,
readonly : true ,
order : 2 ,
width : 200 ,
2024-10-25 18:32:42 +02:00
} ,
2024-10-25 18:51:52 +02:00
salary : {
visible : false ,
readonly : false ,
2024-10-25 18:32:42 +02:00
} ,
} ,
2024-11-08 19:04:53 +01:00
query : {
2024-11-15 18:24:21 +01:00
logicalOperator : "all" ,
onEmptyFilter : "none" ,
groups : [
{
logicalOperator : "any" ,
filters : [
{ operator : "string" , field : "name" , value : "John" } ,
{ operator : "range" , field : "age" , value : { low : 18 , high : 100 } } ,
] ,
} ,
] ,
2024-11-08 19:04:53 +01:00
} ,
2024-10-25 18:51:52 +02:00
primaryDisplay : "name" ,
2024-10-25 18:32:42 +02:00
}
const baseColumnDef = {
2024-10-25 18:51:52 +02:00
visible : {
type : "boolean" ,
2024-10-25 18:32:42 +02:00
description :
2024-10-25 18:51:52 +02:00
"Defines whether the column is visible or not - rows retrieved/updated through this view will not be able to access it." ,
2024-10-25 18:32:42 +02:00
} ,
2024-10-25 18:51:52 +02:00
readonly : {
type : "boolean" ,
2024-10-25 18:32:42 +02:00
description :
2024-10-25 18:51:52 +02:00
"When used in combination with 'visible: true' the column will be visible in row responses but cannot be updated." ,
2024-10-25 18:32:42 +02:00
} ,
2024-10-25 18:51:52 +02:00
order : {
type : "integer" ,
description :
"A number defining where the column shows up in tables, lowest being first." ,
2024-10-25 18:32:42 +02:00
} ,
2024-10-25 18:51:52 +02:00
width : {
type : "integer" ,
description :
"A width for the column, defined in pixels - this affects rendering in tables." ,
2024-10-25 18:32:42 +02:00
} ,
2024-10-31 18:42:46 +01:00
column : {
type : "array" ,
description :
"If this is a relationship column, we can set the columns we wish to include" ,
items : {
type : "object" ,
properties : {
readonly : {
type : "boolean" ,
} ,
} ,
} ,
} ,
2024-10-25 18:32:42 +02:00
}
2024-11-15 18:24:21 +01:00
const logicalOperator = {
description :
"When using groups this defines whether all of the filters must match, or only one of them." ,
type : "string" ,
enum : [ "all" , "any" ] ,
}
const filterGroup = {
description : "A grouping of filters to be applied." ,
type : "array" ,
items : {
type : "object" ,
properties : {
logicalOperator ,
filters : {
description : "A list of filters to apply" ,
type : "array" ,
items : {
type : "object" ,
properties : {
operator : {
type : "string" ,
description :
"The type of search operation which is being performed." ,
enum : [
. . . Object . values ( BasicOperator ) ,
. . . Object . values ( ArrayOperator ) ,
. . . Object . values ( RangeOperator ) ,
] ,
} ,
field : {
type : "string" ,
description : "The field in the view to perform the search on." ,
} ,
value : {
description :
"The value to search for - the type will depend on the operator in use." ,
oneOf : [
{ type : "string" } ,
{ type : "number" } ,
{ type : "boolean" } ,
{ type : "object" } ,
{ type : "array" } ,
] ,
} ,
} ,
} ,
} ,
} ,
} ,
}
// have to clone to avoid constantly recursive structure - we can't represent this easily
const layeredFilterGroup : any = cloneDeep ( filterGroup )
layeredFilterGroup . items . properties . groups = filterGroup
const viewQuerySchema = {
description : "Search parameters for view" ,
type : "object" ,
properties : {
logicalOperator ,
onEmptyFilter : {
description :
"If no filters match, should the view return all rows, or no rows." ,
type : "string" ,
enum : [ "all" , "none" ] ,
} ,
groups : layeredFilterGroup ,
} ,
}
2024-10-25 18:32:42 +02:00
const viewSchema = {
2024-10-25 18:51:52 +02:00
description : "The view to be created/updated." ,
2024-10-25 18:32:42 +02:00
type : "object" ,
2024-11-08 19:04:53 +01:00
required : [ "name" , "schema" , "tableId" ] ,
2024-10-25 18:32:42 +02:00
properties : {
name : {
2024-10-25 18:51:52 +02:00
description : "The name of the view." ,
2024-10-25 18:32:42 +02:00
type : "string" ,
} ,
2024-11-08 19:04:53 +01:00
tableId : {
description : "The ID of the table this view is based on." ,
type : "string" ,
} ,
2024-10-31 18:42:46 +01:00
type : {
description : "The type of view - standard (empty value) or calculation." ,
type : "string" ,
enum : [ "calculation" ] ,
} ,
2024-10-25 18:32:42 +02:00
primaryDisplay : {
type : "string" ,
description :
2024-10-25 18:51:52 +02:00
"A column used to display rows from this view - usually used when rendered in tables." ,
2024-10-25 18:32:42 +02:00
} ,
2024-11-15 18:24:21 +01:00
query : viewQuerySchema ,
2024-11-01 18:53:38 +01:00
sort : {
type : "object" ,
required : [ "field" ] ,
properties : {
field : {
type : "string" ,
description : "The field from the table/view schema to sort on." ,
} ,
order : {
type : "string" ,
description : "The order in which to sort." ,
enum : Object . values ( SortOrder ) ,
} ,
type : {
type : "string" ,
description :
"The type of sort to perform (by number, or by alphabetically)." ,
enum : Object . values ( SortType ) ,
} ,
} ,
} ,
2024-10-25 18:32:42 +02:00
schema : {
type : "object" ,
additionalProperties : {
2024-10-31 18:42:46 +01:00
oneOf : [
{
type : "object" ,
properties : baseColumnDef ,
} ,
{
type : "object" ,
properties : {
calculationType : {
type : "string" ,
description :
"This column should be built from a calculation, specifying a type and field. It is important to note when a calculation is configured all non-calculation columns will be used for grouping." ,
enum : Object . values ( CalculationType ) ,
} ,
field : {
type : "string" ,
description :
"The field from the table to perform the calculation on." ,
} ,
distinct : {
type : "boolean" ,
description :
"Can be used in tandem with the count calculation type, to count unique entries." ,
} ,
} ,
} ,
] ,
2024-10-25 18:32:42 +02:00
} ,
} ,
} ,
}
const viewOutputSchema = {
. . . viewSchema ,
properties : {
. . . viewSchema . properties ,
2024-10-25 18:51:52 +02:00
id : {
2024-10-25 18:32:42 +02:00
description : "The ID of the view." ,
type : "string" ,
} ,
} ,
2024-10-25 18:51:52 +02:00
required : [ . . . viewSchema . required , "id" ] ,
2024-10-25 18:32:42 +02:00
}
export default new Resource ( )
. setExamples ( {
view : {
value : {
data : view ,
} ,
} ,
views : {
value : {
data : [ view ] ,
} ,
} ,
} )
. setSchemas ( {
view : viewSchema ,
viewOutput : object ( {
data : viewOutputSchema ,
} ) ,
viewSearch : object ( {
data : {
type : "array" ,
items : viewOutputSchema ,
} ,
} ) ,
} )