npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sqlite3-transactions

v0.0.5

Published

Adds transactions support to node-sqlite3.

Downloads

563

Readme

#sqlite3-transactions

Adds transactions support to node-sqlite3.

Background

Node-sqlite3 is a great way how to access a SQLite database but id doesn't provide support for transactions yet. The underlying SQLite database can handle transactions easily so you can still do:

db.serialize(function() {
    db.exec("BEGIN");
    ...
    db.exec("COMMIT");
});

This works fine until you add an async operation between BEGIN and COMMIT. The problem is that node-sqlite3 uses single connection to the database and thus queries from other places can end up intermixing with each other messing up the transactions.

Sqlite3-transactions solves this problem by transparently locking the database while in a transaction. If database is locked all queries which don't belong to the transaction are queued and executed after the transaction is finished. As a bonus you got a clean nice API for transactions.

Install

npm install sqlite3
npm install sqlite3-transactions

Usage

var sqlite3 = require("sqlite3"),
	TransactionDatabase = require("sqlite3-transactions").TransactionDatabase;

// Wrap sqlite3 database
var db = new TransactionDatabase(
	new sqlite3.Database("test.sqlite", sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE)
);

// Use db as normal sqlite3.Database object.
db.exec("CREATE TABLE ...", function(err) {
	// table created
});

// Begin a transaction.
db.beginTransaction(function(err, transaction) {
	// Now we are inside a transaction.
	// Use transaction as normal sqlite3.Database object.
	transaction.run("INSERT ...");


	// All calls db.exec(), db.run(), db.beginTransaction(), ... are
	// queued and executed after you do transaction.commit() or transaction.rollback()
	
	// This will be executed after the transaction is finished.
	database.run("INSERT ..."); 
    
    // Feel free to do any async operations.
    someAsync(function() {
    
        // Remember to .commit() or .rollback()
	    transaction.commit(function(err) {
            if (err) return console.log("Sad panda :-( commit() failed.", err);
            console.log("Happy panda :-) commit() was successful.");
        );
	    // or transaction.rollback()
        
    });
});

I haven't mentioned one feature in the example - on error event automatic rollback() is performed on the current transaction.

API

database.beginTransaction(callback)

Call this method to start a transaction. The database is locked and all queries are queued until the transaction is over. callback receives two parameters error and transaction. Use transaction to perform operations directly with database. You must call transaction.commit() or transaction.rollback() to finish the transaction and unlock the database. You don't have to rollback() in the case:

  • there is and error passed to the callback.
  • there was an error event during the transaction.

transaction.commit(callback)

Commits the transaction. The callback(error) is called after the transaction is committed. If error is set then the commit failed for some reason and rollback was performed instead.

Queued operations are executed after the actual commit but before the callback. This helps to prevent starvation.

transaction.rollback(callback)

Rolls back the transaction. The callback(error)is called after the operation is completed.

Important notes

Remember to call commit() or rollback() on each transaction otherwise you lock your DB forever.

sqlite3-transactions is in very early stage of development. Please help me test it.