Skip to content

timtutt/node-mysql-active-record

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

mysql-active-record

NPM Version NPM = Version

Introduction

This is an abstraction framework that leverages the node-mysql module for node.js. It was inspired by CodeIgniter's ActiveRecord class for building SQL queries - former PHP developers will likely recognize this.

Completely written in JavaScript, does not require compiling, and is 100% MIT licensed.

Here is an example on how to use it:

Installation

$ npm install mysql-active-record

Usage

var MysqlActiveRecord = require('mysql-active-record');

var config = {
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'database_name'
}

//confirm connection
var activeRecord = new MysqlActiveRecord(config);

//will produce SELECT * FROM table1;
activeRecord.get('table1', function(results) {
  if (!results.error) {
    if (results.numResults > 0) {
        console.log(results.rows); // actual records
        console.log(results.fields); //fields returned
        console.log(results.numResults); //number of results
        console.log(results.queryExecuted); //sql statement executed
    } else {
        console.log(results.queryExecuted);
    }
  } else {
        console.log(results.error); //any errors returned
        console.log(results.queryExecuted);
  }

});

Note:

  • config options are the same as the ones passed into the node-mysql module
  • connection is implicitly created when a query is run and ended immediately after.

CRUD Operations

SELECT Statements

  • FROM
  • WHERE
  • ORDER BY
  • GROUP BY
  • HAVING
  • JOIN
  • LIMIT

INSERT Statements

UPDATE Statements

DELETE Statements

Todo

  • Update this documentation
  • More testing + adding unit tests
  • Implement create functionality for database tables
  • Implement alter functionality for database tables

Thanks

Huge thanks goes out to Felix Geisendörfer - felixge for doing all of the heavy lifting of getting the node-mysql connector together.

Contributors

Fork this module on github and send a pull requests if you've got some great updates.

For any questions / issues with this module please open an issue.

About

mysql abstraction layer leveraging node-mysql for Node.js

Resources

Stars

Watchers

Forks

Packages

No packages published