mapper
NodeJS Data Mapper for transfer data from old format to new.
Install
npm install mapperjs;
Usage
const Mapper = ; mapper = map options ;mapper
Map
The map must be a binary array consisting of the string/array/functions. The first array element should be a string or an array of strings path to data of the original object.
// Source datasource = title: '' descriptions: short: '' long: '' photos: '1.jpg' '2.jpg' '3.jpg' old_photos: 'a.png' 'b.png' destination = {}; map = title: 'title' /* * in destination object will be field description with data from descriptions.long */ 'descriptions.long': 'description' /* * in preview will be first photo from photos */ 'photos.0': 'preview'
Sync and Async Map functions
On the map are the functions for processing the data sync and async.
map = /** * Async map func * value - content entity id */ { // this.dst - destination // this.src - source retun db /** * first arg - error * second arg - object: * key - path to destination object * value - value * The second argument may contain multiple key/value to setup more fields and values. */ return entity: entity ; ) ; } /** * Sync map func */ { return comments_count: ; }
Debug
Mapperjs uses the debug module internally to log information about route matches and application mode. To see this information, simply set the DEBUG environment variable to substance:* when launching your app and the debug information will appear on the console.
DEBUG=mapper* node appjs
Options
skipError
For skip error from async callback. Default: false
if set to false, then the transfer process will stop after the first error
const Mapper = ; mapper = map skipError: true ; // not passed errors in an asynchronous callback, and do not stop the transfer processmapper;
skipFields
For skip not required fields, you can use the option skipFields:
const Mapper = ; mapper = map skipFields: 'field1 field2 iAnotherField' ; // without fields field1, field2, iAnotherFieldmapper;
Example
const oldObj = username: 'Maksim Chetverikov' avatar: '2fge0923df08r.jpg' country: 'Russia' city: 'Irkutsk' newObj = firstname: '' lastname: '' avatar: '' address: '' ; const map = { const parts = username; return firstname: parts0 lastname: parts1 ; } avatar: 'avatar' 'country city' { return address: valuecountry + ', ' + valuecity } ; const mapper = map ; mapper;
results
firstname: 'Maksim' lastname: 'Chetverikov' avatar: '2fge0923df08r.jpg' address: 'Russia, Irkutsk'
Tests
npm test