Giter Site home page Giter Site logo

node-mysql-chassis's Introduction

Node MySQL Chassis

Node MySQL Chassis wraps node-mysql and provides a small abstraction layer to write SQL more easily. Note that I still have a lot of ideas and work to do here. It's in early release. This project will never turn into an ORM or a Model, but rather it would be a good fit for an ORM or Model to use. Contributions welcome.

Install

npm install --save mysql-chassis

Initialize

var db = require('mysql-chassis');

db.init({
    host: 'localhost',         // optional, defaults to localhost
    database: 'databasename',
    user: 'username',
    password: '',
    sqlPath: './sql'           // optional
});

Usage

.select(sql, callback)

Call without binding SQL values:

db.select('SELECT * FROM user', function(err, rows, fields) {
  // err: Will have node-mysql's error if applicable
  // rows: Is always an array of objects returned from the query. It will be an empty array if no results
  // fields: Is the field data if applicable
});

.select(sql, values, callback)

Safely bind values to SQL:

var values = {id: 1};
db.select('SELECT * FROM user WHERE user_id = :id', values, function(err, rows, fields) {
  ...
});

.selectFile(filename, values, callback)

.selectFile() works almost exactly like .select() except it takes a filename instead of SQL:

var values = {id: 1};
db.selectFile('user.sql', values, function(err, rows, fields) {
  ...
});

Note that the filename can written with or without the .sql extension. The file will be found in the sqlPath as set during initialization. Using files to store SQL will allow developers to write multiline SQL as follows:

user.sql:

SELECT name
FROM user
WHERE user_id = :id

.insert(table, values, callback)

This method will write your INSERT statement. The example assumes database columns: name and email

var values = {name: 'Brad', email: '[email protected]'};
db.insert('user', values, function(err, id) {
  // err: (same as select)
  // id: The Insert ID
});

.update(table, values, where, callback)

This method will write your UPDATE statement. Let's update my name:

var values = {name: 'Bradley'};
db.update('user', values, {user_id: 1}, function(err, affectedRows) {
  // err: (same as select)
  // affectedRows: The number of affected rows
});

Notice that the where argument is an object. This will convert to WHERE user_id = 1

node-mysql-chassis's People

Contributors

therealklanni avatar bradwestfall avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.