Advertisement
Guest User

Cloudant query

a guest
Jan 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const express = require('express');
  4. const router = express.Router();
  5. const Cloudant = require('cloudant');
  6. const debug = require('debug')('hackanoi:server');
  7.  
  8. const account = process.env.CLOUDANT_USERNAME;
  9. const password = process.env.CLOUDANT_PASSWORD;
  10.  
  11. const cloudant = Cloudant({account, password, plugin:'promises'}, (err, db) => {
  12. if (! err) {
  13. debug('Connect to Cloudant successfully');
  14. }
  15. });
  16. const db = cloudant.db.use('rooms');
  17.  
  18. router.get('/', (req, res, next) => {
  19. const defaultOptions = {
  20. selector: {
  21. _id: {
  22. '$gt': 0
  23. }
  24. },
  25. // limit: 10,
  26. sort: [
  27. {
  28. _id: 'desc'
  29. }
  30. ]
  31. };
  32.  
  33. db.find(defaultOptions)
  34. .then(data => {
  35. res.json({
  36. status: 'success',
  37. data: data.docs
  38. });
  39. })
  40. .catch(err => {
  41. debug(err);
  42. res.json(err)
  43. })
  44. });
  45.  
  46. router.get('/:rid', (req, res, next) => {
  47. const options = {
  48. "selector": {
  49. "rid": {
  50. "$eq": `${req.params.rid}`
  51. }
  52. },
  53. "sort": [
  54. {
  55. "_id": "desc"
  56. }
  57. ],
  58. // "limit": 10
  59. }
  60.  
  61. db.find(options)
  62. .then(data => {
  63. res.json({
  64. status: 'success',
  65. data: data.docs
  66. });
  67. })
  68. .catch(err => {
  69. debug(err);
  70. res.json(err)
  71. })
  72. })
  73.  
  74. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement