Typing utils

This commit is contained in:
Adria Navarro 2025-01-13 14:06:24 +01:00
parent 819f6f52e3
commit d97c492d36
3 changed files with 44 additions and 40 deletions

View File

@ -4,7 +4,6 @@ import {
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
AutoFieldSubType, AutoFieldSubType,
Hosting, Hosting,
FieldSubType,
} from "@budibase/types" } from "@budibase/types"
import { Constants } from "@budibase/frontend-core" import { Constants } from "@budibase/frontend-core"
@ -213,13 +212,6 @@ export const Roles = {
BUILDER: "BUILDER", BUILDER: "BUILDER",
} }
export function isAutoColumnUserRelationship(subtype: FieldSubType) {
return (
subtype === AUTO_COLUMN_SUB_TYPES.CREATED_BY ||
subtype === AUTO_COLUMN_SUB_TYPES.UPDATED_BY
)
}
export const PrettyRelationshipDefinitions = { export const PrettyRelationshipDefinitions = {
MANY: "Many rows", MANY: "Many rows",
ONE: "One row", ONE: "One row",

View File

@ -1,12 +1,20 @@
import { AutoFieldSubType, Automation, FieldType } from "@budibase/types" import {
AutoFieldSubType,
Automation,
DateFieldMetadata,
FieldType,
NumberFieldMetadata,
RelationshipFieldMetadata,
RelationshipType,
} from "@budibase/types"
import { ActionStepID } from "@/constants/backend/automations" import { ActionStepID } from "@/constants/backend/automations"
import { TableNames } from "@/constants" import { TableNames } from "@/constants"
import { import {
AUTO_COLUMN_DISPLAY_NAMES, AUTO_COLUMN_DISPLAY_NAMES,
AUTO_COLUMN_SUB_TYPES, AUTO_COLUMN_SUB_TYPES,
FIELDS, FIELDS,
isAutoColumnUserRelationship,
} from "@/constants/backend" } from "@/constants/backend"
import { utils } from "@budibase/shared-core"
type AutoColumnInformation = Partial< type AutoColumnInformation = Partial<
Record<AutoFieldSubType, { enabled: boolean; name: string }> Record<AutoFieldSubType, { enabled: boolean; name: string }>
@ -38,44 +46,47 @@ export function buildAutoColumn(
tableName: string, tableName: string,
name: string, name: string,
subtype: AutoFieldSubType subtype: AutoFieldSubType
) { ): RelationshipFieldMetadata | NumberFieldMetadata | DateFieldMetadata {
let type, constraints const base = {
name,
icon: "ri-magic-line",
autocolumn: true,
}
switch (subtype) { switch (subtype) {
case AUTO_COLUMN_SUB_TYPES.UPDATED_BY: case AUTO_COLUMN_SUB_TYPES.UPDATED_BY:
case AUTO_COLUMN_SUB_TYPES.CREATED_BY: case AUTO_COLUMN_SUB_TYPES.CREATED_BY:
type = FieldType.LINK return {
constraints = FIELDS.LINK.constraints ...base,
break type: FieldType.LINK,
subtype,
constraints: FIELDS.LINK.constraints,
tableId: TableNames.USERS,
fieldName: `${tableName}-${name}`,
relationshipType: RelationshipType.MANY_TO_ONE,
}
case AUTO_COLUMN_SUB_TYPES.AUTO_ID: case AUTO_COLUMN_SUB_TYPES.AUTO_ID:
type = FieldType.NUMBER return {
constraints = FIELDS.NUMBER.constraints ...base,
break type: FieldType.NUMBER,
subtype,
constraints: FIELDS.NUMBER.constraints,
}
case AUTO_COLUMN_SUB_TYPES.UPDATED_AT: case AUTO_COLUMN_SUB_TYPES.UPDATED_AT:
case AUTO_COLUMN_SUB_TYPES.CREATED_AT: case AUTO_COLUMN_SUB_TYPES.CREATED_AT:
type = FieldType.DATETIME return {
constraints = FIELDS.DATETIME.constraints ...base,
break type: FieldType.DATETIME,
subtype,
constraints: FIELDS.DATETIME.constraints,
}
default: default:
type = FieldType.STRING throw utils.unreachable(subtype, {
constraints = FIELDS.STRING.constraints message: "Cannot build auto column with supplied subtype",
break })
} }
if (Object.values(AUTO_COLUMN_SUB_TYPES).indexOf(subtype) === -1) {
throw "Cannot build auto column with supplied subtype"
}
const base = {
name,
type,
subtype,
icon: "ri-magic-line",
autocolumn: true,
constraints,
}
if (isAutoColumnUserRelationship(subtype)) {
base.tableId = TableNames.USERS
base.fieldName = `${tableName}-${name}`
}
return base
} }
export function checkForCollectStep(automation: Automation) { export function checkForCollectStep(automation: Automation) {

View File

@ -109,6 +109,7 @@ export interface LongFormFieldMetadata extends BaseFieldSchema {
export interface StringFieldMetadata extends BaseFieldSchema { export interface StringFieldMetadata extends BaseFieldSchema {
type: FieldType.STRING type: FieldType.STRING
default?: string default?: string
subtype?: never
} }
export interface FormulaFieldMetadata extends BaseFieldSchema { export interface FormulaFieldMetadata extends BaseFieldSchema {