# loadMethods

Load and define methods from directory (recursively).

## Usage

**Use plugin and pass path to directory containing store modules in options (my-store in example below):**

```js
import { join } from 'path'
import loadMethods from 'backend-store/plugins/loadMethods'
// or const loadMethods = require('backend-store/plugins/loadMethods')

store.plugin(loadMethods, { path: join(__dirname, 'my-store') })
```

**In store directory create store modules**

Each module exports single function that takes one argument which is an object with "define" method.
Define methods using this method.

```js
// my-store/api/createPost.js
export default ({ define }) => {
  // define method
  define('createPost', async (payload, methodContext) => {
    // ...
  })
}
```

```js
// my-store/db/insertPost.js
export default ({ define }) => {
  // you can omit method name here - in such case file name (without extension) will be used instead as a method name
  define(async (payload, methodContext) => {
    // ...
  })
}
```

```js
// my-store/auth/authorizers.js
export default ({ define }) => {
  // you can define more than one method
  define('requireAdmin', async (payload, methodContext) => {
    // ...
  })

  define('requireEditor', async (payload, methodContext) => {
    // ...
  })
}
```

Plugin will load and define methods in the store.
According to example above following methods will be defined:

* "api/createPost"
* "db/insertPost"
* "auth/requireAdmin"
* "auth/requireEditor"

## Options

| Option          | Type       | Description
|-----------------|------------|--------------------------
| path            | `string`   | Path to a directory containing store modules to load
| [filter]        | `function` | Optional function to filter loaded files (see below for more details)

**filter**

By default loadMethods plugin loads all files with `.js` extension.
You can customise this by passing a function as a filter option.
This function is executed for each file in store directory - if it returns `true` then given file is loaded. Otherwise it is ignored.
Filter function is called with single argument which is an object with following properties:

| Property     | Example                                      | Description
|--------------|----------------------------------------------|-------------------------------------
| filePath     | /my-project/src/my-store/api/users/create.js | absolute file path
| fileName     | create.js                                    | file name with extension
| relativePath | api/users/create.js                          | file path relative to store directory
| moduleName   | create                                       | name of the file without extension
| namespace    | /api/users                                   | relativePath without file name

```js
// Example
import { join } from 'path'

store.use(loadMethods, {
  path: join(__dirname, 'my-store'),
  filter ({ fileName }) {
    // load all .js files that don't start with underscore
    return fileName.match(/\.js$/) && !fileName.match(/^_/)
  }
})
```
