Git Product home page Git Product logo

davenport's Introduction

Davenport

Davenport is a CouchDB client for simplifying common tasks like get, list, create, update and delete. It comes complete with full TypeScript definitions.

Installing

You can install Davenport with either npm or Yarn:

npm install davenport --save

# Or use Yarn
yarn add davenport

Importing

Davenport can be imported via ES6-style default import syntax, or via Node's require:

// ES6
import Client from "davenport";

// require
const Client = require("davenport").Client;

Using Davenport in the browser

Davenport can be used in the browser, so long as you transpile the async functions with TypeScript or Babel. Just add the node_modules/davenport/bin/browser.js to your web page, and all functions/objects documented below will be available under the Davenport variable.

Make sure your CouchDB installation is configured to allow cross-origin resource sharing (CORS), otherwise Davenport won't be able to connect!

Enable CORS

Async/await and promises

All Davenport functions are implemented as async/awaitable promises. You'll need Node.js v4 and above to use Davenport, as Node v3 and below don't support the generators needed for async/await.

Because async/await implements a promise-like interface in ES6, you can use the functions in this library in two different ways:

With async/await:

//1. async/await
const foo = await client.get(id);

//Do something with the object

With promises:

const foo = client.get(id).then((shop) => {
    //Do something with the object.
}); 

Both methods are supported and the results won't differ. The only difference is an awaited method will throw an error if the method fails, where a promise would just fail silently unless you use .catch.

For the sake of being concise, all examples in this doc will use async/await.

Client vs configureDatabase

Davenport exports a configureDatabase function that can help you create a database, add indexes, set up design documents with views, and then returns a client ready to interact with that database. It will also check that your CouchDB server is at least version 2.0, which is required for many of the functions used by Davenport.

// ES6
import { configureDatabase } from "davenport";

// require
const configureDatabase = require("davenport").configureDatabase;

// Configure the database with an index on the 'foo' object property, and a view that lists all foos greater than 5.
const designDoc = {
    name: "list",
    views: [{
        name: "only-foos-greater-than-5",
        map: function (doc: TestObject) {
            if (doc.foo > 5) {
                emit(doc._id, doc);
            }
        }.toString(),
        reduce: "_count"
    }]
}

const client = await configureDatabase(DB_URL, {
    name: "my-foo-database",
    designDocs: [designDoc]
})

You don't need to use the configureDatabase function to interact with your database, though. If you have no need for setting up design docs or indexes, just instantiate a new Client while passing in a database URL and database name.

import Client from "davenport";

const client = new Client(DB_URL, "my-foo-database");

Typescript declarations

Using TypeScript? The TypeScript compiler will automatically pull in Davenport definitions for you when you install Davenport, as long as you're using TypeScript 2+.

Pass your CouchDoc extending interface to the configureDatabase and new Client functions to get full TypeScript support for all client methods:

import Client, { CouchDoc } from "davenport";

interface Foo extends CouchDoc {
    foo: number,
    bar: number,
}

const client = new Client<Foo>(DB_URL, "my-foo-database");
const myFoo = await client.get(id);

// TypeScript automatically knows that variable `myFoo` is a Foo object.

davenport's People

Contributors

abecher22 avatar nozzlegear avatar stormalong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

davenport's Issues

Stringify query arrays to JSON values.

Axios serializes arrays in the querystring to ?keys[]=abcd&keys[]=1234, but CouchDB expects the array to be JSON e.g. ?keys=['abcd','1234']. Current implementation in Davenport means any keys specified in the Davenport.view method will be ignored.

Workaround for now is to JSON.stringify your keys array:

const result = await db.view("list", "view-name", { keys: JSON.stringify(["abcd", ["1234"]) });

Davenport's use is ok in ES5 Browser when transpiled?

Hi there,

I have this working in Node v6 (using the Async/Await, etc) just fine, however I'm getting a 'GET http://localhost:3000/davenport 404 (Not Found)' error and a 'ZoneAwareError' in Chrome browser (I'm using Angular 2.4). Is Davenport ok to work in the ES5 browser?

I'm using Typescript 2.1 and can confirm the normal Async/Await feature is working in the browser. However, whenever I put this getMessage function in a class:

...
import Client, { CouchDoc } from 'davenport';
import {Foo} from './Foo';
...

export class AppComponent {

    private async getMessage() {
      let s = 'Some random string';
      
      const client = new Client<Foo>('http://127.0.0.1:5984', 'hello-world');  // error when this line is here
      
      this.myStrToDisplay = s;
    }

}

Typescript builds just fine, but the errors appear in the browser when I put the const client = new Client<Foo>('http://127.0.0.1:5984', 'hello-world'); line in there.

It can't seem to find Davenport. Maybe, are you able to provide some example code at all?

Cheers,
bradws

Add a .merge function

Updating a document right now requires pulling the current document from the database, modifying it, and then sending the entire thing back through the PUT request with the revision. CouchDB supports an update function that we can use to merge partial documents into the original document and then return the new doc. It would need to be set up in a unique view, so Davenport should check that the view with update function has been set up before running the merge function.

https://wiki.apache.org/couchdb/Partial_Updates

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.