All Projects → Mattchewone → feathers-shallow-populate

Mattchewone / feathers-shallow-populate

Licence: MIT license
Feathersjs populate relational data

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to feathers-shallow-populate

feathers-casl
feathers.js + casl: hooks & channels
Stars: ✭ 25 (+19.05%)
Mutual labels:  feathersjs
scala-basic-skeleton
Starting point if you want to bootstrap a project in Scala
Stars: ✭ 16 (-23.81%)
Mutual labels:  hooks
use-elapsed-time
React hook to measure elapsed time using requestAnimationFrame
Stars: ✭ 42 (+100%)
Mutual labels:  hooks
angulareact
A way to seamlessly integrate React and AngularJS
Stars: ✭ 17 (-19.05%)
Mutual labels:  hooks
snake-design
🐍 a react component library based React hooks
Stars: ✭ 33 (+57.14%)
Mutual labels:  hooks
use-detect-print
A react hook to detect when a page is being printed
Stars: ✭ 55 (+161.9%)
Mutual labels:  hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-38.1%)
Mutual labels:  hooks
reactjs-player
基于 react hooks 的 video 播放组件,结构简单,代码简洁,扩展方便。
Stars: ✭ 32 (+52.38%)
Mutual labels:  hooks
react-use-redux
Alternative Redux bindings with upcoming React hooks
Stars: ✭ 31 (+47.62%)
Mutual labels:  hooks
transition-hook
☄️ An extremely light-weight react transition animation hook which is simpler and easier to use than react-transition-group
Stars: ✭ 250 (+1090.48%)
Mutual labels:  hooks
no-redux
⚛️ 🎣 Experimenting with using hooks and context instead of Redux
Stars: ✭ 79 (+276.19%)
Mutual labels:  hooks
statery
Surprise-Free State Management! Designed for React with functional components, but can also be used with other frameworks (or no framework at all.)
Stars: ✭ 28 (+33.33%)
Mutual labels:  hooks
react-supabase
React Hooks library for Supabase
Stars: ✭ 168 (+700%)
Mutual labels:  hooks
coconat
🍥 StarterKit Builder for rocket-speed App creation on 🚀 React 17 + 📙 Redux 4 + 🚠 Router 5 + 📪 Webpack 5 + 🎳 Babel 7 + 📜 TypeScript 4 + 🚔 Linters 23 + 🔥 HMR 3
Stars: ✭ 95 (+352.38%)
Mutual labels:  hooks
express
[MOVED] Feathers Express framework bindings and REST transport plugin
Stars: ✭ 15 (-28.57%)
Mutual labels:  feathersjs
serum-price-api
An easy to use API to know SPL Tokens price.
Stars: ✭ 23 (+9.52%)
Mutual labels:  feathersjs
react-use-observer
Performant react hooks for WebApi Observers, useResizeObserver, useInteractionObserver, useMutationObserver
Stars: ✭ 19 (-9.52%)
Mutual labels:  hooks
entangle
Global state management tool for react hooks inspired by RecoilJS and Jotai using proxies.
Stars: ✭ 26 (+23.81%)
Mutual labels:  hooks
react-hook-sticky
react hook sticky
Stars: ✭ 19 (-9.52%)
Mutual labels:  hooks
husky-elixir
🐶 Git hooks made easy
Stars: ✭ 47 (+123.81%)
Mutual labels:  hooks

feathers-shallow-populate

Build Status

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',
      asArray: true, // by default
      params: {} // by default
    },
    {
      service: 'tags',
      nameAs: 'tags',
      params: function(params, context) {
        return { 
          query: { 
            userId: this.userId,
            companyId: this.companyId
          } 
        }
      }
    }
  ]
}

app.service('posts').hooks({
  after: {
    all: shallowPopulate(options)
  }
});

Options for the hook

  • include (required) Can be one include object or an array of include objects. See table below
  • catchOnError (optional) Wether the hook continues populating, if an error occurs (e.g. because of missing authentication) or throws. Also can be set per include individually

Options per include

Option Description
service The service to reference

required
Type: String
nameAs The property to be assigned to on this entry

required
Type: String
keyHere The primary or secondary key for this entry

required if params is not complex (most of the time)
Type: String
keyThere The primary or secondary key for the referenced entry/entries

required if keyHere is defined
Type: String
asArray Is the referenced item a single entry or an array of entries?

optional - default: true
Type: Boolean
requestPerItem Decided wether your params object/function runs against each item individually or bundled. Most of the time you don't need this.

optional - default:
- false
(if keyHere and keyThere are defined)
- true (if keyHere and keyThere are not defined)
Type: String
catchOnError Wether the hook continues populating, if an error occurs (e.g. because of missing authentication) or throws. Also can be set on the prior options

optional - default: false
Type:: Boolean
params Additional params to be passed to the underlying service.
You can mutate the passed params object or return a newly created params object which gets merged deeply
Merged deeply after the params are generated internally.
ProTip #1: You can use this for adding a '$select' property or passing authentication and user data from 'context' to 'params' to restrict accesss
ProTip #2: If you don't define keyHere and keyThere or set requestPerItem to true the function has access to the this keyword being the individual item the request will be made for.
ProTip #3: You can skip a requestPerItem if it returns undefined.
ProTip #4: The hook whats for async functions!

optional - default: {}
Possible types:
- Object: will be merged with params - simple requests
- Function(params, context, { path, service }) => params: needs to return the params or a new one which gets merged deeply - more complex
- Function(params, context, { path, service }) => Promise<params>
- `[Object

Multiple Populates

const { shallowPopulate } = require('feathers-shallow-populate')

const options = {
  include: [
    {
      service: 'tags',
      nameAs: 'tags',
      keyHere: 'tagIds',
      keyThere: '_id',
      asArray: true,
      params: {}
    },
    {
      service: 'comments',
      nameAs: 'comments',
      keyHere: 'commentIds',
      keyThere: '_id',
      asArray: true,
      params: {}
    }
  ]
}

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,
    params: {}
  }
}

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) 2020

Licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].