如何使用REALM连接两个表

时间:2017-11-07 22:03:31

标签: react-native realm mobile-development

我是REALM的新手,我正在尝试加入两个表但我无法找到sql查询的信息(我正在使用React Native)

表是ChatRoom和Message。 ChatRoom有多个消息。我想为每个聊天室提供所有聊天室,其中只有一个最最新的消息。

ChatRoom.schema = {
    name: 'fcm_chat_room',
    primaryKey: 'chat_room_id',
    properties: {
        chat_room_id:  'string',
        chat_room_name: {type: 'string', default: ''},
        chat_room_date: 'date'
    }
};

Message.schema = {
    name: 'fcm_message',
    primaryKey: 'message_id',
    properties: {
        message_id:  'string',
        chat_room_id: 'string',
        sender_id: 'string',
        sender_reg_id: 'string',
        message: 'string',
        msg_date: 'date',
        is_read: {type: 'bool', default: false}
    }
};

1 个答案:

答案 0 :(得分:2)

尝试此示例,它将帮助您在域数据库中连接表。

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

Combine React Native

const Realm = require('realm');

class  extends Component {
render() {
  let realm = new Realm({
    schema: [{name: 'Dog', properties: {name: 'string'}}]
  });

  realm.write(() => {
    realm.create('Dog', {name: 'Rex'});
  });

  return (


        Count of Dogs in Realm: {realm.objects('Dog').length}


  );
}
}