Guacamole

Post on 29-Aug-2014

398 views 1 download

Tags:

description

This is a talk about Guacamole, an ODM for ArangoDB to be used in Rack-based framework and especially Rails. Dirk gave the talk in London at Skills Matter

Transcript of Guacamole

How to make Guacamoleor

Implement a Data Source Pattern for a Multi-Model NoSQL database

11.8.2014Dirk Breuer/ @railsbros_dirk

How to make Guacamoleor

Implement a Data Source Pattern for a Multi-Model NoSQL database

11.8.2014Dirk Breuer/ @railsbros_dirk

Data Source Pattern

Architectural patterns which drive the way in which the domain logic

talks to the database.

– Martin Fowler

• Active Record

• Data Mapper

• Table Data Gateway

• Row Data Gateway

• …

NoSQL

Not ACID compliant

Big DataDistributed

Web Scale

ROFL Scale

CAP

Documents

Flexible

High Availability

NoSQL Databases

• “Not only SQL”

• Not one definition

• Sometimes just a marketing buzz-fuzz

Classifications

• Document-based

• Key-Value stores

• Graph-based

• Column stores

• Time series

Aggregate-based}

Multi-Model

Combine more than one approach within one database system

Guacamole

Source: https://en.wikipedia.org/wiki/File:Guacamole.jpg

A dip consisting at least of ripe avocados and salt

…and…

gem 'guacamole'An ODM for ArangoDB to be used with Rails and any Ruby application

Object Document Mapper

• Open-Source NoSQL database

• Multi-Model

• Document-based

• Graph-based

Why should I useArangoDB in favor of

<any other document store>?

With ArangoDB you can perform joinsover your documents similar

to joins in relational databases.

• Powerful query language (AQL)

• REST HTTP interface

• Embedded V8 to allow custom REST interfaces (Foxx)

• Mostly-Memory based with configurable disk synchronization

• Support for Transactions

• Native driver for most languages

http://docs.arangodb.org/

Everything and more

Before you start building your own ODM

Patterns of Enterprise Application Architecture

Ingredients

1 Design Goal

1 Data Source Pattern

3 📦 Supporting Libraries

1 Database driver

1 solid Test-Suite

1 📦 Documentation

Design Goal

• Support building web applications with Ruby on Rails or other Rack-based frameworks

• Focus on easy integration in Views and the general workflow

• Reflect the nature of NoSQL in general and ArangoDB in particular

Data Source Pattern

Data MapperIt’s a

+ Testability is increased

+ Separation of Concern

+ Easier to support databasefeatures like embedded objects

- The average Rails dev isused to Active Record

- Starting with Data Mapper requires more effort

Supporting Libraries

spec.add_dependency 'ashikawa-core' spec.add_dependency 'virtus' spec.add_dependency 'activemodel' spec.add_dependency 'hamster' spec.add_dependency 'activesupport'

Implementation

Just some bits and pieces…

ActiveRecord example

class Post < ActiveRecord belongs_to :user has_many :comments end

The Model™ in Guacamole

class Post include Guacamole::Model !

attribute :title, String attribute :body, String attribute :comments, Array[Comment] attribute :user, User end

The Collection™ in Guacamole

class PostsCollection include Guacamole::Collection !

map do embeds :comments references :user end end

Retrieving Data

# Get a user by email UsersCollection.by_example(email: 'p.pie@cupcakes.com') # => #<User:0x007fca7186fc98>

With AQL support

# Get a user by name UsersCollection.by_aql('FILTER user.name == @name', name: 'Rainbow Dash') # => #<User:0x007fca7186fc98>

http://docs.arangodb.org/Aql/README.html

Don’t be scared!

CallbacksExternal

class SecurePonyCallbacks include Guacamole::Callbacks !

# Those will be triggered by the collection before_create :encrypt_password !

def encrypt_password object.encrypted_password = BCrypt::Password.create(object.password) end end

class SecurePony include Guacamole::Model !

callbacks :secure_pony_callbacks !

attribute :encrypted_password, String !

# define a virtual attribute attr_accessor :password end

ActiveModel::Callbackspowered by

Active Model compliance

• Really useful, not only for Rails

• A lot of Gems build on top of Active Model

• A lot of useful features (i.e. Validations)

• Not related to the Active Record pattern

Development with Metrics

Check your Code for

• Complexity

• Duplication / Similarity

• Style

• Smells

• Sufficient documentation

Caveats

Virtus and Circular Dependencies

class Author extend ActiveSupport::Autoload include Guacamole::Model !

autoload :Book, 'models/book' !

attribute :name, String attribute :books, Array[Book] end

class Book extend ActiveSupport::Autoload include Guacamole::Model !

autoload :Author, 'models/author' !

attribute :title, String attribute :author, Author end

This is not very Ruby

Next Steps

A DSL to create AQL-Queries

Support for Transactions

Support the Graph API

Up Next

http://guacamolegem.org

Get a Nacho and dip in

Come to us we have cookies animated GIFs.

Sneak Peak

2014.eurucamp.org/podcast

https://www.arangodb.orghttp://guacamolegem.org

@railsbros_dirk

Thanks