GithubHelp home page GithubHelp logo

Comments (3)

gnestor avatar gnestor commented on June 29, 2024 3

I just dealt with this by doing the following:

const queryDatabase = async (query, parameters = {}) => {
  try {
    const { records } = await db.query(query, parameters)
    return records
  } catch (error) {
    console.error(error.message)
    if (error.message.includes('Database returned more than the allowed response size limit')) {
      const getCount = async (_query) => {
        const query = _query.replace(/SELECT\s(.+?)\s(FROM|WHERE)/is, 'SELECT COUNT(*) $2')
        const {
          records: [{ count }],
        } = await db.query(query, parameters)
        return count
      }
      let result = []
      // Get result count
      const count = await getCount(query)
      // Define limit based on your average record size
      const limit = 50
      // Use limits and offsets to page through the query
      for (let page = 1; page <= Math.ceil(count / limit); page++) {
        const limitQuery = query.replace(/;?$/, `\nLIMIT ${limit} OFFSET ${(page - 1) * limit};`)
        const { records } = await db.query(limitQuery, parameters)
        result = [...result, ...records]
      }
      return result
    }
  }
}

It you're doing a batch query that passes parameters as nested arrays, then you will need to handle that for each page query.

from data-api-client.

Lane avatar Lane commented on June 29, 2024 1

thanks @gnestor! one small note in case someone else ends up here:

my query contained a GROUP BY clause, which doesn't return the correct number of rows with the snippet above, so i changed the following:

const getCount = async (_query) => {
  // old: const query = _query.replace(/SELECT\s(.+?)\s(FROM|WHERE)/is, 'SELECT COUNT(*) $2')
  const query = `SELECT COUNT(*) FROM (${_query}) sub`; // wrap the original query and count the number of results
  const {
    records: [{ count }],
  } = await db.query(query, parameters)
  return count
}

other than that, it works great!

from data-api-client.

 avatar commented on June 29, 2024

I would love to see this abstracted as well! +1

from data-api-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.