RxDB populate an array within an array of objects from another collection

Viewed 890

I am trying to populate an array of id's from another collection

My JsonSchema looks like this:

{
  version: 0,
  type: "object",
  properties: {
    id: {
      type: "string",
      primary: true
    },
    // This is populated as expected
    working: {
      type: "array",
      ref: "othercollection",
      items: {
        type: "string"
      }
    },
    // This is where I am having problems
    notWorking: {
      type: "array",
      items: {
        type: "object",
        properties: {
          // This property is not being populated
          problem: {
            type: "array",
            ref: "yetanothercollection",
            items: {
              type: "string"
            }
          }
        }
      }
    }
  }
}

From the docs at https://pubkey.github.io/rxdb/population.html I should be able to:

Example with nested reference

const myCollection = await myDatabase.collection({
  name: 'human',
  schema: {
    version: 0,
    type: 'object',
    properties: {
      name: {
        type: 'string'
      },
      family: {
        type: 'object',
        properties: {
          mother: {
            type: 'string',
            ref: 'human'
          }
        }
      }
    }
  }
});

const mother = await myDocument.family.mother_;
console.dir(mother); //> RxDocument

Example with array

const myCollection = await myDatabase.collection({
  name: 'human',
  schema: {
    version: 0,
    type: 'object',
    properties: {
      name: {
        type: 'string'
      },
      friends: {
        type: 'array',
        ref: 'human',
        items: {
            type: 'string'
        }
      }
    }
  }
});

//[insert other humans here]

await myCollection.insert({
  name: 'Alice',
  friends: [
    'Bob',
    'Carol',
    'Dave'
  ]
});

const doc = await humansCollection.findOne('Alice').exec();
const friends = await myDocument.friends_;
console.dir(friends); //> Array.<RxDocument>

So my question is why can I not access myDocument.notWorking[0].problem_?

Here is a screenshot of the console that might give you a better understanding of my situation:

Screenshot of console

As you can see the ingredients property is not populated with the data from the ingredients collection (not in picture). The taxes property, however, is populated.

1 Answers

This is not possible unless you use OEM methods.

https://rxdb.info/orm.html

const heroes = await myDatabase.collection({
  name: 'heroes',
  schema: mySchema,
  methods: {
    whoAmI: function(otherId){
        // Return the item with id `otherId` from the other collection here
        return 'I am ' + this.name + '!!';
    }
  }
});
await heroes.insert({
  name: 'Skeletor'
});
const doc = await heroes.findOne().exec();
console.log(doc.whoAmI());

Related