Skip to content

Commit

Permalink
Merge pull request #16 from fratzinger/master
Browse files Browse the repository at this point in the history
merge params deeply & ability to pass a function
  • Loading branch information
marshallswain authored Sep 30, 2020
2 parents 07e8026 + 389322d commit 4c89570
Show file tree
Hide file tree
Showing 7 changed files with 1,979 additions and 1,397 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pids
lib-cov

# Coverage directory used by tools like istanbul
.nyc_output
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
Expand Down
File renamed without changes.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const options = {
service: 'tags',
nameAs: 'tags',
keyHere: 'tagIds',
keyThere: '_id'
keyThere: '_id',
asArray: true, // by default
params: {} // by default
}
}

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

## Options for include

| **Option** | **Description** |
|------------|-----------------|
| `service` | The service to reference<br><br>**required**<br>**Type:** `{String}` |
| `nameAs` | The property to be assigned to on this entry<br><br>**required**<br>**Type:** `{String}` |
| `keyHere` | The primary or secondary key for this entry<br><br>**required**<br>**Type:** `{String}` |
| `keyThere` | The primary or secondary key for the referenced entry/entries<br><br>**required**<br>**Type:** `{String}` |
| `asArray` | Is the referenced item a single entry or an array of entries?<br><br>**optional - default:** `true`<br>**Type:** `{Boolean}`
| `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}` |

## Multiple Populates
```js
const { shallowPopulate } = require('feathers-shallow-populate')
Expand All @@ -47,13 +60,17 @@ const options = {
service: 'tags',
nameAs: 'tags',
keyHere: 'tagIds',
keyThere: '_id'
keyThere: '_id',
asArray: true,
params: {}
},
{
service: 'comments',
nameAs: 'comments',
keyHere: 'commentIds',
keyThere: '_id'
keyThere: '_id',
asArray: true,
params: {}
}
]
}
Expand Down Expand Up @@ -105,7 +122,8 @@ const options = {
nameAs: 'publisher',
keyHere: 'publisherId',
keyThere: 'id',
asArray: false
asArray: false,
params: {}
}
}

Expand Down
13 changes: 10 additions & 3 deletions lib/shallow-populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const assert = require('assert')
const getByDot = require('lodash/get')
const setByDot = require('lodash/set')
const isEqual = require('lodash/isEqual')
const isFunction = require('lodash/isFunction')
const merge = require('lodash/merge')
const defaults = {
include: undefined
}
Expand Down Expand Up @@ -104,15 +106,20 @@ module.exports = function (options) {
// }
// }

const paramSets = includes.map(i => {
const params = includes.map(i => {
const keyVals = dataMap[i.keyHere]
let keysHere = Object.keys(keyVals) || []
keysHere = keysHere.map(k => keyVals[k].key)
let params = { query: { [i.keyThere]: { $in: keysHere } }, paginate: false }

return { query: { [i.keyThere]: { $in: keysHere } } }
const providedParams = (isFunction(i.params)) ? i.params(params, context) : i.params

if (providedParams && params != providedParams) params = merge(params, providedParams)

return params
})

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

return Promise.all(requests)
.then(responses => {
Expand Down
Loading

0 comments on commit 4c89570

Please sign in to comment.