Dean Strydom
Dean Strydom

Reputation: 295

Custom rest api's for CouchDB?

I have been googling around trying to find examples or just a straight answer to my question. Is it possible to create/extend my own custom api endpoints for couchDB? Such as for example can I create a api call like http://127.0.0.1:5984/database/FillDatabase/... to fill the database with data?

If couchDB does indeed provide this functionality, then please provide me with a link perhaps to some tutorial/guide/example if possible. Thanks.

Upvotes: 2

Views: 816

Answers (2)

uminder
uminder

Reputation: 26150

The CouchDB bulk document API allows you to create and update multiple documents at the same time within a single HTTP request

With Angular's HttpClient class for example, this can be done as follows:

const baseURL = 'http://localhost:5984/';
let httpHeaders = new HttpHeaders();
httpHeaders = httpHeaders.set('Accept', 'application/json');
httpHeaders = httpHeaders.set('Content-type', 'application/json');
httpHeaders = httpHeaders.set('Authorization', 'Basic ' + btoa(username + ':' + password));
const httpOptions = { headers: httpHeaders, withCredentials: true };

this.httpClient.post<any>(baseURL + database + '/_bulk_docs',
      { docs: myDocuments }, httpOptions);

Upvotes: 3

tephyr
tephyr

Reputation: 1051

You may want the _rewrites feature, which allows any view to rewrite an incoming URL for that database.

However, for root-level APIs, and for ease of customization, it is considered better to impose a web engine in front of CouchDB (nginx, a node.js server, etc.). _rewrites is flexible enough to simplify the API for a specific db, but it's not intended to handle all incoming requests, and can quickly become unwieldy.

You would also be limited to the work that CouchDB can handle - for example, the FillDatabase custom API would need to conform to the bulk API.

Upvotes: 1

Related Questions