1

I have a content model that looks like:

{
  "name": "content",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "url": {
      "type": "string",
      "required": false
    },
    "data": {
      "type": "Object",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}

Example of this data in MySQL looks like this: enter image description here

I've tried querying this data both with REST and the Node API and haven't been able to query by any of the nested object fields inside of the "data" database field. A few examples that don't work:

      contentModel.findOne({ where: { "data.url": url } }, function (
        err,
        content
      ) {
        if (err) {
          console.log(err);
        }
        console.log("find:", content);
      });
const filter = encodeURI(`{"where":{"systemid":"${contentType}"}}`);
      let url = `${apiUrl}contentTypes?filter=${filter}`;
      let contentTypeRecord = await this.getAxios().get(url);

I've also tried many queries in the loopback swagger ui. I usually get no results or it returns all the content records.

The above data access attempts do however work if I have the same data setup in MongoDb.

What am I doing wrong? Loopback should presumably be parsing the object in the data field and allowing me to filter on it.

1

1 Answer 1

1

AFAIK, LoopBack does not support query filters on nested properties. According to this comment, there is a limited support in PostgreSQL and MongoDB connectors:

@michelgokan as you'll see from the thread:

  • There is some support in postgres connector
  • Mongo also has some basic support (only for dot nested properties)
  • Further support isn't going to be added to other RDB connectors as loopback 3 is in LTS and no longer accepting new feature PRs
  • Loopback 3 ends LTS at the end of 2019 so it's even less likely to be added.

Options are:

  • Write the custom SQL yourself as a custom endpoint
  • Create an alternate connector for something like Knex or Bookshelf that has support (big job)
  • Do the filtering in memory
  • Use json for the nested data to use dot queries in something like mongo or postgres
  • Look at migrating to loopback next which may add support in the future

It seems that MySQL supports querying of JSON data, see docs for JSON_CONTAINS, thus it should be possible to implement this feature in loopback-connector-mysql. Feel free to open an issue in https://github.com/strongloop/loopback-connector-mysql/issues. A pull request would be even better! ❤️

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Miroslav Bajtoš, but I'm not sure that is the exact same use case - The Mongo connector for my original question works perfectly. I'd be really surprised if this couldn't work with RDMS -seems like such as common use case (storing a JSON object in a db field and wanting to filter in it). If I can't get this work, I'm really going to have to rethink using Loopback at all for my project, ugh.
Yes, I agree it could work. Now we need somebody to implement and contribute the necessary changes 🤷🏻‍♂️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.