gajus / slonik

A Node.js PostgreSQL client with runtime and build time type safety, and composable SQL.
Other
4.58k stars 139 forks source link

[QUESTION] join replacement for booleanExpressionValues & comparisonPredicate doesn't work #101

Closed johnsonthedev closed 5 years ago

johnsonthedev commented 5 years ago

I use your booleanExpression snipped from Slonik-utilities and wanted to migrate it to new join function but it doesn't work. Is this a bug or am I doing it wrong ?

// former code

 const booleanExpression = sql.booleanExpression(
      Object
        .entries(booleanExpressionValues)
        .map(([key, value]) => {
          // $FlowFixMe
          return sql.comparisonPredicate(sql.identifier([key]), '=', value);
        }),
      'AND'
    );
// tried this. throws "token.members is not iterable"

const booleanExpression = sql.join(
    Object.entries(booleanExpressionValues).map(([key, value]) => {
      return sql.join(sql.identifier([key]), "=", value)
    }),
    "AND"
  )
// this is how I am doing it now. not nice tho 

  const booleanExpression = sql.raw(
    Object.entries(booleanExpressionValues)
      .map(([key, value]) => {
        return `${key} = ${value}`
      })
      .join(" AND ")
  )
gajus commented 5 years ago

Second parameter should be sql token, i.e. sql` AND `.

const booleanExpression = sql.booleanExpression(
      Object
        .entries(booleanExpressionValues)
        .map(([key, value]) => {
          return sql`{sql.identifier([key])} = ${value}`;
        }),
      sql` AND `
    );
johnsonthedev commented 5 years ago

THX!!!! Works!!

baerrach commented 4 years ago

sql.booleanExpression was removed in favour of sql.join but the article has not been updated.

See feat: remove multiple methods in favor of sql.join