Skip to content

mongo filter transformer #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/external-db-mongo/lib/mongo_data_provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// const { SystemFields } = require('velo-external-db-commons')
// const { translateErrorCodes } = require('./sql_exception_translator')

const { isObject } = require('velo-external-db-commons')

class DataProvider {
constructor(client, filterParser) {
this.client = client
Expand All @@ -19,11 +21,9 @@ class DataProvider {
}

async count(collectionName, filter) {
const { filterExpr } = this.filterParser.transform(filter)

const result = await this.pool.collection(collectionName)
.count(filterExpr)
return result
const { filterExpr } = this.filterParser.transform(filter)
return await this.pool.collection(collectionName)
.count(filterExpr)
}

async insert(collectionName, items) {
Expand Down Expand Up @@ -61,13 +61,21 @@ class DataProvider {
async aggregate(collectionName, filter, aggregation) {
const { fieldsStatement, havingFilter } = this.filterParser.parseAggregation(aggregation.processingStep, aggregation.postFilteringStep)
const { filterExpr } = this.filterParser.transform(filter)

return await this.pool.collection(collectionName)
const aggregateResult = await this.pool.collection(collectionName)
.aggregate( [ { $match: filterExpr },
fieldsStatement,
havingFilter
] )
.toArray()

aggregateResult.map((result)=>{
if (isObject(result._id)){
Object.assign(result,result._id)
if (isObject(result._id)) delete result._id
}
})
return aggregateResult

}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/external-db-mongo/lib/mongo_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ const escapeId = s => s //SqlString.escapeId(s)
const escape = s => s //SqlString.escape(s)
const patchFieldName = s => s //`x${SqlString.escape(s).substring(1).slice(0, -1)}`
const validateLiteral = s => s //`@${patchFieldName(s)}`
const EMPTY_FILTER = {filterExpr:{}}

module.exports = { escapeId, validateLiteral, patchFieldName, escape }
module.exports = { escapeId, validateLiteral, patchFieldName, escape, EMPTY_FILTER }
Loading