Giter VIP home page Giter VIP logo

express-couchdb-core's Introduction

Express CouchDb Core

This module provides a generic restful api for accessing couchdb documents. By dropping this module in your express application you can immediately start managing couchdb documents via a restful crud interface in your single page application. This module only includes list, create, read, update and delete, it does not include any view or query functionality, this has to be implemented in your express application.

Install

npm install express-couchdb-core --save
// add model design and all view to couchDb
node init.js [couchdb]

Usage

var core = require('express-couchdb-core');
var config = { couch: 'http://localhost:5984/foo' };
app.config(function() {
  // handle other requests first
  app.use(express.router);
  // handle core requests
  app.use(core(config));
});

Optional configuration

If you would like to dynamically change which database is being used, then use the following configuration and set the request parameter given to the name of the database to use. The database_parameter_name is optional and will default to "COUCH_DB".

var config = { url: 'http://localhost:5984', database_parameter_name: 'COUCH_DB'}

API

The api uses the term model to represent the document type, and it is assumed that when you store your documents you will use the type node in the document to represent the model type. For example,

{
  "_id": "123456789",
  "name": "Johnny Paper",
  "type": "person"
}

RESTFul call

GET /api/person

will return all documents of type "person"

GET /api/:model

Returns a list of documents of the type :model

POST /api/:model

Create a document of type :model

GET /api/:model/:id

Returns a couch document

PUT /api/:model/:id

Updates an existing couch document or creates a couch document with specific id

DEL /api/:model/:id

Deletes a couch document

Adding Child docs

This module comes with a feature to add child docs that can also be pulled down with a get request from the parent as an array in the parent node.

For Example:

POST /api/foo

{
  "_id": 1,
  "_rev": 1,
  "name": "bar",
  "type": "foo"
}

Next we post a child document to foo and we use the underscore to indicate it is a child document type and use the [model]_id to assign ownership to the parent document.

POST /api/foo_bar

{
  "_id": 2,
  "_rev": 1,
  "name": "baz",
  "type": "foo_bar",
  "foo_id": 1
}

This will link the document type foo_bar as a child to document type foo. And when you get the foo document of id 1, it will return the foo document with a key called foo_bar: [] and the child document in that array.

GET /api/foo/1

{
  "_id": 1,
  "_rev": 1,
  "name": "bar",
  "type": "foo",
  "foo_bar": [{
    "_id": 2,
    "_rev": 1,
    "name": "baz",
    "type": "foo_bar",
    "foo_id": 1
  }]
}

you can have multiple children docs attached to a parent doc.

License

MIT

Contributions

Pull Requests Welcome

express-couchdb-core's People

Contributors

brentcobb avatar jaymcaliley avatar warrensplayer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.