0.4.11 • Published 10 years ago

mysql-on-the-rocks v0.4.11

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

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.

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>


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

Alias of mysql.findObjects with count set at 1.


mysql.save(tableName, obj)
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.


mysql.insertObject(tableName, obj)

and

mysql.updateObject(tableName, obj)

See mysql.save()


mysql.deleteObject(tableName, object)
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.


mysql.findObjectsWithQuery(queryString)
ArgumentTypeDescription
queryStringStringThe MySQL query

Returns Promise<Object>

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


mysql.query(queryString)
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.

Examples

Save and update an object

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

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