0.3.1 • Published 9 years ago

express-persistent-resource v0.3.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

express-persistent-resource

Build Status Version Downloads License Join the chat at https://gitter.im/dominikschreiber/express-persistent-resource

express-persistent-resource creates a full-featured CRUD resource (express) middleware that persists all information to a given CouchDB instance. It's as simple as

var resource = require('express-persistent-resource');
app.use('/bread', resource('bread', 'http://localhost:5984/awesome-food', {
  fields: 'title,ingredients:(title)'
}));

You get

GET /bread => list of all bread urls, e.g. ['/bread/french', '/bread/rye', '/bread/brown']
POST /bread => url to the newly created bread, e.g. '/bread/pita'

GET /bread/:id => the single bread
PUT /bread/:id => update of the single bread, gives the url of the updated bread
DELETE /bread/:id => deletes the single bread

as well as advanced features like fields selection and pagination (tbd) as described in Web API Design Crafting Interfaces that Developers Love.

getting started

Grab yourself a npm and go

npm install express-persistent-resource

Then set up a CouchDB instance (e.g. Cloudnode or Cloudant offer hosted instances), grab its url and start rolling:

var express = require('express')
  , resource = require('express-persistent-resource')
  , couchdbInstance = 'your://couch.db/instance' // e.g. http://localhost:5984/awesome-project
  , app = express();

app.use('/api/v1/awesome', resource('awesome', couchdbInstance, {
   ...
}));

app.use('/api/v1/evenmore', resource('evenmore', couchdbInstance, {
   ...
}))

api documentation

require('express-persistent-resource')(name, db, options)

  • name is the name of the resource. This will be prefixed to all CouchDB entries assigned to this resource.
  • db is either a url to a CouchDB instance or a nano-wrapped CouchDB instance (useful if you have more than one resource)
  • options configure the resource creation/storing process. A subset of
    • fields (required): properties the stored entries are allowed to have. POSTed entries are validated against this. See test/model.spec.js for details. In short, this is a string like id,name:(givenname,familyname),age, where each :(...) denotes a nested object/a list of nested objects.
    • id: function that determines how ids of this resource should look like. Is called with the incoming entry to create an id for it. Defaults to a substring of the sha256-hash of the entry.
    • views: additional map/reduce views given to the CouchDB service. May be removed in the future. See the CouchDB Guide for details (this property is merged into the views property of the design document).

features ( planned, x implemented)

  • /: all resources
    • OPTIONS /: list methods
    • GET /: list resource urls
    • POST /: create resource
    • PUT /: bulk update resources
    • DELETE /: delete all resources
  • /:id: a single resource
    • GET /:id: read resource
    • POST /:id: error -> use PUT /:id or POST /
    • PUT /:id: update resource
    • DELETE /:id: delete resource
  • /:id/:field: all nested resources
    • GET /:id/:field: list nested resources (simulate with GET /:id?fields=:field)
    • POST /:id/:field: create nested resource (added to :field list)
    • PUT /:id/:field: bulk update nested resources
    • DELETE /:id/:field: delete all nested resources
  • /:id/:field/:id: a single nested resource
    • GET /:id/:field/:id: read nested resource (simulate with GET /:id/:field, then GET field url)
    • POST /:id/:field/:id: error -> use PUT /:id/:field/:id or POST /:id/:field
    • PUT /:id/:field/:id: update nested resource
    • DELETE /:id/:field/:id: delete nested resource
  • ?: query parameters
    • ?include_docs: when GET /?include_docs, list docs instead of urls
    • ?field=filter: list resources that match filter on field. Support
      • =: exact match
      • ~=: one of
      • |=: exact match or starts with + - (namespacing)
      • ^=: starts with
      • $=: ends with
      • *=: contains
    • ?fields=: partial response (filtered by model.validate)
    • ?limit= and ?offset=: pagination (limit entries per call, offset entries skipped)
    • ?q=: search resources for query
    • ?method=: override http method with method (GET /?method=POST equals POST /)
    • ?suppress_response_codes=true: override response code with 200, put response code in result
  • .:ext: resource serialization
    • .json: (default) resources as json
    • .xml: resources as xml
    • .yml: resources as yaml
  • Accept:: resource serialization
    • */json: resources as json
    • */xml: resources as xml
    • */yml: resources as yaml

changelog

0.2.1

  • feature: GET /:id?fields=<fields> returns subset of /:id matching <fields>

0.2.0

  • feature: GET /?fields=<fields>&include_docs returns result subsets of only <fields>

0.1.1

  • fix: DELETE /:id Accept:application/json returns "/" instead of / now

0.1.0

  • feature: GET /?field=filter filters resources based on the given filter, allowing nested fields via a string for model.parse

0.0.4

  • feature: Accept: */json + Accept: */xml based resource serialization
  • feature: GET /?include_docs returns list of docs instead of urls

0.0.3

  • fix: fields strings with multiple nested objects (e.g. foo:(bar,baz),goo:(g,le)) are now handled correctly

0.0.2

  • feature: DELETE /:id deletes the document specified by :id, returns url of the resource mount point (e.g. /api/v1/cat)

0.0.1

  • feature: GET / returns list of urls relative to / (e.g. when mounted at /api/v1/cat: ['/api/v1/cat/foo','/api/v1/cat/bar'])
  • feature: POST / creates a new document, returns url of that document (e.g. /api/v1/cat/baz)
  • feature: GET /:id returns the document specified by :id
  • feature: PUT /:id updates the document specified by :id, returns url of the updated document (e.g. /api/v1/cat/baz)

license

The MIT License (MIT)

Copyright (c) 2015 Dominik Schreiber

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0.3.1

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago