When retrieving record data from Flatfile via our API, we provide all the relevant information about that record, including any metadata, validation information, and other details. However, you may want to simplify that down to only retrieve the key and value associated with that key for your records.
Here's a snippet to show you one way you could take a record set from Flatfile's API and simplify it down:
import { Flatfile } from "@flatfile/api"
/**
* { foo: bar } => { foo : {value: bar}}
* @param obj
*/
export function formatRecord(obj: SimpleRecord) {
return Object.fromEntries(
Object.entries(obj)
.filter(([key]) => key !== "id")
.map(([key, value]) => [key, { value }])
)
}
/**
* { id: 'us_rec_hjh3h3', values: { foo : {value: bar} } } => { id: 'us_rec_hjh3h3', foo: bar }
* @param obj
*/
export function toSimpleRecord(r: Flatfile.Record_): SimpleRecord {
const obj = Object.fromEntries(Object.entries(r.values).map(([key, value]) => [key, value.value] as [string, any]))
obj.id = r.id
return obj as SimpleRecord
}
/**
* { id: 'us_rec_hjh3h3', foo: bar } => { id: 'us_rec_hjh3h3', values: { foo : {value: bar} } }
* @param obj
*/
export function formatUpdate(obj: SimpleRecord) {
return {
id: obj.id as string,
values: formatRecord(obj),
}
}
export type Primitive = string | number | null | boolean
export type SimpleRecord = Record<string, Primitive>
export type SafeRecord = Record<string, string | undefined | null>