Skip to content
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

Converting Object column with arrary to in clause #131

Open
doshisunny opened this issue Jun 24, 2022 · 0 comments
Open

Converting Object column with arrary to in clause #131

doshisunny opened this issue Jun 24, 2022 · 0 comments

Comments

@doshisunny
Copy link

doshisunny commented Jun 24, 2022

I was trying to implement this is my use case so I was wondering if its possible to add support for this.
Here is the scenario:
I am trying to pass dynamic object to where clause which has list of values for some of the properties.
So if I pass data like this
select().from('person').where({last_name: 'Rubble', codes: ['ABC' , 'XYZ'}).toString();
it will convert to
// SELECT * FROM person WHERE last_name = 'Rubble' and codes in ('ABC' , 'XYZ')

Reason for me to use it like this is due to I have dynamic input for table name and column values so this way it will be able to generate clause dynamically.

But current support is only for equal operator and not in clause.
https://github.com/CSNW/sql-bricks/blob/a004a72c17bc0010366bbd8cfeb5f1590db16c4e/sql-bricks.js#L975-L981

So above instead of expressions.push(sql.equal(col, obj[col]));
I tried using is clause conditionally and it works fine.

  function objToEquals(obj) {
    var expressions = [];
    for (var col in obj) {
      if (!Array.isArray(obj[col])) {
        expressions.push(sql.equal(col, obj[col]))
      } else {
        expressions.push(sql.in(col, obj[col]))
      }
    }
    return expressions;
  }

Is it possible to support this?

I am also open to creating PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant