What is my best option for a local data persistence

Hi guys I am building an app which needs minimal, local json persistence on the device. So far I learned about following options:

  1. $http.get() - worked well when reading json files but I am not really sure if it is possible to write using $http.post()
  2. window.localstorage also works well in development so far. I am just not sure if it will also work well when the app is deployed on a real device.
  3. ngStorage - I could not use this yet, I guess because of a angular version conflict or something.
  4. There also seems to be a SQL-Lite solution but I have not tried this one yet. I also think that an entire database would be overkill for my requirements.

So is there any official How-To or recommendation from Ionicā€™s side? What is suited best? What is most reliable and convenient to use.

I also might have missed some options here.

Greets and thanks in advance.

I used localstorage first but this wasnā€™t persistent so i had to change my code to use sqlite instead which so far hasnā€™t failed me.

That sounds great. Could you provide any tutorial or something? Can I access this database via javascript? Also will the database be deployed along with my app-file once I trigger a build?

hereā€™s a basic script for adding and getting a user. iā€™ve limited it because it comes from one of my apps:

.factory('CartService', ['$q', '$http', '$cordovaSQLite', '$ionicPlatform', '$timeout', function($q, $http, $cordovaSQLite, $ionicPlatform, $timeout) {
  var db;
  $ionicPlatform.ready(function() {
db = $cordovaSQLite.openDB({name: 'my.db', location:'default'});
  });
  var factory = {
addUser: function(user, shipping, datetime) {
  var deferred = $q.defer();
  user = JSON.stringify(user);
  shipping = JSON.stringify(shipping);
  datetime = JSON.stringify(datetime);
  $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS user (id integer primary key, user text, shipping text, datetime text)").then(function(res) {
  });
  var query = "INSERT INTO user (user, shipping, datetime) VALUES (?, ?, ?)";
  $cordovaSQLite.execute(db, query, [user, shipping, datetime]).then(function(res) {
    deferred.resolve('Gegevens opgeslagen.');
  }, function (err) {
    deferred.reject('Er ging iets mis met het toevoegen van je gegevens.');
  });
  return deferred.promise;
},
getUser: function() {
  $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS user (id integer primary key, user text, shipping text, datetime text)").then(function(res) {
  });
  var query = "SELECT * FROM user";
  var items = [];
  var deferred = $q.defer();
  $cordovaSQLite.execute(db, query).then(function(res) {
    var data = res.rows.item(0);
    deferred.resolve(data);
  }, function (err) {
    deferred.reject('Er ging iets mis met het ophalen van je gegevens.');
  });
  return deferred.promise;
}
  };
  return factory;
}]);

Thereā€™s also a ngcordova extension Xoilac TV - Link Xem Trį»±c Tiįŗæp BĆ³ng ĐƔ XĆ“i Lįŗ”c TV Full HD

The database gets created by default if you add the plugin and open it.

1 Like

Thank you for all that information. I just realized that I am not able to test my app in a browser when using this plugin.

Can you maybe explain how localstorage was not persistent? What were the issues, trouble you had while using it?

The data from localstorage was lost after a device reboot, which was really a big problem because i used it for storing JWT.

1 Like

You can test in a browser when using SQLite, search the forum it has been discussed.

Well I found this suggestion:

if(window.cordova) {
    db = $cordovaSQLite.openDB({name: "my.db"});
} else {
    db = window.openDatabase("my.db", '1', 'my database', 5 * 1024 * 1024);
}

This actually does work for me, seems insane to write double the code for testing purposes though. imo

Iā€™d recommend using local forage - better abstraction/lets you switch between sources