From 5c551d61a29031be1a8ebd1fcccb8960c68e077a Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 8 Apr 2019 00:43:50 +0800 Subject: [PATCH 1/4] Support key/value pairs for keyvalue store. --- src/lib/orbitdb-api.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index d448ae9..68b9043 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -66,7 +66,14 @@ class OrbitdbAPI extends Express { var db_put = asyncMiddleware( async (req, res, next) => { let db, hash db = await dbm.get(req.params.dbname) - hash = await db.put(req.body) + + if (db.type == 'keyvalue') { + let params = req.body; + hash = await db.put(params.key, JSON.parse(params.value)) + } else { + hash = await db.put(req.body) + } + return res.json(hash) }); From daf2a623a00ae7afba08a4263d31e365be4940b1 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 8 Apr 2019 11:42:16 +0800 Subject: [PATCH 2/4] Remove redundant JSON.parse. --- src/lib/orbitdb-api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 68b9043..437710b 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -69,7 +69,7 @@ class OrbitdbAPI extends Express { if (db.type == 'keyvalue') { let params = req.body; - hash = await db.put(params.key, JSON.parse(params.value)) + hash = await db.put(params.key, params.value) } else { hash = await db.put(req.body) } From 0b011187fc7c489f2dd5fa5aecd926b5715dc5b4 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 8 Apr 2019 11:50:54 +0800 Subject: [PATCH 3/4] Additional docs for POSTing key/value pairs. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 3328847..bcfb9e7 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,13 @@ curl -X POST http://localhost:3000/db/docstore/put -H "Content-Type: application zdpuAkkFaimxyRE2bsiLRSiybkku3oDi4vFHqPZh29BABZtZU ``` +For the keyvalue store, a JSON object containing the variables `key` and +`value` must be passed in the POST data: + +```shell +curl -X POST http://localhost:3001/db/keyvalue/put -H "Content-Type: application/json" -d '{"key":"Key","value":{ "name": "Value" }}' +``` + ### POST|PUT /db/:dbname/inc Increments the counter database :dbname by 1. From dc2a6cae4ca346b24e4901b5dc5cab9647b1e405 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 8 Apr 2019 11:59:22 +0800 Subject: [PATCH 4/4] Display all keys and values from a keyvalue store. --- README.md | 18 +++++++++++++++++- src/lib/orbitdb-api.js | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bcfb9e7..838a147 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,22 @@ curl -X GET http://localhost:3000/db/feed/iterator -d 'limit=-1' See [OrbitDB's Iterator API](https://github.com/orbitdb/orbit-db/blob/master/API.md#iteratoroptions-1) for more information. +### GET /db/:dbname/all + +Gets all items from a keyvalue database :dbname. + +Returns all items from a keyvalue store as a JSON object. + +Can only be used on keyvalue. + +```shell +curl -X GET http://localhost:3000/db/all +``` + +```json +{"Key1":{"name":"Value1"},"Key2":{"name":"Value2"},"projects":"{["orbitdb","ipfs"]}} +``` + ### POST|PUT /db/:dbname/add Adds a new entry to the eventlog or feed database :dbname. @@ -273,7 +289,7 @@ For the keyvalue store, a JSON object containing the variables `key` and `value` must be passed in the POST data: ```shell -curl -X POST http://localhost:3001/db/keyvalue/put -H "Content-Type: application/json" -d '{"key":"Key","value":{ "name": "Value" }}' +curl -X POST http://localhost:3000/db/keyvalue/put -H "Content-Type: application/json" -d '{"key":"Key","value":{ "name": "Value" }}' ``` ### POST|PUT /db/:dbname/inc diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 437710b..2e12140 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -160,6 +160,12 @@ class OrbitdbAPI extends Express { return res.json(contents) })); + this.get('/db/:dbname/all', asyncMiddleware( async (req, res, next) => { + let db + db = await dbm.get(req.params.dbname) + return res.json(db.all()) + })); + this.get('/db/:dbname/value', asyncMiddleware( async (req, res, next) => { let db, val db = await dbm.get(req.params.dbname)