Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Databases in Node.js

Databases in Node.js

This presentation covers interacting with the PostgreSQL, MongoDB and Redis data stores from Node.js.

Carlos Souza

March 19, 2014
Tweet

More Decks by Carlos Souza

Other Decks in Technology

Transcript

  1. ! with Node.js working with drivers and ORMs Carlos Souza

    (@caike)
 Orlando Node.js User Group March 2014 DataBases https://github.com/node-orlando/march2014 source code for examples
  2. ! ! ! , function (err, result) { if(err) throw

    err;
 done(); ! 
 }); console.log('Results: ' + result.rowCount); console.log(result.rows); var pg = require('pg'); var settings = 'pg://localhost:5432/node_episodes'; pg.connect(settings, function (err, client, done) { }); client.query('SELECT * FROM episodes' if(err) throw err;
  3. pg.connect(settings, function (err, client, done) { }); var pg =

    require('pg'); var settings = 'pg://localhost:5432/node_episodes'; client.query('SELECT * FROM episodes'); query.on('row', function (row, result) { result.addRow(row); }); query.on('end', function (result) { done();
 console.log('Results: ' + result.rowCount); console.log(result.rows); }); if(err) throw err; var query =
  4. pg.connect(settings, function (err, client, done) { }); var pg =

    require('pg'); var settings = 'pg://localhost:5432/node_episodes'; client.query('SELECT * FROM episodes'); query.on('row', function (row, result) { result.addRow(row); }); query.on('end', function (result) { done();
 console.log('Results: ' + result.rowCount); console.log(result.rows); }); if(err) throw err; var query =
  5. }); var query = client.query('SELECT * FROM episodes WHERE title

    = $1', ['Winter is Coming']); var pg = require('pg'); var settings = 'pg://localhost:5432/node_episodes'; pg.connect(settings, function (err, client, done) { query.on('row', function (row, result) { result.addRow(row); }); query.on('end', function (result) { done();
 console.log('Results: ' + result.rowCount); console.log(result.rows); }); if(err) throw err;
  6. ! ! ! ! ! ! ! 
 query.on('row', function(row,

    result) { result.addRow(row); }); query.on('end', function(result) { done(); console.log('Results: ' + result.rowCount); console.log(result.rows); }); pg.connect(settings, function (err, client, done) { if(err) throw err; ! 
 var pg = require('pg'); var settings = 'pg://localhost:5432/node_episodes'; }); var query = client.query('SELECT * FROM episodes WHERE title = $1', ['Winter is Coming']);
  7. runQuery('SELECT * FROM episodes', [], function (rows) { console.log('Results: '

    + rows.length); console.log(rows); }); ! runQuery("SELECT * FROM episodes where title ILIKE $1", 
 ['%snow'], function (rows) { 
 console.log('Results: ' + rows.length); console.log(rows); });
  8. ! ! ! function runQuery(queryString, args, cb){ ! ! !

    ! ! ! ! ! ! } pg.connect(settings, function (err, client, done) { if(err) throw err; client.query(queryString, args, function (err, result) { if(err) throw err; }); }); done(); cb(result.rows); var pg = require('pg'); var settings = 'pg://localhost:5432/node_episodes';
  9. runQuery('SELECT * FROM episodes', [], function (rows) { console.log('Results: '

    + rows.length); console.log(rows); }); ! runQuery("SELECT * FROM episodes where title ILIKE $1", 
 ['%snow'], function (rows) { 
 console.log('Results: ' + rows.length); console.log(rows); });
  10. ! ! ! ! ! db.query = function (queryString, args,

    cb) { ! ! ! ! ! ! ! ! ! ! } ! // File: db.js var pg = require('pg'); var settings = 'pg://localhost:5432/node_episodes'; var db = {}; pg.connect(settings, function (err, client, done) { if(err) throw err; ! client.query(queryString, args, function (err, result) { if(err) throw err; ! done(); cb(result.rows); }) }); module.exports = db;
  11. ! ! db.query('SELECT * FROM episodes', [], function (rows) {

    console.log('Results: ' + rows.length); console.log(rows); }); ! db.query('SELECT * FROM episodes WHERE title ILIKE $1',
 [‘%snow’], function (rows) { 
 console.log('Results: ' + rows.length); console.log(rows); }); var db = require('./db');
  12. ! ! ! Knex.PG = Knex.initialize({ client: 'pg', connection: {

    database: 'node_episodes' } }); // When the app starts var Knex = require('knex');
  13. ! ! ! var episodes = Knex('episodes').select(); episodes.then(function(results) { console.log(results);

    }); // elsewhere, to use the client .PG var Knex = require('knex') ;
  14. // When the app starts var Bookshelf = require('bookshelf'); !

    Bookshelf.PG = Bookshelf.initialize({ client: 'pg', connection: { database: 'node_episodes' } }); ! // elsewhere, to use the client
 var Bookshelf = require('bookshelf').PG;
  15. ! ! ! ! ! ! episodes.fetch().then(function(result){ console.log(result.models); }); var

    Episode = Bookshelf.Model.extend({ tableName: 'episodes' }); = Bookshelf.Collection.forge([], { model: Episode }); var episodes
  16. ! ! ! ! ! ! episodes.fetch().then(function(result){ console.log(result.models); }); =

    Episode.collection(); var Episode = Bookshelf.Model.extend({ tableName: 'episodes' }); var episodes
  17. // app.js ...
 var episode = require('./routes/episode'); 
 app.get('/episodes', episode.list);

    app.get('/episodes/new', episode.new); app.post('/episodes', episode.create); app.get('/episodes/:id/edit', episode.edit); app.patch('/episodes/:id', episode.update); app.del('/episodes/:id', episode.destroy); ! 
 ! ! http.createServer(app).listen(app.get('port'),
 function(){ console.log('Express server running'); } );
  18. var Episode = require('./../models').Episode; ! exports.list = function(req, res){ Episode.findAll(req.param('keyword'),

    function (episodes) { return res.render('episodes', { episodes: episodes }); }) }; // routes/episode.js
  19. 
 ! { ? }); var Episode = Bookshelf.Model.extend({ },

    var Bookshelf = require('bookshelf').PG; // models/index.js ?
  20. ! tableName: 'episodes', ! comments: function () { return this.hasMany(Comment);

    } } ... var Episode = Bookshelf.Model.extend({ }, // models/index.js
  21. { findAll: function (keyword, cb) { ! ! ! !

    ! ! ! ! ! ! ! ! ! } }); var Episode = Bookshelf.Model.extend({ }, ... ! ! if(keyword){ var match = '%' + keyword + '%'; episodes.query(function (qb) { qb.where('title', 'ilike', match). orWhere('description', 'ilike', match); }); } ! ! ! var episodes = this.collection().query('orderBy', 'id', 'ASC'); episodes.fetch({ withRelated: 'comments' }).then(function(result){ cb(result.toJSON()); }); // models/index.js
  22. // models/index.js var Comment = Bookshelf.Model.extend({ tableName: 'comments' }); !

    module.exports = { Episode: Episode, Comment: Comment }
  23. // app.js ...
 var episode = require('./routes/episode');
 app.get('/episodes', episode.list); app.get('/episodes/new',

    episode.new); app.post('/episodes', episode.create); app.get('/episodes/:id/edit', episode.edit); app.patch('/episodes/:id', episode.update); app.del('/episodes/:id', episode.destroy); http.createServer(app).listen(app.get('port'),
 function(){ console.log('Express server running'); } );
  24. // routes/episode.js var Episode = require('./../models').Episode ! exports.list = function(req,

    res){ Episode.findAll(req.param('keyword'), function (err, episodes) { if(err) throw err; return res.render('episodes', { episodes: episodes }); } ); };
  25. // models/index.js var mongoose = require('mongoose'); ! var EpisodeSchema =

    new mongoose.Schema({ title: String, description: String ! 
 
 
 }); ... , comments: [{ description: String, rating: { type: Number, max: 5 } }]
  26. ... EpisodeSchema.static('findAll', function(keyword, cb) { ! if(keyword){ var match =

    new RegExp(keyword, 'i'); return this.find({ $or: [{ title: match}, { description: match }] }).exec(cb); }else{ return this.find().exec(cb); } }); ... // models/index.js
  27. // routes/episode.js exports.edit = function (req, res) { Episode.findById(req.params.id, function(err,

    episode) { return res.render('episode', { episode: episode }); }); } ! exports.update = function (req, res) { Episode.findByIdAndUpdate(req.params.id, req.body.episode, function (err, episode) { return res.redirect('/episodes'); } ); } ! exports.destroy = function (req, res) { Episode.findByIdAndRemove(req.params.id, function () { return res.redirect('/episodes'); }) }
  28. ...
 
 client.sadd('characters', 'Eddard Stark'); client.sadd('characters', 'Cersei Lannister'); client.sadd('characters', 'Robert

    Baratheon'); client.sadd('characters', 'Khal Drogo'); client.sadd('characters', 'Jon Snow'); client.sadd('characters', 'Littlefinger'); ! ...
  29. var character = message; ! client.srem('characters', character, function(err) { if(err)

    throw err; ! client.smembers('characters', function (err, members) { console.log('Remainig characters: ' + members); }) }); clientListener.on('message', function (channel, message) { });
  30. // File: client2.js var redis = require('redis'), client = redis.createClient();

    // $ node client2 <character> var victim = process.argv[2]; // Kills that character client.publish('arya', victim, function () { client.end(); });
  31. ! with Node.js working with drivers and ORMs Carlos Souza

    (@caike)
 Orlando Node.js User Group March 2014 DataBases https://github.com/node-orlando/march2014 source code for examples