Skip to content

grimen/node-document-api

Repository files navigation

NODE-DOCUMENT-API preview Build Status

HTTP API endpoint/middleware for node-document ODM for Node.js.

Work in progress; see TODO.

About

Unified HTTP API for write/read data to/from differen kinds of storages/databases.

HTTP API

POST

  • (:route)/:type

    HTTP POST /post
      {
        "title": "Post",
        "description": "Lorem ipsum..."
      }
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": "eaf56300-7785-4c9a-a103-6895f873bff7",
        "title": "Post",
        "description": "Lorem ipsum..."
      }
    ]
  • (:route)/:type/:id

    HTTP POST /post/1
      {
        "title": "Post 1",
        "description": "Lorem ipsum..."
      }
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      }
    ]
  • (:route)/:type/:id,...,:id

    HTTP POST /post/1,2,3
      [
        {
          "title": "Post 1",
          "description": "Lorem ipsum..."
        },
        {
          "title": "Post 2",
          "description": "Lorem ipsum..."
        },
        {
          "title": "Post 3",
          "description": "Lorem ipsum..."
        }
      ]
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 2,
        "title": "Post 2",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 2,
        "title": "Post 2",
        "description": "Lorem ipsum..."
      }
    ]

PUT

  • (:route)/:type/:id

    HTTP PUT /post/1
      {
        "title": "Post 1",
        "description": "Lorem ipsum...",
        "extra": true
      }
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum...",
        "extra": true
      }
    ]
  • (:route)/:type/:id,...,:id

    HTTP PUT /post/1,2,3
      [
        {
          "title": "Post 1",
          "description": "Lorem ipsum...",
          "extra": true
        },
        {
          "title": "Post 2",
          "description": "Lorem ipsum...",
          "extra": true
        },
        {
          "title": "Post 3",
          "description": "Lorem ipsum...",
          "extra": true
        }
      ]
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 2,
        "title": "Post 2",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 2,
        "title": "Post 2",
        "description": "Lorem ipsum..."
      }
    ]

GET

  • (:route)/:type/:id

    HTTP GET /post/1
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      }
    ]
  • (:route)/:type/:id,...,:id

    HTTP GET /post/1,2,3
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 2,
        "title": "Post 2",
        "description": "Lorem ipsum..."
      },
      {
        "_type": "post",
        "_id": 3,
        "title": "Post 3",
        "description": "Lorem ipsum..."
      }
    ]

DELETE

  • (:route)/:type/:id

    HTTP DELETE /post/1
    
    /*
      200 OK
    */
    [
      {
        "_type": "post",
        "_id": 1,
        "title": "Post 1",
        "description": "Lorem ipsum..."
      }
    ]
  • (:route)/:type/:id,...,:id

    HTTP DELETE /post/1,2,3
    
    /*
      200 OK
    */
    [
      false,
      true,
      true
    ]

Example

Using Connect.js:

  var connect = require('connect')
  var http = require('http');

  var Document = require('node-document');
  var API = require('node-document-api');

  var Post = Document('Post');

  Post.api = API({route: '/api'});

  var app = connect();

  app
    .use(Post.api)
    .use(function(req, res) {
      res.end("Hello world!");
    });

  http.createServer(app).listen(3000, function() {
    console.log('[node-document-api/examples/connect-example.js]: Listening on port %s', 3000);
  });

Using Express.js:

  var express = require('express')

  var Document = require('node-document');
  var API = require('node-document-api');

  var Post = Document('Post');

  Post.api = API({route: '/api'});

  var app = express();

  app
    .use(Post.api)
    .use(function(req, res) {
      res.end("Hello world!");
    });

  app.listen(3000, function() {
    console.log('[node-document-api/examples/express-example.js]: Listening on port %s', 3000);
  });

Installation

  $ npm install node-document-api

Test

Local tests:

  $ make test

License

Released under the MIT license.

Copyright (c) Jonas Grimfelt

Bitdeli Badge

About

HTTP API endpoint/middleware for `node-document` ODM for Node.js.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published