2

I have a simple postgres ltree query

select * from data_objects WHERE path ~ 'root.*{1}';

I would like to implement this in sequelize, but there is no mapping for the ~ operator.

Is it possible to override the mappings? or do I have to use a raw sql query?

Click Ahead
  • 2,782
  • 6
  • 35
  • 60

2 Answers2

3

Found the solution:

params.path = {
        $regexp:'root.*{1}'
};

Which translates as

where "path" ~ 'root.*{1}'

I hadn't seen the $regexp operator

Click Ahead
  • 2,782
  • 6
  • 35
  • 60
  • I'm curious: What would `$regexp` do if you were using SQLIte or SQL Server? These databases don't have any native support for regex like. I think this answer still has a caveat that you are running Postgres. – Tim Biegeleisen Jul 26 '17 at 11:57
  • @TimBiegeleisen $regexp is only supported for MySQL/PG. In any event, the ltree implementation is specific to Postgres. If I change db down the line I'll be looking at a new architecture - so hopefully that doesn't happen :) – Click Ahead Jul 26 '17 at 14:22
1

If you ran your query as a raw query using sequelize, it should work:

sequelize.query("select * from data_objects WHERE path ~ 'root.*{1}'",
                { type: sequelize.QueryTypes.SELECT})
    .then(users => {

    })

Have a look at the documentation for more information. Note that by using a native query you are introducing a hard dependency on Postgres into your Node code. Whether you choose to do this depends on you.

Perhaps you could also rephrase your query using LIKE:

select * from data_objects WHERE LOWER(path) LIKE 'root%{1}'
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360