2016-10-16 56 views
0

我使用React native創建Realm數據庫表。我創建表的功能,不在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通過創建表的方法之一,不能只創建表MstUnitTypedefualt.realm文件等2表一。

回答

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]});