@flatfile/records
Version:
Record management utilities for Flatfile
107 lines (106 loc) • 3.6 kB
TypeScript
export interface FlatfileTypes {
Record_: {
id: string;
values: Record<string, {
value: any;
}>;
};
RecordData: Record<string, {
value: any;
}>;
RecordConfig: {
fields?: Record<string, FlatfileTypes["CellConfig"]>;
[key: string]: any;
};
CellConfig: {
readonly?: boolean;
[key: string]: any;
};
}
export type Primitive = string | number | null | boolean;
export type SimpleRecord = Record<string, Primitive>;
export type SafeRecord = Record<string, string | undefined | null>;
/**
* Converts a simple key-value record into Flatfile's structured record format
* @param obj Simple record with direct key-value pairs
* @returns Record with values wrapped in {value: any} structure
* @example
* ```ts
* formatRecord({ foo: "bar" }) // => { foo: { value: "bar" } }
* ```
*/
export declare function formatRecord(obj: SimpleRecord): FlatfileTypes["RecordData"];
/**
* Converts a Flatfile record to a simple key-value record
* @param r Flatfile record with structured values
* @returns Simple record with direct key-value pairs
* @example
* ```ts
* toSimpleRecord({ id: "1", values: { foo: { value: "bar" } } })
* // => { id: "1", foo: "bar" }
* ```
*/
export declare function toSimpleRecord(r: FlatfileTypes["Record_"]): SimpleRecord;
/**
* Converts a Flatfile record to a simple record, filtering only specified keys
* @param r Flatfile record with structured values
* @param keyFilter Array of keys to include in the output
* @returns Filtered simple record with only specified keys
* @example
* ```ts
* toSimpleFilteredRecord(
* { id: "1", values: { foo: { value: "bar" }, baz: { value: "qux" } } },
* ["foo"]
* ) // => { id: "1", foo: "bar" }
* ```
*/
export declare function toSimpleFilteredRecord(r: FlatfileTypes["Record_"], keyFilter: string[]): SimpleRecord;
/**
* Filters a simple record to only include specified keys
* @param r Simple record to filter
* @param keyFilter Array of keys to include in the output
* @returns Filtered simple record with only specified keys
* @example
* ```ts
* toNarrowRecord({ id: "1", foo: "bar", baz: "qux" }, ["foo"])
* // => { id: "1", foo: "bar" }
* ```
*/
export declare function toNarrowRecord(r: SimpleRecord, keyFilter: string[]): SimpleRecord;
/**
* Formats a simple record for update operations in Flatfile
* @param obj Simple record to format
* @returns Formatted record with id, metadata, and structured values
* @example
* ```ts
* formatUpdate({ id: "1", foo: "bar" })
* // => { id: "1", metadata: undefined, values: { foo: { value: "bar" } } }
* ```
*/
export declare function formatUpdate(obj: SimpleRecord): {
id: string;
metadata: any;
values: Record<string, {
value: any;
}>;
};
/**
* Creates a function to update a single column in a Flatfile record
* @param key Column key to update
* @param cb Callback function that receives the current value, record, and index
* @returns Function that takes a Flatfile record and returns an update object
* @example
* ```ts
* const updateName = patchOneColumn("name", (val) => val.toUpperCase());
* updateName({ id: "1", values: { name: { value: "john" } } }, 0)
* // => { id: "1", values: { name: { value: "JOHN" } } }
* ```
*/
export declare function patchOneColumn(key: string, cb: (val: string, record: Record<string, any>, i: number) => string | null): (record: FlatfileTypes["Record_"], i: number) => {
id: string;
values: {
[key]: {
value: string | null;
};
};
};