领域:写入嵌套模式

时间:2017-02-15 15:29:51

标签: react-native realm realm-list

我有一个拥有大量嵌套列表的模型(示例如下)。我在弄清楚如何写入这些嵌套列表时遇到了问题。

const examSchema = {
  name: 'Exams',
  properties: {
    Exams: { type: 'list', objectType: 'ExamList' },
  },
};

const examList = {
  name: 'ExamList',
  properties: {
    creationDateTime: 'date',
    SITE: 'string',
    examCodeList: { type: 'list', objectType: 'ExamCodeList' },
  },
};

const examCodeList = {
  name: 'ExamCodeList',
  properties: {
    EXAMCODE: 'string',
    DESCRIPTION: 'string',
    SUBEXAMS: { type: 'list', objectType: 'SubExamCodeList' },
  },
};

const subExamCodeList = {
  name: 'SubExamCodeList',
  properties: {
    DESCRIPTION: 'string',
    EXAMCODE: 'string',
    SUBSUBEXAM: { type: 'list', objectType: 'SubSubExamCodeList' },
  },
};

const subSubExamCodeList = {
  name: 'SubSubExamCodeList',
  properties: {
    DESCRIPTION: 'string',
    EXAMCODE: 'string',
  },
};

架构应与此JSON架构匹配:

"Exams": [{
    "creationDateTime": "2017-01-24T09:29:51-05:00",
    "SITE": "SCH",
    "examCodeList": [{
      "EXAMCODE": "VEHICLE",
      "DESCRIPTION": "GE Vehicle Failures",
      "SUBEXAM": [{
        "DESCRIPTION": "WILL NOT START",
        "EXAMCODE": "NOSTART",
        "SUBSUBEXAM": [{
          "DESCRIPTION": "FEEDER/SUPPLY FAULT",
          "EXAMCODE": "FEEDER"
        },{
          "DESCRIPTION": "LOCAL SWITCH FAILURE",
          "EXAMCODE": "LOCSWTC"
        },{
          "DESCRIPTION": "INHIBIT BY LOGIC CIRCUIT",
          "EXAMCODE": "INHIBIT"
        }]
      }]
    }]
  }]

然后我创建的写入函数看起来像这样。

let test;
realm.write(() => {
  test = realm.create('Exams', {});
});

test.Exams.push({
  creationDateTime: new date(2017, 01, 20),
  SITE: 'SCH1',
});

test.Exams.examCodeList.push({
  EXAMCODE: 'example',
  DESCRIPTION: 'example',
});

test.Exams.examCodeList.SUBEXAMS.push({
  DESCRIPTION: 'example',
  EXAMCODE: 'example',
});

test.Exams.examCodeList.SUBEXAMS.SUBSUBEXAM.push({
  DESCRIPTION: 'example',
  EXAMCODE: 'example',
});

我知道必须有更好的方法来做到这一点,但我没有在网上找到关于如何写入嵌套列表模式的任何内容。谢谢您的帮助!

0 个答案:

没有答案