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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sqltool2

v0.1.5

Published

MySQL object modeling tool

Readme

SQLtool

SQLtool is a MySQL object modeling tool designed to work in an asynchronous environment. SQLtool supports both promises and callbacks.

Installation

$ npm install sqltool2

Importing

// Using Node.js `require()`
const sqltool = require('sqltool2');

// Using ES6 imports
import sqltool from 'sqltool2';

Overview

Connecting to MySQL

await sqltool.connect({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'db_name'
});

Once connected, the open event is fired on the Connection instance. If you're using sqltool.connect, the Connection is sqltool.connection. Otherwise, sqltool.createConnection return value is a Connection.

Important! SQLtool buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MySQL in order to define models.

Defining a Model

Models are defined through the Schema interface.

const Schema = sqltool.Schema;

const BlogPost = new Schema({
  author: 'VARCHAR',
  title: 'VARCHAR',
  body: 'TEXT',
  date: 'DATE'
});

The following example with some features.

const Comment = new Schema({
  name: { type: 'VARCHAR', size: 32, default: 'hahaha' },
  age: { type: 'INT', unsigned: true },
  bio: { type: 'VARCHAR' },
  date: { type: 'DATE', default: new Date() }
});

// create index
Comment.index({name: 'text', bio: 'text'});

// middleware
Comment.pre('save', function (next) {
  console.log(this.name);
  next();
});

Important! We define table structure through model and table is created automatically.

Accessing a Model

Once we define a model through sqltool.model('ModelName', mySchema), we can access it through the same function

const MyModel = sqltool.model('ModelName');

Or just do it all at once

const MyModel = sqltool.model('ModelName', mySchema);

The first argument is the singular name of the table your model is for.

Once we have our model, we can then instantiate it, and save it:

const instance = MyModel.new();
instance.name = 'karen';
instance.save(function (err) {
  //
});

or

const instance = MyModel.new({ name: 'karen' });
instance.save(function (err) {
  //
});

We can find documents from the same table

// find all documents
MyModel.find({ }).exec(function (err, docs) {
  // docs.forEach
});

// find all documents named karen and at least 19
await MyModel.find({ name: 'karen', age: { $gte: 19 } }).exec();

// executes, passing results to callback
MyModel.find({ name: 'karen', age: { $gte: 19 } }).exec(function (err, docs) {
  // docs.forEach
});

// executes, name LIKE karen and only selecting the "name" and "age" fields
await MyModel.find({ name: { $in: 'karen' } }, ['name', 'age']).exec();

// find 3 documents and sort name in DESC order
await MyModel.find({ }).limit(3).sort({ name: -1 }).exec();

// find 3 documents skipping 4
await MyModel.find({ }).limit(3).skip(4).exec();

You can also findOne, findById, updateOne, etc.

const instance = await MyModel.findOne({ ... }).exec();
console.log(instance.name);  // 'karen'

Important! If you opened a separate connection using sqltool.createConnection() but attempt to access the model through sqltool.model('ModelName') it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:

const conn = await sqltool.createConnection({ conn_params });
const MyModel = conn.model('ModelName', schema);
const m = MyModel.new();
m.save(); // works

vs

const conn = await sqltool.createConnection({ conn_params });
const MyModel = sqltool.model('ModelName', schema);
const m = MyModel.new();
m.save(); // does not work b/c the default connection object was never connected