动态键名对和值的领域数据类型

问题描述 投票:0回答:1

我在 react native 中使用 realm flexible sync,我想为下面的 json 定义模式,其中键名是动态 objectId 并且有一组属性,所以我尝试了字典和混合但没有奏效。 此外,此 group_taxes 将具有 n 个关键值。

{
    "group_taxes": {
        // This one is dynamic object id as key which holds properties
        "63bbb1372aea3a5f887b4d0e": {
            "tax_id": {
                "$oid": "63bbb1372aea3a5f887b4d0e"
            },
            "tax_name": "CGST",
            "tax_rate": 10,
            "calculated_tax": 44,
            "tax_calculation": "PERCENTAGE"
        },
        "63bbb1372aea3a5f887b4d10": {
            "tax_id": {
                "$oid": "63bbb1372aea3a5f887b4d10"
            },
            "tax_name": "SGST",
            "tax_rate": 20,
            "calculated_tax": 20,
            "tax_calculation": "FLAT_VALUE"
        }
    }
}

我试图在架构中定义如下但没有奏效

{
  "group_taxes" : {
    "bsonType": "mixed"
  }
}
javascript reactjs react-native realm realm-js
1个回答
0
投票

您似乎想在 group_taxes 属性中存储一个对象列表(又名“数组”)。模式可能看起来像这样

{
  "title": "YourParentObject",
  "type": "object",
  "required": [
    "_id"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "group_taxes": {
      "bsonType": "array",
      "child_object_list": {
        "bsonType": "objectId"
        //your child object properties; tax_id etc
      }
    }
  }
}

请记住 - 如果应用程序在 Realm 控制台中处于开发模式,您可以在代码中构建您的 Realm 模型,并且会自动为您创建架构。

例如,如果您用代码构建了这个模型

class Tax extends Realm.Object {
  static schema = {
    name: 'Tax',
    properties: {
      tax_id: 'string',
      group_taxes: {
        type: 'list',
        objectType: 'TaxObject',
        optional: false,
      },
      tax_name: 'string',
    },
  };
}

匹配模式将在控制台中创建。 (您也可以在代码中单独构建 TaxObject)

© www.soinside.com 2019 - 2024. All rights reserved.