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

@eugabrielsilva/rapidsql

v1.1.2

Published

A very simple and easy to use promise-based MySQL wrapper for Node.js

Downloads

3

Readme

rapidSQL

A very simple and easy to use promise-based MySQL wrapper for Node.js.

Installation

npm install @eugabrielsilva/rapidsql

Setting up

const rapidSQL = require('@eugabrielsilva/rapidsql');
const db = new rapidSQL({host: DB_HOST, user: DB_USER, password: DB_PASSWORD, database: DB_DATABASE});

Provide the connection options as an object in the database constructor. Valid options are:

  • host (optional): The hostname of the database you are connecting to. (Defaults to localhost)
  • user (optional): The MySQL user to authenticate as. (Defaults to root)
  • password (optional): The password of that MySQL user. (Defaults to empty)
  • database (required): Name of the database to use for this connection.
  • port (optional): The port number to connect to. (Defaults to 3306)
  • connectionLimit (optional): The maximum number of connections to create at once. (Defaults to 10)
  • stringifyObjects (optional): Stringify objects instead of converting to values. (Defaults to false)
  • debug (optional): Prints protocol details to STDOUT. (Defaults to false)

Connecting to the database

This library uses connection pools as default. You only need to connect to the database once. The connection is made asynchronously.

db.connect().then(() => {
    console.log('Successfully connected!');
}).catch((err) => {
    console.log(err);
});

Running SQL queries

You can run SQL queries by using the sql() method. All queries are made asynchronously and will respond with a result object or an array of objects.

db.sql('SELECT * FROM table_name').then((result) => {
    console.log(result);
}).catch((err) => {
    console.log(err);
});

Escaping query values

In order to avoid SQL Injection attacks, you should always escape any user provided data before using it inside a SQL query. You can do this by using ? characters as placeholders for values you would like to have escaped like this:

let userID = 5;
db.sql('SELECT * FROM users WHERE ID = ?', [userID]).then((result) => {
    console.log(result);
}).catch((err) => {
    console.log(err);
});

Multiple placeholders are mapped to values in the same order as passed. For example, in the following query foo equals a, bar equals b, baz equals c, and ID will be userID:

let userID = 5;
db.sql('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE ID = ?', ['a', 'b', 'c', userID]).then((result) => {
    console.log(result);
}).catch((err) => {
    console.log(err);
});

Closing the connection

If you are not going to use the connection anymore, you can close it by using the close() method. This method will wait until all pending queries are executed.

db.close().then(() => {
    console.log('Successfully disconnected!');
}).catch((err) => {
    console.log(err);
});

Credits

Library developed and currently maintained by Gabriel Silva.