有一种可能的结构:
我会建议你使用 Doc of Realm -> https://realm.io/
有一个可能的结构:
领域.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;
使用.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!
}
}
每次你想在你的数据库中写入你必须使用
realm.write(() => {})
希望对你有帮助:)