# server

The `server` is the [Hapi.Server](http://hapijs.com/api#new-serveroptions)
instance that gets passed to custom routes, transform scripts, services, etc.

See the full API that comes from HAPI at http://hapijs.com/api

## Methods

Sever methods are exposed by default. Plugins can expose custom methods
using [server.method](http://hapijs.com/api#servermethodname-method-options).

### server.methods.getCouchUrl

Returns URL to CouchDB, without credentials

```js
var couchUrl = server.methods.getCouchUrl()
```

### server.methods.getStore

Get store API for any database, e.g. raw-data or integrated-data.

**Note**: Kazana will implement JavaScript APIs for its core modules
[raw dat reports](https://github.com/eHealthAfrica/kazana-raw-data/issues/67),
[indicators](https://github.com/eHealthAfrica/kazana/issues/144),
[accounts](https://github.com/eHealthAfrica/kazana-account/issues/78) and
[entities](https://github.com/eHealthAfrica/kazana-entities/issues/15).

```js
server.methods.getStore(options)
```

<table>
  <thead>
    <tr>
      <th>Argument</th>
      <th>Type</th>
      <th>Description</th>
      <th>Required</th>
    </tr>
  </thead>
  <tr>
    <th align="left"><code>options.name</code></th>
    <td>String</td>
    <td>Database name</td>
    <td>Yes</td>
  </tr>
  <tr>
    <th align="left"><code>options.auth</code></th>
    <td><code>"admin"</code> or <a href="http://hapijs.com/api#request-object">Request Object</a></td>
    <td>
      When set to <code>"admin"</code>, the store API is authenticated as admin.
      When passing in a request objects in a route handler, the store API is
      authenticated as the currently signed in user, reading the session id
      from the <code>Authorization</code> header. Usually access for users is
      based on their <code>roles</code> and the respective database
      <a href="https://wiki.apache.org/couchdb/Security_Features_Overview#Authorization"><code>\_security</code> settings</a>.
    </td>
    <td>Yes</td>
  </tr>
</table>

Returns a [hoodie store API](https://github.com/hoodiehq/pouchdb-hoodie-api#api).

Admin example

```js
server.methods.getStore({
  name: 'raw-data',
  auth: 'admin'
}).on('add', function (report) {
  console.log('new report submitted:\n%s\nby: %s', report.name, report.createdBy.login)
})
```

User Request example

```js
{
  method: 'GET',
  path: '/my/custom/thing',
  handler: function (request, reply) {
    request.server.getStore({
      name: 'my-custom-db'
      auth: request
    }).findAll().then(reply).catch(reply)
  }
}
```
