feathers-shallow-populate
Feathers Shallow Populate
The fastest FeathersJS hook for populating relational data.
Installation
npm install feathers-shallow-populate --save
Complete Example
Here's an example of using feathers-shallow-populate
.
const { shallowPopulate } = require('feathers-shallow-populate')
const options = {
include: {
service: 'tags',
nameAs: 'tags',
keyHere: 'tagIds',
keyThere: '_id'
}
}
app.service('posts').hooks({
after: {
all: shallowPopulate(options)
}
});
Multiple Populates
const { shallowPopulate } = require('feathers-shallow-populate')
const options = {
include: [
{
service: 'tags',
nameAs: 'tags',
keyHere: 'tagIds',
keyThere: '_id'
},
{
service: 'comments',
nameAs: 'comments',
keyHere: 'commentIds',
keyThere: '_id'
}
]
}
app.service('posts').hooks({
after: {
all: shallowPopulate(options)
}
});
// result.data
[
{
id: 1,
title: 'About Timothy',
tagIds: [1, 2]
tags: [
{
id: 1,
name: 'tag 1'
},
{
id: 2,
name: 'tag 2'
}
],
commentIds: [3, 5],
comments: [
{
id: 3,
title: 'My first comment'
},
{
id: 5,
title: 'Another comment'
}
]
}
]
As Object
const { shallowPopulate } = require('feathers-shallow-populate')
const options = {
include: {
service: 'users',
nameAs: 'publisher',
keyHere: 'publisherId',
keyThere: 'id',
asArray: false
}
}
app.service('posts').hooks({
after: {
all: shallowPopulate(options)
}
});
// result.data
[
{
id: 1,
title: 'About Timothy',
publisherId: 2,
publisher: {
id: 2,
name: 'Timothy'
}
}
]
This will go through all the hook.data/hook.result and will create a single query to lookup the tags, it will then populate them back onto the data.
License
Copyright (c) 2017
Licensed under the MIT license.