npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ig3/couchdb

v2.0.2

Published

Simple CouchDB client

Downloads

1

Readme

@ig3/couchdb

Simple CouchDB client

Install

Use npm to install.

npm install @ig3/couchdb

Getting started

To use @ig3/couchdb you need to connect to your CouchDB server:

var couch = require('@ig3/couchdb')({
    hostname: 'localhost',
    port:     5984,
    protocol: 'http',
    username: 'admin',
    password: 'secret'
});

To get server meta information:

couch.get('/')
.then(function(info) {
    console.log(info);
});

Configuration

To configure @ig3/couchdb to access your CouchDB server:

var couch = require('@ig3/couchdb')({
    hostname: 'localhost',
    port:     5984,
    protocol: 'http',
    username: 'admin',
    password: 'secret'
});

Methods

couch.db(opts)

To configure a connection to a database:

var db = couch.db({
    db_name: 'mydb',
    username:   'user',
    password:   'mypassword'
});

The username and password are optional. If they are not provided, the username and password of the server are used.

couch.get(path)

HTTP GET against an arbitrary path on the server.

couch.get('/')
.then(function(info) {
  console.log('CouchDB meta information: ' + JSON.stringify(info));
})
.catch(function(err) {
  console.log('GET / failed with: ', err);
  throw err;
});

This does an HTTP GET on http://hostname:port/path.

If the path does not begin with '/' then '/' is prepended to the path.

couch.post(path,data)

Send an HTTP POST request with an arbitrary path and data.

Path is the path part of the URL to the server.

Data is and object that will be transformed to JSON and sent as the body of the POST request.

Returns a promise that resolves to the CouchDB response or an error.

couch.post('/my_db/_bulk_docs', {
  all_or_nothing: true,
  docs: [{
    _id: 'new_doc_id',
    data: 'some data'
  }]
})
.then(function(info) {
  console.log('POST response: ' + JSON.stringify(info));
})
.catch(function(err) {
  console.log('POST failed with: ', err);
  throw err;
});

Database functions

db.changes(opts)

Requests a continuous change feed and returns an event emitter that emits the following event:

  • change - for each change received
  • error - if there is an error

The changes feed can be cancelled by calling the cancel() method.

var changes = db.changes();

changes.on('change', function(change) {
    console.log('got a change ' + change);
});
changes.on('error', function(error) {
    console.log('oops');
});

changes.cancel();

db.get(path)

HTTP GET from a path relative to the database.

var id = 'some_document_id';
db.get(id)
.then(function(info) {
    console.log('got doc: ' + JSON.stringify(info));
})
.catch(function(err) {
    console.log('get ' + id + ' failed with: ', err);
    throw err;
});

The request is to http://hostname:port/db_name/path.

The path may include query parameters:

var id = 'some_document_id';
db.get(id + '?meta=true')
.then(function(info) {
    console.log('got doc: ' + JSON.stringify(info));
})
.catch(function(err) {
    console.log('get ' + id + ' failed with: ', err);
    throw err;
});

db.post(path,data)

Send an HTTP POST request with an arbitrary path and data.

Path is appended to the database name.

Data is and object that will be transformed to JSON and sent as the body of the POST request.

Returns a promise that resolves to the CouchDB response or an error.

db.post('_bulk_docs', {
    all_or_nothing: true,
    docs: [{
        _id: 'new_doc_id',
        data: 'some data'
    }]
})
.then(function(info) {
    console.log('POST response: ' + JSON.stringify(info));
})
.catch(function(err) {
    console.log('POST failed with: ', err);
    throw err;
});

db.purge(id)

Purge the document with the given id from the database.

Returns a promise wich resolves to an info object if the doc is purged or an error.

db.purge('some_id')
.then(function(info) {
    console.log('document was purged');
})
.catch(function(err) {
    console.log('purge failed with ', err);
});

db.put(path, data)

Submit an HTTP PUT request to path, prefixed with database name, and with given data.

Returns a promise wich resolves to an info object or an error.

db.put('new_doc', {
    _id: 'new_doc',
    data: 'some data'
})
.then(function(info) {
    console.log('put succeeded');
})
.catch(function(err) {
    console.log('put failed with ', err);
});

db.soft_delete(doc)

Given a doc, set property deleted_time to current time and _deleted to true.

Returns a promise that resolves to the updated version of the doc or an error.

db.get('some_doc')
.then(function(doc) {
    return db.soft_delete(doc);
})
.then(function(info) {
    console.log('put succeeded');
})
.catch(function(err) {
    console.log('put failed with ', err);
});

Changes

2.0.0 - 20231104

  • Change the module export to the server factory.
  • Eliminate dependencies other than node core modules
  • Rewrite tests using multi-tape, tape and nock

2.0.1 - 20240620

  • Minimize published package size
  • Update dependencies

2.0.2 - 20240709

  • Rewrite tests using node:test and node:assert
  • Rewrite tests using tape and express instead of nock