How to use the knex.raw function in knex

To help you get started, we’ve selected a few knex 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 ManjunathaN / jsonschema-to-graphql / src / main / knex-resolver / queryBuilder.js View on Github external
_.each(suffixMap, (condition, key) => {
    if (_.endsWith(arg.name.value, key) && (!found)) {
      const columnName = _.head(_.split(arg.name.value, key));

      switch (key) {
        case '_IsNull':
          if (arg.value.value) {
            query.whereNull(columnName);
          } else {
            query.whereNotNull(columnName);
          }
          break;
        case '_In':
          query.whereIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_NotIn':
          query.whereNotIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_LikeNoCase':
          query.whereRaw(condition, [columnName,
            isVarDef ? knex.raw(varValue) : values.toLowerCase()
          ]);
          break;
        case '_Like':
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
        default:
          // console.log('columnName, values :: ', columnName, values);
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
github ManjunathaN / jsonschema-to-graphql / src / main / knex-resolver / queryBuilder.js View on Github external
if (_.endsWith(arg.name.value, key) && (!found)) {
      const columnName = _.head(_.split(arg.name.value, key));

      switch (key) {
        case '_IsNull':
          if (arg.value.value) {
            query.whereNull(columnName);
          } else {
            query.whereNotNull(columnName);
          }
          break;
        case '_In':
          query.whereIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_NotIn':
          query.whereNotIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_LikeNoCase':
          query.whereRaw(condition, [columnName,
            isVarDef ? knex.raw(varValue) : values.toLowerCase()
          ]);
          break;
        case '_Like':
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
        default:
          // console.log('columnName, values :: ', columnName, values);
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
      }

      found = true;
github ManjunathaN / jsonschema-to-graphql / src / main / knex-resolver / queryBuilder.js View on Github external
query.whereNotNull(columnName);
          }
          break;
        case '_In':
          query.whereIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_NotIn':
          query.whereNotIn(columnName, isVarDef ? knex.raw(varValue) : values);
          break;
        case '_LikeNoCase':
          query.whereRaw(condition, [columnName,
            isVarDef ? knex.raw(varValue) : values.toLowerCase()
          ]);
          break;
        case '_Like':
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
        default:
          // console.log('columnName, values :: ', columnName, values);
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
      }

      found = true;
      return false;
    }
  });
github ManjunathaN / jsonschema-to-graphql / src / main / knex-resolver / queryBuilder.js View on Github external
query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
        default:
          // console.log('columnName, values :: ', columnName, values);
          query.whereRaw(condition, [columnName, isVarDef ? knex.raw(varValue) : values]);
          break;
      }

      found = true;
      return false;
    }
  });

  // Its now, safe to assume that this is equals operator..
  if (!found) {
    query.whereRaw('?? = ?', [arg.name.value, isVarDef ? knex.raw(varValue) : values]);
    found = true;
  }

  return found;
}
github js-data / js-data-sql / src / index.js View on Github external
let [latitude, longitude] = value.center

    // Uses indexes on `latitudeColumn` / `longitudeColumn` if available
    query = query
      .whereBetween(latitudeColumn, [
        latitude - (radius / unitsPerDegree),
        latitude + (radius / unitsPerDegree)
      ])
      .whereBetween(longitudeColumn, [
        longitude - (radius / (unitsPerDegree * Math.cos(latitude * (Math.PI / 180)))),
        longitude + (radius / (unitsPerDegree * Math.cos(latitude * (Math.PI / 180))))
      ])

    if (value.calculateDistance) {
      let distanceColumn = (typeof value.calculateDistance === 'string') ? value.calculateDistance : 'distance'
      query = query.select(knex.raw(`
        ${unitsPerDegree} * DEGREES(ACOS(
          COS(RADIANS(?)) * COS(RADIANS(${latitudeColumn})) *
          COS(RADIANS(${longitudeColumn}) - RADIANS(?)) +
          SIN(RADIANS(?)) * SIN(RADIANS(${latitudeColumn}))
        )) AS ${distanceColumn}`, [latitude, longitude, latitude]))
    }
    return query
  }
}