How to use the mongo-query-to-postgres-jsonb.convertSelect function in mongo-query-to-postgres-jsonb

To help you get started, we’ve selected a few mongo-query-to-postgres-jsonb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thomas4019 / pgmongo / index.js View on Github external
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
      }

mongo-query-to-postgres-jsonb

Converts MongoDB queries to postgresql queries for jsonb fields.

MIT
Latest version published 10 months ago

Package Health Score

58 / 100
Full package analysis