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

websqltracer

v1.0.3

Published

Console tracer for Web SQL or SQLite

Downloads

11

Readme

Web SQL Tracer

Console tracer for Web SQL or SQLite.

If your web project uses SQL in some form, tracing functionality might be helpful to you. webSqlTracer provides means to see SQL commands that your project runs printed in console, just like this:

//Somewhere deep in your program
tx.executeSql('INSERT INTO ' + tableName + ' (id, text) VALUES (?, ?)', [ id, inputValue ]);

Let's see the console

'INSERT INTO foo (id, text) VALUES (?, ?)', args=[12, "user just typed this"]

webSqlTracer can trace database access to Web SQL, as well as access to Phonegap SQLite plugin.

We found it very useful to work with JayData data access library as well.

#Table of contents

Install

To install with npm, you can

npm install websqltracer

To install with bower,

bower install websqltracer

Or you can download webSqlTracer.js minified or source from Github, and include it in your page.

webSqlTracer depends on jquery and underscore libraries, so include them as well.

<!-- Dependencies -->
<script src="jquery.min.js"></script>
<script src="underscore-min.js"></script>
<!-- The webSqlTracer library itself -->
<script src="webSqlTracer.min.js"></script>

This will create global webSqlTracer object to start your tracer.

You can include webSqlTracer to your page with CDN:

<!-- Dependencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.1/underscore-min.js"></script>
<!-- The webSqlTracer library itself -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/websqltracer/1.0.1/webSqlTracer.min.js"></script>

Use

webSqlTracer object is used to start and stop tracing.

Let's say we have some program that accesses Web SQL, and we want to trace database access.

Here's the program:

//Here we initialize the database
var db = openDatabase('MyDatabase', '1.0', 'MyDatabase', 2 * 1024 * 1024);

//Here we create and fill the database
db.transaction(function (tx) {
    tx.executeSql('CREATE TABLE foo (id unique, text)');
    tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "hardcoded value")');
    tx.executeSql('INSERT INTO foo (id, text) VALUES (2, ?)', ["parametrized value"]);
});

//Here we selecte values from the database
db.transaction(function (tx) {
    tx.executeSql('SELECT * FROM foo', [], function (tx, results) {
        var len = results.rows.length, i;
        for (i = 0; i < len; i++) {
            console.log(results.rows.item(i).text);
        }
    });
});

This is very simple example, so we just can actually see from code what SQL commands are executed. In real-world program it will be much more trickier to understand, that's where webSqlTracer comes to help.

To trace our simple program, we just add one little line to its start:

webSqlTracer.traceOnOpen('MyDatabase');

Now, when the program runs, following will be printed to the console:

SQL TRACE : started tracing database MyDatabase version '1.0'
SQL TRACE MyDatabase: 'CREATE TABLE foo (id unique, text)'
SQL TRACE MyDatabase: 'INSERT INTO foo (id, text) VALUES (1, "hardcoded value")'
SQL TRACE MyDatabase: 'INSERT INTO foo (id, text) VALUES (2, ?)', args=["parametrized value"]
SQL TRACE MyDatabase: 'SELECT * FROM foo'

It's now easy to see what's going on with database just by analyzing the console.

Demo

demo folder contains simple html page that uses Web SQL. webSqlTracer is used to trace what's going on, just like you've just seen.

API

webSqlTracer.traceOnOpen(tracePredicate, traceCallback) starts trace when database opens. When database matches tracePredicate, will immediately start tracing it.

webSqlTracer.traceOnOpen('MyDatabase');

//Somewhere later
var db = openDatabase('MyDatabase', '1.0', 'MyDatabase', 2 * 1024 * 1024);
//Trace is active here

tracePredicate can be a string or a function. When string, database name will be matched to it. When function, it will be called on each database open with dbName as parameter. Return true to match.

webSqlTracer.traceOnOpen(function (name) { return name.indexOf("My") === 0; });
//All databases which name starts with "My" will be traced. 

traceCallback is a way to subscribe to event of trace start. db will be provided as argument to the callback. This might be useful to stop trace for specific database.

var traceDbs = [];
webSqlTracer.traceOnOpen('MyDatabase', function (db) { traceDbs.push(db); });

//And later on...
webSqlTracer.stopTrace(traceDb[0]);

webSqlTracer.stopTraceOnOpen() stops listening to database open.

webSqlTracer.startTrace(db, dbName, dbVersion) starts trace for provided db object. dbName and dbVersion are optional parameters used to pretty-print the results.

The method returns promise to indicate when trace is ready.

var db = openDatabase('MyDatabase', '1.0', 'MyDatabase', 2 * 1024 * 1024);
webSqlTracer.startTrace(db, 'MyDatabase').then(function () {
	//we have our tracer ready here
});

webSqlTracer.stopTrace(db) stops trace that was previously started with startTrace.

AMD

webSqlTracer can be used with requirejs.

'jquery' and 'underscore' libraries should be configured with requirejs. The usage is simple:

define(['webSqlTracer'], function (webSqlTracer) {
	//Use webSqlTracer here
}

Node module

webSqlTracer can also be loaded as node.js module:

var webSqlTracer = require('websqltracer');

Things to consider for future versions

  • ~~Add library to cdnjs~~
  • ~~Build the minified library with Grunt~~
  • Add tests
  • Make it visible when transaction start/commit/fail. Print transaction ids to distinguish between transactions
  • Add profiling information (how much time the query runs)

Contribute

Use GitHub issues and Pull Requests.

Ideas are welcome as well :)

To build:

npm run build

Before committing, please run jshint:

npm run jshint