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

easydbi

v0.2.9

Published

A simple database interface for NodeJS

Downloads

14

Readme

EasyDBI - A Simple Database Interface for NodeJS

EasyDBI is a simple database interface for NodeJS. The precessor, DBConnect, turns out to still be too complex, so this is even more simplified.

EasyDBI used to come with Sqlite3 by default. This is now separated into its own module.

Easydbi now comes with a typescript definition as well.

Installation

npm install easydbi

Usage

var DBI = require('easydbi'); // already comes with sqlite3

DBI.setup('test', {type: 'sqlite', options: {filePath: './test.db'}})

// "prepare" queries. 
DBI.prepare('test', 'createTest', {exec: 'create table test_t (c1 int, c2 int)'});
DBI.prepare('test', 'insertTest', {exec: 'insert into test_t valeus ($c1, $c2)'});

// load a whole module of prepared queries. => sync operation. 
DBI.load('test', require('some-prepared-module'));

// making a connection
DBI.connect('test', function (err, conn) {
  if (err) {
    return err;
  } else {
    conn.createTest({}, function(... ) { ... })
  }
});

Setup

EasyDBI has a setup process that's a bit different from other database interfaces. The design is meant to make the abstraction over the different drivers as uniform as possible.

DBConnect.setup

  • name - this is the key to the name of the connection. You can refer to this later in DBI.connect
  • options - this has the following information:
    • type - a string that is the name of the registered DBI driver module
    • options - for sqlite it inclues the following
      • filePath: the file path to the database file. This one takes precedence.
      • memory: true or false
      • timeout: used to control timeout for retries when encountering SQLITE_BUSY with multiple concurrent connections - keep in mind that it slows down the queries.

Connection Creation

DBI.connect creates the connection (auto connected). It expects the name that was used in the setup process, as well as a callback function that expects an error and a connection object.

DBI.connect('test', function(err, conn) {
  if (err) {
    //...  
  } else {
    //...
  }
});

Closing the Connection

conn.disconnect(cb) will close the connection.

Database Query

Althoug the database query will be specific to the underlying driver, currently for SQL databases we use the following conventions:

  • The arguments will be an object
  • The query will expect named parameters with $name as the parameter.

For example,

conn.query('select * from test_t where c1 = $c1', {c1: 2}, function(err, rows) { /* ... */ });

DML such as insert, update, and delete should be passed via conn.exec instead of conn.query, since no results are expected.

conn.exec('insert into test_t values ($c1, $c2)', {c1: 3, c2: 4}, function(err) { /* ... */ });

conn.begin, conn.commit, and conn.rollback handles the transactions. Use them rather than passing raw conn.exec('begin') since the underlying driver might not be able to handle raw query statements for transactions.

conn.begin(function(err) { /* ... */ });

conn.commit(function(err) { /* ... */ });

conn.rollback(function(err) { /* ... */ });

Run scripts

conn.execScript(filePath, function(err) { /* ... */ });

This function expects the script to contain SQL statements that are delimited by ;. Since it's a simple split on ;, make sure you do not have string literals in the script that contains ;.

execScript isn't wrapped in a transaction. So if you need it to contain transactions, make sure you call begin and commit around the call.

Promise

Easydbi utilizes bluebird to provide the promise-based version of the calls. Just append Async to each of the above calls, i.e.

  • DBI.connect(name, cb) becomes DBI.connectAsync(name)
  • conn.disconnect(cb) becomes conn.disconnectAsync()
  • conn.query(query, args, cb) becomes conn.queryAsync(query, args)
  • conn.queryOne(query, args, cb) becomes conn.queryOneAsync(query, args)
  • conn.exec(query, args, cb) becomes conn.execAsync(query, args)
  • conn.begin(cb) becomes conn.beginAsync()
  • conn.commit(cb) becomes conn.commitAsync()
  • conn.rollback(cb) becomes conn.rollbackAsync()
  • conn.execScript(filePath, cb) becomes conn.execScriptAsync(filePath)