node.js e Postgresql

27
PGDay 2011 1 di Lavorare con Node.js e PostgreSQL Grenzi Lucio [email protected]

description

Introduction to Node.js and how to use it in conjuction with Postgresql

Transcript of node.js e Postgresql

Page 1: node.js e Postgresql

PGDay 2011 1 di

Lavorare conNode.js e PostgreSQL

Grenzi [email protected]

Page 2: node.js e Postgresql

PGDay 2011 2 di

Mi presento

Lucio GrenziSviluppatore Delphi dal 1999Consulente Attualmente sviluppatore Asp.netPostgresql addicted

Nonantolando.blogspot.com lucio.grenzi lucio grenzi

Page 3: node.js e Postgresql

PGDay 2011 3 di

Agenda

Cos' e' Node.js

Node.js ed i databases

Postgres.js

Live Coding

Page 4: node.js e Postgresql

PGDay 2011 4 di

Node.js is an event-driven I/O server-side JavaScript environment based on V8.

It is intended for writing scalable network programs such as web servers. (Wikipedia)

Page 5: node.js e Postgresql

PGDay 2011 5 di

Node.js

Creato nel 2009 da Ryan Dahl

Sponsorizzato da Joyent

Page 6: node.js e Postgresql

PGDay 2011 6 di

Javascript

Node.js e' un web server basato su V8

Javascript e' single thread

Node.js gira in ambienti Unix-like

Dalla versione 0.6 gira anche su Windows

Libreria con chiamate non bloccanti

Page 7: node.js e Postgresql

PGDay 2011 7 di

Node.js performance?

Single thread ma consente migliaia di connessioniProcessa velocemente le task velociNon si blocca in attesa delle task lentePrevede strumenti per la gestione di software scalabili in rete

Page 8: node.js e Postgresql

PGDay 2011 8 di

Perche usarlo?

Esistono web server molto piu' blasonati (Apache Nginix, ecc.)Node.js risponde piu' velocemente rispetto ad ApacheTrasfer rate piu' elevato e minori richieste fallitePero' necessita di piu' memoria

Page 9: node.js e Postgresql

PGDay 2011 9 di

Installare Node.js su Ubuntu

sudo apt-get install g++ curl libssl-dev apache2-utilswget http://nodejs.org/dist/node-v0.6.2.tar.gzgunzip node-v0.6.2.tar.gztar -xf node-v0.6.2.tarcd node./configuremakesudo make installnode -v

Page 10: node.js e Postgresql

PGDay 2011 10 di

Http Server

var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n');}).listen(8000);

$curl localhost:8000Hello World

var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n');}).listen(8000);

$curl localhost:8000Hello World

Page 11: node.js e Postgresql

PGDay 2011 11 di

Async I/O

La lettura del file non e' bloccante

fs.readFile(“file.txt”, "text", function(data) { dosomething(data); }); doSomethingelse(data);

fs.readFile(“file.txt”, "text", function(data) { dosomething(data); }); doSomethingelse(data);

Page 12: node.js e Postgresql

PGDay 2011 12 di

Anche se ..

Concettualmente semplice, in pratica pessimo

fs.readFile(“file.txt”, "text", function(data) { dosomething(data); } );

while (true) { // Il processo si blocca}

fs.readFile(“file.txt”, "text", function(data) { dosomething(data); } );

while (true) { // Il processo si blocca}

Page 13: node.js e Postgresql

PGDay 2011 13 di

Node.Js npm

npm is a package manager for Node.js that is run through the command line and manages dependencies for an application.

It is the predominant package manager for Node.js

(Wikipedia)

Page 14: node.js e Postgresql

PGDay 2011 14 di

Installiamo mpn

$ curl http://npmjs.org/install.sh | sudo sh$npm -v$npm search

$ curl http://npmjs.org/install.sh | sudo sh$npm -v$npm search

Page 15: node.js e Postgresql

PGDay 2011 15 di

Node.Js npm repository

https://github.com/joyent/node/wiki/modulesRepository contenente tutti i moduli per Node.js

Page 16: node.js e Postgresql

PGDay 2011 16 di

Postgresql vs NoSQL

Progetti giovani, non sempre c'è un supporto commercialeDati risiedono in memoria .. si deve forzare la scritturaIndicizzazione non sempre efficienteDriver verso i linguaggi di programmazione?

Page 17: node.js e Postgresql

PGDay 2011 17 di

Postgresql vs NoSQL

Crisi per CouchDB ( http://www.internetnews.com/blog/skerner/is-couchdb-in-trouble-.html )

Page 18: node.js e Postgresql

PGDay 2011 18 di

&

Page 19: node.js e Postgresql

PGDay 2011 19 di

Postgresql e Node.js

postgres.js: implementazione in puro JShttps://github.com/creationix/postgres-js

node.postgres: libq binding o puro JShttps://github.com/brianc/node-postgres

Installiamo postgres.js:

Npm install pgNpm install pg

Page 20: node.js e Postgresql

PGDay 2011 20 di

postgres.js

Postgres.js non usa la libqE' scritto in Javascript, quindi si deve conoscere il linguaggioUn solo comando per volta verso PostgresqlInternamente crea una coda di comandi

Page 21: node.js e Postgresql

PGDay 2011 21 di

Postgres.js inizializzazione

/* setup di Node.js e Postgres.js */// postgres.js code

var sys = require("sys");var pg = require("./lib/postgres-pure");

var db = new pg.connect("pgsql://test:lucio@localhost:5432/returning_test");

Page 22: node.js e Postgresql

PGDay 2011 22 di

Esempio pratico

db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id", function (sth) { sth.execute("text value", function(rs) { if (rs === undefined) { console.log("No data in table."); } else { console.log(sys.inspect(rs)); } });});

// output:$ node demo.js[ { id: 4 } ]

Page 23: node.js e Postgresql

PGDay 2011 23 di

Pero' ..

Postgresql si basa su request/response in modalita' sincronaNon possiamo bloccare il thread principale di Node.js

Page 24: node.js e Postgresql

PGDay 2011 24 di

Quindi?

Le richieste verso Postgresql vengono bufferizzateNode.Js non attende le risposte dal database

L'utente vede un'applicazione responsiva

Page 25: node.js e Postgresql

PGDay 2011 25 di

Risorse

www.Nodejs.org

https://github.com/creationix/postgres-js

www.postgresql.org

Page 26: node.js e Postgresql

PGDay 2011 26 di

Q & A

?

Page 27: node.js e Postgresql

PGDay 2011 27 di

Grazie

http://www.slideshare.net/lucio_grenzi/nodejs-e-postgresql