budibase/packages/shared-core/src/helpers/schema.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-05-09 12:28:44 +02:00
import {
BBReferenceFieldSubType,
2024-06-03 16:55:02 +02:00
FieldConstraints,
2024-05-09 12:28:44 +02:00
FieldSchema,
FieldType,
} from "@budibase/types"
2024-05-13 12:13:57 +02:00
export function isDeprecatedSingleUserColumn(
schema: Pick<FieldSchema, "type" | "subtype" | "constraints">
2024-05-14 17:43:42 +02:00
): schema is {
type: FieldType.BB_REFERENCE
subtype: BBReferenceFieldSubType.USER
} {
2024-05-09 12:28:44 +02:00
const result =
schema.type === FieldType.BB_REFERENCE &&
schema.subtype === BBReferenceFieldSubType.USER &&
schema.constraints?.type !== "array"
return result
}
2024-06-03 16:55:02 +02:00
export function isRequired(constraints: FieldConstraints | undefined) {
const isRequired =
!!constraints &&
((typeof constraints.presence !== "boolean" &&
constraints.presence?.allowEmpty === false) ||
constraints.presence === true)
return isRequired
}
2024-07-30 12:03:54 +02:00
// SQS does not support non-ASCII characters in column names, so we need to
// replace them with unicode escape sequences.
export function encodeNonAscii(str: string): string {
return str
.split("")
.map(char => {
return char.charCodeAt(0) > 127
? "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0")
: char
})
.join("")
}
export function decodeNonAscii(str: string): string {
return str.replace(/\\u([0-9a-fA-F]{4})/g, (match, p1) =>
String.fromCharCode(parseInt(p1, 16))
)
}
2024-09-04 10:29:05 +02:00
export function isNumeric(field: FieldSchema) {
return field.type === FieldType.NUMBER || field.type === FieldType.BIGINT
}