kysely-mapper
Version:
Flexible Kysely-based utility for mapping between tables and objects
72 lines • 2.61 kB
JavaScript
/**
* Transforms for a table mapper that only receives and returns
* entire table rows, given by type `Selectable<DB[TB]>`.
* @typeParam DB Interface whose fields are table names defining tables.
* @typeParam TB Name of the table.
* @typeParam KeyColumns Tuple of the names of the table's key columns.
* Defaults to `[]`, indicating no key columns. Supports up to 4 columns.
* @typeParam InsertReturnColumns Columns to return from the table on insert
* queries that return columns. `['*']` returns all columns; `[]` returns
* none. May specify aliases. Defaults to `KeyColumns`.
* @typeParam UpdateReturnColumns Columns to return from the table on update
* queries that return columns. `['*']` returns all columns; `[]` returns
* none and is the default. May specify aliases.
*/
export class EntireRowTransforms {
/**
* Constructs an object providing transforms for entire table rows.
*/
constructor(keyColumns) {
this.keyColumns = keyColumns;
}
/**
* Transform a count of the number of rows affected into a number.
*/
countTransform(count) {
return Number(count);
}
/**
* Transforms inserted objects into inserted rows, removing the columns
* that are keys having falsy values.
*/
insertTransform(obj, _columns) {
const insertedValues = Object.assign({}, obj);
this.keyColumns.forEach((column) => {
if (!obj[column]) {
delete insertedValues[column];
}
});
return insertedValues;
}
/**
* Transforms the returns of an insert query into the the object returned
* to the caller, merging the returned values into the inserted object.
*/
insertReturnTransform(source, returns) {
if (returns === undefined)
return source;
return Object.assign(Object.assign({}, source), returns);
}
/**
* Returns selected rows to the caller as selected objects, unchanged.
*/
selectTransform(row) {
return row;
}
/**
* Provides updating objects as the update values for an update query.
*/
updateTransform(source, _columns) {
return source;
}
/**
* Transforms the returns of an update query into the the object returned
* to the caller, merging the returned values into the updating object.
*/
updateReturnTransform(source, returns) {
if (returns === undefined)
return source;
return Object.assign(Object.assign({}, source), returns);
}
}
//# sourceMappingURL=entire-row-transforms.js.map