I am new to REALM and I am trying to join two tables but I can't find information of sql query for realm (I am using React Native)
Tables are ChatRoom and Message. ChatRoom has multiple Messages. I would like to get all chatrooms with only one most lastest message for each 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}
}
};
try this example, it will help you to join table in realm database.
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}
);
}
}
Related
This is my database and the basic idea here is that a user can have multiple notifications and that notification contains title and message so we have created a Notification schema and in user_details schema we have used the Notification schema as a list and object type of Notification
export const NotificationSchema = {
name: 'Notification',
properties: {
title: 'string',
message: 'string',
},
};
export const Users = {
name: 'user_details',
properties: {
_id: 'string',
userName: 'string',
email: 'string',
pass: 'string',
gender: 'string',
state: 'string',
notify: {type: 'list', objectType: 'Notification'},
},
primaryKey: '_id',
};
export const databaseOptions = {
path: 'userdata.realm',
schema: [Users, NotificationSchema],
};
PS: I'm new to realm if u can suggest any changes in schema plzz do tell me
Realm react native returning array of objects without values.
package version in package.json
"react": "16.11.0",
"react-native": "0.62.2",
"realm": "^6.0.2"
node version : v12.17.0
react-native-cli: 2.0.1
Defined Students class and schema as below:
export class Students {}
Students.schema = {
name: 'students',
primaryKey: 'roll_no',
properties: {
name: {type: 'string'},
roll_no: {type: 'string'},
order: {type: 'int', default: 0},
},
};
Creating realm database object globally:
export const realmDatabase = new Realm({
schema: [Students],
path: 'school.realm',
schemaVersion: 1,
});
Using below function to insert data into student table
export function insertStudents(students) {
realmDatabase.write(() => {
students.forEach((student) => {
try {
realmDatabase.create('students', {
name: student['name'],
order: student['order'],
roll_no: student['roll_no'],
});
} catch (error) {}
});
});
}
Fetch students from database using below function, but it retrieving empty Array of objects
export function getStudents() {
try {
let students = realmDatabase.objects('students');
console.log(students);
} catch (error) {}
}
Output :
{ '0': {},
'1': {},
'2': {},
'3': {},
'4': {},
'5': {},
'6': {}}
Got the issue finally, earlier is my Object model is type of class and is working fine on older version of realm but now its not working for me for updated version of realm and need to change it this as:
In older versions model object like this:
export class Students {}
Students.schema = {
name: 'students',
primaryKey: 'roll_no',
properties: {
name: {type: 'string'},
roll_no: {type: 'string'},
order: {type: 'int', default: 0},
},
};
Updated versions :
export const Students = {
name: 'students',
primaryKey: 'roll_no',
properties: {
name: {type: 'string'},
roll_no: {type: 'string'},
order: {type: 'int', default: 0},
},
};
i have a Database with code below:
realm = new Realm({
path: 'DeviceDB.realm',
schema: [
{
name: 'Schedule',
primaryKey: 'id',
properties: {
id: 'int',
startingTime: 'string',
endingTime: 'string'
},
},
{
name: 'user_devices',
primaryKey: 'id',
properties: {
id: 'int',
schedules: 'Schedule[]',
dev_name: 'string',
dev_serial: 'string'
},
},
],
});
but when i try to get schedules, it returns undefined. any idea whats wrong
const devs = realm.objects('user_devices').filtered('id =' + this.state.deviceId);
const fetched_schedules = devs.schedules;
deviceId is in my state and i checked it, its correct.
import Realm from 'realm';
class Cities extends Realm.Object {}
class Users extends Realm.Object {}
Cities.schema = {
name: 'Cities',
properties: {
'name': {
type: 'string'
},
'pincode': {
type: 'int'
}
}
};
Users.schema = {
name: 'Users',
primaryKey: 'id',
properties: {
'id': 'string',
'name': {
type: 'string'
},
'city': {
type: 'list',
objectType: 'Cities'
}
}
};
const schemaList = [Users, Cities];
const realmInstance = new Realm({schema: schemaList});
export default realmInstance;
// pushing a cityObj (that is already present in 'Cities') for a existing User:
onPress={() => this.addCity({name: 'Delhi', pincode: 110004})}
addCity = (cityObj) => {
realm.write(() => {
let user = realm.create('Users', {
'id': 'someUniqueID'
}, true);
user.city.push(cityObj);
});
let cities = realm.objects('Cities');
console.log('cities.length', cities.length);
}
though, trying to update a record in 'Users', The write transaction is writing a new record in Cities table as well creating duplicates. Why so?
Adding to a list will in general create a new object. But you can add a primary key to Cities, create/update the object first and finally push it to the list. Something like:
const Realm = require('realm');
const CitiesSchema = {
name: 'Cities',
primaryKey: 'pincode',
properties: {
'name': {
type: 'string'
},
'pincode': {
type: 'int'
}
}
};
const UsersSchema = {
name: 'Users',
primaryKey: 'id',
properties: {
'id': 'string',
'name': {
type: 'string'
},
'city': {
type: 'list',
objectType: 'Cities'
}
}
};
const schemaList = [UsersSchema, CitiesSchema];
const realm = new Realm({schema: schemaList});
addCity = (cityObj) => {
realm.write(() => {
let city = realm.create('Cities', cityObj, true);
let user = realm.create('Users', {
id: 'someUniqueID',
name: 'Foo Bar'
}, true);
user.city.push(city);
});
let cities = realm.objects('Cities');
console.log('cities.length', cities.length);
}
addCity({name: 'Delhi', pincode: 110004});
addCity({name: 'Delhi', pincode: 110004});
Who knows how to filter the Store right?
I tried to do it in listener of leafItemTap of Nested List, but my leaf items not tapping now. Massage in console: "Uncaught TypeError: Cannot call method 'filter' of undefined "
Here is Nested list, where Store must be filtered:
Ext.define('Application.view.SplitView', {
extend: 'Ext.Container',
xtype: 'splitview',
config: {
layout: 'card',
store: null
},
initialize: function() {
this.nestedList = Ext.create('Ext.NestedList', {
title : 'Рецепты',
detailCard: Ext.create('Application.view.MainDetail'),
store: this.getStore(),
listeners: {
scope: this,
leafitemtap: this.onLeafItemTap
}
});
this.setItems([this.nestedList]);
},
updateStore: function(newStore) {
if (this.nestedList) {
this.nestedList.setStore(newStore);
}
},
onLeafItemTap: function(nestedList, list, index, node, record, e) {
var psn = record.get('text');
console.log(psn);
var detailCard = nestedList.getDetailCard();
var store = Ext.getStore('Application.store.DetailStore');
store.filter('title', 'Brownies');
console.log(store);
}
});
This is my Store, which I want to filter:
Ext.define('Application.store.DetailStore', {
extend: 'Ext.data.Store',
config: {
model: 'Application.model.DetailModel',
autoLoad :true,
sorters: 'title',
grouper : function(record) {
return record.get('title')[0];
},
proxy: {
type: 'ajax',
url : '/data/data1.php',
reader: {
type: 'json',
rootProperty:'recipes'}
}
}
});
And Store's model:
Ext.define('Application.model.DetailModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'title', type: 'string'},
{name: 'serves', type: 'string'},
{name: 'cooktime', type: 'string'},
{name: 'ingridients', type: 'string'},
{name: 'picture', type: 'string'},
{name: 'kitchen', type: 'string'},
{name: 'category', type: 'string'},
{name: 'instructions', type: 'string'}
]
},
fullName: function() {
var d = this.data,
names = [
d.title
];
return names.join(" ");
}
});
I'm new in Sencha and every advice will be useful
The following error means the object which you're calling the filter function on is undefined
"Uncaught TypeError: Cannot call method 'filter' of undefined "
In your case, the store is undefined.
Try to get it by doing :
var store = Ext.getStore('DetailStore');
Also, you could check what stores are in the StoreManager by doing :
console.log(Ext.data.StoreManager.all);
Hope this helps