不在 Realm 中创建多个 table。

Not create multiple table in Realm.

我正在使用 React native 创建 table 的 Realm 数据库。我创建table的函数是,

const Realm = require('realm');

exports.createTables = function(tableName, pkey, structure) {

  let realm = new Realm({
     schema: [{name: tableName, primaryKey: pkey, properties: structure}]
   });

  return realm;
};

我调用了这个方法,

import realmDatabase from './RealmDatabase';

   realmDatabase.createTables("MstUnitType", "UnitTypeId", {
       "UnitTypeName"           : "string",
       "UnitTypeId"             : "string",
     } );

     realmDatabase.createTables("MstTime", "TimeId", {
       "TimeId"           : "string",
       "From"             : "string",
       "To"               : "string",
     } );

    realmDatabase.createTables("MstQuestions", "QuestionId", {
       "QuestionId"           : "string",
       "Question"             : "string",
     } );

我在 defualt.realm 文件中只有 MstUnitType table 其他 2 table 未创建,而我 运行 以上 3 个创建 table 方法一个一个。

是的,我找到了上述解决方案。按照这种方式我们可以一次创建多个表,

var Realm = require('realm');

const CarSchema = {
  name: 'Car',
  properties: {
    make:  'string',
    model: 'string',
    miles: {type: 'int', default: 0},
  }
};
const PersonSchema = {
  name: 'Person',
  properties: {
    name:     'string',
    birthday: 'date',
    cars:     {type: 'list', objectType: 'Car'},
    picture:  {type: 'data', optional: true}, // optional property
  }
};

// Initialize a Realm with Car and Person models
let realm = new Realm({schema: [CarSchema, PersonSchema]});