57
How to make Guacamole or Implement a Data Source Pattern for a Multi-Model NoSQL database 11.8.2014 Dirk Breuer/ @railsbros_dirk

Guacamole

Embed Size (px)

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

Citation preview

Page 1: Guacamole

How to make Guacamoleor

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

11.8.2014Dirk Breuer/ @railsbros_dirk

Page 2: Guacamole

How to make Guacamoleor

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

11.8.2014Dirk Breuer/ @railsbros_dirk

Page 3: Guacamole

Data Source Pattern

Architectural patterns which drive the way in which the domain logic

talks to the database.

– Martin Fowler

Page 4: Guacamole

• Active Record

• Data Mapper

• Table Data Gateway

• Row Data Gateway

• …

Page 5: Guacamole

NoSQL

Not ACID compliant

Big DataDistributed

Web Scale

ROFL Scale

CAP

Documents

Flexible

High Availability

Page 6: Guacamole

NoSQL Databases

• “Not only SQL”

• Not one definition

• Sometimes just a marketing buzz-fuzz

Page 7: Guacamole

Classifications

• Document-based

• Key-Value stores

• Graph-based

• Column stores

• Time series

Aggregate-based}

Page 8: Guacamole

Multi-Model

Combine more than one approach within one database system

Page 9: Guacamole

Guacamole

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

A dip consisting at least of ripe avocados and salt

Page 10: Guacamole

…and…

Page 11: Guacamole

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

Object Document Mapper

Page 12: Guacamole

• Open-Source NoSQL database

• Multi-Model

• Document-based

• Graph-based

Page 13: Guacamole

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.

Page 14: Guacamole
Page 15: Guacamole

• 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

Page 16: Guacamole
Page 17: Guacamole

http://docs.arangodb.org/

Everything and more

Page 18: Guacamole

Before you start building your own ODM

Page 19: Guacamole

Patterns of Enterprise Application Architecture

Page 20: Guacamole

Ingredients

Page 21: Guacamole

1 Design Goal

1 Data Source Pattern

3 📦 Supporting Libraries

1 Database driver

1 solid Test-Suite

1 📦 Documentation

Page 22: Guacamole

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

Page 23: Guacamole

Data Source Pattern

Page 24: Guacamole

Data MapperIt’s a

Page 25: Guacamole

+ 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

Page 26: Guacamole

Supporting Libraries

Page 27: Guacamole

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

Page 28: Guacamole

Implementation

Page 29: Guacamole

Just some bits and pieces…

Page 30: Guacamole

ActiveRecord example

class Post < ActiveRecord belongs_to :user has_many :comments end

Page 31: Guacamole

The Model™ in Guacamole

class Post include Guacamole::Model !

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

Page 32: Guacamole

The Collection™ in Guacamole

class PostsCollection include Guacamole::Collection !

map do embeds :comments references :user end end

Page 33: Guacamole

Retrieving Data

# Get a user by email UsersCollection.by_example(email: '[email protected]') # => #<User:0x007fca7186fc98>

Page 34: Guacamole

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!

Page 35: Guacamole

CallbacksExternal

Page 36: Guacamole

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

Page 37: Guacamole

class SecurePony include Guacamole::Model !

callbacks :secure_pony_callbacks !

attribute :encrypted_password, String !

# define a virtual attribute attr_accessor :password end

Page 38: Guacamole

ActiveModel::Callbackspowered by

Page 39: Guacamole

Active Model compliance

Page 40: Guacamole

• 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

Page 41: Guacamole

Development with Metrics

Page 42: Guacamole

Check your Code for

• Complexity

• Duplication / Similarity

• Style

• Smells

• Sufficient documentation

Page 43: Guacamole
Page 44: Guacamole
Page 45: Guacamole

Caveats

Virtus and Circular Dependencies

Page 46: Guacamole
Page 47: Guacamole

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

Page 48: Guacamole

Next Steps

Page 49: Guacamole

A DSL to create AQL-Queries

Page 50: Guacamole

Support for Transactions

Page 51: Guacamole

Support the Graph API

Up Next

Page 52: Guacamole

http://guacamolegem.org

Get a Nacho and dip in

Page 53: Guacamole

Come to us we have cookies animated GIFs.

Page 54: Guacamole
Page 55: Guacamole

Sneak Peak

Page 56: Guacamole

2014.eurucamp.org/podcast

Page 57: Guacamole

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

@railsbros_dirk

Thanks