File tree Expand file tree Collapse file tree 4 files changed +96
-0
lines changed
Expand file tree Collapse file tree 4 files changed +96
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
1420router . post ( "/query/:database" , auth ( {
1521 scopes : [ "api:db-query" ] ,
1622 dbScope : "r" ,
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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+
1420router . post ( "/query/:schema" , auth ( {
1521 scopes : [ "api:ds-query" ] ,
1622 dsScope : "r" ,
You can’t perform that action at this time.
0 commit comments