【问题标题】:How can I join two tables with REALM如何使用 REALM 连接两个表
【发布时间】:2018-04-20 11:03:02
【问题描述】:

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

表格是聊天室和消息。 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}
    }
};

【问题讨论】:

  • Realm 不是关系型数据库,它有 relationship 直接指向另一个“表”,它没有通过 id 连接
  • 这又是什么版本的Realm
  • 也许阅读realm.io/docs/javascript/latest/#relationships会有所帮助。
  • 我的Realm版本是v2.0.4

标签: react-native realm mobile-development


【解决方案1】:

试试这个例子,它将帮助你加入领域数据库中的表。

    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}


  );
}
}

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多