问题
I am writing a dictionary app with Cordova. The data size is about 20MB.
I want the app to work completely offline, and query data locally. And I find PouchDB really suitable for that.
However, I also want to have all the data pre-installed with the app (in JSON format). That is, to put all the data in the app package (in www/data folder of the Cordova project).
From what I've known so far, PouchDB can create data or sync data from a server, but I want to know how it can utilize data files inside the www folder of the app.
And, how should I format the data file so that PouchDB can query from it directly, without having to first read from the file and then insert data into PouchDB.
Does anyone have any ideas? Or perhaps any other JS libraries that can accomplish this?
Thanks!
回答1:
https://pouchdb.com/2016/04/28/prebuilt-databases-with-pouchdb.html covers step by step instructions of how to include pre-built data in your application. You can use either sqlite database files or raw texts files.
回答2:
After some research I want to compare some of the possible options at the moment:
Use PouchDB directly
This is not practical because: 1) you have to insert data into PouchDB on the first startup, which can be extremely slow; 2) memory usage is high because you will first load all data into memory (unless you split them); 3) Cordova apps can't run in the background; user may exit the app while inserting data.
PouchDB's sqlite plugin
A possible solution. You need to use the File Transfer plugin to copy data into a 'working folder'.
Cons:
- a bit cumbersome because you need another workflow for creating the sqlite file.
- PouchDB's sqlite adapter does not pass all test suites and can be slower as suggested by offical docs.
- Extra space usage (2x); the data in app package has to be copied into data folder in order for Sqlite to use.
Split data into smaller json files inside the app package (in /www folder)
This is what I currently use. I split my data in to about 100 smaller .json files and put them inside /www folder. Then I can use XHR requests to load them dynamically based on query.
Pro:
- no extra space needed; only one copy of data
Con:
- lose all features of PouchDB and its query methods; it is basically implementing a custom read-only database.
Lokijs
Lokijs is a high performance in-memory database. It can load database from JSON, or (using an unoffical plugin) load database from the Filesystem.
Con:
- Because Lokijs is an in-memory database. Memory usage may be an issue with large data size and on mobile devices.
Another thing to look forward to is a Filesystem adapter for PouchDB. As least some are thinking about it: https://github.com/pouchdb/pouchdb/issues/4631
Issues when preloading data:
To preload data usually means to copy data from inside the app package to a data directory provided by the OS (different depending on platform).
Cordova's File plugin and File Transfer plugin are often involved. Currently File plugin cannot access data inside app package, (although appDirectory is defined in the plugin). You have to use File Transfer plugin to copy your data files from the app package to data folder.
回答3:
http://pouchdb.com/adapters.html Look specifically at the sqlite plugin. You can find the sqlite db generated and distribute it.
来源:https://stackoverflow.com/questions/33461054/offline-data-with-cordova-and-pouchdb