I am working on a react native project and I am using realm to store data in local database to my object is like bellow
[
{
title: 'title1',
data: [{name: 'name1', city: 'paris'},{name: 'name2', city: madrid}]
},
{
title: 'title2',
data: [{name: 'name4', city: 'londer'},{name: 'name6', city: roma}]
}
]
my question is how can i filter by data properties so for example select only the object that in their data there is an element city === 'londer'
You can do something like:
Realm.open({schema:[*Your_Schema*]}).then(realm => const data = realm.objects('*Your_Type*').filtered('*Your_Condition*'));
Check this link for more details.
Related
I would like to push several arrays to the same state(array) like below.
constructor(props){
super(props);
this.state = {
itemsArray1: [],
itemsArrayMixed: [],
}
}
handleSetState1() {
this.setState({
itemsArray1: [
{name: 'aaa', id: '001', address: 'xxx' },
{name: 'aaa', id: '002', address: 'yyy'},
{name: 'ccc', id: '003', address: 'zzz'},
]
});
this.setState({ itemsArrayMixed: [...itemsArray1] });
}
handleSetState2() {
this.setState({
itemsArray1: [
{name: 'ddd', id: '004', address: 'ttt' },
{name: 'eee', id: '005', address: 'uuu'},
{name: 'fff', id: '006', address: 'www'},
]
});
this.setState({ itemsArrayMixed: [...itemsArray1] });
}
console.log(itemsArrayMixed) // => in itemsArrayMixed, I want to get 6 objects.
Above code is just an example.
In my project, I import 6 arrays that include objects from a different store.
And I want to push all these arrays to the same state (in this case, itemsArrayMixed).
Of course, if I want to handle only one array, that's easy to handle and I know how to do that.
However, I don't know how to push multi arrays to the same state...
You are just adding the new array(itemsArray1) in the mixed array without getting its previous value. you should try this and it will work
handleSetState1() {
let itemArray1 = [
{name: 'aaa', id: '001', address: 'xxx' },
{name: 'aaa', id: '002', address: 'yyy'},
{name: 'ccc', id: '003', address: 'zzz'},
]
this.setState({
itemsArray1
});
this.setState({ itemsArrayMixed: [...this.state.itemsArrayMixed, itemArray1] });
}
This will update the itemArrayMixed and store all value.
I have list of data that want to show using FlatList , as all examples I have seen like this from reactnativeexpress.com :
const sections = [
{
id: 0,
title: 'Basic Components',
data: [
{id: 0, text: 'View'},
{id: 1, text: 'Text'},
{id: 2, text: 'Image'},
]
},
{
id: 1,
title: 'List Components',
data: [
{id: 3, text: 'ScrollView'},
{id: 4, text: 'ListView'},
]
}
]
Items should be a list named data but if I get data from an API that items list is not named data, and it will not show the content. ( I tested with this example and if change data to dat items will not shown) , is there any way to change data default name or not?
I finally realized that item list should be named data so I added a pointer to my list :
content.payload.APIList.map((eachcat) => eachcat.data = eachcat.APISubList)
I'm new to realm. I have a schema like the following:
class MealItem {}
MealItem.schema = {
name: 'MealItem',
properties: {
mealId: {type: 'int'},
foodId: {type: 'int'},
},
};
I want to do the equivalent of :
SELECT
mealId
FROM MealItem
WHERE foodId = #food1
AND mealId IN (SELECT mealId FROM MealItem WHERE foodId = #food2
AND mealId IN (SELECT mealId FROM MealItem WHERE foodId = #food3))
I'm not sure where to begin other than to write A LOT of javascript for loops. Is there a less verbose way to do it?
more info
Basically I have two other objects like this:
class Meal {}
Meal.schema = {
name: 'Meal',
properties: {
mealId: {type: 'int'},
mealName: {type: 'string'},
},
};
class Food{}
Food.schema = {
name: 'Food',
properties: {
foodId: {type: 'int'},
foodName: {type: 'string'},
},
};
So my original query is meant to determine if given 3 different foods, does my restaurant offer a meal / combo for it.
For example, if you set food1 to Big Mac, food2 to Fries, food3 to Soft Drink, you should get a meal back called Big Mac Meal.
If you set food1 to Durian, food2 to Hot Pockets, food3 to Krusty Brand Imitation Gruel, no meal id will return, because my restaurant doesn't serve combo like that.
I don't know much about SQL but what I understand from your question is that you are trying to find meals that contains specific foods.
Realm queries and models works like a NoSQL database so you might need to think a little different while creating your models.
For example, lets say you have 3 tables. One contains Meals, one contains foods and one contains data about which MealId contains which FoodId. In Realm you don't need the 3 table. You can think as its a javascript object.
Example
const meal = {
mealId: 0001,
name: 'Big Fat Menu',
price: '4.99'
foods: [{
foodId: 11,
name: 'Burger'
}, {
foodId: 12,
name: 'Fries'
}, {
foodId: 13,
name: 'Coke'
}]
}
This javascript object can be modeled with Realm like this
const FoodSchema = {
name: 'Food',
primaryKey: 'foodId',
properties: {
foodId: 'string'
name: 'string',
}
};
const MealsSchema = {
name: 'Meal',
primaryKey: 'mealId',
properties: {
mealId: 'string'
name: 'string',
price: 'double',
foods: { type: 'list', , objectType: 'Food'}
}
};
With this schema you can make following query to get a meal that contains a specific food,
const meals = realm..objects('Meal').filtered(`foods.id == ${foodId}`)
This is just a small example to help you out a bit. Hope it helps.
I want to easily query such results:
[{
name: 'john_doe',
info: {
age: 24,
notes: 'custom text',
phoneNumbers: {
home: 112345678,
work: 1234567,
},
},
}, {...}, {...}...]
... by such query:
contacts.filtered("info.age = 24 AND info.notes CONTAINS 'custom'");
How should i create such schema? docs are very confusing about data types and nested properties:
https://realm.io/docs/react-native/0.14.0/api/Realm.html#~PropertyType
https://realm.io/docs/react-native/latest/#nested-objects
I do not need to retrieve any parts of this data separately - only complete object with all nested objects at once.
You could put all fields into a single object:
var ContactSchema = {
name: 'Contact',
properties: {
name: 'string',
age: 'int',
notes: 'string',
homePhone: 'string',
workPhone: 'string'
}
};
Alternatively you could create child objects for info and phoneNumbers but if you are not sharing this data across multiple contacts then this probably isn't needed.
EDIT: the answer is to use a formatter function (see below)
If you have a data store something like this:
var store = new Store({
data: [
{ id: 1, username: {first: 'John', last: 'Doe'},
score: 130, city: 'New York', birthday: '1980/2/5'}
]
});
Is there a way to tell GridX to reference username.first in a structure something like this? (this doesn't work, but is there a way to do this?)
var columns = [
{field: 'username.first', name: 'Name'}
];
ANSWER:
var columns = [
{name: 'Name', field: 'username',
formatter: function(rowData) {
return rowData.username.first;
}
}
];