PGDay 2011 2 di
Mi presento
Lucio GrenziSviluppatore Delphi dal 1999Consulente Attualmente sviluppatore Asp.netPostgresql addicted
Nonantolando.blogspot.com lucio.grenzi lucio grenzi
PGDay 2011 3 di
Agenda
Cos' e' Node.js
Node.js ed i databases
Postgres.js
Live Coding
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)
PGDay 2011 5 di
Node.js
Creato nel 2009 da Ryan Dahl
Sponsorizzato da Joyent
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
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
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
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
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
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);
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}
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)
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
PGDay 2011 15 di
Node.Js npm repository
https://github.com/joyent/node/wiki/modulesRepository contenente tutti i moduli per Node.js
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?
PGDay 2011 17 di
Postgresql vs NoSQL
Crisi per CouchDB ( http://www.internetnews.com/blog/skerner/is-couchdb-in-trouble-.html )
PGDay 2011 18 di
&
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
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
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");
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 } ]
PGDay 2011 23 di
Pero' ..
Postgresql si basa su request/response in modalita' sincronaNon possiamo bloccare il thread principale di Node.js
PGDay 2011 24 di
Quindi?
Le richieste verso Postgresql vengono bufferizzateNode.Js non attende le risposte dal database
L'utente vede un'applicazione responsiva
PGDay 2011 25 di
Risorse
www.Nodejs.org
https://github.com/creationix/postgres-js
www.postgresql.org
PGDay 2011 26 di
Q & A
?
PGDay 2011 27 di
Grazie
http://www.slideshare.net/lucio_grenzi/nodejs-e-postgresql