In Parse Need to fetch nested relation data in one call - react-native

I am currently working with parse in react native.
I have a query that fetches data from a collection which has a property with relation to other collection.
Now i want to fetch all these relational data in a single call rather then calling each relation separately.
Currently i get one collection then get its relational data separately in a new call.
const data1 = await result
.get("data1")
.query()
.descending("createdAt")
.find();
const data2 = data1.relation("test");
const data3 = await data2.query().find();
Now i want to fetch the relational data along with the data1 in the very first call.
I would like to know is it even possible in parse.
If yes how?
I have been trying to fetch relational data all day but no success.
Any help would really be appreciated.

I hope you're fine :)
Using relations, you will need to fetch the data with more than a single call.
Why is it necessary?
It is required because the relation type creates a new collection on the database to store the relational data.
Please, take a look at the code below:
query.find().then(results => {
for (var i = results.length - 1; i >= 0; i--) {
let object = results[i];
object.relation("data1").query().each(function(relatedObject) {
console.log(relatedObject);
/* .... */
});
}
}).catch(console.error());
Is there another option to receive the data in a single call?
Yes, there is! To configure this option, you will need to use the pointer or array type, then you will be able to use include() method and get the column value, please read more about it below:
https://docs.parseplatform.org/js/guide/#one-to-many

Related

How can i know if a relationship is fetched or not in neo4j?

For example I load an entity in the following way:
Movie m = session.load(Movie.class, id, 0); // load properties but not relationships
m.getActors(); // empty since depth was 0
A bit later in another method:
// Do I need to load it?
if (needsLoad(m) {
m = session.load(Movie.class, m.getId(), 1);
}
for (Actor a : m.getActors()) {
// ...
}
The only solution I've found is to load it every time.
Is there a better approach?
There is no API for accessing the session cache.
As a result it is not possible to get information about how deep the object graph got loaded.
I wonder how you get in the situation to need this.
The standard approach would be: load data from the database with the "right" depth, manipulate the data and save it back.
All within one transaction.

How to create several new records in another SQL table from one button-click

I'm new here. Thanks in advance for your advice.
I’m working on an app which will ask the user how many items they made.
The user will enter a number. My app should then create that many new records in a table called 'Items_Made'.
E.g. The app asks “How many items did you make?”, the user enters “19”, the app then creates 19 new records in the 'Items_Made' table.
I've managed to pull together some code (shown below) that creates ONE new record, but I would like it to create several. I probably need some kind of loop or 'while' function but am unsure how to do so.
var ceateDatasource = app.datasources.Items_Made.modes.create;
var newItem = ceateDatasource.item;
ceateDatasource.createItem();
This code successfully creates 1 record. I would like it to be able to create several.
Creating a lot of records via client script is not recommended, especially if you loose connection or the app gets closed by mistake. In my opinion, the best way to handle this would be via server script for two things: First, It's more reliable and second, it's faster. As in the example from the official documentation, to create a record you need to do something like this:
// Assume a model called "Fruits" with a string field called "Name".
var newRecord = app.models.Fruits.newRecord();
newRecord.Name = "Kiwi"; // properties/fields can be read and written.
app.saveRecords([newRecord]); // save changes to database.
The example above is a clear example on how to create only one record. To create several records at once, you can use a for statement like this:
function createRecordsInBulk(){
var newRecords = [];
for(var i=0; i<19; i++){
var newRecord = app.models.Fruits.newRecord();
newRecord.Name = "Kiwi " + i;
newRecords.push(newRecord);
}
app.saveRecords(newRecords);
}
In the example above, you initiate newRecords, an empty array that will be responsible for holding all the new records to create at once. Then using a for statement, you generate 19 new records and push them into the newRecords. Finally, once the loop is finished, you save all the records at once by using app.saveRecords and passing the newRecords array as an argument.
Now, all this is happening on the server side. Obviously you need a way to call this from the client side. For that, you need to use the google.script.run method. So from the client side you need to do the following:
google.script.run.withSuccessHandler(function(result) {
app.datasources.Fruits.load();
}).createRecordsInBulk();
All this information is clearly documented on the app maker official documentation site. I strongly suggest you to always check there first as I believe you can get a faster resolution by reading the documentation.
I'd suggest making a dropdown or textbox where the user can select/enter the number of items they want to create and then attach the following code to your 'Create' button:
var createDatasource = app.datasources.Items_Made.modes.create;
var userinput = Number(widget.root.descendants.YourTextboxOrDropdown.value);
for (var i = 0; i <= userinput; i++) {
var newItem = createDatasource.item;
createDatasource.createItem();
}
Simple loop with your user input should get this accomplished.

Linq order by with a field to retrieve dynamically in vb.net

I have a object Ob with several fields f1,..,fn (of different types).
Now a list of object is shown in a GridView and I need to implement the sorting method.
The real problem is:
how can I run
(from ob in Ob_list orderby ob.f1 ascending)
when the sorting field is represented by a string (i.e. "f1")?
Unfortunately I am not able to get it with the reflection (I am not able to do something like ob.GetType().GetField("f1"), this is not mapped into sql code).
I have several fields to possibly sort the rows, which is the best&fastest approach to this?
Thank you very much!
LINQ execution is deferred until you actually enumerate over the results or access the "count", etc. Because of this, you can build up your LINQ statement in stages.
The below code is done in C#, but I'm sure the equivalent is possible in VB.NET.
First setup your basic query:
var query = (from ob in Ob_list);
At this point, nothing has actually gone to the database due to deferred execution.
Next, conditionally add your order by components:
if (sortField == "f1")
{
query = query.OrderBy(o => o.f1);
}
else if (sortField == "f2")
{
query = query.OrderBy(o => o.f2);
}
else
{
//...
}
And finally, collect your results
foreach (var item in query)
{
// Process the item
}
I've found this question: How do I specify the Linq OrderBy argument dynamically?
I'm using Entity Framework, so the first answer did not solved my problem. The second one however, worked great!
Hope it helps!

How to serialize/deserialize large list of items with protobuf-net

I have a list of about 500 million items. I am able to serialize this into a file with protobuf-net file if I serialize individual items, not a list -- I cannot collect the items into List of Price and then serialize because I run out of memory. So, I have to serialize one record at a time:
using (var input = File.OpenText("..."))
using (var output = new FileStream("...", FileMode.Create, FileAccess.Write))
{
string line = "";
while ((line = input.ReadLine()) != null)
{
Price price = new Price();
(code that parses input into a Price record)
Serializer.Serialize(output, price);
}
}
My question is about deserialization part. It appears that Deserialize method does not move the Position of the stream to the next record. I tried:
using (var input = new FileStream("...", FileMode.Open, FileAccess.Read))
{
Price price = null;
while ((price = Serializer.Deserialize<Price>(input)) != null)
{
}
}
I see one real-looking Price record, and then the rest are empty records -- I get the Price object back but all fields are initialized to default values.
How to properly deserialize a stream that contains a list of objects which are not serialized as a list?
Good news! The protobuf-net API is setup for exactly this scenario. You should see a SerializeItems and DeserializeItems pair of methods that work with IEnumerable<T>, allowing streaming both in and out. The easiest way to do feed it an enumerate is via an "iterator block" over the source data.
If, for whatever reason, that isn't convenient, that is 100% identical to using SerializeWithLengthPrefix and DeserializeWithLengthPrefix on a per-item basis, specifying (as parameters) field: 1 and prefix-style: base-128. You could even use SerializeWithLengthPrefix for the writing, and DeserializeItems for the reading (as long as you use field 1 and base-128).
Re the example - id have to see that in a fully reproducible scenario to comment; actually, what I would expect there is that you only get a single object back out, containing the combined values from each object - because without the length-prefix, the protobuf spec assumes you are just concatenating values to a single object. The two approaches mentioned above avoid this issue.
May be I am too late on this... but just to add to what Marc already said.
As you use Serializer.Serialize(output, price); protobuf treat consecutive messages as part of a (same)single object. So when you use Deserialize using
while ((price = Serializer.Deserialize<Price>(input)) != null)
you will get all the records back. Hence you will see only the last Price record.
To do what you want to do, change the serialization code to:
Serializer.SerializeWithLengthPrefix(output, price, PrefixStyle.Base128, 1);
and
while ((price = Serializer.DeserializeWithLengthPrefix<Price>(input, PrefixStyle.Base128, 1)) != null)
The API apprently has changed since Marc's answer.
It seems there's no SerializeItems method any more.
Here's some more up to date info that should help:
ProtoBuf.Serializer.Serialize(stream, items);
can take an IEnumerable as seen above and it does the job when it comes to serialization.
However there's a DeserializeItems(...) method and the devil is in the details :)
If you serialize IEnumerable like above, then you need to call DeserializeItems passing PrefixStyle.Base128 and 1 as fieldNumber cause apprently those are the defaults.
Here's an example:
ProtoBuf.Serializer.DeserializeItems<T>(stream, ProtoBuf.PrefixStyle.Base128, 1));
Also as pointed out by Marc and Vic you can serialize/deserialize on a per item basis like this (using custom values for PrefixStyle and fieldNumber):
ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, item, ProtoBuf.PrefixStyle.Base128, fieldNumber: 1);
and
T item;
while ((item = ProtoBuf.Serializer.DeserializeWithLengthPrefix<T>(stream, ProtoBuf.PrefixStyle.Base128, fieldNumber: 1)) != null)
{
// do stuff here
}

How can I create the dynamic field in the store?

How can I create the dynamic field in the store?
because of if my data resource has 100 fields,I have to create 100 fields in the store property 'fields' or create the model within 100 fields.
Do I have good way to dynamic get the fields from Json?
Without knowing exactly how your JSON is structured, I'll assume that each record has 100 fields with no nested data. Here's some basic pseudo-code you can start with:
var json = getJsonResponse();
var meta = json[0]; // Get a sample record
var fields = [];
for (field in meta) {
if (typeof meta[field] !== 'function') {
fields.push({name: field, type: "auto"});
}
}
Assuming the first record in your JSON is representative of the whole, this should help dynamically declare your fields. You'll need to tweak the example to fit your particular data though.