cross platform data storage questions

edited April 2012 in General
What is the best way to store data locally in a cross platform way? I assume that in a PhoneGap app on Android I won't have a DBService running, so I can't use my old Enyo 1.0 code anymore. Which is too bad because I really like the non-SQL/JSON object interface.

I've seen references to localStorage (WebKit specific?) and the wrapper that omastudios wrote that enyo-izes it. I understand it has a size limit of 5MB. But how does this compare to DBService?

For larger data stores, PhoneGap's Storage interface provides access to a SQLite Db. Is there an enyo-ized interface available for this -- something that would convert from JSON objects to SQL? Or is that in works for Enyo 2.0?

Comments

  • At the moment, the best Enyo wrapper for WebSQL storage is by onecrayon -- see https://github.com/onecrayon/database-webos for details. I'm not sure if it works in 2.0 directly, but I'm sure he's open to patches.
  • Here is a version of OneCrayon's database.js updated for Enyo 2.0:

    https://gist.github.com/2317017

    Since it's not actually a patch, but a different version, I wasn't sure how to bring it to his attention using GitHub. Ben, can you figure out what to do with this? Thanks!

    Scott
  • Ah, I'll suggest @onecrayon post this as a different branch or a new repo.
  • edited April 2012
    @unwiredben: I actually updated the master branch of the main repo so that the Enyo script is now compatible with both Enyo 1 and 2 based on Scott's changes just an hour or two ago. :-)

    Incidentally, @cookie, SQLite is not really a great alternative for localStorage if you are worried about hitting the 5 MB limit, because the same limit is typically applied to SQLite databases, as well. It's mainly a good alternative if you don't want to keep all your data in memory, need to do selects that only target certain columns, sort things database-side instead of in Javascript, etc.

    However, relying on PhoneGap's Storage/WebSQL is not a fantastic option, either, because recent iOS updates will arbitrarily trash your local data from time to time if memory gets tight and there's nothing you can do to prevent it. For TapWatch I ended up writing a simple custom PhoneGap plugin that acts like localStorage, but writes the strings you pass it straight to the application sandbox as text files (and falls back on localStorage for other platforms). If you're working with small data sets, it works great. I'll try to remember to clean up the code and open source it soon.
  • From reading the dev list, the upcoming Cordova/PhoneGap 1.6 has changes on iOS to store the SQLite databases in a different folder that's no subject to being erased.
  • That's excellent news! And certainly makes the database class a lot more useful.
  • I really like the enyo-ized Database class. How do you get the ID of the last inserted row? The lastInsertId method doesn't work - it always returns 0. I realize I can query to get it, but that is quite a headache because its asynchronous. I'm using it on Android (ICS). Is there something I may be missing?
  • A couple things to note with the lastInsertId() method:

    1. It will only be set after an insert query succeeds, so you have to access it in your onSuccess method for the insert. A common mistake with the Database class is to try and do things synchronously, which won't work

    2. The insert ID will only be set if you have done a single insert. If you are doing a bulk insert using the queries() method, it theoretically should not save anything to lastInsertId.

    It's certainly possible that setting the insertID might be failing. The call that sets it is wrapped in a try/catch block, so if you want to debug it try adding some console logging or similar to the catch and see what errors it's reporting. I haven't tested the class on Android at all, so it's quite possible that there's a wrinkle to how it handles the WebSQL API or similar that the code doesn't take into account yet.
  • Incidentally, since interest in this component is picking up I've added full documentation for it here:

    http://onecrayon.github.com/database-webos/
  • Think you can make a gallery entry for this? See https://github.com/enyojs/gallery for details.
  • Turns out lastInsertId() works fine. I was calling it after an insertData call, which uses the queries function, not query. Thanks.
  • @onecrayon: Lovely documentation, well done.
Sign In or Register to comment.