0.3.11 • Published 9 years ago

sand-mysql-simple v0.3.11

Weekly downloads
3
License
ISC
Repository
github
Last release
9 years ago

sand-mysql-simple

A minimalist MySQL Model extension for Sand.js

###Usage

  1. Create a model for a table

// your first model (i.e. /models/User.js)
var User;
module.exports = User = require('sand-mysql-simple').Model.extend({
	table: 'user', // this is the only required parameter

	values: function() {
		return {
			user_id: 1,
			email_address: 'test@test.com'
		}
	},	

	getById: function(id, callback) {
		sand.mysqlSimple.models.User.global().selectRow('user_id', id, function(err, row) {
			if (err) {
				return callback(err);
			}
			
			var userModel = sand.mysqlSimple.models.User.global().createFromRow(row);
			callback(null, userModel);
		});
	},


	save: function(callback) {
		// this calls your .values() function above, and collects all the non-null properties together into an Array with a corresponding query string that has placeholders ready to use with node-mysql2 (which is what sand-mysql uses)
		var insert = this.getInsert(); 
		
		// note that you need to use 'sand-mysql'
		sand.mysql.query(insert.query, insert.values, function(err, result) {
			if (err) {
				return callback(err);
			}
			callback(null, result);
		});
	},

	// creates a model from a db row
	createFromRow: function(row) {
		var user = new User;
		user.id = row.user_id;
		user.email = row.email_address;
		return user;
	}
});
  1. Use it in your app
var mysqlSimple = require('sand-mysql-simple');

// initialize your sand instance
var sand = require('sand')();

sand
	.use(mysqlSimple, {modelsPath: __dirname + '/models'}) // use mysqlSimple
	.start(); // start your sand app

var myUserId = 1;

// now use your models
sand.mysqlSimple.models.User.getById(myUserId, sand.log);
0.3.11

9 years ago

0.3.10

9 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.2.0

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago