question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use $regexp and $iRegexp in a query

See original GitHub issue

I’m trying to search an ltree column in postgresql, and followed this advice https://github.com/sequelize/sequelize/issues/11629

Steps to reproduce

I’ve whitelisted $regex and $iRegex in feathers-client.js

const {
  makeServicePlugin, makeAuthPlugin, BaseModel, models, clients, FeathersVuex,
} = feathersVuex(feathersClient, {
  idField: 'id',
  whitelist: ['$regexp', '$iRegexp', '$options']
});

I also tried $regex and iRegex

I used the following query in the component

computed: {
    findQuery() {
      return {
        $sort: {
          id: 1
        },
        $limit: 100,
        path: {
          $regexp: 'root.*'
        }
      }
    }
  },

  methods: {
    ...mapActions('data', {
      findItems: 'find',
    })
  },

  created: async function() {
    await this.findItems({
      query: this.findQuery,
    })
  }

I’ve also tried setting operator aliases in sequelize.js, although I understand that’s not a good idea

const Sequelize = require('sequelize');
const Op = Sequelize.Op;

module.exports = function(app) {
  const connectionString = app.get('postgres_data');
  const sequelize_data = new Sequelize(connectionString, {
    dialect: 'postgres',
    operatorsAliases: {
      $regexp: Op.regexp,
      $iRegexp: Op.iRegexp
    }

Expected behavior

To be able to query with $regexp and $iRegexp

Actual behavior

I get a error

type: "FeathersError",
className: "bad-request"
code: 400
data: Object { "$regexp": "root.*" }
errors: Object {  }
hook: Object { type: "before", method: "find", path: "data", … }

System configuration

Tell us about the applicable parts of your setup. Postgresql 12

Module versions (especially the part that’s not working): “@feathersjs/feathers”: “^4.4.3”, “@feathersjs/socketio-client”: “^4.4.3”, “feathers-vuex”: “^3.2.0”, “socket.io-client”: “^2.3.0”, “vue”: “^2.6.11”, “vuex”: “^3.1.2”, “vuex-router-sync”: “^5.0.0”

NodeJS version: v13.3.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
okaracalikcommented, Jan 1, 2020

I hade a similar problem and I got a working solution for it. The solution steps:

  1. Firstly, I imported the Op (Operators) from the sequelize library. [example-model.js]
  2. Then, I modified the operators and whitelist properties of the model. [example-model.js]
  3. Lastly, I used the new operator in my hook file. [example-hook.js]

I hope, it helps.

example-model.js

const { Players } = require('./players.class');
const createModel = require('../../models/players.model');
const hooks = require('./players.hooks');
const Sequelize = require('sequelize');

module.exports = function (app) {
  const options = {
    Model: createModel(app),
    paginate: app.get('paginate'),
    operators: {
      '$iRegexp': Sequelize.Op.iRegexp
    },
    whitelist: ['$iRegexp']
  };

  app.use('/players', new Players(options, app));

  const service = app.service('players');

  service.hooks(hooks);
};

example-hook.js

const { params, service } = context;
if (params.query['search']) {
  params.sequelize = {
    where: {
      'name': {
        [service.options.operators.$iRegexp]: params.query.search
      }
    },
    raw: false
  };
}
1reaction
Adam-0-Moorecommented, Dec 30, 2019

I finally figured out Objection, and querying ltree columns with regex just works.

I wanted to write back with a solution for sequelize, but I never could figure it out.

Thanks for recommending feathers-objection 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

MYSQL Regular Expressions (REGEXP) with Syntax ... - Guru99
Let's modify our above script and use the NOT charlist and see what results we will get after executing our query. SELECT *...
Read more >
SQL Operators - Apache Impala
IREGEXP Operator. Tests whether a value matches a regular expression, using case-insensitive string comparisons. Uses the POSIX regular expression syntax where ...
Read more >
How Regex in SQL Works - The Data School
Learn how Regex works in SQL and how to use it in your queries. ... Regex, or Regular Expressions, is a sequence of...
Read more >
sequelize.iRegexp JavaScript and Node.js code examples
Builds query to get records where the value of the field ends with the value. * Setting caseSensitive to true will cause the...
Read more >
mysql regular expression search is not working - Stack Overflow
If you are looking for a query equivalent to like '%ab%' , remove the ... will make the regex united states or ^united...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found