Skip to content

Commit 4c89570

Browse files
Merge pull request #16 from fratzinger/master
merge params deeply & ability to pass a function
2 parents 07e8026 + 389322d commit 4c89570

File tree

7 files changed

+1979
-1397
lines changed

7 files changed

+1979
-1397
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pids
1313
lib-cov
1414

1515
# Coverage directory used by tools like istanbul
16+
.nyc_output
1617
coverage
1718

1819
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
File renamed without changes.

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const options = {
2626
service: 'tags',
2727
nameAs: 'tags',
2828
keyHere: 'tagIds',
29-
keyThere: '_id'
29+
keyThere: '_id',
30+
asArray: true, // by default
31+
params: {} // by default
3032
}
3133
}
3234

@@ -37,6 +39,17 @@ app.service('posts').hooks({
3739
});
3840
```
3941

42+
## Options for include
43+
44+
| **Option** | **Description** |
45+
|------------|-----------------|
46+
| `service` | The service to reference<br><br>**required**<br>**Type:** `{String}` |
47+
| `nameAs` | The property to be assigned to on this entry<br><br>**required**<br>**Type:** `{String}` |
48+
| `keyHere` | The primary or secondary key for this entry<br><br>**required**<br>**Type:** `{String}` |
49+
| `keyThere` | The primary or secondary key for the referenced entry/entries<br><br>**required**<br>**Type:** `{String}` |
50+
| `asArray` | Is the referenced item a single entry or an array of entries?<br><br>**optional - default:** `true`<br>**Type:** `{Boolean}`
51+
| `params` | Additional params to be passed to the underlying service.<br>You can mutate the passed `params` object or return a newly created `params` object which gets merged deeply <br>Merged deeply after the params are generated internally.<br><quote>**ProTip:** You can use this for adding a '$select' property or passing authentication and user data from 'context' to 'params' to restrict accesss</quote><br><br>**optional - default:** `{}`<br>**Type:** `{Object | Function(params, context): undefined|params}` |
52+
4053
## Multiple Populates
4154
```js
4255
const { shallowPopulate } = require('feathers-shallow-populate')
@@ -47,13 +60,17 @@ const options = {
4760
service: 'tags',
4861
nameAs: 'tags',
4962
keyHere: 'tagIds',
50-
keyThere: '_id'
63+
keyThere: '_id',
64+
asArray: true,
65+
params: {}
5166
},
5267
{
5368
service: 'comments',
5469
nameAs: 'comments',
5570
keyHere: 'commentIds',
56-
keyThere: '_id'
71+
keyThere: '_id',
72+
asArray: true,
73+
params: {}
5774
}
5875
]
5976
}
@@ -105,7 +122,8 @@ const options = {
105122
nameAs: 'publisher',
106123
keyHere: 'publisherId',
107124
keyThere: 'id',
108-
asArray: false
125+
asArray: false,
126+
params: {}
109127
}
110128
}
111129

lib/shallow-populate.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const assert = require('assert')
22
const getByDot = require('lodash/get')
33
const setByDot = require('lodash/set')
44
const isEqual = require('lodash/isEqual')
5+
const isFunction = require('lodash/isFunction')
6+
const merge = require('lodash/merge')
57
const defaults = {
68
include: undefined
79
}
@@ -104,15 +106,20 @@ module.exports = function (options) {
104106
// }
105107
// }
106108

107-
const paramSets = includes.map(i => {
109+
const params = includes.map(i => {
108110
const keyVals = dataMap[i.keyHere]
109111
let keysHere = Object.keys(keyVals) || []
110112
keysHere = keysHere.map(k => keyVals[k].key)
113+
let params = { query: { [i.keyThere]: { $in: keysHere } }, paginate: false }
111114

112-
return { query: { [i.keyThere]: { $in: keysHere } } }
115+
const providedParams = (isFunction(i.params)) ? i.params(params, context) : i.params
116+
117+
if (providedParams && params != providedParams) params = merge(params, providedParams)
118+
119+
return params
113120
})
114121

115-
const requests = includes.map((i, index) => app.service(i.service).find(Object.assign({}, paramSets[index], { paginate: false }, i.params)))
122+
const requests = includes.map((include, index) => app.service(include.service).find(params[index]))
116123

117124
return Promise.all(requests)
118125
.then(responses => {

0 commit comments

Comments
 (0)