If you're using a full ORM (namely: sequelize), consider using express-rest-orm instead. If not, consider using both sequelize and express-rest-orm.
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.
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, {
...
}))
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 offields
(required): properties the stored entries are allowed to have.POST
ed entries are validated against this. See test/model.spec.js for details. In short, this is a string likeid,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 theviews
property of the design document).
-
/
: 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 -> usePUT /:id
orPOST /
-
PUT /:id
: update resource -
DELETE /:id
: delete resource
-
-
/:id/:field
: all nested resources-
GET /:id/:field
: list nested resources (simulate withGET /: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 withGET /:id/:field
, thenGET
field url) -
POST /:id/:field/:id
: error -> usePUT /:id/:field/:id
orPOST /:id/:field
-
PUT /:id/:field/:id
: update nested resource -
DELETE /:id/:field/:id
: delete nested resource
-
-
?
: query parameters-
?include_docs
: whenGET /?include_docs
, list docs instead of urls -
?field=filter
: listresource
s that matchfilter
onfield
. Support-
=
: exact match -
~=
: one of -
|=
: exact match or starts with +-
(namespacing) -
^=
: starts with -
$=
: ends with -
*=
: contains
-
-
?fields=
: partial response (filtered bymodel.validate
) -
?limit=
and?offset=
: pagination (limit
entries per call,offset
entries skipped) -
?q=
: search resources for query -
?method=
: override http method withmethod
(GET /?method=POST
equalsPOST /
) -
?suppress_response_codes=true
: override response code with200
, 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
-
- feature:
GET /:id?fields=<fields>
returns subset of/:id
matching<fields>
- feature:
GET /?fields=<fields>&include_docs
returns result subsets of only<fields>
- fix:
DELETE /:id Accept:application/json
returns"/"
instead of/
now
- feature:
GET /?field=filter
filters resources based on the given filter, allowing nested fields via a string formodel.parse
- feature:
Accept: */json
+Accept: */xml
based resource serialization - feature:
GET /?include_docs
returns list of docs instead of urls
- fix: fields strings with multiple nested objects (e.g.
foo:(bar,baz),goo:(g,le)
) are now handled correctly
- feature:
DELETE /:id
deletes the document specified by:id
, returns url of the resource mount point (e.g./api/v1/cat
)
- 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
)
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.