shennan
shennan

Reputation: 11676

Connecting NodeJS to MySQL without a module

I'm aware of the popularity of a module like node-mysql for connecting to a database from an application, but I can't find any info on the connecting process without using a module like this.

Obviously I could go fishing around the modules themselves for the answer, but is there really no user-case for simple connections with simple queries without module dependency and bloated functionality?

I find it strange given the very simple I/O of a process like MySQL.

Upvotes: 2

Views: 2231

Answers (2)

mr.freeze
mr.freeze

Reputation: 14060

This has less to do with node.js and more to do with knowing how to implement the MySql client/server protocol. You simply need to create a tcp connection to the server and send the correct format and sequence of data per the protocol. node-mysql has done the difficult part: abstracting the protocol into something much easier to use.

Upvotes: 4

alfonsodev
alfonsodev

Reputation: 2754

This is subjective, but looking at the example in https://github.com/felixge/node-mysql
for me looks like simple connection and simple Query

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

If you have a look to the source code you'll see what it takes to implement the mysql client protocol, I would say that is not that simple
https://github.com/felixge/node-mysql/blob/master/lib/Connection.js
https://github.com/felixge/node-mysql/tree/master/lib/protocol

But again this is something subjective,IMHO I don't think that there is a simpler way to query MySql.

Upvotes: 0

Related Questions