{"name":"nano","version":"11.0.5","keywords":["couchdb","data","request","json","nosql","micro","nano","database"],"author":{"url":"http://couchdb.apache.org","name":"Apache CouchDB","email":"dev@couchdb.apache.org"},"license":"Apache-2.0","_id":"nano@11.0.5","maintainers":[{"name":"jan","email":"jan@apache.org"},{"name":"garrensmith","email":"garren.smith@gmail.com"},{"name":"glynnbird","email":"glynn.bird@gmail.com"},{"name":"jo","email":"schmidt@netzmerk.com"}],"homepage":"http://github.com/apache/couchdb-nano","bugs":{"url":"https://github.com/apache/couchdb-nano/issues"},"dist":{"shasum":"4a6e5fe3e6b7824e3fdd75b6b51fa9ae08680dba","tarball":"http://registry-npm.ywwl.com/nano/download/nano-11.0.5.tgz","fileCount":12,"integrity":"sha512-yLYmFH7S33OWYPAG12wNqMR6l9hC3qOWLbr7hZPqKBVKO8BYhAaLlWENeJEpPY8Rm12lGvzKWBp8LGEgWhEqmw==","signatures":[{"sig":"MEUCIQDipNipbiz0yDJ1Np0D1YcMZymWVTLCah5JCHsEgezNEAIgWcsx9u44l/jhwHKk4jVfBS59sVMQvSVwFyteO2R3D5M=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":177460,"size":42838,"noattachment":false,"key":"/nano/-/nano-11.0.5.tgz"},"main":"./lib/nano.js","types":"./lib/nano.d.ts","engines":{"node":">=20.0"},"gitHead":"d268038e28d3816d2fbf29e1706eb9180945511d","scripts":{"test":"tsc lib/nano.d.ts && node --test ./test/*.test.js"},"_npmUser":{"name":"glynnbird","email":"glynn.bird@gmail.com"},"pre-commit":["test"],"repository":{"url":"git+ssh://git@github.com/apache/couchdb-nano.git","type":"git"},"_npmVersion":"11.6.2","description":"The official CouchDB client for Node.js","directories":{},"_nodeVersion":"22.16.0","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"undici":"^7.24.6","typescript":"^6.0.2","@types/node":"^25.5.0"},"_npmOperationalInternal":{"tmp":"tmp/nano_11.0.5_1775035269264_0.07167150238752651","host":"s3://npm-registry-packages-npm-production"},"readme":"[![NPM](http://img.shields.io/npm/v/nano.svg?style=flat-square)](https://www.npmjs.com/package/nano)\n\n# Nano\n\nOffical [Apache CouchDB](https://couchdb.apache.org/) library for [Node.js](https://nodejs.org/).\n\n> Note: Nano >=11.0.0 is a **breaking change for Node.js versions 16 and older**. Nano 11 uses Node.js's built-in \"fetch\" HTTP client but this is only available in Node.js versions 18 or later. If you are using Node 16 or older then continue using Nano 10.\n> See our [migration guide](migration_guide_v10_to_v11.md) for moving from Nano 10 to Nano 11.\n\nFeatures:\n\n* **Minimalistic** - There is only a minimum of abstraction between you and\n  CouchDB.\n* **Pipes** - Proxy requests from CouchDB directly to your end user. ( `...AsStream` functions only)\n* **Promises** - The vast majority of library calls return native Promises.\n* **TypeScript** - Detailed TypeScript definitions are built in.\n* **Errors** - Errors are proxied directly from CouchDB: if you know CouchDB\n  you already know `nano`.\n\n## Installation\n\n1. Install [npm][1]\n2. `npm install nano`\n\nor save `nano` as a dependency of your project with\n\n    npm install --save nano\n\nNote the minimum required version of Node.js is 10.\n\n## Table of contents\n\n- [Getting started](#getting-started)\n- [Tutorials & screencasts](#tutorials-examples-in-the-wild--screencasts)\n- [Configuration](#configuration)\n- [Database functions](#database-functions)\n  - [nano.db.create(name)](#nanodbcreatename)\n  - [nano.db.get(name)](#nanodbgetname)\n  - [nano.db.destroy(name)](#nanodbdestroyname)\n  - [nano.db.list()](#nanodblist)\n  - [nano.db.listAsStream()](#nanodblistasstream)\n  - [nano.db.compact(name, [designname])](#nanodbcompactname-designname)\n  - [nano.db.replicate(source, target, [opts])](#nanodbreplicatesource-target-opts)\n  - [nano.db.replication.enable(source, target, [opts])](#nanodbreplicationenablesource-target-opts)\n  - [nano.db.replication.query(id, [opts])](#nanodbreplicationenablesource-target-opts)\n  - [nano.db.replication.disable(id, [opts])](#nanodbreplicationdisableid-opts)\n  - [nano.db.changes(name, [params])](#nanodbchangesname-params)\n  - [nano.db.changesAsStream(name, [params])](#nanodbchangesasstreamname-params)\n  - [nano.db.info()](#nanodbinfo)\n  - [nano.use(name)](#nanousename)\n  - [nano.request(opts)](#nanorequestopts)\n  - [nano.config](#nanoconfig)\n  - [nano.updates([params])](#nanoupdatesparams)\n  - [nano.info()](#nanoinfo)\n\n- [Document functions](#document-functions)\n  - [db.insert(doc, [params])](#dbinsertdoc-params)\n  - [db.destroy(docname, rev)](#dbdestroydocname-rev)\n  - [db.get(docname, [params])](#dbgetdocname-params)\n  - [db.head(docname)](#dbheaddocname)\n  - [db.bulk(docs, [params])](#dbbulkdocs-params)\n  - [db.list([params])](#dblistparams)\n  - [db.listAsStream([params])](#dblistasstreamparams)\n  - [db.fetch(docnames, [params])](#dbfetchdocnames-params)\n  - [db.fetchRevs(docnames, [params])](#dbfetchrevsdocnames-params)\n  - [db.createIndex(indexDef)](#dbcreateindexindexdef)\n  - [db.changesReader](#reading-changes-feed)\n- [Partitioned database functions](#partition-functions)\n  - [db.partitionInfo(partitionKey)](#dbpartitioninfopartitionkey))\n  - [db.partitionedList(partitionKey, [params])](#dbpartitionedlistpartitionkey-params)\n  - [db.partitionedListAsStream(partitionKey, [params])](#dbpartitionedlistasstreampartitionkey-params)\n  - [db.partitionedFind(partitionKey, query)](#dbpartitionedfindpartitionkey-query-params)\n  - [db.partitionedFindAsStream(partitionKey, query)](#dbpartitionedfindasstreampartitionkey-query)\n  - [db.partitionedSearch(partitionKey, designName, searchName, params)](#dbpartitionedsearchpartitioney-designname-searchname-params)\n  - [db.partitionedSearchAsStream(partitionKey, designName, searchName, params)](#dbpartitionedsearchasstreampartitionkey-designName-searchName-params)\n  - [db.partitionedView(partitionKey, designName, viewName, [params])](#dbpartitionediewpartitionkey-designname-viewname-params)\n  - [db.partitionedViewAsStream(partitionKey, designName, viewName, [params])](#dbpartitionediewasstreampartitionkey-designname-viewname-params)\n- [Multipart functions](#multipart-functions)\n  - [db.multipart.insert(doc, attachments, [params])](#dbmultipartinsertdoc-attachments-params)\n  - [db.multipart.get(docname, [params])](#dbmultipartgetdocname-params)\n- [Attachments functions](#attachments-functions)\n  - [db.attachment.insert(docname, attname, att, contenttype, [params])](#dbattachmentinsertdocname-attname-att-contenttype-params)\n  - [db.attachment.insertAsStream(docname, attname, att, contenttype, [params])](#dbattachmentinsertasstreamdocname-attname-att-contenttype-params)\n  - [db.attachment.get(docname, attname, [params])](#dbattachmentgetdocname-attname-params)\n  - [db.attachment.getAsStream(docname, attname, [params])](#dbattachmentgetasstreamdocname-attname-params)\n  - [db.attachment.destroy(docname, attname, [params])](#dbattachmentdestroydocname-attname-params)\n- [Views and design functions](#views-and-design-functions)\n  - [db.view(designname, viewname, [params])](#dbviewdesignname-viewname-params)\n  - [db.viewAsStream(designname, viewname, [params])](#dbviewasstreamdesignname-viewname-params)\n  - [db.viewWithList(designname, viewname, listname, [params])](#dbviewwithlistdesignname-viewname-params)\n  - [db.viewWithListAsStream(designname__viewname, listname, [params])](#dbviewwithlistasstreamdesignname-viewname-params)\n  - [db.show(designname, showname, doc_id, [params])](#dbshowdesignname-showname-doc_id-params)\n  - [db.atomic(designname, updatename, docname, [body])](#dbatomicdesignname-updatename-docname-body)\n  - [db.search(designname, viewname, params)](#dbsearchdesignname-searchname-params)\n  - [db.searchAsStream(designname, viewname, params)](#dbsearchasstreamdesignname-searchname-params)\n  - [db.find(selector)](#dbfindselector)\n  - [db.findAsStream(selector)](#dbfindasstreamselector)\n- [Using cookie authentication](#using-cookie-authentication)\n- [Advanced features](#advanced-features)\n  - [getting uuids](#getting-uuids)\n  - [extending nano](#extending-nano)\n  - [pipes](#pipes)\n- [Tests](#tests)\n- [Release](#release)\n\n## Getting started\n\nTo use `nano` you need to connect it to your CouchDB install, to do that:\n\n```js\nconst nano = require('nano')('http://127.0.0.1:5984');\n```\n\n> Note: Supplying authentication credentials in the URL e.g. `http://admin:mypassword@localhost:5984` is deprecated. Use `nano.auth` instead.\n\nTo create a new database:\n\n```js\nnano.db.create('alice');\n```\n\nand to use an existing database:\n\n```js\nconst alice = nano.db.use('alice');\n```\n\nUnder-the-hood, calls like `nano.db.create` are making HTTP API calls to the CouchDB service. Such operations are *asynchronous*. There are two ways to receive the asynchronous data back from the library\n\n1) Promises\n\n```js\nnano.db.create('alice').then((data) => {\n  // success - response is in 'data'\n}).catch((err) => {\n  // failure - error information is in 'err'\n})\n```\n\n2) or in the async/await style:\n\n```js\ntry {\n  const response = await nano.db.create('alice')\n  // succeeded\n  console.log(response)\n} catch (e) {\n  // failed\n  console.error(e)\n}\n```\n\nThe documentation will follow the *async/await* style.\n\n------------------\n\n\nA simple but complete example in the [async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) style:\n\n```js\nasync function asyncCall() {\n  await nano.db.destroy('alice')\n  await nano.db.create('alice')\n  const alice = nano.use('alice')\n  const response = await alice.insert({ happy: true }, 'rabbit')\n  return response\n}\nasyncCall()\n```\n\nRunning this example will produce:\n\n```\nyou have inserted a document with an _id of rabbit.\n{ ok: true,\n  id: 'rabbit',\n  rev: '1-6e4cb465d49c0368ac3946506d26335d' }\n```\n\nYou can also see your document in futon (http://127.0.0.1:5984/_utils).\n\n## Configuration\n\nConfiguring nano to use your database server is as simple as:\n\n```js\nconst nano = require('nano')('http://127.0.0.1:5984')\nconst db = nano.use('foo');\n```\n\nIf you don't need to instrument database objects you can simply:\n\n```js\n// nano parses the URL and knows this is a database\nconst db = require('nano')('http://127.0.0.1:5984/foo');\n```\n\nYou can tell nano to not parse the URL (maybe the server is behind a proxy, is accessed through a rewrite rule or other):\n\n```js\n// nano does not parse the URL and return the server api\n// \"http://127.0.0.1:5984/prefix\" is the CouchDB server root\nconst couch = require('nano')(\n  { \n    url : \"http://127.0.0.1:5984/prefix\"\n    parseUrl : false\n  });\nconst db = couch.use('foo');\n```\n\n### Pool size and open sockets\n\nTo specify the number of connections, timeouts and pool size, supply an `agentOptions` object when starting up Nano. \n\n```js\nconst agentOptions = {\n  bodyTimeout: 30000,\n  headersTimeout: 30000,\n  keepAliveMaxTimeout: 600000,\n  keepAliveTimeout: 30000,\n  keepAliveTimeoutThreshold: 1000,\n  maxHeaderSize: 16384,\n  maxResponseSize: -1,\n  pipelining: 6,\n  connect: { \n    timeout: 10000\n  },\n  strictContentLength: true,\n  connections: null,\n  maxRedirections: 0\n}\nconst undici = require('undici')\nconst undiciOptions = new undici.Agent(agentOptions)\nconst nano = Nano({ url: 'http://127.0.0.1:5984', undiciOptions })\n```\n\nThe meanings of the agentOptions attributes is described [here](https://undici.nodejs.org/#/docs/api/Agent?id=new-undiciagentoptions), [here](https://undici.nodejs.org/#/docs/api/Pool?id=parameter-pooloptions) and [here](https://undici.nodejs.org/#/docs/api/Client?id=parameter-clientoptions)\n\nYou may also supply a pre-existing `undici.Agent` e.g.\n\n```js\nconst agent = new undici.Agent({bodyTimeout: 30000 })\nconst nano = Nano({ url: 'http://127.0.0.1:5984', agentOptions: agent })\n```\n\n> Note `requestDefaults` is no longer supported.\n\n## Custom headers\n\nTo supply customer headers with each request, supply a headers object when starting up Nano:\n\n\n```js\nconst couch = require('nano')(\n  { \n    url : \"http://127.0.0.1:5984/prefix\"\n    headers: {\n      mycustomheader: '42'\n    }\n  });\n```\n\n## TypeScript\n\nThere is a full TypeScript definition included in the the *nano* package. Your TypeScript editor will show you hints as you write your code with the *nano* library with your own custom classes:\n\n```ts\nimport * as Nano  from 'nano'\n\nlet n = Nano('http://USERNAME:PASSWORD@127.0.0.1:5984')\nlet db = n.db.use('people')\n\ninterface iPerson extends Nano.MaybeDocument {\n  name: string,\n  dob: string\n}\n\nclass Person implements iPerson {\n  _id: string\n  _rev: string\n  name: string\n  dob: string\n\n  constructor(name: string, dob: string) {\n    this._id = undefined\n    this._rev = undefined\n    this.name = name\n    this.dob = dob\n  }\n\n  processAPIResponse(response: Nano.DocumentInsertResponse) {\n    if (response.ok === true) {\n      this._id = response.id\n      this._rev = response.rev\n    }\n  }\n}\n\nlet p = new Person('Bob', '2015-02-04')\ndb.insert(p).then((response) => {\n  p.processAPIResponse(response)\n  console.log(p)\n})\n```\n\n## Database functions\n\n### nano.db.create(name, [opts])\n\nCreates a CouchDB database with the given `name`, with options `opts`.\n\n```js\nawait nano.db.create('alice', { n: 3 })\n```\n\n### nano.db.get(name)\n\nGet information about the database `name`:\n\n```js\nconst info = await nano.db.get('alice')\n```\n\n### nano.db.destroy(name)\n\nDestroys the database `name`:\n\n```js\nawait nano.db.destroy('alice')\n```\n\n### nano.db.list()\n\nLists all the CouchDB databases:\n\n```js\nconst dblist = await nano.db.list()\n```\n\n### nano.db.listAsStream()\n\nLists all the CouchDB databases as a stream:\n\n```js\nnano.db.listAsStream()\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout);\n```\n\n### nano.db.compact(name, [designname])\n\nCompacts `name`, if `designname` is specified also compacts its views.\n\n### nano.db.replicate(source, target, [opts])\n\nReplicates `source` to `target` with options `opts`. The `target`database\nhas to exist, add `create_target:true` to `opts` to create it prior to\nreplication:\n\n```js\nconst response = await nano.db.replicate('alice',\n                  'http://admin:password@otherhost.com:5984/alice',\n                  { create_target:true })\n```\n\n### nano.db.replication.enable(source, target, [opts])\n\nEnables replication using the new CouchDB api from `source` to `target`\nwith options `opts`. `target` has to exist, add `create_target:true` to\n`opts` to create it prior to replication. Replication will survive server restarts.\n\n```js\nconst response = await nano.db.replication.enable('alice',\n                  'http://admin:password@otherhost.com:5984/alice',\n                  { create_target:true })\n```\n\n### nano.db.replication.query(id, [opts])\n\nQueries the state of replication using the new CouchDB API. The `id` comes from the response\ngiven by the call to `replication.enable`:\n\n```js\nconst r = await nano.db.replication.enable('alice',\n                  'http://admin:password@otherhost.com:5984/alice',\n                   { create_target:true })\nconst q = await nano.db.replication.query(r.id)\n```\n\n### nano.db.replication.disable(id, [opts])\n\nDisables replication using the new CouchDB API. The `id` comes from the response given\nby the call to `replication.enable`:\n\n```js\nconst r = await nano.db.replication.enable('alice',\n                   'http://admin:password@otherhost.com:5984/alice',\n                   { create_target:true })\nawait nano.db.replication.disable(r.id);\n```\n\n### nano.db.changes(name, [params])\n\nAsks for the changes feed of `name`, `params` contains additions\nto the query string.\n\n```js\nconst c = await nano.db.changes('alice')\n```\n\n### nano.db.changesAsStream(name, [params])\n\nSame as `nano.db.changes` but returns a stream.\n\n```js\nnano.db.changes('alice').pipe(process.stdout);\n```\n\n### nano.db.info()\n\nGets database information:\n\n```js\nconst info = await nano.db.info()\n```\n\n### nano.use(name)\n\nReturns a database object that allows you to perform operations against that database:\n\n```js\nconst alice = nano.use('alice');\nawait alice.insert({ happy: true }, 'rabbit')\n```\n\nThe database object can be used to access the [Document Functions](#document-functions).\n\n### nano.db.use(name)\n\nAlias for `nano.use`\n\n### nano.db.scope(name)\n\nAlias for `nano.use`\n\n### nano.scope(name)\n\nAlias for `nano.use`\n\n### nano.request(opts)\n\nMakes a custom request to CouchDB. This can be used to create your own HTTP request to the CouchDB\nserver, to perform operations where there is no `nano` function that encapsulates it. The available `opts` are:\n\n* `opts.db` – the database name\n* `opts.method` – the http method, defaults to `get`\n* `opts.path` – the full path of the request, overrides `opts.doc` and\n  `opts.att`\n* `opts.doc` – the document name\n* `opts.att` – the attachment name\n* `opts.qs` – query string parameters, appended after any existing `opts.path`, `opts.doc`, or `opts.att`\n* `opts.content_type` – the content type of the request, default to `json`\n* `opts.headers` – additional http headers, overrides existing ones\n* `opts.body` – the document or attachment body\n* `opts.encoding` – the encoding for attachments\n* `opts.multipart` – array of objects for multipart request\n* `opts.stream` - if `true`, a `request` object is returned. Default `false` and a Promise is returned.\n\n### nano.relax(opts)\n\nAlias for `nano.request`\n\n### nano.config\n\nAn object containing the `nano` configurations, possible keys are:\n\n* `url` - the CouchDB URL\n* `db` - the database name\n\n### nano.updates([params])\n\nListen to db updates, the available `params` are:\n\n* `params.feed` – Type of feed. Can be one of\n * `longpoll`: Closes the connection after the first event.\n * `continuous`: Send a line of JSON per event. Keeps the socket open until timeout.\n * `eventsource`: Like, continuous, but sends the events in EventSource format.\n* `params.timeout` – Number of seconds until CouchDB closes the connection. Default is 60.\n* `params.heartbeat` – Whether CouchDB will send a newline character (\\n) on timeout. Default is true.\n\n### nano.info()\n\nFetch information about the CouchDB cluster:\n\n```js\nconst info = await nano.info()\n```\n\nThe response is an object with [CouchDB cluster information](https://docs.couchdb.org/en/stable/intro/api.html#server).\n\n## Document functions\n\n### db.insert(doc, [params])\n\nInserts `doc` in the database with optional `params`. If params is a string, it's assumed it is the intended document `_id`. If params is an object, it's passed as query string parameters and `docName` is checked for defining the document `_id`:\n\n```js\nconst alice = nano.use('alice');\nconst response = await alice.insert({ happy: true }, 'rabbit')\n```\n\nThe `insert` function can also be used with the method signature `db.insert(doc,)`, where the `doc` contains the `_id` field e.g.\n\n```js\nconst alice = nano.use('alice')\nconst response = await alice.insert({ _id: 'myid', happy: true })\n```\n\nand also used to update an existing document, by including the `_rev` token in the document being saved:\n\n```js\nconst alice = nano.use('alice')\nconst response = await alice.insert({ _id: 'myid', _rev: '1-23202479633c2b380f79507a776743d5', happy: false })\n```\n\n### db.destroy(docname, rev)\n\nRemoves a document from CouchDB whose `_id` is `docname` and whose revision (`_rev`) is `rev`:\n\n```js\nconst response = await alice.destroy('rabbit', '3-66c01cdf99e84c83a9b3fe65b88db8c0')\n```\n\n### db.get(docname, [params])\n\nGets a document from CouchDB whose `_id` is `docname`:\n\n```js\nconst doc = await alice.get('rabbit')\n```\n\nor with optional [query string `params`](https://docs.couchdb.org/en/stable/api/document/common.html#get--db-docid):\n\n```js\nconst doc = await alice.get('rabbit', { revs_info: true })\n```\n\nIf you pass `attachments=true`, the `doc._attachments.attachmentNameN.data` fields will contain the \n[base-64 encoded attachments](https://docs.couchdb.org/en/stable/json-structure.html#document-with-attachments).\nOr, you can use [`db.multipart.get`](https://github.com/DougReeder/couchdb-nano#dbmultipartgetdocname-params)\nand parse the returned buffer to get the document and attachments.\n\nSee the [attachments methods](https://github.com/apache/couchdb-nano#attachments-functions) to retrieve\n*just* an attachment.\n\n### db.head(docname)\n\nSame as `get` but lightweight version that returns headers only:\n\n```js\nconst headers = await alice.head('rabbit')\n```\n\n### db.bulk(docs, [params])\n\nBulk operations(update/delete/insert) on the database, refer to the\n[CouchDB doc](https://docs.couchdb.org/en/stable/api/database/bulk-api.html#post--db-_bulk_docs) e.g:\n\n```js\nconst documents = [\n  { a:1, b:2 },\n  { _id: 'tiger', striped: true}\n];\nconst response = await alice.bulk({ docs: documents })\n```\n\n### db.list([params])\n\nList all the docs in the database .\n\n```js\nconst doclist = await alice.list().then((body)=>{\n    body.rows.forEach((doc) => {\n        console.log(doc);\n    })\n});\n```\n\nor with optional query string additions `params`:\n\n```js\nconst doclist = await alice.list({include_docs: true})\n```\n\n### db.listAsStream([params])\n\nList all the docs in the database as a stream.\n\n```js\nalice.listAsStream()\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n```\n\n### db.fetch(docnames, [params])\n\nBulk fetch of the database documents, `docnames` are specified as per\n[CouchDB doc](https://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs).\nadditional query string `params` can be specified, `include_docs` is always set\nto `true`.\n\n```js\nconst keys = ['tiger', 'zebra', 'donkey'];\nconst datat = await alice.fetch({keys: keys})\n```\n\n### db.fetchRevs(docnames, [params])\n\n** changed in version 6 **\n\nBulk fetch of the revisions of the database documents, `docnames` are specified as per\n[CouchDB doc](https://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs).\nadditional query string `params` can be specified, this is the same method as fetch but\n `include_docs` is not automatically set to `true`.\n\n### db.createIndex(indexDef)\n\nCreate index on database fields, as specified in\n[CouchDB doc](https://docs.couchdb.org/en/latest/api/database/find.html#db-index).\n\n```js\nconst indexDef = {\n  index: { fields: ['foo'] },\n  name: 'fooindex'\n};\nconst response = await alice.createIndex(indexDef)\n```\n\n## Reading Changes Feed\n\nNano provides a low-level API for making calls to CouchDB's changes feed, or if you want a \nreliable, resumable changes feed follower, then you need the `changesReader`.\n\nThere are three ways to start listening to the changes feed:\n\n1. `changesReader.start()` - to listen to changes indefinitely by repeated \"long poll\" requests. This mode continues to poll for changes until `changesReader.stop()` is called, at which point any active long poll will be canceled.\n2. `changesReader.get()` - to listen to changes until the end of the changes feed is reached, by repeated \"long poll\" requests. Once a response with zero changes is received, the 'end' event will indicate the end of the changes and polling will stop.\n3. `changesReader.spool()` - listen to changes in one long HTTP request. (as opposed to repeated round trips) - spool is faster but less reliable.\n\n> Note: for `.get()` & `.start()`, the sequence of API calls can be paused by calling `changesReader.pause()` and resumed by calling `changesReader.resume()`.\n\nSet up your database connection and then choose `changesReader.start()` to listen to that database's changes:\n\n```js\nconst db = nano.db.use('mydb')\ndb.changesReader.start()\n  .on('change', (change) => { console.log(change) })\n  .on('batch', (b) => {\n    console.log('a batch of', b.length, 'changes has arrived');\n  }).on('seq', (s) => {\n    console.log('sequence token', s);\n  }).on('error', (e) => {\n    console.error('error', e);\n  })\n```\n\n> Note: you probably want to monitor *either* the `change` or `batch` event, not both.\n\nIf you want `changesReader` to hold off making the next `_changes` API call until you are ready, then supply `wait:true` in the options to `get`/`start`. The next request will only fire when you call `changesReader.resume()`:\n\n```js\ndb.changesReader.get({wait: true})\n  .on('batch', (b) => {\n    console.log('a batch of', b.length, 'changes has arrived');\n    // do some asynchronous work here and call \"changesReader.resume()\"\n    // when you're ready for the next API call to be dispatched.\n    // In this case, wait 5s before the next changes feed request.\n    setTimeout( () => {\n      db.changesReader.resume()\n    }, 5000)\n  }).on('end', () => {\n    console.log('changes feed monitoring has stopped');\n  });\n```\n\nYou may supply a number of options when you start to listen to the changes feed:\n\n| Parameter | Description                                                                                                                                                                             | Default value | e.g.                            |   |\n|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|---------------------------------|---|\n| batchSize | The maximum number of changes to ask CouchDB for per HTTP request. This is the maximum number of changes you will receive in a `batch` event. | 100           | 500                             |   |\n| since     | The position in the changes feed to start from where `0` means the beginning of time, `now` means the current position or a string token indicates a fixed position in the changes feed | now           | 390768-g1AAAAGveJzLYWBgYMlgTmGQ |   |\n| includeDocs | Whether to include document bodies or not | false | e.g. true |\n| wait | For `get`/`start` mode, automatically pause the changes reader after each request. When the the user calls `resume()`, the changes reader will resume.  | false | e.g. true |\n| fastChanges | Adds a seq_interval parameter to fetch changes more quickly | false           | true                             |   |\n| selector | Filters the changes feed with the supplied Mango selector |    null       | {\"name\":\"fred}                            |   |\n| timeout | The number of milliseconds a changes feed request waits for data| 60000         | 10000    |\n\nThe events it emits are as follows:s\n\n| Event  | Description                                                                                                                                                               | Data                       |   |\n|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------|---|\n| change | Each detected change is emitted individually. Only available in `get`/`start` modes.                                                                                                                          | A change object            |   |\n| batch  | Each batch of changes is emitted in bulk in quantities up to `batchSize`.                                                                                                                              | An array of change objects |   |\n| seq    | Each new sequence token (per HTTP request). This token can be passed into `ChangesReader` as the `since` parameter to resume changes feed consumption from a known point. Only available in `get`/`start` modes. | String                     |   |\n| error  | On a fatal error, a descriptive object is returned and change consumption stops.                                                                                         | Error object               |   |\n| end    | Emitted when the end of the changes feed is reached. `ChangesReader.get()` mode only,                                                                                     | Nothing                    |   |\n\nThe *ChangesReader* library will handle many temporal errors such as network connectivity, service capacity limits and malformed data but it will emit an `error` event and exit when fed incorrect authentication credentials or an invalid `since` token.\n\nThe `change` event delivers a change object that looks like this:\n\n```js\n{\n\t\"seq\": \"8-g1AAAAYIeJyt1M9NwzAUBnALKiFOdAO4gpRix3X\",\n\t\"id\": \"2451be085772a9e588c26fb668e1cc52\",\n\t\"changes\": [{\n\t\t\"rev\": \"4-061b768b6c0b6efe1bad425067986587\"\n\t}],\n\t\"doc\": {\n\t\t\"_id\": \"2451be085772a9e588c26fb668e1cc52\",\n\t\t\"_rev\": \"4-061b768b6c0b6efe1bad425067986587\",\n\t\t\"a\": 3\n\t}\n}\n```\n\nN.B\n\n- `doc` is only present if `includeDocs:true` is supplied\n- `seq` is not present for every change\n\nThe `id` is the unique identifier of the document that changed and the `changes` array contains the document revision tokens that were written to the database.\n\nThe `batch` event delivers an array of change objects.\n\n## Partition Functions\n\nFunctions related to [partitioned databases](https://docs.couchdb.org/en/latest/partitioned-dbs/index.html).\n\nCreate a partitioned database by passing `{ partitioned: true }` to `db.create`:\n\n```js\nawait nano.db.create('my-partitioned-db', { partitioned: true })\n```\n\nThe database can be used as normal:\n\n```js\nconst db = nano.db.use('my-partitioned-db')\n```\n\nbut documents must have a two-part `_id` made up of `<partition key>:<document id>`. They are insert with `db.insert` as normal:\n\n```js\nconst doc = { _id: 'canidae:dog', name: 'Dog', latin: 'Canis lupus familiaris' }\nawait db.insert(doc)\n```\n\nDocuments can be retrieved by their `_id` using `db.get`:\n\n```js\nconst doc = db.get('canidae:dog')\n```\n\nMango indexes can be created to operate on a per-partition index by supplying `partitioned: true` on creation:\n\n```js\nconst i = {\n  ddoc: 'partitioned-query',\n  index: { fields: ['name'] },\n  name: 'name-index',\n  partitioned: true,\n  type: 'json'\n}\n\n// instruct CouchDB to create the index\nawait db.index(i)\n```\n\nSearch indexes can be created by writing a design document with `opts.partitioned = true`:\n\n```js\n// the search definition\nconst func = function(doc) {\n  index('name', doc.name)\n  index('latin', doc.latin)\n}\n\n// the design document containing the search definition function\nconst ddoc = {\n  _id: '_design/search-ddoc',\n  indexes: {\n    search-index: {\n      index: func.toString()\n    }\n  },\n  options: {\n    partitioned: true\n  }\n}\n \nawait db.insert(ddoc)\n```\n\nMapReduce views can be created by writing a design document with `opts.partitioned = true`:\n\n```js\nconst func = function(doc) {\n  emit(doc.family, doc.weight)\n}\n\n// Design Document\nconst ddoc = {\n  _id: '_design/view-ddoc',\n  views: {\n    family-weight: {\n      map: func.toString(),\n      reduce: '_sum'\n    }\n  },\n  options: {\n    partitioned: true\n  }\n}\n\n// create design document\nawait db.insert(ddoc)\n```\n\n### db.partitionInfo(partitionKey)\n\nFetch the stats of a single partition:\n\n```js\nconst stats = await alice.partitionInfo('canidae')\n```\n\n### db.partitionedList(partitionKey, [params])\n\nFetch documents from a database partition:\n\n```js\n// fetch document id/revs from a partition\nconst docs = await alice.partitionedList('canidae')\n\n// add document bodies but limit size of response\nconst docs = await alice.partitionedList('canidae', { include_docs: true, limit: 5 })\n```\n\n### db.partitionedListAsStream(partitionKey, [params])\n\nFetch documents from a partition as a stream:\n\n```js\n// fetch document id/revs from a partition\nnano.db.partitionedListAsStream('canidae')\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n\n// add document bodies but limit size of response\nnano.db.partitionedListAsStream('canidae', { include_docs: true, limit: 5 })\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n```\n\n### db.partitionedFind(partitionKey, query, [params])\n\nQuery documents from a partition by supplying a Mango selector:\n\n```js\n// find document whose name is 'wolf' in the 'canidae' partition\nawait db.partitionedFind('canidae', { 'selector' : { 'name': 'Wolf' }})\n```\n\n### db.partitionedFindAsStream(partitionKey, query)\n\nQuery documents from a partition by supplying a Mango selector as a stream:\n\n```js\n// find document whose name is 'wolf' in the 'canidae' partition\ndb.partitionedFindAsStream('canidae', { 'selector' : { 'name': 'Wolf' }})\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n```\n\n### db.partitionedSearch(partitionKey, designName, searchName, params)\n\nSearch documents from a partition by supplying a Lucene query:\n\n```js\nconst params = {\n  q: 'name:\\'Wolf\\''\n}\nawait db.partitionedSearch('canidae', 'search-ddoc', 'search-index', params)\n// { total_rows: ... , bookmark: ..., rows: [ ...] }\n```\n\n### db.partitionedSearchAsStream(partitionKey, designName, searchName, params)\n\nSearch documents from a partition by supplying a Lucene query as a stream:\n\n```js\nconst params = {\n  q: 'name:\\'Wolf\\''\n}\ndb.partitionedSearchAsStream('canidae', 'search-ddoc', 'search-index', params)\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n// { total_rows: ... , bookmark: ..., rows: [ ...] }\n```\n\n### db.partitionedView(partitionKey, designName, viewName, params)\n\nFetch documents from a MapReduce view from a partition:\n\n```js\nconst params = {\n  startkey: 'a',\n  endkey: 'b',\n  limit: 1\n}\nawait db.partitionedView('canidae', 'view-ddoc', 'view-name', params)\n// { rows: [ { key: ... , value: [Object] } ] }\n```\n\n### db.partitionedViewAsStream(partitionKey, designName, viewName, params)\n\nFetch documents from a MapReduce view from a partition as a stream:\n\n```js\nconst params = {\n  startkey: 'a',\n  endkey: 'b',\n  limit: 1\n}\ndb.partitionedViewAsStream('canidae', 'view-ddoc', 'view-name', params)\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout)\n// { rows: [ { key: ... , value: [Object] } ] }\n```\n\n## Multipart functions\n\n### db.multipart.insert(doc, attachments, params)\n\nInserts a `doc` together with `attachments` and `params`. If params is a string, it's assumed as the intended document `_id`. If params is an object, its passed as query string parameters and `docName` is checked for defining the `_id`. Refer to the [doc](https://docs.couchdb.org/en/stable/api/document/common.html) for more details.\n The `attachments` parameter must be an array of objects with `name`, `data` and `content_type` properties.\n\n```js\nconst fs = require('fs');\n\nfs.readFile('rabbit.png', (err, data) => {\n  if (!err) {\n    await alice.multipart.insert({ foo: 'bar' }, [{name: 'rabbit.png', data: data, content_type: 'image/png'}], 'mydoc')\n  }\n});\n```\n\n### db.multipart.get(docname, [params])\n\nGet `docname` together with its attachments via `multipart/related` request with optional [query string additions](https://docs.couchdb.org/en/stable/api/document/common.html#get--db-docid). The multipart response body is a `Buffer`.\n\n```js\nconst response = await alice.multipart.get('rabbit')\n```\n\n## Attachments functions\n\n### db.attachment.insert(docname, attname, att, contenttype, [params])\n\nInserts an attachment `attname` to `docname`, in most cases\n `params.rev` is required. Refer to the\n [CouchDB doc](https://docs.couchdb.org/en/latest/api/document/attachments.html#db-doc-attachment) for more details.\n\n```js\nconst fs = require('fs');\n\nfs.readFile('rabbit.png', (err, data) => {\n  if (!err) {\n    await alice.attachment.insert('rabbit', \n      'rabbit.png', \n      data, \n      'image/png',\n      { rev: '12-150985a725ec88be471921a54ce91452' })\n  }\n});\n```\n\n### db.attachment.insertAsStream(docname, attname, att, contenttype, [params])\n\nAs of Nano 9.x, the function `db.attachment.insertAsStream` is now deprecated. Now simply pass\na readable stream to `db.attachment.insert` as the third paramseter.\n\n### db.attachment.get(docname, attname, [params])\n\nGet `docname`'s attachment `attname` with optional query string additions\n`params`.\n\n```js\nconst fs = require('fs');\n\nconst body = await alice.attachment.get('rabbit', 'rabbit.png')\nfs.writeFile('rabbit.png', body)\n```\n\n### db.attachment.getAsStream(docname, attname, [params])\n\n```js\nconst fs = require('fs');\nalice.attachment.getAsStream('rabbit', 'rabbit.png')\n  .on('error', e => console.error)\n  .pipe(fs.createWriteStream('rabbit.png'));\n```\n\n### db.attachment.destroy(docname, attname, [params])\n\n**changed in version 6**\n\nDestroy attachment `attname` of `docname`'s revision `rev`.\n\n```js\nconst response = await alice.attachment.destroy('rabbit', 'rabbit.png', {rev: '1-4701d73a08ce5c2f2983bf7c9ffd3320'})\n```\n\n## Views and design functions\n\n### db.view(designname, viewname, [params])\n\nCalls a view of the specified `designname` with optional query string `params`. If you're looking to filter the view results by key(s) pass an array of keys, e.g\n`{ keys: ['key1', 'key2', 'key_n'] }`, as `params`.\n\n```js\nconst body = await alice.view('characters', 'happy_ones', { key: 'Tea Party', include_docs: true })\nbody.rows.forEach((doc) => {\n  console.log(doc.value)\n})\n```\n\nor\n\n```js\nconst body = await alice.view('characters', 'soldiers', { keys: ['Hearts', 'Clubs'] })\n```\n\nWhen `params` is not supplied, or no keys are specified, it will simply return all documents in the view:\n\n```js\nconst body = await alice.view('characters', 'happy_ones')\n```\n\n```js\nconst body = alice.view('characters', 'happy_ones', { include_docs: true })\n```\n\n### db.viewAsStream(designname, viewname, [params])\n\nSame as `db.view` but returns a stream:\n\n```js\nalice.viewAsStream('characters', 'happy_ones', {reduce: false})\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout);\n```\n\n### db.viewWithList(designname, viewname, listname, [params])\n\nCalls a list function fed by the given view from the specified design document.\n\n```js\nconst body = await alice.viewWithList('characters', 'happy_ones', 'my_list')\n```\n\n### db.viewWithListAsStream(designname, viewname, listname, [params])\n\nCalls a list function fed by the given view from the specified design document as a stream.\n\n```js\nalice.viewWithListAsStream('characters', 'happy_ones', 'my_list')\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout);\n```\n\n### db.show(designname, showname, doc_id, [params])\n\nCalls a show function from the specified design for the document specified by doc_id with\noptional query string additions `params`.\n\n```js\nconst doc = await alice.show('characters', 'format_doc', '3621898430')\n```\n\nTake a look at the [CouchDB wiki](https://guide.couchdb.org/draft/show.html)\nfor possible query paramaters and more information on show functions.\n\n### db.atomic(designname, updatename, docname, [body])\n\nCalls the design's update function with the specified doc in input.\n\n```js\nconst response = await db.atomic('update', 'inplace', 'foobar', {field: 'foo', value: 'bar'})\n```\n\nNote that the data is sent in the body of the request.\nAn example update handler follows:\n\n```js\n\"updates\": {\n  \"in-place\" : \"function(doc, req) {\n      var request_body = JSON.parse(req.body)\n      var field = request_body.field\n      var value = request_body.value\n      var message = 'set ' + field + ' to ' + value\n      doc[field] = value\n      return [doc, message]\n  }\"\n}\n```\n\n### db.search(designname, searchname, params)\n\nCalls a view of the specified design with optional query string additions `params`.\n\n```js\nconst response = await alice.search('characters', 'happy_ones', { q: 'cat' })\n```\n\nor\n\n```js\nconst drilldown = [['author', 'Dickens']['publisher','Penguin']]\nconst response = await alice.search('inventory', 'books', { q: '*:*', drilldown: drilldown })\n```\n\nCheck out the tests for a fully functioning example.\n\n### db.searchAsStream(designname, searchname, params)\n\nCalls a view of the specified design with optional query string additions `params`. Returns stream.\n\n```js\nalice.search('characters', 'happy_ones', { q: 'cat' }).pipe(process.stdout);\n```\n\n### db.find(selector)\n\nPerform a [\"Mango\" query](https://docs.couchdb.org/en/2.1.1/api/database/find.html) by supplying a JavaScript object containing a selector:\n\n```js\n// find documents where the name = \"Brian\" and age > 25.\nconst q = {\n  selector: {\n    name: { \"$eq\": \"Brian\"},\n    age : { \"$gt\": 25 }\n  },\n  fields: [ \"name\", \"age\", \"tags\", \"url\" ],\n  limit:50\n};\nconst response = await alice.find(q)\n```\n\n### db.findAsStream(selector)\n\nPerform a [\"Mango\" query](http://docs.couchdb.org/en/2.1.1/api/database/find.html) by supplying a JavaScript object containing a selector, but return a stream:\n\n```js\n// find documents where the name = \"Brian\" and age > 25.\nconst q = {\n  selector: {\n    name: { \"$eq\": \"Brian\"},\n    age : { \"$gt\": 25 }\n  },\n  fields: [ \"name\", \"age\", \"tags\", \"url\" ],\n  limit:50\n};\nalice.findAsStream(q)\n  .on('error', (e) => console.error('error', e))\n  .pipe(process.stdout);\n```\n\n## using cookie authentication\n\nNano supports making requests using CouchDB's [cookie authentication](http://guide.couchdb.org/editions/1/en/security.html#cookies) functionality. If you initialise *Nano* so that it is cookie-aware, you may call `nano.auth` first to get a session cookie. Nano will behave like a web browser, remembering your session cookie and refreshing it if a new one is received in a future HTTP response.\n\n```js\nconst nano = require('nano')({\n  url: 'http://127.0.0.1:5984'\n})\nconst username = 'user'\nconst userpass = 'pass'\nconst db = nano.db.use('mydb')\n\n// authenticate\nawait nano.auth(username, userpass)\n\n// requests from now on are authenticated\nconst doc = await db.get('mydoc')\nconsole.log(doc)\n```\n\nThe second request works because the `nano` library has remembered the `AuthSession` cookie that was invisibily returned by the `nano.auth` call.\n\nWhen you have a session, you can see what permissions you have by calling the `nano.session` function\n\n```js\nconst doc = await nano.session()\n// { userCtx: { roles: [ '_admin', '_reader', '_writer' ], name: 'rita' },  ok: true }\n```\n\n## Advanced features\n\n### Getting uuids\n\nIf your application needs to generate UUIDs, then CouchDB can provide some for you\n\n```js\nconst response = await nano.uuids(3)\n// { uuids: [\n// '5d1b3ef2bc7eea51f660c091e3dffa23',\n// '5d1b3ef2bc7eea51f660c091e3e006ff',\n// '5d1b3ef2bc7eea51f660c091e3e007f0',\n//]}\n```\n\nThe first parameter is the number of uuids to generate. If omitted, it defaults to 1.\n\n### Extending nano\n\n`nano` is minimalistic but you can add your own features with\n`nano.request(opts)`\n\nFor example, to create a function to retrieve a specific revision of the\n`rabbit` document:\n\n```js\nfunction getrabbitrev(rev) {\n  return nano.request({ db: 'alice',\n                 doc: 'rabbit',\n                 method: 'get',\n                 params: { rev: rev }\n               });\n}\n\ngetrabbitrev('4-2e6cdc4c7e26b745c2881a24e0eeece2').then((body) => {\n  console.log(body);\n});\n```\n\n### Pipes\n\nYou can pipe the return values of certain nano functions like other stream. For example if our `rabbit` document has an attachment with name `picture.png` you can pipe it to a `writable stream`:\n\n```js\nconst fs = require('fs');\nconst nano = require('nano')('http://127.0.0.1:5984/');\nconst alice = nano.use('alice');\nalice.attachment.getAsStream('rabbit', 'picture.png')\n  .on('error', (e) => console.error('error', e))\n  .pipe(fs.createWriteStream('/tmp/rabbit.png'));\n```\n\nthen open `/tmp/rabbit.png` and you will see the rabbit picture.\n\nFunctions that return streams instead of a Promise are:\n\n- nano.db.listAsStream\n\nattachment functions:\n\n- db.attachment.getAsStream\n- db.attachment.insertAsStream\n\nand document level functions\n\n- db.listAsStream\n\n### Logging\n\nWhen instantiating Nano, you may supply the function that will perform the logging of requests and responses. In its simplest for, simply pass `console.log` as your logger:\n\n```js\nconst nano = Nano({ url: process.env.COUCH_URL, log: console.log })\n// all requests and responses will be sent to console.log\n```\n\nYou may supply your own logging function to format the data before output:\n\n```js\nconst url = require('url')\nconst logger = (data) => {\n  // only output logging if there is an environment variable set\n  if (process.env.LOG === 'nano') {\n    // if this is a request\n    if (typeof data.err === 'undefined') {\n      const u = new url.URL(data.uri)\n      console.log(data.method, u.pathname, data.qs)\n    } else {\n      // this is a response\n      const prefix = data.err ? 'ERR' : 'OK'\n      console.log(prefix, data.headers.statusCode, JSON.stringify(data.body).length)\n    }\n  }\n}\nconst nano = Nano({ url: process.env.COUCH_URL, log: logger })\n// all requests and responses will be formatted by my code\n// GET /cities/_all_docs { limit: 5 }\n// OK 200 468\n```\n\n## Tutorials, examples in the wild & screencasts\n\n* article: [nano - a minimalistic CouchDB client for nodejs](https://writings.nunojob.com/2011/08/nano-minimalistic-couchdb-client-for-nodejs.html)\n* article: [getting started with Node.js and CouchDB](https://writings.nunojob.com/2011/09/getting-started-with-nodejs-and-couchdb.html)\n* article: [nano 3](https://writings.nunojob.com/2012/05/Nano-3.html)\n* article: [how to update a document with nano](https://writings.nunojob.com/2012/07/How-To-Update-A-Document-With-Nano-The-CouchDB-Client-for-Node.js.html)\n* article: [thoughts on development using CouchDB with Node.js](https://tbranyen.com/post/thoughts-on-development-using-couchdb-with-nodejs)\n* example in the wild: [nanoblog](https://github.com/grabbeh/nanoblog)\n\n## Roadmap\n\nCheck [issues][2]\n\n## Tests\n\nTo run (and configure) the test suite simply:\n\n``` sh\ncd nano\nnpm install\nnpm run test\n```\n\n## Meta\n\n* code: `git clone git://github.com/apache/couchdb-nano.git`\n* home: <https://github.com/apache/couchdb-nano>\n* bugs: <https://github.com/apache/couchdb-nano/issues>\n* chat: [Freenode IRC @ #couchdb-dev][8]\n\n[1]: https://npmjs.org\n[2]: https://github.com/apache/couchdb-nano/issues\n[4]: https://github.com/apache/couchdb-nano/blob/main/cfg/couch.example.js\n[8]: https://webchat.freenode.net?channels=%23couchdb-dev\n\nhttps://freenode.org/\n\n## Release\n\nTo create a new release of nano. Run the following commands on the main branch\n\n```sh\n  npm version {patch|minor|major}\n  github push  origin main --tags\n  npm publish\n```\n","publish_time":1775035269446,"_cnpm_publish_time":1775035269446,"dist-tags":{"latest":"11.0.8"}}