Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 667654f

Browse files
committed
Add unified /db/:dbname/all endpoint
1 parent 8f3acea commit 667654f

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/lib/orbitdb-api.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ class OrbitdbAPI extends Express {
162162
return res.json(contents)
163163
}));
164164

165+
this.get('/db/:dbname/all', asyncMiddleware( async (req, res, next) => {
166+
let db, result, contents
167+
db = await dbm.get(req.params.dbname)
168+
if (typeof db._query == 'function') {
169+
contents = db._query({limit:-1})
170+
result = contents.map((e) => Object.keys(e.payload.value)[0])
171+
} else {
172+
contents = db.all
173+
result = unpack_contents(contents)
174+
}
175+
return res.json(result)
176+
}));
177+
165178
this.get('/db/:dbname/index', asyncMiddleware( async (req, res, next) => {
166179
let db
167180
db = await dbm.get(req.params.dbname)
@@ -174,16 +187,24 @@ class OrbitdbAPI extends Express {
174187
return res.json(db.value)
175188
}));
176189

190+
var unpack_contents = (contents) => {
191+
if (contents){
192+
if (contents.map) {
193+
return contents.map((e) => {
194+
if (e.payload) return e.payload.value
195+
return e
196+
})
197+
} else if (contents.payload) {
198+
return contents.payload.value
199+
}
200+
}
201+
return contents
202+
}
203+
177204
this.get('/db/:dbname/:item', asyncMiddleware( async (req, res, next) => {
178205
let result, contents
179206
contents = await getraw(req,res, next)
180-
if (contents && contents.map && contents.payload) {
181-
result = contents.map((e) => e.payload.value)
182-
} else if (contents && contents.payload) {
183-
result = contents.payload.value
184-
} else {
185-
result = contents
186-
}
207+
result = unpack_contents(contents)
187208
return res.json(result)
188209
}));
189210

0 commit comments

Comments
 (0)