1
0
Fork 0
mirror of synced 2024-09-26 06:11:49 +12:00
budibase/packages/server/src/utilities/rowProcessor/map.ts
melohagan 0e93717f1a
Allow null strings (#12298)
* Allow null strings

* Don't update null to blank

* Save empty as null

* Make blank string map to null

* Add mappings for BigInt

* Fix unit tests
2023-11-23 09:53:35 +00:00

117 lines
2.2 KiB
TypeScript

// @ts-nocheck
import { FieldTypes } from "../../constants"
const parseArrayString = value => {
if (typeof value === "string") {
if (value === "") {
return []
}
let result
try {
result = JSON.parse(value.replace(/'/g, '"'))
return result
} catch (e) {
return value
}
}
return value
}
/**
* A map of how we convert various properties in rows to each other based on the row type.
*/
export const TYPE_TRANSFORM_MAP: any = {
[FieldTypes.LINK]: {
"": [],
[null]: [],
[undefined]: undefined,
parse: link => {
if (Array.isArray(link) && typeof link[0] === "object") {
return link.map(el => (el && el._id ? el._id : el))
}
if (typeof link === "string") {
return [link]
}
return link
},
},
[FieldTypes.OPTIONS]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.ARRAY]: {
[null]: [],
[undefined]: undefined,
parse: parseArrayString,
},
[FieldTypes.STRING]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.BARCODEQR]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.FORMULA]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.LONGFORM]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.NUMBER]: {
"": null,
[null]: null,
[undefined]: undefined,
parse: n => parseFloat(n),
},
[FieldTypes.BIGINT]: {
"": null,
[null]: null,
[undefined]: undefined,
},
[FieldTypes.DATETIME]: {
"": null,
[undefined]: undefined,
[null]: null,
parse: date => {
if (date instanceof Date) {
return date.toISOString()
}
return date
},
},
[FieldTypes.ATTACHMENT]: {
[null]: [],
[undefined]: undefined,
parse: parseArrayString,
},
[FieldTypes.BOOLEAN]: {
"": null,
[null]: null,
[undefined]: undefined,
true: true,
false: false,
},
[FieldTypes.AUTO]: {
parse: () => undefined,
},
[FieldTypes.JSON]: {
parse: input => {
try {
if (input === "") {
return undefined
}
return JSON.parse(input)
} catch (err) {
return input
}
},
},
}