Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

dag api: get examples #122

Closed
jbenet opened this issue Mar 9, 2017 · 3 comments
Closed

dag api: get examples #122

jbenet opened this issue Mar 9, 2017 · 3 comments

Comments

@jbenet
Copy link

jbenet commented Mar 9, 2017

Example: ipfs.dag.get

var IPFSAPI = require('ipfs-api')
var ipfs = IPFSApi('/ip4/127.0.0.1/tcp/5001')

// given an object such as: 
//
// var obj = {
//     "a": 1,
//     "b": [1, 2, 3],
//     "c": {
//         "ca": [5, 6, 7],
//         "cb": "foo"
//     }
// }
// 
// ipfs.dag.put(obj, function(err, cid2) {
//     assert(cid == zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y)
// })

function errOrLog(err, result) {
    if (err) console.error('error: ' + err)
    else console.log(result)
}

ipfs.dag.get('zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y/a', errOrLog)
// Returns:
// 1

ipfs.dag.get('zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y/b', errOrLog)
// Returns:
// [1, 2, 3]

ipfs.dag.get('zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y/c', errOrLog)
// Returns:
// {
//   "ca": [5, 6, 7],
//   "cb": "foo"
// }

ipfs.dag.get('zdpuAkxd9KzGwJFGhymCZRkPCXtBmBW7mB2tTuEH11HLbES9Y/c/ca/1', errOrLog)
// Returns:
// 6
@daviddias
Copy link
Contributor

The difference here from what we have is that we expect the arguments to be dag.get(cid, path, callback), where cid is an actual CID object and path is the string you want to search through.

I can add support for this way too.

@daviddias
Copy link
Contributor

The other difference is that the result has two properties:

{
  value: <the last resolved value>
  remainderPath: <the path missing to be resolved, if any, if not, empty string>
}

@daviddias daviddias mentioned this issue Mar 9, 2017
3 tasks
@daviddias daviddias changed the title dag api: get examples dag api: get examples Mar 9, 2017
@daviddias daviddias added the ready label Mar 9, 2017
@daviddias
Copy link
Contributor

Added these examples (modified a little to respect the actual API) on #123

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants