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

jsdbc

v0.2.0

Published

A database querying module for MySQL, Postgres, Oracle and SQLite. Supports transactions and result sets. libzdb is required for compiling and using.

Downloads

6

Readme

jsdbc

This is a database client for node.js that uses libzdb to query some relational databases. Internally it uses a connection pool. It supports prepared statements and result sets. You can use it with MySQL, Postgres, Oracle and SQLite databases.

How to build

You need libzdb installed, with headers. Just run

node-gyp configure && node-gyp build

to build the module.

How to use

Include it in node.js

var mysql = require('jsdbc');

(this name will be changed in the future).

Connect with

mysql.connect({
  type: 'mysql',
  host: 'localhost',
  user: 'myuser,
  password: 'mypassword',
  port: 3306,
  database: 'mydatabase'
});

These values are non optional and must be set.

Type has to be one of mysql, postgresql, oracle or sqlite.

Querying

jsdbc has one method for selecting and one for other queries.

mysql.select('SELECT * FROM table', function(data, err) {
  if(err) throw err;
  console.log(data);
});
//--> [ {name: 'FrontierPsychiatrist', id: '1', age: '27'}, {name : ...}]

mysql.query("UPDATE table SET name = 'MCS' WHERE id = 1", function(data, err) {
  if(err) throw err;
  console.log(data);
});
//--> {affectedRows: 1}

//you can leave the callback function and no result parsing will take place.
mysql.query("UPDATE table SET name = 'MCS' WHERE mid = 1");

Prepared statements

Prepared Statements are also allowed, they are used if the second argument is an array:

mysql.select('SELECT * FROM table WHERE id = ?', [1], function(data, err) {
  if(err) throw err;
  console.log(data);
});
//--> [ {name: 'FrontierPsychiatrist', id: '1', age: '27'} ]

The callback function is mandatory for prepared statements.

Transactions

Transactions are used like this:

mysql.transact( function(connection) {
  connection.query('INSERT INTO table (id, name) VALUES (1, "FrontierPsychiatrist")', function(data, err) {
    if(err) {
      connection.rollback();
      connection.close();
      throw err;
    } else {
      connection.select('SELECT * FROM table WERE id = ?', [1], function(data, err) {//contains an error!
        if(err) {
          connection.rollback();
          connection.close();
          throw err;
        } else {
          connection.commit();
          connection.close();
        }
      })
    }
  );
});

You pass a function that takes a connection object to mysql.transact and everything executed on this object will be transactional. Make sure to close your connection! If the connection object gets garbage collected by V8 the connection will be closed, but garbage collection does only happen that often and the connection pool isn't infinite.

Result Sets

The standard query approach loads all resulting rows into memory. This can be unfortunate if you have a large number of rows selected. To overcome this, the standard streaming result set approach is implemented.

mysql.stream("SELECT * FROM table WHERE age > ?", [18], function(resultSet, err) {
  if (err) throw err;
  while(resultSet.next()) {
    console.log(resultSet.getString(1));
  }
  resultSet.close();
});

Again, it is important that you close your result set to return the connection to the pool.

  • Currently, only getString is implemented
  • mixing transactions and streaming result sets is not yet possible

Future features

  • currently all fields in a prepared statement are treated as strings, this will be improved.
  • a method to obtain the lastInserId from inserts