Categories
Mozilla

New Asynchronous Storage API Has Landed

Some time in January I spec’ed out and API to allow the binding of multiple sets of parameters to a prepared statement and execute it asynchronously. This could be done in the past, but it was difficult and resource intensive to do (you would have to first clone the statement that you had and then bind and then pass all the clones to executeAsync). Say you want to insert a number of rows into a table now. Your code will look something like this:

// Note: db is the mozIStorageConnection for our database.
var stmt = db.createStatement("INSERT INTO table_name (table_value)" +
                              "VALUES (:value)");
var values = [
  "val1",
  "val2",
  "val3",
];
var array = stmt.newBindingParamsArray();
for (let i = 0; i < values.length; i++) {
  let params = array.newBindingParams();
  params.bindByName("value", values[i]);
  array.addParams(params);
}
stmt.bindParameters(array);
stmt.executeAsync(); // This will insert all the values into the table.

It's not the prettiest thing in the wold, and I hope to add some language helpers for JavaScript to make this even easier to use from JavaScript. Hope you find it useful!