mysql-on-the-rocks

A dead-simple MySQL module for Node.js

MySQL On The Rocks

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.

"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();

Finds an object in the database.

ArgumentTypeDescription
tableNameStringName of the table to query on
queryObjectAn object describing the query
orderObjectAn object with fields as keys and order direction as values.
countNumberThe max amount of objects to return
offsetNumberThe starting offset of the result set

Returns Promise<Object>


Alias of mysql.findObjects with count set at 1.


ArgumentTypeDescription
tableNameStringName of the table to query on
objObjectThe 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.


and

See mysql.save()


ArgumentTypeDescription
tableNameStringName of the table to query on
objObjectThe object to delete (id must be defined)

Returns Promise<Object>

Deletes the object from the table.


ArgumentTypeDescription
queryStringStringThe MySQL query

Returns Promise<Object>

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


ArgumentTypeDescription
queryStringStringThe 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.

mysql.save("users", {
  email: "hello@olivierkaisin.co"
}).then(function (user) {
  // .. user.id is now defined! 
  
  // Let's update the email now 
  user.email = "hi@olivierkaisin.co";
  return mysql.save("users", user);
}).fail(function (error) {
  // ... 
}).done();
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();

MIT