# Custom controllers

From time to time you probably want to add some special behaviour to your mocks like filtering a list of resources, apply pagination, upload or download some files and so on. **Mockly** provides you a simple way to do it using [NestJS](https://nestjs.com/).

Before all, you should be aware that we are using NestJS under the hood to create the server that exposes all your mocked resources. We've taking advantage of some of the awesome features provided by this library like [controllers](https://docs.nestjs.com/controllers), [interceptors](https://docs.nestjs.com/interceptors) or [middlewares](https://docs.nestjs.com/middleware) to build the tool and all the versatility it has to offer.

### Creating a controller

{% hint style="info" %}
If you're familiar with NestJS there is nothing new here. Otherwise, we recommend you to give a quick read to the [controllers documentation](https://docs.nestjs.com/controllers) to be up to date and ready to start.
{% endhint %}

When **Mockly** starts it looks in the current working directory for files with this pattern: `*.custom-controller.ts`. It's supposed that this files holds at least one NestJS's controller, and as a recommendation, we pray you to keep it this way. As you can imagine, this files contains something like that:

{% code title="artists.custom-controller.ts" %}

```typescript
@Controller('artists')
export class ArtistCustomController {

    @Get()
    search (@Query('q') query: string) {
        // TODO Implement method logic...
    }
    
}
```

{% endcode %}

### Manipulating data

**Mockly** uses [PouchDB](https://pouchdb.com/), an in-memory CouchDB-like database, to store your resources and data and kept all the manipulations you do to them in a session. When you're creating a custom controller you'll be able to get the database for your resource and control it as you desire. You can also use the [built-in query system](https://pouchdb.com/guides/queries.html) or the [find API](https://pouchdb.com/guides/mango-queries.html) to create powerful queries to filter, sort or paginate your collections.

To get the database you need to inject the `DatabaseRegistry` service in your controller and then take the database you're interested in:

```typescript
import { DatabaseRegistry } from '@mockly/server';

@Controller('artists')
export class ArtistsCustomController {
    private readonly database: PouchDB.Database;
    
    constructor (readonly registry: DatabaseRegistry) {
        this.database = registry.get('artists');
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mockly.gitbook.io/docs/extending-mockly/custom-controllers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
