Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const distinctField = mongoToPostgres.pathToText(['data'].concat(doc.key.split('.')), false)
const arrayCondition = `jsonb_typeof(${distinctField})='array'`
const query1 = `SELECT DISTINCT ${distinctField} AS data FROM ${collectionName} WHERE ${where} AND NOT ${arrayCondition}`
const query2 = `SELECT DISTINCT jsonb_array_elements(${distinctField}) AS data FROM ${collectionName} WHERE ${where} AND ${arrayCondition}`
const query = `${query1} UNION ${query2}`
const res = await tryOrCreateTable(async () => await doQuery(databaseName, query), databaseName, collectionName)
const rows = res.rows.map((row) => convertToBSON(row.data))
debug('pgmongo:rows')(rows)
const data = { values: rows, ok: 1 }
socket.write(build(reqId, data, {}))
return true
}
if (commandName === 'find') {
const filter = doc.filter
const where = safeWhereConversion(filter, rawCollectionName)
let select = mongoToPostgres.convertSelect('data', doc.projection)
let query = `SELECT ${select} FROM ${collectionName}`
if (where !== 'TRUE') {
query += ` WHERE ${where}`
}
if (doc.sort) {
query += ' ORDER BY ' + mongoToPostgres.convertSort('data', doc.sort, doc.collation && doc.collation.numericOrdering)
}
if (doc.limit) {
query += ' LIMIT ' + Math.abs(doc.limit)
}
if (doc.skip) {
if (doc.skip < 0) {
socket.write(build(reqId, { ok: 0, errmsg: 'negative skip not allowed' }, {}))
return true
}