Custom rest api's for CouchDB?

北城余情 提交于 2020-01-05 05:00:50

问题


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.


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/56557243/custom-rest-apis-for-couchdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!