Skip to content

Commit 2245c37

Browse files
author
Chris
committed
Add count() endpoint to database and datastore
1 parent 9b1f438 commit 2245c37

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/api/rest/v1/db/controller.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,45 @@ export class DbController {
133133
}
134134
}
135135

136+
public async count(req: Request, res: Response) {
137+
try {
138+
const dbName = req.params.database
139+
const { context } = req.veridaNetworkConnection
140+
const permissions = Utils.buildPermissions(req)
141+
142+
const db = await context.openDatabase(dbName, {
143+
// @ts-ignore
144+
permissions
145+
})
146+
147+
const selector = req.body.query
148+
const limit = 1000
149+
const fields = ['_id']
150+
151+
const loops = 0
152+
let count = 0
153+
while (true) {
154+
const result = await db.getMany(selector, {
155+
fields,
156+
limit,
157+
skip: loops * limit
158+
})
159+
160+
if (result.length < limit) {
161+
count = loops*limit + result.length
162+
break
163+
}
164+
}
165+
166+
res.json({
167+
count
168+
})
169+
} catch (error: any) {
170+
console.log(error)
171+
res.status(500).send(error.message);
172+
}
173+
}
174+
136175
public async query(req: Request, res: Response) {
137176
try {
138177
const dbName = req.params.database

src/api/rest/v1/db/routes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ router.get("/get/:database/:recordId", auth({
1111
credits: CONFIG.verida.billing.defaultCredits
1212
}), controller.getById)
1313

14+
router.post("/count/:database", auth({
15+
scopes: ["api:db-query"],
16+
dbScope: "r",
17+
credits: 0
18+
}), controller.count)
19+
1420
router.post("/query/:database", auth({
1521
scopes: ["api:db-query"],
1622
dbScope: "r",

src/api/rest/v1/ds/controller.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,51 @@ export class DsController {
132132
}
133133
}
134134

135+
public async count(req: Request, res: Response) {
136+
try {
137+
const schemaName = Utils.getSchemaFromParams(req.params.schema)
138+
const { context } = req.veridaNetworkConnection
139+
const permissions = Utils.buildPermissions(req)
140+
141+
const ds = await context.openDatastore(schemaName, {
142+
// @ts-ignore
143+
permissions
144+
})
145+
146+
const selector = req.body.query
147+
const limit = 1000
148+
const fields = ['_id']
149+
150+
const loops = 0
151+
let count = 0
152+
while (true) {
153+
const result = await ds.getMany(selector, {
154+
fields,
155+
limit,
156+
skip: loops * limit
157+
})
158+
159+
if (result.length < limit) {
160+
count = loops*limit + result.length
161+
break
162+
}
163+
}
164+
165+
res.json({
166+
count
167+
})
168+
} catch (error: any) {
169+
let message = error.message
170+
if (error.message.match('invalid encoding')) {
171+
message = 'Invalid encoding (check permissions header)'
172+
}
173+
174+
res.status(500).send({
175+
error: message
176+
});
177+
}
178+
}
179+
135180
public async query(req: Request, res: Response) {
136181
try {
137182
const schemaName = Utils.getSchemaFromParams(req.params.schema)

src/api/rest/v1/ds/routes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ router.get("/get/:schema/:recordId", auth({
1111
credits: CONFIG.verida.billing.defaultCredits
1212
}), controller.getById)
1313

14+
router.post("/count/:schema", auth({
15+
scopes: ["api:ds-query"],
16+
dsScope: "r",
17+
credits: 0,
18+
}), controller.count)
19+
1420
router.post("/query/:schema", auth({
1521
scopes: ["api:ds-query"],
1622
dsScope: "r",

0 commit comments

Comments
 (0)