0.3.11 • Published 10 years ago

sand-mysql-simple v0.3.11

Weekly downloads
3
License
ISC
Repository
github
Last release
10 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

10 years ago

0.3.10

10 years ago

0.3.9

10 years ago

0.3.8

10 years ago

0.3.7

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.2.0

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago