@campfhir/object-mapper v3.5.1
Object Mapper
This is a utility that helps with object mapping from one data structure to another with declarative instructions.
APIs
- Map
- Compare Objects
- Coalesce Array Object
- Add Parameters to SQL Request (requires mssql)
- SQL Parameter Mapping (requires mssql)
- SQL Table Parameter Mapping (requires mssql)
Map
This method takes an object
and a mapping definition object
and returns a new object
based on that definition.
Can throw an error or array or errors if definition functions throws internally or if options.throwOnFalseValidation
is true
and the evaluation of validation or pre-validation returns false
.
Returns any
Argument/Parameter | Required | Purpose | Default |
---|---|---|---|
data | Required | The data to map from, should be an object or array of object | |
map | Required | The mapping definition to determine how to map | |
options.throwOnFalseValidation | Optional | If pre or post-validator is false will throw an error | false |
options.mapNullIfUndefined | Optional | If value is undefined it will cast to null | false |
options.allErrors | Optional | Finish mapping logic before throwing the error on the function level. Will throw an array of all Errors. | false |
Example
import ObjectMapper, { ObjectMap } from "@campfhir/object-mapper"
const data = {
firstName: "scott",
lastName: "eremia-roden"
age: 18
}
const definition: ObjectMap<typeof data> = {
firstName: {
name: "fName",
validator: {
validate: (firstName) => {
if (firstName === "scott") return true;
else if (firstName === "not my name") return false
else return false
}
message: "Name should be scott"
},
transformer: (firstName) => {
return firstName.toUpperCase()
}
}
lastName: false,
age: "ageInYears"
}
let newData = ObjectMapper.map(data, definition);
console.log(newData.fName) // "SCOTT"
console.log(newData.ageInYears) // 18
Compare Objects
This method is used to compare one object to another.
Returns {status: string; errors: Error[]}
Arguments/Parameters | Required | Purpose | Default |
---|---|---|---|
lh | Required | The object to compare | |
rh | Required | The object to compare against | |
options.matchDataTypes | optional | Test that the keys in left hand object match and the data type is identical to the right hand object | false |
options.exactValue | optional | Test that the keys and the values in left hand match exactly the data in right hand of the same key | false |
Coalesce Array Object
Given an array
of objects
we group by like objects and reduce/coalesce to a single object
. Returns an object
. Throws if group by properties do not have identical elements.
Returns ReverseCoalesce<T,P>
Arguments/Parameters | Required | Purpose |
---|---|---|
arr | Required | The array of object to coalesce |
groupBy | Required | Array of properties to group by |
Add Parameters to SQL Request
Add array of parameters to mssql Request object
Returns void
Arguments/Parameters | Required | Purpose |
---|---|---|
params | Required | The array of SQL Parameters to add to the request |
sp | Required | The request that the parameters should be added to |
SQL Parameter Mapping
Given some data and mapping definition to generate an array of parameters, that can later be applied to a mssql Request object.
Returns SQLParameter[]
Arguments/Parameters | Required | Purpose | Default |
---|---|---|---|
input | Required | The data to map against | |
map | Required | A definition on what parameters to create and set | |
mapOptions.throwOnFalseValidation | Optional | If validator evaluates to false and this parameter is set to true the whole method will throw | false |
mapOptions.allErrors | Optional | When true will iterate through the entire map before throwing error from method | false |
SQL Table Parameter Mapping
Given some data and table definition will generate a table variable parameter to be used in a mssql Request object.
Returns mssql.Table
Arguments/Parameters | Required | Purpose |
---|---|---|
input | Required | The data used to map against |
tableDefinition | Required | Array of column definitions |
tableDefinition.name | Required | Name of the column |
tableDefinition.option.type | Required | The SQL data type for the column |