map-factory
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,6 @@ | ||
### 1.2.0 | ||
Add map method which will improve readability in some use cases | ||
Added support for fluent coding style | ||
### 1.1.0 | ||
@@ -7,3 +12,3 @@ | ||
Previous did not publish properly. | ||
Previous version did not publish properly. | ||
@@ -10,0 +15,0 @@ ### 1.0.0 |
@@ -0,3 +1,12 @@ | ||
export interface IKeyDefinition { | ||
key: string; | ||
transform: Function; | ||
} | ||
export interface IMapData { | ||
transform: Object; | ||
multiMaps: Object[]; | ||
} | ||
export interface IMapFactory { | ||
(stringOrArray: string | string[]): IMapping; | ||
map(stringOrArray: string | string[]): IMapping; | ||
execute(source?: any, destination?: any): any; | ||
@@ -9,10 +18,4 @@ } | ||
to(target: string, fnc?: Function): any; | ||
map(stringOrArray: string | string[]): IMapping; | ||
execute(source?: any, destination?: any): any; | ||
} | ||
export interface IKeyDefinition { | ||
key: string; | ||
transform: Function; | ||
} | ||
export interface IMapData { | ||
transform: Object; | ||
multiMaps: Object[]; | ||
} |
"use strict"; | ||
var mapper_1 = require("./mapper"); | ||
var mapping_1 = require("./mapping"); | ||
function createMapper() { | ||
@@ -8,11 +7,12 @@ var me = { | ||
}; | ||
var map = function map(source) { | ||
var mapping = new mapping_1["default"](source); | ||
this.mapper.registerMapping(mapping); | ||
return mapping; | ||
var mapper = function map(source) { | ||
return this.mapper.map(source); | ||
}.bind(me); | ||
map.execute = function (source, destination) { | ||
mapper.map = function (source) { | ||
return this.mapper.map(source); | ||
}.bind(me); | ||
mapper.execute = function (source, destination) { | ||
return this.mapper.execute(source, destination); | ||
}.bind(me); | ||
return map; | ||
return mapper; | ||
} | ||
@@ -19,0 +19,0 @@ exports.__esModule = true; |
import { IMapping } from "./interfaces"; | ||
import Mapping from "./mapping"; | ||
export default class Mapper { | ||
assignment: IMapping[]; | ||
private mapCache; | ||
constructor(); | ||
registerMapping(mapping: IMapping): void; | ||
map(source: string | string[]): Mapping; | ||
execute(source: any, destination: any): any; | ||
@@ -8,0 +9,0 @@ private createMapData(); |
"use strict"; | ||
var mod = require("object-mapper"); | ||
var mapping_1 = require("./mapping"); | ||
var objectMapper = mod; | ||
@@ -12,2 +13,7 @@ var Mapper = (function () { | ||
}; | ||
Mapper.prototype.map = function (source) { | ||
var mapping = new mapping_1["default"](source, this); | ||
this.registerMapping(mapping); | ||
return mapping; | ||
}; | ||
Mapper.prototype.execute = function (source, destination) { | ||
@@ -36,10 +42,11 @@ if (source === null || source === undefined) { | ||
if (Array.isArray(item.source)) { | ||
if (!target.transform) | ||
if (!target.transform) { | ||
throw new Error("Multiple selections must map to a transform. No transform provided."); | ||
} | ||
mapData.multiMaps.push(item); | ||
continue; | ||
} | ||
; | ||
if (!target) | ||
if (!target) { | ||
target = sourceKey; | ||
} | ||
mapData.transform[sourceKey] = target; | ||
@@ -46,0 +53,0 @@ } |
@@ -5,4 +5,7 @@ import { IMapping, IKeyDefinition } from "./interfaces"; | ||
target: string | IKeyDefinition; | ||
constructor(source: string | string[]); | ||
to(target: string, fnc?: Function): void; | ||
mapper: any; | ||
constructor(source: string | string[], mapper: any); | ||
map(stringOrArray: string | string[]): any; | ||
execute(source?: any, destination?: any): any; | ||
to(target: string, fnc?: Function): any; | ||
} |
"use strict"; | ||
var Mapping = (function () { | ||
function Mapping(source) { | ||
if (!source) | ||
function Mapping(source, mapper) { | ||
if (!source) { | ||
throw new Error("the source field name cannot be null"); | ||
} | ||
this.mapper = mapper; | ||
this.source = source; | ||
} | ||
Mapping.prototype.map = function (stringOrArray) { | ||
return this.mapper.map(stringOrArray); | ||
}; | ||
Mapping.prototype.execute = function (source, destination) { | ||
return this.mapper.execute(source, destination); | ||
}; | ||
Mapping.prototype.to = function (target, fnc) { | ||
if (!target) | ||
if (!target) { | ||
throw new Error("the target field name cannot be null"); | ||
} | ||
if (fnc) { | ||
@@ -16,5 +25,6 @@ this.target = { | ||
}; | ||
return; | ||
return this.mapper; | ||
} | ||
this.target = target; | ||
return this.mapper; | ||
}; | ||
@@ -21,0 +31,0 @@ return Mapping; |
"use strict"; | ||
// tslint:disable-next-line no-var-requires | ||
var createMapper = require("../lib/index"); | ||
@@ -19,6 +20,5 @@ var exampleGroup = { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -42,6 +42,5 @@ "Map a source field to a different object structure": function (test) { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -80,6 +79,5 @@ "Supports deep references for source and target objects": function (test) { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -115,6 +113,5 @@ "You can also reference specific items in an array": function (test) { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -163,6 +160,5 @@ "More complicated transformations can be handled by providing a function": function (test) { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -190,6 +186,5 @@ "An existing object can be provided as the target object": function (test) { | ||
var result = map.execute(source, destination); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -216,6 +211,94 @@ "Select from multiple sources at once": function (test) { | ||
var result = map.execute(source); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
return test.done(); | ||
}, | ||
"create a single transform mapping object which is used to map all of your data together": function (test) { | ||
var expected = { | ||
"blog": { | ||
"post": { | ||
"text": "<p>Some Text</p>", | ||
"comments": ["not too bad", "pretty good", "awful"], | ||
"topComment": "not too bad" | ||
}, | ||
"author": { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
} | ||
} | ||
}; | ||
// Start example | ||
// assume the following inputs | ||
var post = { | ||
"body": "<p>Some Text</p>" | ||
}; | ||
var comments = { | ||
"list": ["not too bad", "pretty good", "awful"] | ||
}; | ||
var user = { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
}; | ||
// combine multiple objects into a single source object | ||
var source = { post: post, comments: comments, user: user }; | ||
var map = createMapper(); | ||
map("post.body").to("blog.post.text"); | ||
map("comments.list").to("blog.post.comments"); | ||
map("comments.list[0]").to("blog.post.topComment"); | ||
map("user.id").to("blog.author.id"); | ||
map("user.name").to("blog.author.name"); | ||
map("user.email").to("blog.author.email"); | ||
var final = map.execute(source); | ||
// End example | ||
test.deepEqual(final, expected); | ||
test.done(); | ||
}, | ||
"Or use multiple mappers and chain them together": function (test) { | ||
var expected = { | ||
"blog": { | ||
"post": { | ||
"text": "<p>Some Text</p>", | ||
"comments": ["not too bad", "pretty good", "awful"], | ||
"topComment": "not too bad" | ||
}, | ||
"author": { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
} | ||
} | ||
}; | ||
// Start Example | ||
// assume the following inputs | ||
var post = { | ||
"body": "<p>Some Text</p>" | ||
}; | ||
var comments = { | ||
"list": ["not too bad", "pretty good", "awful"] | ||
}; | ||
var user = { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
}; | ||
var postMapper = createMapper(); | ||
var commentMapper = createMapper(); | ||
var authorMapper = createMapper(); | ||
postMapper | ||
.map("body").to("blog.post.text"); | ||
commentMapper | ||
.map("list").to("blog.post.comments") | ||
.map("list[0]").to("blog.post.topComment"); | ||
authorMapper | ||
.map("id").to("blog.author.id") | ||
.map("name").to("blog.author.name") | ||
.map("email").to("blog.author.email"); | ||
var result = postMapper.execute(post); | ||
result = commentMapper.execute(comments, result); | ||
result = authorMapper.execute(user, result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
} | ||
@@ -222,0 +305,0 @@ }; |
"use strict"; | ||
// tslint:disable-next-line no-var-requires | ||
var createMapper = require("../lib/index"); | ||
@@ -17,3 +18,3 @@ var basicMappingGroup = { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
} | ||
@@ -20,0 +21,0 @@ }; |
"use strict"; | ||
var map_factory_1 = require("../lib/map-factory"); | ||
// var createMapper = require("../lib/index"); | ||
var basicMappingGroup = { | ||
var mapper_1 = require("../lib/mapper"); | ||
var mapping_1 = require("../lib/mapping"); | ||
var mapGroup = { | ||
"default function and map() function are logically equivalent": function (test) { | ||
var source = { | ||
"fieldName": "name1" | ||
}; | ||
var expected = { | ||
"field": { | ||
"name": "name1" | ||
} | ||
}; | ||
var map = map_factory_1["default"](); | ||
var mapper = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
mapper.map("fieldName").to("field.name"); | ||
var defaultActual = map.execute(source); | ||
var functionActual = mapper.execute(source); | ||
test.deepEqual(defaultActual, expected); | ||
test.deepEqual(defaultActual, functionActual); | ||
return test.done(); | ||
} | ||
}; | ||
var fluentChainingGroup = { | ||
"map() returns a chainable object": function (test) { | ||
var mapper = map_factory_1["default"](); | ||
var actual = mapper.map("userId"); | ||
test.notEqual(actual, null); | ||
test.equal(actual instanceof mapping_1["default"], true); | ||
return test.done(); | ||
}, | ||
"to() returns a chainable object": function (test) { | ||
var mapper = map_factory_1["default"](); | ||
var actual = mapper.map("userId").to("user.id"); | ||
test.notEqual(actual, null); | ||
test.equal(actual instanceof mapper_1["default"], true); | ||
return test.done(); | ||
}, | ||
"to() with a function returns a chainable object": function (test) { | ||
var mapper = map_factory_1["default"](); | ||
var actual = mapper.map("userId").to("user.id", function (value) { | ||
return "a"; | ||
}); | ||
test.notEqual(actual, null); | ||
test.equal(actual instanceof mapper_1["default"], true); | ||
return test.done(); | ||
}, | ||
"mapper can fluently chain call map() after the map() method": function (test) { | ||
var source = { | ||
"userId": 123, | ||
"userName": "my name" | ||
}; | ||
var expected = { | ||
"userId": 123, | ||
"name": "my name" | ||
}; | ||
var mapper = map_factory_1["default"](); | ||
mapper | ||
.map("userId") | ||
.map("userName").to("name"); | ||
var actual = mapper.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"mapper can fluently chain call map() after the to() method": function (test) { | ||
var source = { | ||
"userId": 123, | ||
"userName": "my name" | ||
}; | ||
var expected = { | ||
"id": 123, | ||
"name": "my name" | ||
}; | ||
var mapper = map_factory_1["default"](); | ||
mapper | ||
.map("userId").to("id") | ||
.map("userName").to("name"); | ||
var actual = mapper.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"mapper can fluently chain call execute() after the to() method": function (test) { | ||
var source = { | ||
"userId": 123, | ||
"userName": "my name" | ||
}; | ||
var expected = { | ||
"id": 123, | ||
"name": "my name" | ||
}; | ||
var mapper = map_factory_1["default"](); | ||
var actual = mapper | ||
.map("userId").to("id") | ||
.map("userName").to("name") | ||
.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"mapper can fluently chain call execute() after the map() method": function (test) { | ||
var source = { | ||
"userId": 123 | ||
}; | ||
var expected = { | ||
"userId": 123 | ||
}; | ||
var mapper = map_factory_1["default"](); | ||
var actual = mapper | ||
.map("userId") | ||
.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
} | ||
}; | ||
var defaultGroup = { | ||
"Can map one field that exists to another": function (test) { | ||
@@ -18,3 +130,3 @@ var source = { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -32,3 +144,3 @@ "Throws if no source is provided": function (test) { | ||
}); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -48,3 +160,3 @@ "Can reuse the map for multiple transforms": function (test) { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -74,3 +186,3 @@ "Can reuse map for different transform": function (test) { | ||
test.deepEqual(actual2, expected2); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -88,3 +200,3 @@ "Can map from a source where source name is not formatted as a string": function (test) { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -105,3 +217,3 @@ "A field that doesn't exists on the source doesn't affect the resulting object": function (test) { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -113,3 +225,3 @@ "A null source field throws an error": function (test) { | ||
}); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -121,3 +233,3 @@ "A null target field throws an error": function (test) { | ||
}); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -132,3 +244,3 @@ "The source field is used if no target field is provided": function (test) { | ||
test.deepEqual(actual, source, "field was not mapped to new object"); | ||
test.done(); | ||
return test.done(); | ||
} | ||
@@ -154,3 +266,3 @@ }; | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -175,3 +287,3 @@ "Can map a field from source over an existing field on a destination object": function (test) { | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
return test.done(); | ||
} | ||
@@ -193,3 +305,3 @@ }; | ||
test.deepEqual(actual, expected, "field was not mapped to new object"); | ||
test.done(); | ||
return test.done(); | ||
} | ||
@@ -216,3 +328,3 @@ }; | ||
test.deepEqual(actual, expected, "field was not mapped to new object"); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -242,3 +354,3 @@ "Can extract multiple selections into a single transform while allowing simpler mappings to work": function (test) { | ||
test.deepEqual(actual, expected, "field was not mapped to new object"); | ||
test.done(); | ||
return test.done(); | ||
}, | ||
@@ -267,9 +379,11 @@ "If Multiple selections aren't mapped to a transform and error will occur": function (test) { | ||
}); | ||
test.done(); | ||
return test.done(); | ||
} | ||
}; | ||
exports.basicMapping = basicMappingGroup; | ||
exports.basicMapping = defaultGroup; | ||
exports.mapMethod = mapGroup; | ||
exports.sourceAndDestination = sourceAndDestinationGroup; | ||
exports.customFunctions = customFunctionsGroup; | ||
exports.multipleSelection = multipleSelectionGroup; | ||
exports.fluentChaining = fluentChainingGroup; | ||
//# sourceMappingURL=map-factory-test.js.map |
{ | ||
"name": "map-factory", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Object mapping tool", | ||
@@ -13,11 +13,14 @@ "main": "./dist/lib/index.js", | ||
"coveralls": "istanbul cover ./node_modules/nodeunit/bin/nodeunit ./dist/test && cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"lint": "tslint -c tslint.json -e 'dist/**/*' -e 'node_modules/**/*' -e 'Scripts/**/*' '**/*.ts'", | ||
"pretest": "npm run build", | ||
"test": "nodeunit ./dist/test/", | ||
"test": "npm run lint && nodeunit ./dist/test/", | ||
"pret": "npm run build", | ||
"t": "nodeunit ./dist/test/", | ||
"pretravis": "npm run build", | ||
"travis": "npm run coveralls", | ||
"premajor": "npm run build", | ||
"premajor": "npm run test", | ||
"major": "npm version major -m \"published to npm as v%s\" && git push --follow-tags && npm publish", | ||
"preminor": "npm run build", | ||
"preminor": "npm run test", | ||
"minor": "npm version minor -m \"published to npm as v%s\" && git push --follow-tags && npm publish", | ||
"prepatch": "npm run build", | ||
"prepatch": "npm run test", | ||
"patch": "npm version patch -m \"published to npm as v%s\" && git push --follow-tags && npm publish" | ||
@@ -54,2 +57,3 @@ }, | ||
"rimraf": "^2.5.3", | ||
"tslint": "^3.13.0", | ||
"typescript": "^1.8.10" | ||
@@ -56,0 +60,0 @@ }, |
190
README.md
@@ -5,7 +5,45 @@ # map-factory | ||
A simple utility to map data from an existing object to a new one. This is an alternative interface for the excellent [object-mapper](http://www.npmjs.com/object-mapper). | ||
[](https://www.npmjs.com/package/map-factory/) | ||
A simple utility that greatly simplifies mapping data from one shape to another. **map-factory** provides a fluent interface and supports deep references, custom transformations, and object merging. | ||
This is an alternative interface for the excellent [object-mapper](http://www.npmjs.com/object-mapper). | ||
See [Change Log](./CHANGELOG.md) for changes from previous versions. | ||
### Map a source field to the same object structure | ||
## Usage | ||
**map-factory** supports two similar interfaces. Which you use is up to you and your use case. | ||
The first is a fluent interface: | ||
```js | ||
const createMapper = require("map-factory"); | ||
const mapper = createMapper(); | ||
mapper | ||
.map("sourceField").to("source.field") | ||
.map("sourceId").to("source.id"); | ||
const result = map.execute(source); | ||
``` | ||
Alternatively you can you the slightly shorter version: | ||
```js | ||
const createMapper = require("map-factory"); | ||
const map = createMapper(); | ||
map("sourceField").to("source.field"); | ||
map("sourceId").to("source.id"); | ||
const result = map.execute(source); | ||
``` | ||
There is no functional difference between the two and they can be used interchangeably. | ||
## Map a source field to the same object structure | ||
Mapping is explicit so unmapped fields are discarded. | ||
@@ -38,3 +76,4 @@ | ||
### Map a source field to a different object structure | ||
## Map a source field to a different object structure | ||
Of course, we probably want a different structure for our target object. | ||
@@ -68,4 +107,5 @@ | ||
``` | ||
### Supports deep references for source and target objects | ||
## Supports deep references for source and target objects | ||
```js | ||
@@ -244,5 +284,6 @@ const source = { | ||
## Select from multiple sources at once | ||
### Select from multiple sources at once | ||
You can also provide an array or source fields and they can be extracted together. You must provide a transform for the target field. | ||
You can also provide an array of source fields and they can be extracted together if you provide a transform for the target field. | ||
```js | ||
@@ -280,1 +321,138 @@ const createMapper = require("map-factory"); | ||
``` | ||
## Common patterns | ||
### Dealing with multiple sources of data | ||
There are two ways to deal with multiple sources of data. | ||
- Combine your data in to a single object before mapping | ||
- Use multiple mappers and combine the objects as you go | ||
#### Combine data first | ||
This method is useful when you are retrieving all of your data at once. It involves taking your source data and appending it all onto a single object. | ||
The advantage of this method is that you can create a single transform mapping object which is used to map all of your data together and that you do not have to mutate your objects. | ||
We'd recommend this approach for most use cases. | ||
```js | ||
const createMapper = require("map-factory"); | ||
// assume the following inputs | ||
const post = { | ||
"body": "<p>Some Text</p>" | ||
}; | ||
const comments = { | ||
"list": ["not too bad", "pretty good", "awful"] | ||
}; | ||
const user = { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
}; | ||
// combine multiple objects into a single source object | ||
const source = { post, comments, user }; | ||
const map = createMapper(); | ||
map("post.body").to("blog.post.text"); | ||
map("comments.list").to("blog.post.comments"); | ||
map("comments.list[0]").to("blog.post.topComment"); | ||
map("user.id").to("blog.author.id"); | ||
map("user.name").to("blog.author.name"); | ||
map("user.email").to("blog.author.email"); | ||
const final = map.execute(source); | ||
console.log(final); | ||
/* | ||
{ | ||
"blog": | ||
{ | ||
"post": | ||
{ | ||
"text": "<p>Some Text</p>", | ||
"comments": ["not too bad", "pretty good", "awful"], | ||
"topComment": "not too bad" | ||
}, | ||
"author": { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
#### Merge objects with multiple mappers | ||
The other option is to decorate your existing data objects in a piece by piece fashion using the merge ability. Note that when using a named mapper like ```postMapper``` the code reads better when you use the explicit ```map()``` method. | ||
```js | ||
const createMapper = require("map-factory"); | ||
// assume the following inputs | ||
const post = { | ||
"body": "<p>Some Text</p>" | ||
}; | ||
const comments = { | ||
"list": ["not too bad", "pretty good", "awful"] | ||
}; | ||
const user = { | ||
"id": 123, | ||
"name": "John Doe", | ||
"email": "john.doe@nobody.com" | ||
}; | ||
const postMapper = createMapper(); | ||
const commentMapper = createMapper(); | ||
const authorMapper = createMapper(); | ||
postMapper | ||
.map("body").to("blog.post.text"); | ||
commentMapper | ||
.map("list").to("blog.post.comments") | ||
.map("list[0]").to("blog.post.topComment"); | ||
authorMapper | ||
.map("id").to("blog.author.id") | ||
.map("name").to("blog.author.name") | ||
.map("email").to("blog.author.email"); | ||
let result = postMapper.execute(post); | ||
result = commentMapper.execute(comments, result); | ||
result = authorMapper.execute(user, result); | ||
``` | ||
The above approach appears untidy when compared with combining the data into a single object but it is useful in situations where your mapping logic is distributed. | ||
For example, a mapper used within a class may build its map in the constructor and execute the mapper in a method. | ||
```js | ||
class BlogService { | ||
constructor(blogRepos) { | ||
this.blogRepos = blogRepos; | ||
// initialise mapper | ||
this.authorMapper = createMapper(); | ||
this.authorMapper.map("id").to("blog.author.id"); | ||
this.authorMapper.map("name").to("blog.author.name"); | ||
this.authorMapper.map("email").to("blog.author.email"); | ||
} | ||
// Here post is created somewhere else and we are extending it with user information | ||
decorateBlogPostWithAuthor(userId, post) { | ||
return this.blogRepos.getUser(userId) | ||
.then(user => this.authorMapper.execute(user, post)); | ||
} | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
65384
28
864
454
8