Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CouchDB Day NYC 2017: JSON Documents

300 views

Published on

CouchDB is a document database. It stores JSON objects with a few special field names. The _id field represents a unique identifier for a document. The _rev field is the revision marker for a document. The _rev field is used for Multi-Version Concurrency Control, a form of optimistic concurrency.

Published in: Software
  • Be the first to comment

CouchDB Day NYC 2017: JSON Documents

  1. 1. Lab: JSON Documents (30 minutes) Bradley Holt, Developer Advocate Thursday, February 9, 2017 CouchDB Developer Day @BradleyHolt
  2. 2. JSON Documents { _id: "6EF9D2B0-13D3-1378-8D30-39E3CE0B36C2", _rev: "1-0b457efcf82fb29492ef927ba5b6ee15", type: "Feature", geometry: { type: "Point", coordinates: [ -71.1028, 42.3691 ] }, properties: { session_id: "3486b13f-7b8a-8a96-dfbf-9b82800e367f", timestamp: 1422928591717 } } @BradleyHolt
  3. 3. http-console $ npm install http-console -g @BradleyHolthttps://github.com/cloudhead/http-console
  4. 4. http-console $ npm install http-console -g $ http-console 127.0.0.1:5984 @BradleyHolthttps://github.com/cloudhead/http-console
  5. 5. http-console $ npm install http-console -g $ http-console 127.0.0.1:5984 > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to 127.0.0.1 on port 5984. @BradleyHolthttps://github.com/cloudhead/http-console
  6. 6. Connecting to CouchDB 2.0 $ http-console root:passw0rd@127.0.0.1:5984 --json @BradleyHolt
  7. 7. Connecting to CouchDB 2.0 $ http-console root:passw0rd@127.0.0.1:5984 --json > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to 127.0.0.1 on port 5984. @BradleyHolt
  8. 8. Connecting to IBM Cloudant $ http-console https://bradley-holt:passw0rd@bradley-holt.cloudant.com --json @BradleyHolt
  9. 9. Connecting to IBM Cloudant $ http-console https://bradley-holt:passw0rd@bradley-holt.cloudant.com --json > http-console 0.6.3 > Welcome, enter .help if you're lost. > Connecting to bradley-holt.cloudant.com on port 443. @BradleyHolt
  10. 10. PUT a Database /> @BradleyHolt
  11. 11. PUT a Database /> PUT /kittens @BradleyHolt
  12. 12. PUT a Database /> PUT /kittens ... @BradleyHolt
  13. 13. PUT a Database /> PUT /kittens ... HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens { ok: true } @BradleyHolt
  14. 14. POST a Document /> @BradleyHolt
  15. 15. POST a Document /> /kittens @BradleyHolt
  16. 16. POST a Document /kittens> @BradleyHolt
  17. 17. POST a Document /kittens> POST / @BradleyHolt
  18. 18. POST a Document /kittens> POST / ... @BradleyHolt
  19. 19. POST a Document /kittens> POST / ... { "_id": "mittens", "age_weeks": 10, "weight_kilograms": 0.997 } @BradleyHolt
  20. 20. POST a Document /kittens> POST / ... { "_id": "mittens", "age_weeks": 10, "weight_kilograms": 0.997 } HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens { ok: true, id: 'mittens', rev: '1-e665a40d9ea9711c983e907f0b0b6e8a' } @BradleyHolt
  21. 21. GET All Documents /kittens> @BradleyHolt
  22. 22. GET All Documents /kittens> GET /_all_docs @BradleyHolt
  23. 23. GET All Documents /kittens> GET /_all_docs HTTP/1.1 200 OK Content-Type: application/json Etag: "92afa0f309a9fd9f140cd31ff5000b5c" { total_rows: 1, offset: 0, rows: [ { id: 'mittens', key: 'mittens', value: { rev: '1-e665a40d9ea9711c983e907f0b0b6e8a' } } ] } @BradleyHolt
  24. 24. GET a Document /kittens> @BradleyHolt
  25. 25. GET a Document /kittens> GET /mittens @BradleyHolt
  26. 26. GET a Document /kittens> GET /mittens HTTP/1.1 200 OK Content-Type: application/json Etag: "1-e665a40d9ea9711c983e907f0b0b6e8a" { _id: 'mittens', _rev: '1-e665a40d9ea9711c983e907f0b0b6e8a', age_weeks: 10, weight_kilograms: 0.997 } @BradleyHolt
  27. 27. PUT an Attachment /kittens> @BradleyHolt
  28. 28. PUT an Attachment /kittens> .headers @BradleyHolt
  29. 29. PUT an Attachment /kittens> .headers Accept: application/json Content-Type: application/json Authorization: Basic cm9vdDorMi95N3Y2aA== Host: 127.0.0.1 @BradleyHolt
  30. 30. PUT an Attachment /kittens> .headers Accept: application/json Content-Type: application/json Authorization: Basic cm9vdDorMi95N3Y2aA== Host: 127.0.0.1 /kittens> Content-Type: image/gif @BradleyHolt
  31. 31. PUT an Attachment /kittens> @BradleyHolt
  32. 32. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a @BradleyHolt
  33. 33. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... @BradleyHolt
  34. 34. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= @BradleyHolt
  35. 35. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } @BradleyHolt
  36. 36. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } /kittens> @BradleyHolt
  37. 37. PUT an Attachment /kittens> PUT /mittens/photo?rev=1-e665a40d9ea9711c983e907f0b0b6e8a ... R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= HTTP/1.1 201 Created Content-Type: application/json Location: http://127.0.0.1/kittens/mittens/photo { ok: true, id: 'mittens', rev: '2-d858e51453a5785bafe517b7eddc5a98' } /kittens> Content-Type: application/json @BradleyHolt
  38. 38. GET an Attachment /kittens> @BradleyHolt
  39. 39. GET an Attachment /kittens> GET /mittens/photo @BradleyHolt
  40. 40. GET an Attachment /kittens> GET /mittens/photo HTTP/1.1 200 OK Content-Type: image/gif Etag: "UsqjdPnY6ApD2ENFOglFHg==" R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs= @BradleyHolt
  41. 41. Conditional Caching /kittens> @BradleyHolt
  42. 42. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" @BradleyHolt
  43. 43. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> @BradleyHolt
  44. 44. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo @BradleyHolt
  45. 45. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified @BradleyHolt
  46. 46. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified /kittens> @BradleyHolt
  47. 47. Conditional Caching /kittens> If-None-Match: "UsqjdPnY6ApD2ENFOglFHg==" /kittens> GET /mittens/photo HTTP/1.1 304 Not Modified /kittens> If-None-Match: @BradleyHolt
  48. 48. DELETE a Document /kittens> @BradleyHolt
  49. 49. DELETE a Document /kittens> DELETE /mittens @BradleyHolt
  50. 50. DELETE a Document /kittens> DELETE /mittens HTTP/1.1 409 Conflict Content-Type: application/json { error: 'conflict', reason: 'Document update conflict.' } @BradleyHolt
  51. 51. DELETE a Document /kittens> @BradleyHolt
  52. 52. DELETE a Document /kittens> HEAD /mittens @BradleyHolt
  53. 53. DELETE a Document /kittens> HEAD /mittens HTTP/1.1 200 OK Etag: "2-d858e51453a5785bafe517b7eddc5a98" @BradleyHolt
  54. 54. DELETE a Document /kittens> @BradleyHolt
  55. 55. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" @BradleyHolt
  56. 56. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> @BradleyHolt
  57. 57. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens @BradleyHolt
  58. 58. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } @BradleyHolt
  59. 59. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } /kittens> @BradleyHolt
  60. 60. DELETE a Document /kittens> If-Match: "2-d858e51453a5785bafe517b7eddc5a98" /kittens> DELETE /mittens HTTP/1.1 200 OK Content-Type: application/json { ok: true, id: 'mittens', rev: '3-d0780627ddff7a7f536fe273100cec41' } /kittens> If-Match: @BradleyHolt
  61. 61. Delete the Database /> @BradleyHolt
  62. 62. Delete the Database /> DELETE /kittens @BradleyHolt
  63. 63. Delete the Database /> DELETE /kittens HTTP/1.1 200 OK Content-Type: application/json { ok: true } @BradleyHolt
  64. 64. Try It On Your Own 1. Create a database 2. Create a document in the database 3. Read the document 4. Update the document 5. Delete the document 6. Delete the database @BradleyHolt
  65. 65. Lab: JSON Documents CouchDB Developer Day @BradleyHolt

×