Class: DB

clerk. DB

new DB(client, name, authopt)

Methods for CouchDB database.

Parameters:
Name Type Attributes Description
client Client

Clerk client.

name String

DB name.

auth String <optional>

Authentication header value.

Source:
Returns:

This object for chaining.

Methods

all(queryopt, headersopt, callbackopt) → {Promise}

Query all documents by ID.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

Properties
Name Type Attributes Default Description
startkey JSON <optional>

Start returning results from this document ID.

endkey JSON <optional>

Stop returning results at this document ID.

limit Integer <optional>

Limit number of results returned.

descending Boolean <optional>
false

Lookup results in reverse order by key, returning documents in descending order by key.

skip Integer <optional>

Skip this many records before returning results.

include_docs Boolean <optional>
false

Include document source for each result.

include_end Boolean <optional>
true

Include query.endkey in results.

update_seq Boolean <optional>
false

Include sequence value of the database corresponding to the view.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

attach(docopt, attachmentName, data, queryopt, headersopt, callbackopt) → {Promise}

Upload attachment to document.

Set the Content-Type header.

Parameters:
Name Type Attributes Description
doc Object <optional>

Document. Requires id. rev can be specified here or in query.

attachmentName String

Attachment name.

data Object

Data.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

attachment(docOrId, attachmentName, queryopt, headersopt, callbackopt) → {Promise}

Download attachment from document.

Parameters:
Name Type Attributes Description
docOrId Object | String

Document or document ID.

attachmentName String

Attachment name.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

changes(queryopt, headersopt, callbackopt) → {Promise}

Get database changes.

The feed option determines how the callback is called:

  • normal calls the callback once.
  • longpoll waits for a response, then calls the callback once.
  • continuous calls the callback each time an update is received. Implemented as the database#follow() method.
Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

Properties
Name Type Attributes Default Description
feed String <optional>
"normal"

Type of feed. See comments above.

filter String <optional>

Filter updates using this filter.

limit Integer <optional>

Maximum number of rows to return.

since Integer <optional>
0

Start results from this sequence number.

include_docs Boolean <optional>
false

Include documents with results.

timeout Integer <optional>
1000

Maximum period in milliseconds to wait for a change before sending a response, even if there are no results.

heartbeat Integer <optional>
1000

Period in milliseconds after which an empty line is sent. Applicable only to feed types longpoll and continuous. Overrides query.timeout to keep the feed alive indefinitely.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

commit(queryopt, headersopt, callbackopt) → {Promise}

Ensure recent changes are committed to disk.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

compact(designopt, queryopt, headersopt, callbackopt) → {Promise}

Compact database or design.

Parameters:
Name Type Attributes Description
design String <optional>

Design name if compacting design indexes.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

copy(source, target, queryopt, headersopt, callbackopt) → {Promise}

Copy document.

Parameters:
Name Type Attributes Description
source Object

Source document.

Properties
Name Type Attributes Description
id String

Source document ID.

rev String <optional>

Source document revision.

_id String <optional>

Source document ID. Alternate key for source.id.

_rev String <optional>

Source document revision. Alternate key for source.id.

target Object

Target document.

Properties
Name Type Attributes Description
id String

Target document ID.

rev String <optional>

Target document revision.

_id String <optional>

Target document ID. Alternate key for target.id.

_rev String <optional>

Target document revision. Alternate key for target.id.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

create(queryopt, headersopt, callbackopt) → {Promise}

Create database.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

del(docs, queryopt, headersopt, callbackopt) → {Promise}

Delete document(s).

Parameters:
Name Type Attributes Description
docs Object | Array.<Object>

Document or array of documents.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

destroy(queryopt, headersopt, callbackopt) → {Promise}

Destroy database.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

exists(queryopt, headersopt, callbackopt) → {Promise}

Check if database exists.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

find(view, queryopt, headersopt, callbackopt) → {Promise}

Query a view.

Parameters:
Name Type Attributes Description
view String | Object

View name (e.g. mydesign/myview) or temporary view definition. Using a temporary view is strongly not recommended for production use.

query Object <optional>

HTTP query options.

Properties
Name Type Attributes Default Description
key JSON <optional>

Key to lookup.

startkey JSON <optional>

Start returning results from this key.

startkey_docid String <optional>

Start returning results from this document ID. Allows pagination with duplicate keys.

endkey JSON <optional>

Stop returning results at this key.

endkey_docid String <optional>

Stop returning results at this document ID. Allows pagination with duplicate keys.

limit Integer <optional>

Limit number of results returned.

stale Boolean | String <optional>

Do not refresh view even if stale. For CouchDB versions 1.1.0 and up, set to update_after to update view after results are returned.

descending Boolean <optional>
false

Lookup results in reverse order by key, returning documents in descending order by key.

skip Integer <optional>

Skip this many records before returning results.

group Boolean | Integer <optional>
false

Use the reduce function to group results by key. Set to an integer specify group_level.

reduce Boolean | Integer <optional>
true

Use the reduce function.

fetch Boolean <optional>
false

Include document source for each result.

include_end Boolean <optional>
true

Include query.endkey in results.

update_seq Boolean <optional>
false

Include sequence value of the database corresponding to the view.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

follow()

Follow database changes.

Source:
See:
  • `#changes()`.

get(id, queryopt, headersopt, callbackopt) → {Promise}

Fetch document.

Set rev in query.

Parameters:
Name Type Attributes Description
id String

Document ID.

query Object <optional>

HTTP query options.

Properties
Name Type Attributes Description
revs Boolean <optional>

Fetch list of revisions.

revs_info Boolean <optional>

Fetch detailed revision information.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

Get document metadata.

Parameters:
Name Type Attributes Description
id String

Document ID.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

info(queryopt, headersopt, callbackopt) → {Promise}

Get database info.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

post(doc, queryopt, headersopt, callbackopt) → {Promise}

Post document(s) to database.

If documents have no ID, a document ID will be automatically generated on the server. Attachments are not currently supported.

Parameters:
Name Type Attributes Description
doc Object | Array.<Object>

Document or array of documents.

Properties
Name Type Attributes Description
_id String <optional>

Document ID. If set, uses given document ID.

_rev String <optional>

Document revision. If set, allows update to existing document.

_attachments Object <optional>

Attachments. If given, must be a map of filenames to attachment properties.

_attachments[filename] String <optional>

Attachment filename, as hash key.

_attachments[filename].contentType String <optional>

Attachment MIME content type.

_attachments[filename].data String | Object <optional>

Attachment data. Will be Base64 encoded.

query Object <optional>

HTTP query options.

Properties
Name Type Attributes Description
batch Boolean <optional>

Allow server to write document in batch mode. Documents will not be written to disk immediately, increasing the chances of write failure.

all_or_nothing Boolean <optional>

For batch updating of documents, use all-or-nothing semantics.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

purge(revs, queryopt, headersopt, callbackopt) → {Promise}

Purge deleted documents from database.

Parameters:
Name Type Attributes Description
revs Object

Map of document IDs to revisions to be purged.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

put(doc, optionsopt, queryopt, headersopt, callbackopt) → {Promise}

Put document in database.

Parameters:
Name Type Attributes Description
doc Object

Document data. Requires _id and _rev.

options String <optional>

Options.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

replicate(options, queryopt, headersopt, callbackopt) → {Promise}

Replicate database.

This convenience function sets options.source and options.target to the selected database name. Either options.source or options.target must be overridden for a successful replication request.

Parameters:
Name Type Attributes Description
options Options

Options. Accepts all options from Client.replicate().

Properties
Name Type Attributes Default Description
source String <optional>
this.name

Source database URL or local name. Defaults to the selected database name if not given.

target String <optional>
this.name

Target database URL or local name. Defaults to the selected database name if not given.

query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

update(handler, idopt, queryopt, dataopt, headersopt, callbackopt) → {Promise}

Update document using server-side handler.

Parameters:
Name Type Attributes Description
handler String

Update handler. Example: mydesign/myhandler

id String <optional>

Document ID.

query Object <optional>

HTTP query options.

data Object | String <optional>

Data.

headers Object <optional>

Headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise

vacuum(queryopt, headersopt, callbackopt) → {Promise}

Remove unused views.

Parameters:
Name Type Attributes Description
query Object <optional>

HTTP query options.

headers Object <optional>

HTTP headers.

callback handler <optional>

Callback function.

Source:
See:
Returns:

A Promise, if no callback is provided, otherwise null.

Type
Promise