Coder Social home page Coder Social logo

Transaction API about node-sqlite3 HOT 11 CLOSED

tryghost avatar tryghost commented on May 28, 2024
Transaction API

from node-sqlite3.

Comments (11)

koistya avatar koistya commented on May 28, 2024 1

If this library is used with ES2015+ async/await (via sqlite), how can you make sure that some extraneous queries are not being injected into the transaction?

  await db.run('BEGIN');
  try {
    await db.exec(sql);
    await db.run('COMMIT');
  } catch (err) {
    await db.run('ROLLBACK');
    throw err;
  }

from node-sqlite3.

Pita avatar Pita commented on May 28, 2024

Yes, that would be awesome +1

from node-sqlite3.

kkaefer avatar kkaefer commented on May 28, 2024

@Pita: Please note that transactions are already possible with node-sqlite3, there's just no specific interface for it. Nothing stops you from running db.run("BEGIN"); /* some queries inside the transaction */ db.run("COMMIT"). You only have to pay attention that no other code holding the database handle injects queries inside the transaction that shouldn't be there.

from node-sqlite3.

Pita avatar Pita commented on May 28, 2024

I know that it's possible. I'm already doing it like this. But it's a ugly way cause I have to build a huge sql string manually.

var sql = "BEGIN TRANSACTION;\n";
  for(var i in bulk)
  {
    if(bulk[i].type == "set")
    {
      sql+="REPLACE INTO store VALUES (" + escape(bulk[i].key) + ", " + escape(bulk[i].value) + ");\n";
    }
    else if(bulk[i].type == "remove")
    {
      sql+="DELETE FROM store WHERE key = " + escape(bulk[i].key) + ";\n";
    }
  }
  sql += "END TRANSACTION;";

   this.db.exec(sql, function(err){ ...

from node-sqlite3.

kkaefer avatar kkaefer commented on May 28, 2024

You don't have to build a single string; you can just use the regular APIs between db.run("BEGIN") and db.run("COMMIT")

from node-sqlite3.

miccolis avatar miccolis commented on May 28, 2024

I'd also really love to see this API implemented. I'm refactoring https://github.com/developmentseed/couch-sqlite to work with long lived connections and it would be great to use this, rather than a pool of one connection.

/cc @gundersen

from node-sqlite3.

kkaefer avatar kkaefer commented on May 28, 2024

2 eggrolls.

from node-sqlite3.

 avatar commented on May 28, 2024

You only have to pay attention that no other code holding the database handle injects queries inside the transaction that shouldn't be there.

What's the best way to do this? Is it sufficient to put the transaction inside db.serialize? Will that block other non-db things like http server events?

from node-sqlite3.

JoshuaWise avatar JoshuaWise commented on May 28, 2024

You only have to pay attention that no other code holding the database handle injects queries inside the transaction that shouldn't be there.

What's the best way to do this? Is it sufficient to put the transaction inside db.serialize? Will that block other non-db things like http server events?

I'd like an answer to ghost's question, please. It's not clear in the docs.

from node-sqlite3.

bpasero avatar bpasero commented on May 28, 2024

I would like to know under which condition a ROLLBACK should be issues? It is not clear to me if this is done automatically by the engine. In other words, I would assume that upon any failure, the entire transaction is being rolled back to the state before. Otherwise, what would be the motivation to use a transaction in the first place?

from node-sqlite3.

daniellockyer avatar daniellockyer commented on May 28, 2024

Clsoing in favor of #304

from node-sqlite3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.