More typings

This commit is contained in:
Adria Navarro 2024-12-23 19:34:58 +01:00
parent 795da9f976
commit 32bba9b1ab
3 changed files with 38 additions and 14 deletions

View File

@ -65,7 +65,7 @@ export const deriveStores = (context: StoreContext): DerivedDatasourceStore => {
} = context } = context
const schema = derived(definition, $definition => { const schema = derived(definition, $definition => {
let schema: Record<string, FieldSchema> = getDatasourceSchema({ let schema: Record<string, UIFieldSchema> = getDatasourceSchema({
API, API,
datasource: get(datasource), datasource: get(datasource),
definition: $definition, definition: $definition,

View File

@ -1,5 +1,11 @@
import { FieldType, RelationshipType, UIFieldSchema } from "@budibase/types"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import {
FieldType,
isRelationshipField,
RelationshipType,
Row,
UIFieldSchema,
} from "@budibase/types"
const columnTypeManyTypeOverrides = { const columnTypeManyTypeOverrides = {
[FieldType.DATETIME]: FieldType.STRING, [FieldType.DATETIME]: FieldType.STRING,
@ -8,8 +14,8 @@ const columnTypeManyTypeOverrides = {
} }
const columnTypeManyParser = { const columnTypeManyParser = {
[FieldType.DATETIME]: (value, field) => { [FieldType.DATETIME]: (value: any[], field: any) => {
function parseDate(value) { function parseDate(value: any) {
const { timeOnly, dateOnly, ignoreTimezones } = field || {} const { timeOnly, dateOnly, ignoreTimezones } = field || {}
const enableTime = !dateOnly const enableTime = !dateOnly
const parsedValue = Helpers.parseDate(value, { const parsedValue = Helpers.parseDate(value, {
@ -26,14 +32,14 @@ const columnTypeManyParser = {
return value.map(v => parseDate(v)) return value.map(v => parseDate(v))
}, },
[FieldType.BOOLEAN]: value => value.map(v => !!v), [FieldType.BOOLEAN]: (value: any[]) => value.map(v => !!v),
[FieldType.BB_REFERENCE_SINGLE]: value => [ [FieldType.BB_REFERENCE_SINGLE]: (value: any[]) => [
...new Map(value.map(i => [i._id, i])).values(), ...new Map(value.map(i => [i._id, i])).values(),
], ],
[FieldType.BB_REFERENCE]: value => [ [FieldType.BB_REFERENCE]: (value: any[]) => [
...new Map(value.map(i => [i._id, i])).values(), ...new Map(value.map(i => [i._id, i])).values(),
], ],
[FieldType.ARRAY]: value => Array.from(new Set(value)), [FieldType.ARRAY]: (value: any[]) => Array.from(new Set(value)),
} }
export function enrichSchemaWithRelColumns( export function enrichSchemaWithRelColumns(
@ -46,7 +52,11 @@ export function enrichSchemaWithRelColumns(
const field = schema[fieldName] const field = schema[fieldName]
result[fieldName] = field result[fieldName] = field
if (field.visible !== false && field.columns) { if (
field.visible !== false &&
isRelationshipField(field) &&
field.columns
) {
const fromSingle = const fromSingle =
field?.relationshipType === RelationshipType.ONE_TO_MANY field?.relationshipType === RelationshipType.ONE_TO_MANY
@ -72,8 +82,13 @@ export function enrichSchemaWithRelColumns(
return result return result
} }
export function getRelatedTableValues(row, field, fromField) { export function getRelatedTableValues(
row: Row,
field: UIFieldSchema,
fromField: UIFieldSchema
) {
const fromSingle = const fromSingle =
isRelationshipField(fromField) &&
fromField?.relationshipType === RelationshipType.ONE_TO_MANY fromField?.relationshipType === RelationshipType.ONE_TO_MANY
let result = "" let result = ""
@ -85,7 +100,8 @@ export function getRelatedTableValues(row, field, fromField) {
const value = row[field.related.field] const value = row[field.related.field]
?.flatMap(r => r[field.related.subField]) ?.flatMap(r => r[field.related.subField])
?.filter(i => i !== undefined && i !== null) ?.filter(i => i !== undefined && i !== null)
result = parser(value || [], field) const parsed = parser(value || [], field)
result = parsed
if ( if (
[ [
FieldType.STRING, FieldType.STRING,
@ -97,7 +113,7 @@ export function getRelatedTableValues(row, field, fromField) {
FieldType.BARCODEQR, FieldType.BARCODEQR,
].includes(field.type) ].includes(field.type)
) { ) {
result = result?.join(", ") result = parsed?.join(", ")
} }
} }

View File

@ -1,3 +1,11 @@
import { BasicViewFieldMetadata, FieldSchema } from "@budibase/types" import {
BasicViewFieldMetadata,
FieldSchema,
RelationSchemaField,
} from "@budibase/types"
export type UIFieldSchema = FieldSchema & BasicViewFieldMetadata export type UIFieldSchema = FieldSchema &
BasicViewFieldMetadata & {
related?: { field: string; subField: string }
columns?: Record<string, RelationSchemaField & { type?: string }>
}