0

I have an app where I can manually choose to go offline. When a user does that I fire an ajax requests to get data and save it in websql db. My retrieved data consists of a collection of models, which I want to write separately in a single transaction. (each model has its own id, which I want to use as a keypath for easier queries afterwards)

The closest method that I can use is add(store_name, values) but I don't know exactly how to use it. There is no example, and I can't figure it out. I am also not sure that this method is exactly what I need to do. Any feedback here? Can you give me a short example how I can achieve this?

Regards, Florin

bboydflo
  • 907
  • 1
  • 15
  • 24

2 Answers2

1

Sorry, I found an answer. I just need to try it out. here it is from the docs "Notice multiple records are stored by using array of records in one transaction.":

var data = [{id: 'a', message: 'a record'}, {id: 'b', message: 'b record'}];
db.put('store2', data).always(function(x) {
    console.log(x);
});
bboydflo
  • 907
  • 1
  • 15
  • 24
  • 1
    You can also use `db.run` for multiple requests in single transaction. – Kyaw Tun Aug 01 '14 at 00:38
  • @KyawTun could you provide an example? – bboydflo May 19 '16 at 19:08
  • @KyawTun please see the example below! Any idea why is failing on older android? As said, I specifically force the mechanism to be websql... I didn't had much success with the transaction either, the CSVStreamer example seems the most close to what I am trying to achieve, but too complicated. Could you provide a simple example, of how to insert a collection in a single transaction? Thanks! – bboydflo May 20 '16 at 08:13
  • @KyawTun this is my attempt using transactions, but still no success in android 4.1.2 :) https://gist.github.com/bboydflo/21c986b9a6c0ca4b703330130dac8f84 – bboydflo May 20 '16 at 08:24
0

Still have issues. I am using a workaround, that works well in chrome when I force the mechanism to be websql. but doesn't work well on android 4.1.2.

function insertRows(store, rows){

  // array of promises
  var insertPromises = [];

  // create deffered object
  var d = $.Deferred();

  // loop through each row
  map(rows, function(row){

    // update promises array
    insertPromises.push( db.put(store, row) );
  });

  // group promises
  Bluebird.all( insertPromises ).then(function(rows){

    // resolve promise
    d.resolve(null, rows);
  }, d.reject);

  // return a promise
  return d.promise();
}

It doesn't seem to reject either. So I assume it fails silently? Does anyone has a simple example of how to insert a collection in a single transaction using YDN-DB?

bboydflo
  • 907
  • 1
  • 15
  • 24