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

crudite

v2.0.3

Published

A simple promise-based CRUD wrapper for Node-MySQL2

Downloads

6

Readme

Node MySQL 2 Crudite

A simple promise-based query wrapper to perform basic CRUD on node-mysql2 easily.

Disclaimer

This library hasn't been tested for various and complex use case, so use it at your own risk.

Table of contents

Why Crudite

MySQL2 is a great library that allow us to use MySQL on Node easily. To perform a crud operation you'd write the query like this:

// simple query
db.query(
  'SELECT * FROM `table`,
  function (err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

But, what if you could just call read() instead of SELECT ..., call create() instead of INSERT INTO ..., call update() instead of UPDATE table SET column = value... everytime you perform CRUD? That would be great isn't it? That's the purpose of this library. It provides us a simple promise-based method to perform CRUD on MySQL2.

// Crudite way of reading MySQL table
db.read("table").then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

Installation

npm install crudite

Setup

First, import Crudite and assign it to a variable. Then we instantiate Crudite by passing the config object (like the one that we usually pass to the createPool() method of mysql2).

// import Crudite
const Crudite = require("Crudite");

// Instantiate Crudite 
const db = Crudite({
  host: "localhost",
  user: "root",
  password: "secret",
  database: "test",
});

CRUD

To perform CRUD, we simply call the available crud method on the Crudite instance. In our example above, it's the db.

Create

To create an entry we need to pass the table name (string) as the first argument and an object with a property named data - that contain the key-value pair for each table column - as second argument:

// Promise 
db.create("table", { data: { column1: "value", column2: "value" } })
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler() {
  try {
    console.log(await db.create("table", { data }));
  } catch (error) {
    console.log(error);
  }
}

Read

To retrieve all entries, we call read() method and pass the table name as an argument:

// Promise
db.read("table").then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler() {
  try {
    const data = await db.read('table');
    console.log(data);
  } catch (error) {
    console.log(error);
  }
},

To retrieve an entry by id, we add the second argument which is an object with id property and (obviously) its value:

// Promise
db.read("table", { id: 1 })
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler() {
  try {
    const data = await db.read('table', { id: 1 });
    console.log(data);
  } catch (error) {
    console.log(error);
  }
}

If we want to specify what column returned, add fields property to the second parameter:

// Promise
db.read("table", { id: 1, fields: ["column1", "column2"] })
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler() {
  try {
    const data = await db.read('table', { id: 1, fields: ["column1", "column2"] });
    console.log(data);
  } catch (error) {
    console.log(error);
  }
}

Update

To update an entry, we pass an object with id (integer) and data (object that contain key-value pair of the updated column) property:

// Promise
db.update("table", { id: 1, data: { column1: "Value1", column2: "value" } })
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler() {
  try {
    console.log(await db.update('table', { id: 1, data: { column1: "Value1", column2: "value" } }));
  } catch (error) {
    console.log(error);
  }
},

Delete

To delete an entry, we pass the table name as first argument and object with id property as second argument:

// Promise
db.delete("table", { id: 1 })
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

// Async/Await
async handler(request, h) {
  try {
    console.log(await db.delete('table', { id: 1 }));
  } catch (error) {
    console.log(error);
  }
},

Raw Query

Need more than basic CRUD query such as a join or are you more comfortable writing raw sql instead? We got you covered, just use the query() method.

db.query("SELECT table1.column, table2.column FROM table1 JOIN table2 ON table1.column = table2.column")
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

You can even use prepared statement:

db.query("INSERT INTO table (column1, column2) VALUES (?, ?)", ['value1', 'value2'])
.then((results) => {
  console.log(results);
}).catch((error) => {
  console.log(error);
});

Configuration

Configuration for Crudite is basically a config object that you provide to MySQL2 createPool() method. You should check their API documentation to see all available API options.

Roadmap

  • Perform crud in bulk
  • Search feature for read operation
  • Enable user to specify the 'where' column for read, update, and delete

Acknowledgements

Contributing

Found bug or want to improve crudite? Email me at [email protected]