React Native Realm Project Structure

↘锁芯ラ 提交于 2019-12-06 21:45:31

There a possible structure:

I will suggest you to use the Doc of Realm -> https://realm.io/

There a possible structure: realm.js

import * as RealmDB from 'realm';

class Passenger extends RealmDB.Object {}
Passenger.schema = {
name: 'Passenger',
primaryKey: 'id',
properties: {
  'id'                : 'string',
  'firstname'         : { type: 'string', optional: true },
  'lastname'          : { type: 'string', optional: true },
  'birthdate'         : { type: 'int',    optional: true },
  'email'             : { type: 'string', optional: true },
  'phone'             : { type: 'string', optional: true },
  'child'             : { type: 'linkingObjects', objectType: 'Child', property: 'passengers' }
}
};


class Child extends RealmDB.Object {}
Child.schema = {
 name: 'Child',
 primaryKey: 'id',
  properties: {
   'id'                  : 'string',
   'name'                : 'string',
   'parents_1'           : { type: 'linkingObjects', objectType: 'Passenger', property: 'child' }
  }
 };


const realmInstance = new RealmDB({
schema: [Passenger, Child],
});
export default realmInstance;

use.js

import realm from "./realm";
export default class use {

 static writeToRealm(){
  realm.write(() => {

  let passenger = realm.create('Passenger', {
  'id'            : "..."
  'firstname'     : "...",
  'lastname'      : "...",
  "..."
  })
 }

 static readPassengers(){
  const passengers = realm.objects('Passengers');
  return passengers // Be careful Realm use List instead of Array quite the same but not! 
 }
}

Every time you want to write in your database you have to use realm.write(() => {})

Hope it's help :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!