3.5.1 • Published 4 years ago

@campfhir/object-mapper v3.5.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

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/ParameterRequiredPurposeDefault
dataRequiredThe data to map from, should be an object or array of object
mapRequiredThe mapping definition to determine how to map
options.throwOnFalseValidationOptionalIf pre or post-validator is false will throw an errorfalse
options.mapNullIfUndefinedOptionalIf value is undefined it will cast to nullfalse
options.allErrorsOptionalFinish 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/ParametersRequiredPurposeDefault
lhRequiredThe object to compare
rhRequiredThe object to compare against
options.matchDataTypesoptionalTest that the keys in left hand object match and the data type is identical to the right hand objectfalse
options.exactValueoptionalTest that the keys and the values in left hand match exactly the data in right hand of the same keyfalse

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/ParametersRequiredPurpose
arrRequiredThe array of object to coalesce
groupByRequiredArray of properties to group by

Add Parameters to SQL Request

Add array of parameters to mssql Request object

Returns void

Arguments/ParametersRequiredPurpose
paramsRequiredThe array of SQL Parameters to add to the request
spRequiredThe 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/ParametersRequiredPurposeDefault
inputRequiredThe data to map against
mapRequiredA definition on what parameters to create and set
mapOptions.throwOnFalseValidationOptionalIf validator evaluates to false and this parameter is set to true the whole method will throwfalse
mapOptions.allErrorsOptionalWhen true will iterate through the entire map before throwing error from methodfalse

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/ParametersRequiredPurpose
inputRequiredThe data used to map against
tableDefinitionRequiredArray of column definitions
tableDefinition.nameRequiredName of the column
tableDefinition.option.typeRequiredThe SQL data type for the column
3.5.1

4 years ago

3.4.8

4 years ago

3.4.7

4 years ago

3.4.6

4 years ago

3.4.5

4 years ago

3.4.4

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago