@flatfile/records
Version:
Record management utilities for Flatfile
126 lines (125 loc) • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatRecord = formatRecord;
exports.toSimpleRecord = toSimpleRecord;
exports.toSimpleFilteredRecord = toSimpleFilteredRecord;
exports.toNarrowRecord = toNarrowRecord;
exports.formatUpdate = formatUpdate;
exports.patchOneColumn = patchOneColumn;
const casting_1 = require("../utils/casting");
/**
* 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" } }
* ```
*/
function formatRecord(obj) {
return Object.fromEntries(Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "metadata")
.map(([key, value]) => [key, { value }]));
}
/**
* 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" }
* ```
*/
function toSimpleRecord(r) {
const obj = Object.fromEntries(Object.entries(r.values).map(([key, value]) => [key, value.value]));
obj.id = r.id;
return obj;
}
/**
* 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" }
* ```
*/
function toSimpleFilteredRecord(r, keyFilter) {
const obj = Object.fromEntries(Object.entries(r.values).reduce((acc, [key, value]) => {
if (keyFilter && !keyFilter.includes(key)) {
return acc;
}
acc.push([key, value.value]);
return acc;
}, []));
obj.id = r.id;
return obj;
}
/**
* 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" }
* ```
*/
function toNarrowRecord(r, keyFilter) {
keyFilter.push("id");
const obj = Object.fromEntries(Object.entries(r).reduce((acc, [key, value]) => {
if (keyFilter && !keyFilter.includes(key)) {
return acc;
}
acc.push([key, value]);
return acc;
}, []));
obj.id = r.id;
return obj;
}
/**
* 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" } } }
* ```
*/
function formatUpdate(obj) {
return {
id: obj.id,
metadata: obj.metadata,
values: formatRecord(obj),
};
}
/**
* 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" } } }
* ```
*/
function patchOneColumn(key, cb) {
return (record, i) => {
const obj = toSimpleRecord(record);
return {
id: record.id,
values: {
[key]: { value: cb((0, casting_1.asString)(obj[key]), obj, i) },
},
};
};
}