0

我正在使用 React native 创建 Realm 数据库表。我创建表的功能是,

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",
     } );

当我在 3 个创建表方法上逐一运行时,我MstUnitType在文件中只得到了其他 2 个表未创建的表。defualt.realm

4

1 回答 1

1

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

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]});
于 2016-10-17T11:13:25.867 回答