RxDB在另一个集合的对象数组中填充一个数组

时间:2018-10-21 16:19:21

标签: javascript database rxjs

我正在尝试填充另一个集合中的ID数组

我的JsonSchema如下:

SELECT 
    `attribute_2` as attribute, COUNT(*) AS counter 
FROM
    TABLENAME
WHERE
    creation_date > "2018-10-10"
GROUP BY
    attribute_2

UNION ALL

SELECT `attribute_1` as attribute, COUNT(*) AS counter
FROM
    TABLENAME
WHERE
    creation_date > "2018-10-10"
GROUP BY
    attribute_1

https://pubkey.github.io/rxdb/population.html的文档中,我应该能够:

带有嵌套引用的示例

{
  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"
            }
          }
        }
      }
    }
  }
}

数组示例

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

所以我的问题是为什么我不能访问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>

以下是控制台的屏幕截图,可以使您更好地了解我的情况:

Screenshot of console

如您所见,myDocument.notWorking[0].problem_属性未填充有成分集合中的数据(未显示在图片中)。但是,会填充ingredients属性。

1 个答案:

答案 0 :(得分:0)

除非您使用OEM方法,否则这是不可能的。

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());