christroutner

orbit-db-kv

Jun 9th, 2021
621
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Looking at the replication events and data for an OrbitDB key-value store.
  3.     Here is a snippet of the code I'm looking at, based on this Event
  4.     documentation:
  5.     https://github.com/orbitdb/orbit-db-store#events
  6. */
  7.  
  8. // Load data persisted to the hard drive.
  9. await this.db.load()
  10.  
  11. // console.log('this.db: ', this.db)
  12.  
  13. // Studying events:
  14. // https://github.com/orbitdb/orbit-db-store#replicationstatus
  15. this.db.events.on('replicated', (address, count) => {
  16.   console.log('replicated event fired')
  17.   console.log('replicated address: ', address)
  18.   console.log('replicated count: ', count)
  19. })
  20.  
  21. this.db.events.on('replicate', (address, entry) => {
  22.   console.log('replicate event fired')
  23.   console.log('replicate address: ', address)
  24.   console.log('replicate entry: ', entry)
  25.  
  26.   const data = this.db.get(entry)
  27.   console.log('entry data: ', data)
  28.  
  29.   const all = this.db.all
  30.   console.log('all entries: ', all)
  31. })
  32.  
  33. this.db.events.on('write', (address, entry, heads) => {
  34.   console.log('write event fired!')
  35.   console.log('write address: ', address)
  36.   console.log('write entry: ', entry)
  37.   console.log('write heads: ', heads)
  38. })
  39.  
  40. console.log('...finished createDb()')
  41.  
  42.  
  43.  
  44. /*
  45.     The above code produces this output on the console:
  46.    
  47. replicate event fired
  48. replicate address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  49. replicate entry:  {
  50.   hash: 'zdpuAroa3Hqu9v5mKXvbQ5qxj4SHvEfT7vBkHZY3xaHQk4Mdj',
  51.   id: '/orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access',
  52.   payload: { op: 'PUT', key: 'write', value: [ '*' ] },
  53.   next: [ 'zdpuAnAVCL1zJ6V545G8ev6g2CfkW3nPsvyWrhaaNpHYBc7em' ],
  54.   refs: [ 'zdpuB1sr6B4soQc7U4eCbkB1e8SCUxCgBP1g5Uog6L17uPgWS' ],
  55.   v: 2,
  56.   clock: {
  57.     id: '04fb1bcc9d4c7d12ae30eb2aee1cdfd80eec75733db574e24b5f426041e8b1212019afcddb2321c169370e86a23c61965f83d9e1e6b8df93f4b2de4582c8bc163e',
  58.     time: 3
  59.   },
  60.   key: '04fb1bcc9d4c7d12ae30eb2aee1cdfd80eec75733db574e24b5f426041e8b1212019afcddb2321c169370e86a23c61965f83d9e1e6b8df93f4b2de4582c8bc163e',
  61.   identity: {
  62.     id: '03db9af05643d65cb9f741b5898bd4962998922f500a58cf407b32d444f580c6f8',
  63.     publicKey: '04fb1bcc9d4c7d12ae30eb2aee1cdfd80eec75733db574e24b5f426041e8b1212019afcddb2321c169370e86a23c61965f83d9e1e6b8df93f4b2de4582c8bc163e',
  64.     signatures: {
  65.       id: '3044022059a83a3d28129f855a8b0a508d02e45982b8916e64a37492f6f0f4cddba633cc022001f729f78628412fcecc59b7c4ba479f18fc602dccae256195aa6ec02aa2d6f7',
  66.       publicKey: '304402207cd87f0d5d4dc3e7efaf336ffcfeba895558d6cfd04447800b8f481fa59fd5d0022063d863929b970ee0b94e32682584f7f119094f168830cca7b5275b6684d324b1'
  67.     },
  68.     type: 'orbitdb'
  69.   },
  70.   sig: '304502210089fece55e7ca1429d2efd65315ad221799807d4010ebd0d225038ee4a00dae4502201391ff5285bb6758b1dde6def80636c27d7518a4db90dba731766b72d41e7e88'
  71. }
  72. entry data:  undefined
  73. all entries:  {}
  74. replicate event fired
  75. replicate address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  76. replicate entry:  zdpuAnAVCL1zJ6V545G8ev6g2CfkW3nPsvyWrhaaNpHYBc7em
  77. entry data:  undefined
  78. all entries:  { write: [ '*' ] }
  79. replicated event fired
  80. replicated address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  81. replicated count:  1
  82. replicate event fired
  83. replicate address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  84. replicate entry:  zdpuB1sr6B4soQc7U4eCbkB1e8SCUxCgBP1g5Uog6L17uPgWS
  85. entry data:  undefined
  86. all entries:  { write: [ '*' ] }
  87. replicated event fired
  88. replicated address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  89. replicated count:  1
  90. replicated event fired
  91. replicated address:  /orbitdb/zdpuAvLPKXcHLneHE7NmPjQtDfbcbRy69pioRE1n4is3xhd4U/testdb009/_access
  92. replicated count:  1
  93.  
  94.  
  95. Question:
  96. Given a hash like this:
  97. zdpuB1sr6B4soQc7U4eCbkB1e8SCUxCgBP1g5Uog6L17uPgWS
  98.  
  99. How do I get the actualy key-value entry represented by that hash?
  100. */
RAW Paste Data