Skip to content

Commit

Permalink
feat(db-proxy): add count option to read operation
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Oct 19, 2021
1 parent fbcccea commit e7545ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 9 additions & 4 deletions packages/database-proxy/src/accessor/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,14 @@ export class MongoAccessor implements AccessorInterface {
protected async read(collection: string, params: Params): Promise<ReadResult> {
const coll = this.db.collection(collection)

let { query, order, offset, limit, projection } = params
query = this.deserializedEjson(query || {})
const { order, offset, limit, projection, count } = params
const query: any = this.deserializedEjson(params.query || {})

let options: any = {
const options: any = {
limit: 100,
skip: 0
}

if (order) options.sort = this.processOrder(order)
if (offset) options.skip = offset
if (projection) options.projection = projection
Expand All @@ -220,10 +221,14 @@ export class MongoAccessor implements AccessorInterface {
const data = await coll.find(query, options).toArray()
this.logger.debug(`mongo end of read {${collection}}: `, { query, options, dataLength: data.length })

let total: number
if (count) {
total = await coll.countDocuments(query as Filter<any>)
}

this.emitResult(params, { data })
const serialized = data.map(doc => this.serializeBson(doc))
return { list: serialized, limit: options.limit, offset: options.skip }
return { list: serialized, limit: options.limit, offset: options.skip, total }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/database-proxy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface Params {

}

const ReadAcceptParams = ['query', 'order', 'offset', 'limit', 'projection', 'multi', 'joins', 'nested']
const ReadAcceptParams = ['query', 'order', 'offset', 'limit', 'projection', 'multi', 'count', 'joins', 'nested']
const UpdateAcceptParams = ['query', 'data', 'multi', 'upsert', 'merge', 'joins']
const AddAcceptParams = ['data', 'multi']
const RemoveAcceptParams = ['query', 'multi', 'joins']
Expand Down
13 changes: 13 additions & 0 deletions packages/database-proxy/tests/mongo_db/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ describe('Database read', function () {
assert.equal(data.list[0].title, TEST_DATA[0].title)
})

it('read with count should be ok', async () => {
let params = {
collection: 'test_read',
action: ActionType.READ,
query: {},
count: true
}
const data = await entry.execute(params)
assert.ok(data.list instanceof Array)
assert.equal(data.list.length, TEST_DATA.length)
assert.equal(data.total, TEST_DATA.length)
})

it('read with projection should be ok', async () => {
let params = {
collection: 'test_read',
Expand Down

0 comments on commit e7545ca

Please sign in to comment.