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

mysql-on-the-rocks

v0.4.11

Published

A dead-simple MySQL module for Node.js

Downloads

3

Readme

MySQL On The Rocks alt text Build Status

A dead-simple Node.js module designed for handling storage / retrieval of Javascript objects to MySQL.

The underlying layer uses Q for Promises and node-mysql as adapter.

Quick example

"use strict";


var mysql = require("mysql-on-the-rocks");


// First you need to create a connection to the db
mysql.create({
  host: "localhost",
  port: 3306,
  user: "admin", 
  password: "abcdefgh"
}).then(function (db) {

  // Retrieve an object
  return db.findObject("wallets", { 
    id: 123 
  }).then(function (obj) {

    // Update it
    obj.dollars += 100;
    
    // Save to the db
    return db.save(obj);
  });

}).fail(function (error) {
  // ...
}).done();

API Reference

mysql.findObjects(tableName, query, [order, count, offset])

Finds an object in the database.

| Argument | Type | Description | |---------------------------|-------------------------|---------------------------------------------------------------| | tableName | String | Name of the table to query on | | query | Object | An object describing the query | | order | Object | An object with fields as keys and order direction as values. | | count | Number | The max amount of objects to return | | offset | Number | The starting offset of the result set |

Returns Promise<Object>


mysql.findObject(tableName, query, [order, offset])

Alias of mysql.findObjects with count set at 1.


mysql.save(tableName, obj)

| Argument | Type | Description | |---------------------------|-------------------------|---------------------------------------------------------------| | tableName | String | Name of the table to query on | | obj | Object | The object to save |

Returns Promise<Object>

If the object has an id already, it will be updated, otherwise it will be inserted and the id will be appended.


mysql.insertObject(tableName, obj)

and

mysql.updateObject(tableName, obj)

See mysql.save()


mysql.deleteObject(tableName, object)

| Argument | Type | Description | |---------------------------|-------------------------|---------------------------------------------------------------| | tableName | String | Name of the table to query on | | obj | Object | The object to delete (id must be defined) |

Returns Promise<Object>

Deletes the object from the table.


mysql.findObjectsWithQuery(queryString)

| Argument | Type | Description | |---------------------------|-------------------------|---------------------------------------------------------------| | queryString | String | The MySQL query |

Returns Promise<Object>

Use this method if you want to retrieve objects and not use the internal query builder.


mysql.query(queryString)

| Argument | Type | Description | |---------------------------|-------------------------|---------------------------------------------------------------| | queryString | String | The MySQL query |

Returns Promise<Object>

You probably won't use this method that much, but it allows you to call manually the internal query() method of the MySQL driver.

Examples

Save and update an object

mysql.save("users", {
  email: "[email protected]"
}).then(function (user) {
  // .. user.id is now defined!
  
  // Let's update the email now
  user.email = "[email protected]";
  return mysql.save("users", user);
}).fail(function (error) {
  // ...
}).done();

Retrieve a set of objects

var tableName = "users";

var query = { 
  planCode: "pro"
};

var order = {
  created: "desc"
};


mysql.findObjects(
  tableName, query, order, 100
).then(function (users) {
  // .. do something with the received users  
}).fail(function (error) {
  // .. handle the error
}).done();

License

MIT