boco-object-source-map
Define sources for object properties to support mapping between two object types (ie: Model/Record for DataMapper).
Installation
Installation is available via npm or github.
$ npm install boco-object-source-map$ git clone https://github.com/bocodigitalmedia/boco-object-source-map
Usage
Each property must be mapped to a property source, consisting of one or more resolvers.
null
to use thedefaultResolver
- a string identifying the property to read from the target object
- a function
- if it is the first resolver, receives
(sourceData, targetPropertyName)
- otherwise, receives
(previousResult, targetPropertyName)
- if it is the first resolver, receives
ObjectSourceMap = require"boco-object-source-map"ObjectSourceMap modelSourceMap = # Assign a default resolver to return the value of # the equivalent database record property = datarequire"lodash"snakeCasekey modelSourceMapdefine id: null type: "record_type" createdAt: null payload: nullJSONparse : id: recorduser_idtype: recorduser_type
Resolving
Pass in the data you would like mapped to the resolve
function to create a new object from the given source.
record = id: 1 record_type: "User" created_at: payload: '{ "foo": "bar" }' user_id: 2 user_type: "admin" model = modelSourceMapresolve record expectmodelidtoEqual recordidexpectmodelcreatedAttoEqual recordcreated_atexpectmodelpayloadfootoEqual "bar"expectmodeluseridtoEqual recorduser_idexpectmodelusertypetoEqual recorduser_type
The MIT License (MIT)
Copyright (c) 2016 Christian Bradley + Boco Digital Media
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.