Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 5308

Connect Node.js to SQL Azure with Tedious

$
0
0

The following shows how to create a simple Node.js project that reads and writes data to SQL Azure.

To get a clean foundation for further development i decided to split things into multiple files (dataaccess, model and management). I also added a appdbtest.js file to demo how to use the management class to create new customers and persist them to the database.

First of all you need to create a new empty folder and run

npm init

then install Tedious (a java script library to access SQL Databases)

npm install tedious

Next step is to create a config.js file like this:

var config = {

userName: 'yourAzureDbUser',

password: 'yourAzureDbPassword',

server: 'yourserver.database.windows.net',

options: {

database: 'yourDatabaseName',

encrypt: true,

rowCollectionOnRequestCompletion: true

}

};

module.exports = config;

Then you can create a customer.js file:

function Customer(email,password,credits) {

this.id = -1;

this.email = email;

this.password = password;

this.credits = credits;

this.greeting = function(){

console.log('Hi! I'm ' + this.email + '.' + this.id);

};

};

module.exports = Customer;

With this you can create a dbcall.js file and include the customer class as well as the tedious config file:

(note that the following is just example code and you should make sure that not sql injection can happen, and that you hash all your passwords)

const Connection = require('tedious').Connection;

const Request = require('tedious').Request;

var sqlconfig = require('./config.js');

var Customer = require ('../models/customer.js');

module.exports.insertCustomer = function(Customer,callback){

//neue Verbindung aufmachen

const connection = new Connection(sqlconfig);

connection.on('connect', err => {

if (err) {throw new Error('Cannot connect to SQL Server!'); }

connection.execSql(

new Request('Insert Into dbo.Customer Values (''+Customer.email + '','' + Customer.password + '','+ Customer.credits+');  SELECT SCOPE_IDENTITY();', 

(err, rowCount, rows) => {

err ? console.log(err) :

console.log(rows[0][0].value);

Customer.Id = rows[0][0].value;

callback(Customer);

})

);

});

}

module.exports.getCustomerById = function(Id,callback){

//neue Verbindung aufmachen

console.log('Reading rows from the Table...');

var connection = new Connection(sqlconfig);

connection.on('connect', err => {

err ? console.log(err) :

connection.execSql(new Request('Select * from Customer where Id = ' + Id,

function(err, rowCount, rows)

{

var c = new Customer();

c.id = rows[0][0].value;

c.email = rows[0][1].value;

c.password = rows[0][2].value;

c.credits = rows[0][3].value;

callback(c);

//process.exit();

})

);

});

}

To provide an easier interface you should create the following management.js file:

var customer = require ('./customer.js');

var dbcall = require('../dataaccess/dbcall.js');

function Management() {

this.getCustomerById = function(id,callback)

{

dbcall.getCustomerById(id,callback);

};

this.createCustomer = function (email,password,credits,callback) {

console.log('creating new customer..');

var x = new customer(email,password,credits);

dbcall.insertCustomer(x,callback);

};

};

module.exports = Management;

Finally you can create a appdbtest.js file to test your management-API:

var management = require ('./models/management.js');

var m = new management();

m.createCustomer('Hans@Huber.at','thisshouldbehashed',1,(Customer) => {

console.log('Created a new Customer with ID!' + Customer.Id);

})

m.getCustomerById(1, (Customer)=>

{

console.log('retrieved Customer 1 with Email:' +Customer.email);

})

It should be easy to extend this pattern with additional elements to create a nice express webapp that uses SQL Azure instead of Mongodb.


Viewing all articles
Browse latest Browse all 5308

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>