boolean handling in nodejs+mysql

add typecast to db connection pool

https://www.bennadel.com/blog/3188-casting-bit-fields-to-booleans-using-the-node-js-mysql-driver.htm

var db = mysql.createConnection({
host: “localhost”,
user: “root”,
password: “”,
database: “testing”,
typeCast: function castField( field, useDefaultTypeCasting ) {

// We only want to cast bit fields that have a single-bit in them. If the field
// has more than one bit, then we cannot assume it is supposed to be a Boolean.
if ( ( field.type === “BIT” ) && ( field.length === 1 ) ) {

var bytes = field.buffer();

// A Buffer in Node represents a collection of 8-bit unsigned integers.
// Therefore, our single “bit field” comes back as the bits ‘0000 0001’,
// which is equivalent to the number 1.
return( bytes[ 0 ] === 1 );

}

return( useDefaultTypeCasting() );

}
});

Advertisements
This entry was posted in DB, Nodejs, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s