jeremydaly / data-api-client

A "DocumentClient" for the Amazon Aurora Serverless Data API
MIT License
446 stars 63 forks source link

Can't retrieve records with a WHERE clause based on a OR condition #18

Closed misner closed 5 years ago

misner commented 5 years ago

I am trying to get all records where one column called 'job_status' is EITHER equal to 'submitted' or to 'in_process'. Should be easy but it doe snot work.

let submitted_jobs_db_query_results = await data.query(
    `
      SELECT 
        job_id
      FROM
        aws_async_comprehend_jobs
      WHERE
        type = :type AND
        job_status = :job_status
      ORDER BY
        aws_async_comprehend_job_id
      LIMIT 5
    `,
    { 
      type: "text-analysis",
      job_status: "SUBMITTED" || "IN_PROGRESS"
    }

I thought of using the "OR" concept directly inside the SQL query (instead of inside the {} parameters block) but I failed at that too (I think you actually say in the Docs it does not work : https://github.com/jeremydaly/data-api-client#you-cant-send-in-an-array-of-values)

So how to do this "rather basic" query (saying if "value is A or B") with data-api-client ?

Thanks a lot M.

jeremydaly commented 5 years ago

Hi @misner,

The simplest way to do this is just to update your query with the OR condition:

let submitted_jobs_db_query_results = await data.query(
    `
      SELECT 
        job_id
      FROM
        aws_async_comprehend_jobs
      WHERE
        type = :type AND
        (job_status = :job_status OR job_status = :job_status2)
      ORDER BY
        aws_async_comprehend_job_id
      LIMIT 5
    `,
    { 
      type: "text-analysis",
      job_status: "SUBMITTED",
      job_status2: "IN_PROGRESS"
    }

It's not ideal (or very flexible), but hopefully the Data API will support arrays soon so we can use an IN clause.

Jeremy

misner commented 5 years ago

Brilliant, thanks! should have thought about this!