@hookcompany/feathers-helpers

1.1.7 • Public • Published

Feathers Helpers

🦅 Helpers to simplify feathers configuration.

NPM version

Npm Downloads

Getting Started

You can see an example project on this repository: Example

Use npm:

npm install --save @hookcompany/feathers-helpers

Or use yarn:

yarn add @hookcompany/feathers-helpers

Step 1: Services Index

Initially, you have to understand Feathers Helpers has an especific archtecture, which is divided into platforms, in this case app mobile and dashboard.

services/index.js

import feathersHelpers from '@hookcompany/feathers-helpers';
import models from 'models';
import common from './common';
import appMobile from './app-mobile';
import dashboard from './dashboard';

export default app => {
  feathersHelpers(models, { basePath: 'base' })(app)
    .platform(appMobile)
    .platform(dashboard);
};

Step 2: Models

Feathers Helpers only works with feathers-mongoose. So, if you use mongoose and feathers-mongoose, you can create a model like this:

models/user.js

export default {
  model: {
    email: { type: String, unique: true },
    password: { type: String }
  },
  options: {
    timestamps: true
  }
};

And you have to export an object that has the name of models as object attributes.

models/index.js

import product from './product';
import user from './user';

export default { product, user };

The attributes names are importants, because the model will be a feathers-mongoose model service that works like a pure service with all feathers methods to manipulate data. And by default the service path is "/base/model-name" with pluralized model name using pluralize, but you can configure base prefix on options as you can see on step 1.

Step 3: Platform

To configure a platform you only have to specify a prefix to the platform and pass an array of services. The prefix will be on platform services paths.

services/app-mobile/index.js

import products from './products';
import users from './users';
import feeds from './feeds';

export default {
  basePath: '/app-mobile',
  services: [products, users, feeds]
};

Step 4: Service

To configure a service you have to pass a service class, hooks, path and options. About the options, if you pass true in customMethods option you will activate feathers-custom-methods, by default customMethods is false.

services/app-mobile/feeds/index.js

import hooks from './hooks';
import service from './class';

export default {
  service,
  hooks,
  path: '/feeds',
  options: {
    customMethods: true
  }
};

services/app-mobile/feeds/class.js

import { find, get, create, update, patch, remove } from '@hookcompany/feathers-custom-methods';

class Service {
  setup = app => {
    this.app = app;
  }

  @find
  foo = params => {
    return Promise.resolve(args);
  }

  @get
  bar = (id, params) => {
    return Promise.resolve(args);
  }
}

export default options => new Service(options);

services/app-mobile/feeds/hooks.js

import { logger } from 'hooks';

export default {
  before: {
    all: [logger()],
    foo: [logger()],
    bar: [logger()]
  },
  after: {
    all: [logger()],
    foo: [logger()],
    bar: [logger()]
  },
  error: {
    all: [logger()],
    foo: [logger()],
    bar: [logger()]
  }
};

If you don't activate custom methods all will work like feathers normally. And the index will be like this:

services/app-mobile/feeds/index.js

import hooks from './hooks';
import service from './class';

export default {
  service,
  hooks,
  path: '/feeds'
};

Another way to create a service is extending a base service, by the way a base service is a model service which was shown on step 2.

services/app-mobile/products/class.js

import { BaseService } from '@hookcompany/feathers-helpers';

class Service extends BaseService {
  constructor(options) {
    super(options);
  }
}

export default options => new Service(options);

In this situation you have to pass in options the base path of the extended base service, the methods you want to allow and if you want pagination or not.

services/app-mobile/products/index.js

import hooks from './hooks';
import service from './class';

export default {
  service,
  hooks,
  path: '/products',
  options: {
    base: 'base/products',
    methods: ['find', 'create'],
    paginate: true
  }
};

Step 5: Consuming

With custom methods:

localhost:4000/app-mobile/feeds/:customMethod

Without custom methods:

localhost:4000/app-mobile/feeds/

Package Sidebar

Install

npm i @hookcompany/feathers-helpers

Weekly Downloads

0

Version

1.1.7

License

MIT

Unpacked Size

18.7 kB

Total Files

12

Last publish

Collaborators

  • hileomsi
  • iagosilva96
  • rastelli
  • roganmelo