Open thorbenandresen opened 9 years ago
Read/write performance is an issue right now that is next on my to-do list when I have more time to dedicate to personal projects during the holidays. The problem is that currently the whole database is written on each create/update/delete.
What I can suggest right now as a temporary hack is to add all your data in one add
call and instead of using the interface in the Model
class, copy filter.js
to your working directory. You can write the whole array in read call and make your queries using filter.js
like so:
var filter = require('./filter.js');
//other code here...
this.model = await reactNativeStore.model('articles');
this.filter = new Filter();
await model.add({ articles: arr });
var allData = await model.find();
var desiredArticles = this.filter.apply(allData.articles, {
where: {
//Query parameters here, note _id wont work since these
//were elements of rows in the database
}
});
Updating, deleting will be a hassle. If there is some unique property to an article, like a url, name, etc. you can update/delete the element in the allData array using that property and then update the model:
this.model.update(allData, {
where: {
_id: allData._id
}
});
Thank you! I am just trying to implement this.
Quick question, how would I write my filter function if I want to find all articles with "favorite" == "1" (see object screenshot)?
http://i.imgur.com/HyM1O3O.jpg
var desiredArticles = this.filter.apply(allData.articles, {
where: {
//Query parameters here, note _id wont work since these
//were elements of rows in the database
}
});
This is how you would test for equality:
var desiredArticles = this.filter.apply(allData.articles, {
where: {
favorite: 1
}
});
Note that every other comparison operator other than '==' is different:
var desiredArticles = this.filter.apply(allData.articles, {
where: {
favorites: { neq: 1 } // != 1
}
});
See the LoopBack Where filter documentation for better reference, which were the specs the filter's in react-native-store were based on.
Hi @terminull and @Thorbenandresen I need to do a similar thing (adding something like 2000 rows to the DB), so tried this:
// Add data to local database
var num_rows = responseData.data.length;
for(var key in responseData.data) {
if(key == num_rows-1) {
await thisModel.add(responseData.data[key]);
} else {
thisModel.add(responseData.data[key]);
}
}
It basically makes all iterations async except for the final iteration, which it'll make the rest of the code wait for. It seems to work almost instantly.
Do you see any issues with this?
Scratch that, my suggestion doesn't work :-1:
I instead tried creating a separate API method - https://github.com/mcnamee/react-native-store/blob/develop/src/model.js#L79
If it looks ok with you @terminull - I'll create a pull request
+1 performant bulk-add is critical
+1
I switched to SQLite which is super quick (around 1.5 seconds for 2500 record insert).
@mcnamee Does SQLite have the same simple query syntax? I found the multiAdd to be pretty quick, but need some more advanced querying capabilities.
It's pretty simple - you just pass SQL queries (be it an insert, search, update, delete) into a method. It provides a way more advanced option for finding records, given the ability to use SQL.
You can use multiAdd, may be it's help for you
Hi,
I have big file JSON object (700 articles from Pocket's API) i'd like to add, but I experience some problems.
Is there a solution or workaround for this?