1

Schema of Product has many details like: productName, manufactured date, manufacturer name, product type etc. But I need to support flexibility of having no defined schema for product details. To achieve this below is the Realm-JS schema.

import Realm from 'realm';
export default class ProductModel extends Realm.Object { }
    ProductModel.schema = {
        name: 'ProductModel',
        properties: {
            productAttributes: { type: 'list', objectType: 'ProductDetailsModel' },
            ID: { type: 'string', optional: true },        
        }
    };

import Realm from 'realm';
export default class ProductDetailsModel extends Realm.Object { }
ProductDetailsModel.schema = {
    name: 'ProductDetailsModel',
    properties: {
        attributeName: { type: 'string', optional: true },
        attributeValue: { type: 'string', optional: true },
    }
};

This flexibility is yielding in complexity of writing filter query. I need to be able to filter based on particular Product attribute name and Product Attribute values.

let query = "productAttributes.attributeName = " + "\"" + "Manufacturer" + "\"" + " AND productAttributes.attributeValue = [c]" + "\"" + "Pepsico" + "\"";        

        var productList = this.realm.objects('ProductModel').filtered(query).snapshot();

Please help me to write query to filter the ProductModel from DB matching ProductDetailsModel both attributeName and attributeValue ?

Current query does not match single ProductDetailsModel where attributeName ="Manufacturer" AND attributeValue = "Pepsico"

Raviprakash
  • 2,410
  • 6
  • 34
  • 56

1 Answers1

0

You reference it by the Objects local attribute, not the name of the linked object. e.g.

let attributeName = "Manufacturer"
let attributeValue = "Pepsico"
let query = `productAttributes.attributeName  ==[c] '${attributeName}' AND productAttributes.attributeValue ==[c] '${attributeValue}'`
sam
  • 164
  • 8
  • My bad, i did a typo in question, now corrected it. But result is same. – Raviprakash Sep 19 '18 at 09:03
  • The syntax is ok. is it a fully or partially synced realm? Can you fetch the models without the filtered? – sam Sep 19 '18 at 17:57
  • I wanted to high level ProductModel object which matches attributeName and attributeValue together. With the current query it returns ProductModel when at least one attributeName = "Manufacturer" and at least one attributeValue = "Pepsico" . How can I express it should match attributeValue if and only if it matches attributeName. Hope I am clear. – Raviprakash Sep 20 '18 at 09:03