Search code examples
loopbackjsloopback

Does loopback 3 support filters on JSON field of a table ? I am using MySQL database


Does loopback 3 support filters on JSON field of a table ? I am using MySQL database.

For example, we create table 'events' - CREATE TABLE events( id int auto_increment primary key, event_name varchar(255), visitor varchar(255), properties json, browser json );

Is there any way to apply filter on json fields 'properties' and 'browser' ?


Solution

  • First of all, MySQL doesn't index JSON column (there is workaround but not supported natively), if you go with MySQL, it can be problem in the near feature.

    It think it is not possible because doc says that:

    Data source connectors for relational databases don’t support filtering nested properties.

    However you can implement your own logic for build-in methods via using operation hooks.

    For example

    Model - the constructor of the model that will be queried
    query - the query containing fields where, include, order, etc.
    MyModel.observe('access', function **queryToJsonField**(ctx, next) {
          // operation goes here
           next();
     });
    

    More detailed explanation can be found at doc