Seven Databases

in Node.js


Julián Duque : @julian_duque

Alex Ramirez : @RamirezAlex_



SQL




PostgreSQL


 $ npm install pg

Others...
 $ npm install mysql
$ npm install mariasql
$ npm install sqlite2


Sequelize


 $ npm install sequelize



Redis



 $ npm install redis
$ npm install hiredis



CouchDB



 $ npm install request
$ npm install nano



Firebase


 $ npm install firebase




Entering Alex!



MongoDB



Es un sistema de base de datos NoSQL orientado a documentos En vez de

guardar los datos en tablas como se hace en las base de datos

relacionales, MongoDB guarda estructuras de datos en documentos tipo

JSON con un esquema dinámico (MongoDB llama ese formato BSON - Binary

JSON)



Document





Main Features


Ad hoc queries

Indexing

Sever-side JavaScript Excecution

Aggreagation - Map-Reduce

Load balancing


Mongoose, MongoDB + Node.JS



Let's face it, writing MongoDB validation, casting and business logic
boilerplate is a drag. That's why we wrote Mongoose.


Mongoose plugins




But NPM :)

Title

Code Samples

// Dependencies - Driver
var mongoose = require('mongoose');

// Connection
mongoose.connect('mongodb://localhost/7db'); 
var db = mongoose.connection;

db.on('error',console.error.bind(console,'connection error:'));

db.once('open', function callback(){
console.log('Connected'); });


Schema Definition
// Schema
var dogSchema = mongoose.Schema({ name: String });

// Model
var Dog = mongoose.model('Dog', dogSchema);
    
// Instanciate 2 new dogs.
var basko = new Dog({ name: 'Basko' }); var matilda = new Dog({ name: 'Matilda' });

// Save dogs in db
basko.save(function(err, basko){ if(err) return console.error(err);
  console.log(basko.name + " was saved!");
});
matilda.save();  


Queries

// Query all dogs
Dog.find(function (err, dogs){ 
if(err) return console.error(err); console.log(dogs); }); // Find dogs starting with 'Bas' Dog.find({ name: /^Bas/ }, function(err,dogs){ // console.log(dogs); }); // Delete Dog.find({ _id: '53574eaac1c57183679370b9' }).remove().exec();

Title



RethinkDB



RethinkDB es un sistema de base de datos construído para almacenar documentos JSON.

Es distribuído y escalable
r.db('Disney')
.table('StarWars')
.get('Luke')
.update({'father':'Vader'})
.run()


Main Features


JSON Data models
Distributed Joins
Subqueries
Aggregation
Atomic updates


Cuando es es bueno usar

RethinkDB


Cuando se necesitan schemas de manera distribuída que van a estar en nodo o en un cluster de hasta 16 nodos

Cuando no es bueno usar RethinkDB

Cuando se necesitan schemas que requieran ACID


Code Sampless

$ npm install rethinkdb
// Connection
var r = require('rethinkdb');

r.connect({ 
host: 'localhost',
port: 28015,
db: 'marvel',
authKey: 'hunter2' }, function(err, conn) { ... });


// Insert
r.table("posts")
.insert({ id: 1,
title: "Lorem ipsum",
content: "Dolor sit amet"
}).run(conn, callback);
// Update r.table("posts")
.get(1)
.update({status: "published"})
.run(conn, callback); // Delete r.table("comments").get("7eab9e63-73f1-4f33-8ce4-95cbea626f59")
.delete().run(conn, callback);



LevelDB

LevelDB is a fast key-value storage library written at Google that
provides an ordered mapping from string keys to string values.

Powered by Google
Jeffrey Dean
Sanjay Ghemawat

Written in C++ and uses B+ trees


LevelDB

is NOT a Database


Main Features


Keys and values are arbitrary byte arrays
Basic operations are Put(key, value), Get(key), Delete(key)


Rod Vagg & LevelUP




Modules


Complete databases
ORMs
Storage back-ends
Plugings


Code Samples


var level = require('levelup');
var db = level('./DatabaseDirectory');
db.put('key', 'value', function (err) { /* ... */ });
db.get('key', function(error, data){ //data == 'value' }); 
db.del('key' , function (err) { /* ... */ });

// a Batch operation made up of 3 Puts
db.batch([
  { type: 'put', key: 'spouse', value: 'Ri Sol-ju' }
, { type: 'put', key: 'dob', value: '8 January 1983' }
, { type: 'put', key: 'occupation', value: 'Clown' }
], function (err) {
 });    

Seven Databases

By Julián Duque

Seven Databases

  • 2,136