npm package: @hookcompany/feathers-helpers
Description: 🦅 Helpers to simplify feathers configuration.
Author: Hook Company Team
Latest version: 1.1.7

# Feathers Helpers

## 🦅 Helpers to simplify feathers configuration.

[![NPM version](https://badge.fury.io/js/%40hookcompany%2Ffeathers-helpers.png)](http://badge.fury.io/js/%40hookcompany%2Ffeathers-helpers)

[![Npm Downloads](https://nodei.co/npm/@hookcompany%2Ffeathers-helpers.png?downloads=true)](https://nodei.co/npm/@hookcompany%2Ffeathers-helpers.png?downloads=true)

## Getting Started

You can see an example project on this repository: 
[Example](https://github.com/hookcompany/feathers-boilerplate)

Use npm:

```sh
npm install --save @hookcompany/feathers-helpers
```

Or use yarn:

```sh
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

```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

```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

```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](https://github.com/blakeembrey/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

```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](https://github.com/hookcompany/feathers-custom-methods), by default customMethods is false.

#### services/app-mobile/feeds/index.js

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

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

#### services/app-mobile/feeds/class.js

```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

```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

```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

```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

```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/