Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import the core node modules.
- var mysql = require( "mysql" );
- // Create the connection to your database. This time, we're going to use our own custom
- // type casting function to manually coerce some of the columns.
- 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() );
- }
- });
- // Gather records that we know contain a BIT column.
- db.query(
- `
- SELECT
- id,
- name,
- isBFF -- This is a BIT field.
- FROM
- friend
- `,
- function handleResults( error, rows ) {
- console.log( "Results:" );
- console.log( rows );
- }
- );
- // Gracefully close the connection to the database (queued queries will still run).
- db.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement