PouchDB has an asynchronous API, supporting callbacks, promises, and async functions. For beginners, we recommend promises, although you are free to use whatever format you prefer. If you are unsure, check out our guide to asynchronous code.
Most of the API is exposed as:
… where both the options
and callback
are optional.
Callbacks
Callbacks use the standard Node.js idiom of:
… where the error
will be undefined if there’s no error.
Promises
If you don’t specify a callback
, then the API returns a promise. In supported browsers or Node.js, native promises are used, falling back to the minimal library lie as needed.
Using Ionic/AngularJS? You can wrap PouchDB promises in $q.when()
. This will notify AngularJS to update the UI when the PouchDB promise has resolved.
To use a custom promise implementation with PouchDB, you must redefine a global Promise
object before loading PouchDB:
Async functions
Another option is to use async
/await
pattern instead of promises chain (ie. .then().catch()
). async
/await
is widely supported by all major browsers and Node.js. Use a transpiler with async
/await
polyfill such as Babel if you need to support older browsers.
Note that the samples for async
/await
in the API documentation assume that your code is inside an async function. So for instance:
Any await
not inside of an async function is a syntax error. For more information about async
/await
, read our introductory blog post.
This method creates a database or opens an existing one. If you use a URL like 'http://domain.com/dbname'
, then PouchDB will work as a client to an online CouchDB instance. Otherwise it will create a local database using whatever backend is present.
Options
name
: You can omit thename
argument and specify it viaoptions
instead. Note that the name is required.
Options for local databases:
auto_compaction
: This turns on auto compaction, which meanscompact()
is called after every change to the database. Defaults tofalse
.adapter
: One of'indexeddb'
,'idb'
,'leveldb'
, or'http'
.revs_limit
: Specify how many old revisions we keep track (not a copy) of. Specifying a low value means Pouch may not be able to figure out whether a new revision received via replication is related to any it currently has which could result in a conflict. Defaults to1000
.deterministic_revs
: Use a md5 hash to create a deterministic revision number for documents. Setting it to false will mean that the revision number will be a random UUID. Defaults to true.view_update_changes_batch_size
: Specify how many change records will be consumed at a time when rebuilding view indexes when thequery()
method is used. Defaults to 50.view_adapter
: Specify a different adapter to store the view index data.purged_infos_limit
: Specify how many purged revisions we keep track of. Specifying a low value can result in Pouch not delegating older purges down to views. Defaults to1000
.
Options for remote databases:
fetch(url, opts)
: Intercept or override the HTTP request, you can add or modify any headers or options relating to the http request then return a new fetch Promise.auth.username
+auth.password
: You can specify HTTP auth parameters either by using a database with a name in the formhttp://user:pass@host/name
or via theauth.username
+auth.password
options.skip_setup
: Initially PouchDB checks if the database exists, and tries to create it, if it does not exist yet. Set this totrue
to skip this setup.
Notes:
- In IndexedDB PouchDB will use
_pouch_
to prefix the internal database names. Do not manually create databases with the same prefix. - When acting as a client on Node, any other options given will be passed to request.
- When using the
'leveldb'
adapter (the default on Node), any other options given will be passed to levelup. - If you are using the jwt auth handler, please use the fetch option to add the required headers and handle connected logic like token refreshes.
Example Usage:
Create an in-memory Pouch (must install pouchdb-adapter-memory
first):
Create a remote PouchDB with special fetch options:
For more info, check out adapters.
Delete the database. Note that this has no impact on other replicated databases.
Example Usage
Callback:
Async:
Promise:
Example Response:
Using db.put()
Create a new document or update an existing document. If the document already exists, you must specify its revision _rev
, otherwise a conflict will occur.
If you want to update an existing document even if there’s conflict, you should specify the base revision _rev
and use force=true
option, then a new conflict revision will be created.
doc
must be a “pure JSON object”, i.e. a collection of name/value pairs. If you try to store non-JSON data (for instance Date
objects) you may see inconsistent results.
Example Usage:
Create a new doc with an _id
of 'mydoc'
:
Callback:
Async:
Promise:
You can update an existing doc using _rev
:
Callback:
Async:
Promise:
Example Response:
The response contains the id
of the document, the new rev
, and an ok
to reassure
you that everything is okay.
Using db.post()
Create a new document and let PouchDB auto-generate an _id
for it.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Put vs. post: The basic rule of thumb is: put()
new documents with an _id
, post()
new documents without an _id
.
You should also prefer put()
to post()
, because when you post()
, you are missing an opportunity to use allDocs()
to sort documents by _id
(because your _id
s are random). For more info, read the PouchDB pro tips.
Retrieves a document, specified by docId
.
Options
All options default to false
unless otherwise specified.
options.rev
: Fetch specific revision of a document. Defaults to winning revision (see the CouchDB guide).options.revs
: Include revision history of the document.options.revs_info
: Include a list of revisions of the document, and their availability.options.open_revs
: Fetch all leaf revisions ifopen_revs="all"
or fetch all leaf revisions specified inopen_revs
array. Leaves will be returned in the same order as specified in input array.options.conflicts
: If specified, conflicting leaf revisions will be attached in_conflicts
array.options.attachments
: Include attachment data.options.binary
: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings.
options.latest
: Forces retrieving latest “leaf” revision, no matter what rev was requested. Default isfalse
.
Example Usage:
Callback:
Async:
Promise:
Example Response:
The response contains the document as it is stored in the database, along with its
_id
and _rev
.
Or:
Deletes the document. doc
is required to be a document with at least an _id
and a _rev
property. Sending the full document will work as well.
See filtered replication for why you might want to use put()
with {_deleted: true}
instead.
Example Usage:
Callback:
Async:
Promise:
Example Response:
You can also delete a document by just providing an id
and rev
:
Callback:
Async:
Promise:
You can also delete a document by using put()
with {_deleted: true}
:
Callback:
Async:
Promise:
Create, update or delete multiple documents. The docs
argument is an array of documents.
If you omit an _id
parameter on a given document, the database will create a new document and assign the ID for you. To update a document, you must include both an _id
parameter and a _rev
parameter, which should match the ID and revision of the document on which to base your updates. Finally, to delete a document, include a _deleted
parameter with the value true
.
Example Usage:
Put some new docs, providing the _id
s:
Callback:
Async:
Promise:
Post some new docs and auto-generate the _id
s:
Callback:
Async:
Promise:
Example Response:
The response contains an array of the familiar ok
/rev
/id
from the put()/post() API. If there are any errors, they
will be provided individually like so:
The results are returned in the same order as the supplied “docs” array.
Note that bulkDocs()
is not transactional, and that you may get
back a mixed array of errors/non-errors. In CouchDB/PouchDB, the smallest
atomic unit is the document.
Bulk update/delete:
You can also use bulkDocs()
to update/delete many documents at once:
Callback:
Async:
Promise:
Or delete them:
Callback:
Async:
Promise:
Note: If you set a new_edits
property on the options object to false
, you can post existing documents from other databases without having new revision IDs assigned to them. Normally, only the replication algorithm needs to do this.
Fetch multiple documents, indexed and sorted by the _id
. Deleted documents are only included if options.keys
is specified.
Options
All options default to false
unless otherwise specified.
options.include_docs
: Include the document itself in each row in thedoc
field. Otherwise by default you only get the_id
and_rev
properties.options.conflicts
: Include conflict information in the_conflicts
field of a doc.options.attachments
: Include attachment data as base64-encoded string.options.binary
: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings.options.startkey
&options.endkey
: Get documents with IDs in a certain range (inclusive/inclusive).options.inclusive_end
: Include documents having an ID equal to the givenoptions.endkey
. Default:true
.options.limit
: Maximum number of documents to return.options.skip
: Number of docs to skip before returning (warning: poor performance on IndexedDB/LevelDB!).options.descending
: Reverse the order of the output documents. Note that the order ofstartkey
andendkey
is reversed whendescending
:true
.options.key
: Only return documents with IDs matching this string key.options.keys
: Array of string keys to fetch in a single shot.- Neither
startkey
norendkey
can be specified with this option. - The rows are returned in the same order as the supplied
keys
array. - The row for a deleted document will have the revision ID of the deletion, and an extra key
"deleted":true
in thevalue
property. - The row for a nonexistent document will just contain an
"error"
property with the value"not_found"
. - For details, see the CouchDB query options documentation.
- Neither
options.update_seq
: Include anupdate_seq
value indicating which sequence id of the underlying database the view reflects.
Notes: For pagination, options.limit
and options.skip
are also available, but the same performance concerns as in CouchDB apply. Use the startkey/endkey pattern instead.
Example Usage:
Callback:
Async:
Promise:
Example Response:
In the response, you have three things:
total_rows
the total number of non-deleted documents in the databaseoffset
theskip
if provided, or in CouchDB the actual offset-
rows
: rows containing the documents, or just the_id
/_revs
if you didn’t setinclude_docs
totrue
. - You may optionally also have
update_seq
if you setupdate_seq
totrue
You can use startkey
/endkey
to find all docs in a range:
Callback:
Async:
Promise:
This will return all docs with _id
s between 'bar'
and 'quux'
.
Prefix search
You can do prefix search in allDocs()
– i.e. “give me all the documents whose _id
s start with 'foo'
” – by using the special high Unicode character '\ufff0'
:
Callback:
Async:
Promise:
This works because CouchDB/PouchDB _id
s are sorted lexicographically.
A list of changes made to documents in the database, in the order they were made.
It returns an object with the method cancel()
, which you call if you don’t want to listen to new changes anymore.
It is an event emitter and will emit a 'change'
event on each document change, a 'complete'
event when all the changes have been processed, and an 'error'
event when an error occurs. Calling cancel()
will unsubscribe all event listeners automatically.
Options
All options default to false
unless otherwise specified.
options.live
: Will emit change events for all future changes until cancelled.options.include_docs
: Include the associated document with each change.options.conflicts
: Include conflicts.options.attachments
: Include attachments.options.binary
: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings.
options.descending
: Reverse the order of the output documents.options.since
: Start the results from the change immediately after the given sequence number. You can also pass'now'
if you want only new changes (whenlive
istrue
). Ignored ifdescending
is true.options.limit
: Limit the number of results to this number.options.timeout
: Request timeout (in milliseconds), usefalse
to disable.options.heartbeat
: For http adapter only, time in milliseconds for server to give a heartbeat to keep long connections open. Defaults to 10000 (10 seconds), usefalse
to disable the default.
Filtering Options:
options.filter
: Reference a filter function from a design document to selectively get updates. To use a view function, pass_view
here and provide a reference to the view function inoptions.view
. See filtered changes for details.options.doc_ids
: Only show changes for docs with these ids (array of strings).options.query_params
: Object containing properties that are passed to the filter function, e.g.{"foo:"bar"}
, where"bar"
will be available in the filter function asparams.query.foo
. To access theparams
, define your filter function likefunction (doc, params) {/* ... */}
.options.view
: Specify a view function (e.g.'design_doc_name/view_name'
or'view_name'
as shorthand for'view_name/view_name'
) to act as a filter. Documents counted as “passed” for a view filter if a map function emits at least one record for them. Note:options.filter
must be set to'_view'
for this option to work.options.selector
: Filter using a query/pouchdb-find selector. Note: Cannot be used in combination with the filter option.
Advanced Options:
options.return_docs
: Defaults totrue
except whenoptions.live = true
then it defaults tofalse
. Passingfalse
prevents the changes feed from keeping all the documents in memory – in other words complete always has an empty results array, and thechange
event is the only way to get the event. Useful for large change sets where otherwise you would run out of memory.options.batch_size
: Only available for http databases, this configures how many changes to fetch at a time. Increasing this can reduce the number of requests made. Default is 25.options.style
: Specifies how many revisions are returned in the changes array. The default,'main_only'
, will only return the current “winning” revision;'all_docs'
will return all leaf revisions (including conflicts and deleted former conflicts). Most likely you won’t need this unless you’re writing a replicator.options.seq_interval
: Only available for http databases. Specifies that seq information only be generated every N changes. Larger values can improve changes throughput with CouchDB 2.0 and later. Note thatlast_seq
is always populated regardless.
Example Usage:
Example Response:
Change events
change
(info
) - This event fires when a change has been found.info
will contain details about the change, such as whether it was deleted and what the new_rev
is.info.doc
will contain the doc if you setinclude_docs
totrue
. See below for an example response.complete
(info
) - This event fires when all changes have been read. In live changes, only cancelling the changes should trigger this event.info.results
will contain the list of changes. See below for an example.error
(err
) - This event is fired when the changes feed is stopped due to an unrecoverable failure.
Example response
Example response in the 'change'
listener (using {include_docs: true}
):
Example response in the 'change'
listener when a doc was deleted:
Example response in the 'complete'
listener:
seq
and last_seq
correspond to the overall sequence number of the entire database, and it’s what is passed in when using since
(except for the special 'now'
). It is the primary key for the changes feed, and is also used as a checkpointer by the replication algorithm.
Single-shot
If you don’t specify {live: true}
, then you can also use changes()
in the standard
callback/promise style, and it will be treated as a single-shot request, which
returns a list of the changes (i.e. what the 'complete'
event emits):
Callback:
Async:
Promise:
Example Response:
When live
is false
, the returned object is also an event emitter as well as a promise,
and will fire the 'complete'
event when the results are ready.
Note that this 'complete'
event only fires when you aren’t doing live changes.
Filtered changes
As with replicate(), you can filter using:
- an ad-hoc
filter
function - an array of
doc_ids
- a
filter
function inside of a design document - a
filter
function inside of a design document, withquery_params
- a
view
function inside of a design document
If you are running changes()
on a remote CouchDB, then the first method will run client-side, whereas the last four will filter on the server side. Therefore the last four should be preferred, especially if the database is large, because you want to send as few documents over the wire as possible.
If you are running changes()
on a local PouchDB, then obviously all five methods will run client-side. There are also no performance benefits to using any of the five, so can also just filter yourself, in your own on('change')
handler. These methods are implemented in PouchDB purely for consistency with CouchDB.
The named functions can either be specified with 'designdoc_id/function_name'
or (if both design doc id and function name are equal) as 'fname'
as shorthand for 'fname/fname'
.
Filtering examples
In these examples, we’ll work with some mammals. Let’s imagine our docs are:
Here are 5 examples using the 5 different systems.
Example 1: Ad-hoc filter
function
Warning: this runs client-side, if the database is remote.
Filter by type === 'marsupial'
:
Example 2: Array of doc_ids
Filter documents with _id
s ['a', 'c']
.
Example 3: filter
function inside of a design document
First put()
a design document:
Then filter by type === 'marsupial'
:
Example 4: filter
function inside of a design document, with query_params
This is the most powerful way to filter, because it allows you to pass in arbitrary options to your filter function.
First put()
a design document:
Then filter by type === 'marsupial'
:
Since both the design document and the filter function have the same name, we can shorten the function name to 'myfilter'
.
Example 5: view
function inside of a design document
This doesn’t really offer any advantages compared to the previous two methods, unless you are already using a view
for map/reduce queries, and you want to reuse it.
Any documents that emit()
anything will be considered to have passed this filter method.
First put()
a design document:
Then filter by type === 'marsupial'
:
Replicate data from source
to target
. Both the source
and target
can be a PouchDB instance or a string representing a CouchDB database URL or the name of a local PouchDB database. If options.live
is true
, then this will track future changes and also replicate them automatically. This method returns an object with the method cancel()
, which you call if you want to cancel live replication.
Replication is an event emitter like changes() and emits the 'complete'
, 'active'
, 'paused'
, 'change'
, 'denied'
, 'error'
and 'checkpoint'
events.
Options
All options default to false
unless otherwise specified.
options.live
: Iftrue
, starts subscribing to future changes in thesource
database and continue replicating them.options.retry
: Iftrue
will attempt to retry replications in the case of failure (due to being offline), using a backoff algorithm that retries at longer and longer intervals until a connection is re-established, with a maximum delay of 10 minutes. Only applicable ifoptions.live
is alsotrue
.
Filtering Options:
options.filter
: Reference a filter function from a design document to selectively get updates. To use a view function, pass_view
here and provide a reference to the view function inoptions.view
. See filtered replication for details.options.doc_ids
: Only show changes for docs with these ids (array of strings).options.query_params
: Object containing properties that are passed to the filter function, e.g.{"foo":"bar"}
, where"bar"
will be available in the filter function asparams.query.foo
. To access theparams
, define your filter function likefunction (doc, params) {/* ... */}
.options.view
: Specify a view function (e.g.'design_doc_name/view_name'
or'view_name'
as shorthand for'view_name/view_name'
) to act as a filter. Documents counted as “passed” for a view filter if a map function emits at least one record for them. Note:options.filter
must be set to'_view'
for this option to work.options.selector
: Filter using a query/pouchdb-find selector.
Advanced Options:
options.since
: Replicate changes after the given sequence number.options.heartbeat
: Configure the heartbeat supported by CouchDB which keeps the change connection alive.options.timeout
: Request timeout (in milliseconds).options.batch_size
: Number of change feed items to process at a time. Defaults to 100. This affects the number of docs and attachments held in memory and the number sent at a time to the target server. You may need to adjust downward if targeting devices with low amounts of memory (e.g. phones) or if the documents and/or attachments are large in size or if there are many conflicted revisions. If your documents are small in size, then increasing this number will probably speed replication up.options.batches_limit
: Number of batches to process at a time. Defaults to 10. This (along withbatch_size
) controls how many docs are kept in memory at a time, so the maximum docs in memory at once would equalbatch_size
×batches_limit
.options.back_off_function
: backoff function to be used inretry
replication. This is a function that takes the current backoff as input (or 0 the first time) and returns a new backoff in milliseconds. You can use this to tweak when and how replication will try to reconnect to a remote database when the user goes offline. Defaults to a function that chooses a random backoff between 0 and 2 seconds and doubles every time it fails to connect. The default delay will never exceed 10 minutes. (See Customizing retry replication below.)options.checkpoint
: Can be used if you want to disable checkpoints on the source, target, or both. Setting this option tofalse
will prevent writing checkpoints on both source and target. Setting it tosource
will only write checkpoints on the source. Setting it totarget
will only write checkpoints on the target.options.style
: Specifies whether all revisions of a document including conflicts and deleted former conflicts (all_docs
) or only the winning revision (main_only
) should be replicated. This option is passed to thechanges
endpoint of the replication source. Defaults toall_docs
.
Example Usage:
There are also shorthands for replication given existing PouchDB objects. These behave the same as PouchDB.replicate()
:
The remoteDB
can either be a string or a PouchDB
object. If you have a fetch override on a remote database, you will want to use PouchDB
objects instead of strings, so that the options are used.
Replication events
change
(info
) - This event fires when the replication has written a new document.info
will contain details about the change.info.docs
will contain the docs involved in that change. See below for an example response.complete
(info
) - This event fires when replication is completed or cancelled. In a live replication, only cancelling the replication should trigger this event.info
will contain details about the replication. See below for an example response.paused
(err
) - This event fires when the replication is paused, either because a live replication is waiting for changes, or replication has temporarily failed, witherr
, and is attempting to resume.active
- This event fires when the replication starts actively processing changes; e.g. when it recovers from an error or new changes are available.denied
(err
) - This event fires if a document failed to replicate due to validation or authorization errors.error
(err
) - This event is fired when the replication is stopped due to an unrecoverable failure. Ifretry
isfalse
, this will also fire when the user goes offline or another network error occurs (so you can handle retries yourself, if you want).checkpoint
- This event exposes information about the internal steps of the replication process. It includes one of the fieldspending_batch
,start_next_batch
,revs_diff
orcheckpoint
.
Single-shot
As with changes(), you can also omit live
, in which case you can use replicate()
in the callback/promise style and it will be treated as a single-shot operation.
Callback:
Async:
Promise:
For non-live replications, the returned object is also an event emitter as well as a promise, and you can use all the events described above (except for 'paused'
and 'active'
, which only apply to retry
replications).
Example Response:
Example response in the 'change'
listener:
Example response in the 'complete'
listener:
Note that replication is supported for both local and remote databases. So you can replicate from local to local or from remote to remote.
However, if you replicate from remote to remote, then the changes will flow through PouchDB. If you want to trigger a server-initiated replication, please use regular ajax to POST to the CouchDB _replicate
endpoint, as described in the CouchDB docs.
Filtered replication
As with changes(), you can filter from the source database using:
- an ad-hoc
filter
function - an array of
doc_ids
- a
filter
function inside of a design document - a
filter
function inside of a design document, withquery_params
- a
view
function inside of a design document
If you are replicating from a remote CouchDB, then the first method will run client-side, whereas the last four will filter on the server side. Therefore the last four should be preferred, especially if the database is large, because you want to send as few documents over the wire as possible.
You should also beware trying to use filtered replication to enforce security, e.g. to partition a database per user. A better strategy is the “one database per user” method.
Deleting filtered docs: When you use filtered replication, you should avoid using remove()
to delete documents, because that removes all their fields as well, which means they might not pass the filter function anymore, causing the deleted revision to not be replicated. Instead, set the doc._deleted
flag to true
and then use put()
or bulkDocs()
.
Filtering examples
In these examples, we’ll work with some mammals. Let’s imagine our docs are:
Here are 5 examples using the 5 different systems.
Example 1: Ad-hoc filter
function
Warning: this runs client-side, if you are replicating from a remote database.
Filter by type === 'marsupial'
:
Example 2: Array of doc_ids
Filter documents with _id
s ['a', 'c']
.
Example 3: filter
function inside of a design document
First put()
a design document in the remote database:
Then filter by type === 'marsupial'
:
Example 4: filter
function inside of a design document, with query_params
This is the most powerful way to filter, because it allows you to pass in arbitrary options to your filter function.
First put()
a design document in the remote database:
Then filter by type === 'marsupial'
:
Example 5: view
function inside of a design document
This doesn’t really offer any advantages compared to the previous two methods, unless you are already using a view
for map/reduce queries, and you want to reuse it.
Any documents that emit()
anything will be considered to have passed this filter method.
First put()
a design document in the remote database:
Then filter by type === 'marsupial'
:
Customizing retry replication
During retry
replication, you can customize the backoff function that determines how long to wait before reconnecting when the user goes offline.
Here’s a simple backoff function that starts at 1000 milliseconds and triples it every time a remote request fails:
The first time a request fails, this function will receive 0 as input. The next time it fails, 1000 will be passed in, then 3000, then 9000, etc. When the user comes back online, the delay
goes back to 0.
By default, PouchDB uses a backoff function that chooses a random starting number between 0 and 2000 milliseconds and will roughly double every time, with some randomness to prevent client requests from occurring simultaneously.
Sync data from src
to target
and target
to src
. This is a convenience method for bidirectional data replication.
In other words, this code:
is equivalent to this code:
Options
options.push
+options.pull
: Allows you to specify separate replication options for the individual replications.
Replication options such as filter
passed to sync directly will be passed to both replications. Please refer to replicate() for documentation on those options.
Example Usage:
There is also a shorthand for syncing given existing PouchDB objects. This behaves the same as PouchDB.sync()
:
It is also possible to combine “one-way” replication and sync for performance reasons. When your PouchDB application starts up it could perform a one-off, one-way replication to completion and then initiate the two-way, continuous retryable sync:
The above technique results in fewer HTTP requests being used and better performance than just using db.sync
on its own.
Example Response:
Change events in sync
have an extra property direction
which refers to the direction the change was going. Its value will either be push
or pull
.
For any further details, please refer to replicate().
Attaches a binary object to a document.
This method will update an existing document to add the attachment, so it requires a rev
if the document already exists. If the document doesn’t already exist, then this method will create an empty document containing the attachment.
What’s the point of attachments? If you’re dealing with large binary data (such as PNGs), you may incur a performance or storage penalty if you naïvely include them as base64- or hex-encoded strings inside your documents. But if you insert the binary data as an attachment, then PouchDB will attempt to store it in the most efficient way possible.
For details, see the CouchDB documentation on attachments.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Within Node, you must use a Buffer
instead of a Blob
:
For details, see the Mozilla docs on Blob
or the Node docs on Buffer
.
If you need a shim for older browsers that don’t support the Blob
constructor, or you want some convenience methods for Blobs, you can use blob-util.
Save a base64 attachment
If you supply a string instead of a Blob
/Buffer
, then it will be assumed to be a base64-encoded string, and will be processed accordingly:
Callback:
Async:
Promise:
Save an inline attachment
You can also inline attachments inside the document. The attachment data may be supplied as a base64-encoded string with the content_type
:
Callback:
Async:
Promise:
Save an inline Blob/Buffer attachment
You can also inline Blob
s/Buffer
s:
Callback:
Async:
Promise:
Save many attachments at once
The inline approach allows you to save multiple attachments to the same document in a single shot:
Callback:
Async:
Promise:
See Inline Attachments on the CouchDB wiki for details.
Get attachment data.
Options
options.rev
: as with get(), you can pass arev
in and get back an attachment for the document at that particular revision.
Example Usage:
Get an attachment with filename 'att.txt'
from document with ID 'doc'
:
Callback:
Async:
Promise:
Get an attachment with filename 'att.txt'
from document with ID 'doc'
, at
the revision '1-abcd'
:
Callback:
Async:
Promise:
Response type:
The response will be a Blob
object in the browser, and a Buffer
object in Node.js. See blob-util for utilities to transform Blob
s to other formats, such as base64-encoded strings, data URLs, array buffers, etc.
Inline base64 attachments
You can specify {attachments: true}
to most “read” operations, such as get()
, allDocs()
, changes()
, and query()
. The attachment data will then be included inlined in the resulting doc(s). However, it will always be supplied as base64. For example:
For such APIs, when you don’t specify {attachments: true}
, you will instead get metadata about the attachments. For example:
This “summary” operation may be faster in some cases, because the attachment itself does not need to be read from disk.
Delete an attachment from a doc. You must supply the rev
of the existing doc.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Create an index if it doesn’t exist, or do nothing if it already exists.
pouchdb-find plugin needed: This API requires the pouchdb-find
plugin. See
Mango queries for installation instructions.
Example Usage:
Callback:
Async:
Promise:
Example Response:
If the index was created, you’ll see:
Or if the index already exists:
You can also create an index on multiple fields:
Callback:
Async:
Promise:
Or an index on deep fields:
Callback:
Async:
Promise:
You can also specify additional options, if you want more control over how your index is created:
Callback:
Async:
Promise:
If you want to index a subset of the documents in the database, you can use partial_filter_selector
. This has the same syntax as the selector you’d pass to find()
, and only documents matching the selector will be included in the index.
When using pouchdb-adapter-indexeddb: On the indexeddb
-adapter, indexes with
apartial_filter_selector
will fall back to using map-reduce,
instead of the native indexes, negating all index performance gains from the
indexeddb
-adapter over the idb
-adapter.
Callback:
Async:
Promise:
Options
fields
: a list of fields to indexname
(optional): name of the index, auto-generated if you don’t include itddoc
(optional): design document name (i.e. the part after'_design/'
), auto-generated if you don’t include ittype
(optional): only supports'json'
, which is also the defaultpartial_filter_selector
(optional): a selector used to filter the set of documents included in the index
When a PouchDB instance updates an index, it emits indexing
events that include information about the progress of the index update task.
The event
object contains the following fields:
view
: the name of the view that’s being updatedindexed_docs
: the total number of document updates processed during the current run
When an index is updated this event is emitted once at the start of the update, with indexed_docs
equal to 0
. It will then emit further events as batches of changes are processed, and those events will also include these fields:
last_seq
: theseq
value of the last event from the database change feed that’s been processedresults_count
: the number of changes in the most recent batch of changes
Query an index and return the list of documents that match the request.
pouchdb-find plugin needed: This API requires the pouchdb-find
plugin. See
Mango queries for installation instructions.
Example Usage:
Callback:
Async:
Promise:
Example Response:
The above is a simple example. For an in-depth tutorial, please refer to the Mango guide.
Options
selector
Defines a selector to filter the results. Required.$lt
Match fields “less than” this one.$gt
Match fields “greater than” this one.$lte
Match fields “less than or equal to” this one.$gte
Match fields “greater than or equal to” this one.$eq
Match fields equal to this one.$ne
Match fields not equal to this one.$exists
True if the field should exist, false otherwise.$type
One of: “null”, “boolean”, “number”, “string”, “array”, or “object”.$in
The document field must exist in the list provided.$and
Matches if all the selectors in the array match.$nin
The document field must not exist in the list provided.$all
Matches an array value if it contains all the elements of the argument array.$size
Special condition to match the length of an array field in a document.$or
Matches if any of the selectors in the array match. All selectors must use the same index.$nor
Matches if none of the selectors in the array match.$not
Matches if the given selector does not match.$mod
Matches documents where (field % Divisor == Remainder) is true, and only when the document field is an integer.$regex
A regular expression pattern to match against the document field.$elemMatch
Matches all documents that contain an array field with at least one element that matches all the specified query criteria.
fields
(Optional) Defines a list of fields that you want to receive. If omitted, you get the full documents.sort
(Optional) Defines a list of fields defining how you want to sort. Note that sorted fields also have to be selected in theselector
.limit
(Optional) Maximum number of documents to return. Default is25
when connected to a local database.skip
(Optional) Number of docs to skip before returning.use_index
(Optional) Set which index to use for the query. It can be “design-doc-name” or “[‘design-doc-name’, ‘name’]”.
If there’s no index that matches your selector
/sort
, then this method will issue a warning:
The best index will be chosen automatically.
See the CouchDB _find
documentation for more details on
selectors and the Mango query language.
More examples
Use $eq
for “equals”:
Callback:
Async:
Promise:
This is equivalent to:
Callback:
Async:
Promise:
You can also do selections on multiple fields. For instance, to
find all docs where series
is 'Mario'
and debut
is greater than 1990
:
Callback:
Async:
Promise:
This is equivalent to:
Callback:
Async:
Promise:
You can also sort the returned documents. For instance, to find all docs sorted by debut
descending:
Callback:
Async:
Promise:
Explain the query plan for a given query
pouchdb-find plugin needed: This API requires the pouchdb-find
plugin. See
Mango queries for installation instructions.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Get a list of all the indexes you’ve created. Also tells you about the
special _all_docs
index, i.e. the default index on the _id
field.
pouchdb-find plugin needed: This API requires the pouchdb-find
plugin. See
Mango queries for installation instructions.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Delete an index, remove any orphaned design documents, and clean up any leftover data on disk.
pouchdb-find plugin needed: This API requires the pouchdb-find
plugin. See
Mango queries for installation instructions.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Note that the easiest way to do this is to locate the index you want to delete using getIndexes()
.
For instance, here is how you would delete the second index from that list (which should be the
one after the built-in _all_docs
index):
Callback:
Async:
Promise:
Notes:
- You don’t need to provide a
_rev
when deleting an index. - The associated design doc is automatically deleted, assuming it only contains one index.
- There is no need to call
viewCleanup
to clean up any leftover data.deleteIndex()
does this automatically for you.
Invoke a map/reduce function, which allows you to perform more complex queries on PouchDB than what you get with allDocs(), changes(), or find(). The CouchDB documentation for map/reduce applies to PouchDB.
Since views perform a full scan of all documents, this method may be slow, unless you first save your view in a design document. Read the query guide for a good tutorial.
Warning: advanced API. The map/reduce API is designed for cases that are too complex for Mango queries, which are described in createIndex(), find(), listIndexes(), and deleteIndex().
Under the hood, Mango indexes are the same as map/reduce indexes. The Mango API is just a simplified user-facing API on top of map/reduce.
Options
All options default to false
unless otherwise specified.
fun
: Map/reduce function, which can be one of the following:- A full CouchDB-style map/reduce view:
{map : ..., reduce: ...}
. - A map function by itself (no reduce).
- The name of a view in an existing design document (e.g.
'mydesigndoc/myview'
, or'myview'
as a shorthand for'myview/myview'
).
- A full CouchDB-style map/reduce view:
options.reduce
: Defaults totrue
when a reduce function is defined, orfalse
otherwise. Valid values:true
- calls the definedreduce
function, or themap
function if noreduce
is defined.false
- calls the definedmap
function.- A reduce function.
- The string name of a built-in function:
'_sum'
,'_count'
, or'_stats'
.- Tip: if you’re not using a built-in, you’re probably doing it wrong.
- PouchDB will always call your reduce function with rereduce == false. As for CouchDB, refer to the CouchDB documentation.
options.include_docs
: Include the document in each row in thedoc
field.options.conflicts
: Include conflicts in the_conflicts
field of a doc.options.attachments
: Include attachment data.options.binary
: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings.
options.startkey
&options.endkey
: Get rows with keys in a certain range (inclusive/inclusive).options.inclusive_end
: Include rows having a key equal to the givenoptions.endkey
. Default:true
.options.limit
: Maximum number of rows to return.options.skip
: Number of rows to skip before returning (warning: poor performance on IndexedDB/LevelDB!).options.descending
: Reverse the order of the output rows.options.key
: Only return rows matching this key.options.keys
: Array of keys to fetch in a single shot.- Neither
startkey
norendkey
can be specified with this option. - The rows are returned in the same order as the supplied
keys
array. - The row for a deleted document will have the revision ID of the deletion, and an extra key
"deleted":true
in thevalue
property. - The row for a nonexistent document will just contain an
"error"
property with the value"not_found"
.
- Neither
options.group
: True if you want the reduce function to group results by keys, rather than returning a single result. Defaults tofalse
.options.group_level
: Number of elements in a key to group by, assuming the keys are arrays. Defaults to the full length of the array.options.stale
: One of'ok'
or'update_after'
. Only applies to saved views. Can be one of:- unspecified (default): Returns the latest results, waiting for the view to build if necessary.
'ok'
: Returns results immediately, even if they’re out-of-date.'update_after'
: Returns results immediately, but kicks off a build afterwards.
options.update_seq
: Include anupdate_seq
value indicating which sequence id of the underlying database the view reflects.
For details, see the CouchDB query options documentation.
Example Usage:
Callback:
Async:
Promise:
Example Response:
In the result,total_rows
is the total number of possible results in the view. The response is very similar to that of allDocs()
.
The result may also have update_seq
if you set update_seq
to true
Note: you can also pass in the map function instead of saving a design doc first, but this is slow because it has to do a full database scan. The following examples will use this pattern for simplicity’s sake, but you should normally avoid it.
Complex keys
You can also use complex keys for fancy ordering:
Callback:
Async:
Promise:
Example Response:
Tips:
- The sort order is
[nulls, booleans, numbers, strings, arrays, objects]
, so{startkey: ['Williams'], endkey: ['Williams', {}]}
would return all people with the last name'Williams'
because objects are higher than strings. Something like'zzzzz'
or'\ufff0'
would also work. group_level
can be very helpful when working with complex keys. In the example above, you can use{group_level: 1}
to group by last name, or{group_level: 2}
to group by last and first name. (Be sure to set{reduce: true, group: true}
as well.)
Linked documents
PouchDB fully supports linked documents. Use them to join two types of documents together, by simply adding an _id
to the emitted value:
Callback:
Async:
Promise:
Example response:
Closures
If you pass a function to db.query
and give it the emit
function as the second argument, then you can use a closure. (Since PouchDB has to use eval()
to bind emit
.)
Callback:
Async:
Promise:
Note that closures are only supported by local databases with temporary views. So if you are using closures, then you must use the slower method that requires a full database scan.
Cleans up any stale map/reduce indexes.
As design docs are deleted or modified, their associated index files (in CouchDB) or companion databases (in local PouchDBs) continue to take up space on disk. viewCleanup()
removes these unnecessary index files.
See the CouchDB documentation on view cleanup for details.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Get information about a database.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Response object:
db_name
is the name of the database you gave when you callednew PouchDB()
, and also the unique identifier for the database.doc_count
is the total number of non-deleted documents in the database.update_seq
is the sequence number of the database. It starts at 0 and gets incremented every time a document is added or modified.
There are also some details you can use for debugging. These are unofficial and may change at any time:
adapter
: The name of the adapter being used (idb, leveldb, …).idb_attachment_format
: (IndexedDB) either'base64'
or'binary'
, depending on whether the browser supports binary blobs.backend_adapter
: (Node.JS) the backend *DOWN adapter being used (MemDOWN, RiakDOWN, …).
Triggers a compaction operation in the local or remote database. This reduces the database’s size by removing unused and old data, namely non-leaf revisions and attachments that are no longer referenced by those revisions. Note that this is a separate operation from viewCleanup()
.
For remote databases, PouchDB checks the compaction status at regular intervals and fires the callback (or resolves the promise) upon completion. Consult the compaction section of CouchDB’s maintenance documentation for more details.
Also see auto-compaction, which runs compaction automatically (local databases only).
options.interval
: Number of milliseconds to wait before asking again if compaction is already done. Defaults to 200. (Only applies to remote databases.)
Example Usage:
Callback:
Async:
Promise:
Example Response:
Given a set of document/revision IDs, returns the subset of those that do not correspond to revisions stored in the database. Primarily used in replication.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Given a set of document/revision IDs, returns the document bodies (and, optionally, attachment data) for each ID/revision pair specified.
Options
options.docs
: An array ofid
andrev
pairs representing the revisions to fetch.id
: ID of the document to fetch.rev
: Revision of the document to fetch. If this is not specified, all available revisions are fetched.atts_since
: Optional and supported by the http adapter only. Includes attachments only since specified revisions. Doesn’t includes attachments for specified revisions.
options.revs
: Each returned revision body will include its revision history as a_revisions
property. Default isfalse
.options.attachments
: Include attachment data in the response. Default isfalse
, resulting in only stubs being returned.options.binary
: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings. Default isfalse
.
Example Usage:
Callback:
Async:
Promise:
Example Response:
Close the database, this closes any open connection to the underlying storage and frees memory (event listeners) the database may be using.
Example Usage
Callback:
Async:
Promise:
Purges a specific revision of a document, specified by docId
and rev
. rev
must be a leaf revision.
Purge permanently removes data from the database. Normal deletion with db.remove()
does not, it only marks the document as _deleted=true
and creates a new revision. This behaviour ensures that deletes can be replicated across databases, and deleted documents don’t get undeleted by syncing with a database that still has this document.
db.purge()
is not intended as a regular method for deleting documents, instead, it is meant as an admin function for cases where some secret was erroneously added to the database and must now be removed completely, eg. a credit card or social security number. Purge effectively puts the database in a state where the offending write never happened.
Purge is currently only implemented in the indexeddb
adapter.
Using Purge with any other adapter will return an error.
Example Usage:
Callback:
Async:
Promise:
Example Response:
If the document has no conflicts, purging its only leaf rev deletes the document completely, and purge()
returns documentWasRemovedCompletely: true
in its result object.
If the document does have conflicts, purging will remove the specified rev and pick a leaf from another rev branch as the winner. Fetching the doc after the purge will return the updated state of the document with the new winning revision in _rev
.
Notes:
- The
rev
specified for purging must be a leaf, otherwise an error will be returned - Purges do not sync, they only apply to the database they are performed on
- If the document has conflicts, purging a leaf will also remove its ancestors up to the next branching rev, and pick a new winning rev
- Any attachments referenced in the specified rev will also be deleted
PouchDB is an event emitter and will emit a 'created'
event when a database is created. A 'destroyed'
event is emitted when a database is destroyed.
List all active database tasks. There are three types of internal tasks: database_compaction
, view_indexing
, and replication
. PouchDB will report progress of these tasks to the active tasks API and remove tasks as soon as they are completed or have failed.
Example Usage:
Example Result:
Real-time updates
You can use JavaScript Proxies to monitor calls to the active tasks API. For the PouchDB.activeTasks.add()
function, which is used internally to announce new tasks to PouchDB, you can monitor calls as follows:
If you find yourself using the same constructor options repeatedly,
you can simplify your code with PouchDB.defaults()
:
The returned object is a constructor function that works the same as PouchDB
, except that whenever you invoke it (e.g. with new
), the given options will be passed in by default.
Example Usage:
Note the special constructor option prefix
, which appends a prefix to the database name
and can be helpful for URL-based or file-based LevelDOWN path names.
All constructor options are supported. Default options can still be overriden individually.
This section covers authoring plugins. For a list of third-party plugins, see Plugins, or for a list of first-party plugins that you can use to customize the PouchDB build, see Custom Builds.
Writing a plugin is easy! The API is:
This will add a db.methodName()
to all databases, which runs myFunction
.It will always be called in context, so that within the function, this
refers to the database object.
There is a PouchDB Plugin Seed project, which is the fastest way to get started writing, building and testing your very own plugin.
Example Usage:
Alternatively, instead of passing in an object to .plugin()
, you can pass in
a function that takes the PouchDB
object and performs whatever operations
you want on it. You can use this to load multiple plugins, add adapters,
or attach event listeners to the PouchDB
object.
Example Usage:
(Most likely, if you are writing a PouchDB plugin, you will export either the object-style or the function-style plugin, so that your users can then attach it to their PouchDB object.)
Load Plugins from require()
You can load plugins into PouchDB when you load it via require()
.
You can chain plugins, as well:
Example Plugin: Intercept Updates
A useful feature of plugins is to intercept updates before they are stored in PouchDB. In this way, a plugin might validate that the data is correct for the application, or even alter documents before they are committed to the database.
The best way to intercept all updates to a PouchDB database is to override the bulkDocs()
method. All changes to PouchDB documents ultimately pass through the bulkDocs()
method. For example, a call to put()
will become a bulkDocs()
call with a “batch” of one document.
Because PouchDB guarantees to plugin authors that all data changes ultimately happen via bulkDocs()
, it is the ideal place for an application or plugin to intercept updates.
The above plugin would return an error if anything ever attempts to store an unnamed document, including documents which change during replication.
Note: this is a very, very simple validation example. It does not behave, for example, like the Apache CouchDB validate_doc_update()
API.