Created
August 8, 2015 03:58
-
-
Save krhoyt/8fa2683d3ae02c6275fd to your computer and use it in GitHub Desktop.
Full CRUD for Cloudant on Express 4.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var router = require( 'express' ).Router(); | |
var Reading = { | |
// Get all readings | |
// Get latest reading | |
// Get readings within a page | |
getReadings: function( req, res ) { | |
var params = null; | |
// Query parameters | |
params = { | |
selector: { | |
timestamp: {'$gt': 0} | |
}, | |
sort: [ | |
{timestamp: 'desc'} | |
] | |
}; | |
// Support getting only latest reading | |
// Also supports paging | |
if( req.query.limit != undefined ) | |
{ | |
params.limit = parseInt( req.query.limit ); | |
} | |
// More support for paging | |
// Arguments supplied via query string | |
if( req.query.skip != undefined ) | |
{ | |
params.skip = parseInt( req.query.skip ); | |
} | |
// Find using selector | |
// Returns documents | |
// Not the list function | |
req.data.find( params, function( error, body ) { | |
if( error ) | |
{ | |
req.logger.info( 'Problem reading documents.' ); | |
req.logger.info( 'Message: ' + error.message ); | |
} | |
res.json( body ); | |
} ); | |
}, | |
// Get a specific reading by ID | |
getReading: function( req, res ) { | |
req.data.get( req.params.id, function( error, body ) { | |
if( error ) | |
{ | |
req.logger.info( 'Problem reading document.' ); | |
req.logger.info( 'Message: ' + error.message ); | |
} | |
res.json( body ); | |
} ); | |
}, | |
// Create a new reading | |
createReading: function( req, res ) { | |
req.data.insert( req.body, function( error, body ) { | |
if( error ) | |
{ | |
req.logger.info( 'Problem creating document.' ); | |
req.logger.info( 'Message: ' + error.message ); | |
} | |
res.json( body ); | |
} ); | |
}, | |
// Update an existing reading by ID | |
// Must include _id and _rev | |
// TODO: Assume latest revision | |
updateReading: function( req, res ) { | |
req.data.insert( req.body, function( error, body ) { | |
if( error ) | |
{ | |
req.logger.info( 'Problem updating document.' ); | |
req.logger.info( 'Message: ' + error.message ); | |
} | |
res.json( body ); | |
} ); | |
}, | |
// Delete an existing reading by ID | |
deleteReading: function( req, res ) { | |
req.data.destroy( req.params.id, req.query.rev, function( error, body ) { | |
if( error ) | |
{ | |
req.logger.info( 'Problem destroying document.' ); | |
req.logger.info( 'Message: ' + error.message ); | |
} | |
res.json( body ); | |
} ); | |
} | |
}; | |
// Routes | |
router.get( '/reading', Reading.getReadings ); | |
router.post( '/reading', Reading.createReading ); | |
router.get( '/reading/:id', Reading.getReading ); | |
router.put( '/reading/:id', Reading.updateReading ); | |
router.delete( '/reading/:id', Reading.deleteReading ); | |
// Module export | |
module.exports = exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment