<commit_before># Cassandro [![Gem Version](https://badge.fury.io/rb/cassandro.svg)](http://badge.fury.io/rb/cassandro)

Cassandro is a small Ruby ORM for Apache Cassandra 2.0 and CQL 3.0. Cassandro uses the new Datastax Ruby Driver (official driver).

## Install
 
`gem install cassandro`

## Basic Cassandro

Connecting to Cassandra DB: `Cassandro.connect(Hash options)`. For full list of options visit [Ruby Driver Documentation](http://datastax.github.io/ruby-driver/api/#cluster-class_method)

```ruby
Cassandro.connect(
                  hosts: ['127.0.0.1'],
                  keyspace: 'some_keyspace'
                 )
```

Creating a new keyspace. For full details of keyspace creation visit [CLI keyspace](http://www.datastax.com/documentation/cassandra/2.0/cassandra/reference/referenceStorage_r.html)

```ruby
# < 1.0.0
Cassandro.create_keyspace('new_keyspace', 'SimpleStrategy', 1)

# >= 1.0.0, allow more options
Cassandro.create_keyspace('new_keyspace', {
  replication: {
    class: 'NetworkTopologyStrategy',
    dc_1: 2,
    dc_2: 2
  },
  durable_writes: true
})
```

Select keyspace outside `#connect`

```ruby
Cassandro.use('keyspace_name')
```

Create table.
```ruby
table = <<-TABLEDEF                                                              
  CREATE TABLE IF NOT EXISTS users (                                              
    email VARCHAR,                                                                       
    first_name VARCHAR,                                                                 
    age INT,
    created_at TIMESTAMP,                                                          
    PRIMARY KEY(email,created_at)                                                          
  )                                                                                
TABLEDEF

Cassandro.execute(table)
```

Execute queries.
```ruby
result = Cassandro.execute("SELECT * FROM table_name;")
```

Using Driver directly.
```ruby
statement = Cassandro.client.prepare("SELECT * FROM table_name WHERE colname = ?;")
result = Cassandro.client.execute(statement, id)
```

## Cassandro::Model

### Creating model
Creating new model: make you class inherits form `Cassandro::Model`

```ruby
class User < Cassandro::Model
end
```

Specifying table name using the method `table(table_name)`:

```ruby
class User < Cassandro::Model

  table 'users'
end
```

Adding attributes using the method `attribute(name, type, options)`:

```ruby
class User < Cassandro::Model

  attribute :email, :text
  attribute :first_name, :text
  attribute :age, :integer
  attribute :created_at, :datetime
end
```

types: :uuid, :text, :integer, :float, :timestamp, :datetime

Setting the primary key using the method `primary_key(pk_name | Array)`:

```ruby
class User < Cassandro::Model

  primary_key [:email, :created_at]

end

```

Setting unique field using the method `unique(field | Array)`:

```ruby
class  User < Cassandro::Model

  unique :email
end
```

__A complete example__

```ruby
class User < Cassandro::Model

  table 'users'

  attribute :email, :text
  attribute :first_name, :text
  attribute :age, :integer
  attribute :created_at, :datetime
  
  primary_key [:email, :created_at]

  unique :email
end
```

### Interacting

Creating a new row:

```ruby
user = User.create(email: '<EMAIL>', first_name: 'Test', age: 30, created_at: DateTime.now)<commit_msg>Adds TTL usage to README<commit_after># Cassandro [![Gem Version](https://badge.fury.io/rb/cassandro.svg)](http://badge.fury.io/rb/cassandro)

Cassandro is a small Ruby ORM for Apache Cassandra 2.0 and CQL 3.0. Cassandro uses the new Datastax Ruby Driver (official driver).

## Install
 
`gem install cassandro`

## Basic Cassandro

Connecting to Cassandra DB: `Cassandro.connect(Hash options)`. For full list of options visit [Ruby Driver Documentation](http://datastax.github.io/ruby-driver/api/#cluster-class_method)

```ruby
Cassandro.connect(
                  hosts: ['127.0.0.1'],
                  keyspace: 'some_keyspace'
                 )
```

Creating a new keyspace. For full details of keyspace creation visit [CLI keyspace](http://www.datastax.com/documentation/cassandra/2.0/cassandra/reference/referenceStorage_r.html)

```ruby
# < 1.0.0
Cassandro.create_keyspace('new_keyspace', 'SimpleStrategy', 1)

# >= 1.0.0, allow more options
Cassandro.create_keyspace('new_keyspace', {
  replication: {
    class: 'NetworkTopologyStrategy',
    dc_1: 2,
    dc_2: 2
  },
  durable_writes: true
})
```

Select keyspace outside `#connect`

```ruby
Cassandro.use('keyspace_name')
```

Create table.
```ruby
table = <<-TABLEDEF                                                              
  CREATE TABLE IF NOT EXISTS users (                                              
    email VARCHAR,                                                                       
    first_name VARCHAR,                                                                 
    age INT,
    created_at TIMESTAMP,                                                          
    PRIMARY KEY(email,created_at)                                                          
  )                                                                                
TABLEDEF

Cassandro.execute(table)
```

Execute queries.
```ruby
result = Cassandro.execute("SELECT * FROM table_name;")
```

Using Driver directly.
```ruby
statement = Cassandro.client.prepare("SELECT * FROM table_name WHERE colname = ?;")
result = Cassandro.client.execute(statement, id)
```

## Cassandro::Model

### Creating model
Creating new model: make you class inherits form `Cassandro::Model`

```ruby
class User < Cassandro::Model
end
```

Specifying table name using the method `table(table_name)`:

```ruby
class User < Cassandro::Model

  table 'users'
end
```

Adding attributes using the method `attribute(name, type, options)`:

```ruby
class User < Cassandro::Model

  attribute :email, :text
  attribute :first_name, :text
  attribute :age, :integer
  attribute :created_at, :datetime
end
```

types: :uuid, :text, :integer, :float, :timestamp, :datetime

Setting the primary key using the method `primary_key(pk_name | Array)`:

```ruby
class User < Cassandro::Model

  primary_key [:email, :created_at]

end

```

Setting unique field using the method `unique(field | Array)`:

```ruby
class  User < Cassandro::Model

  unique :email
end
```

Setting TTL to a model:

```Ruby
class Person < Cassandro::Model
  table :people
  ttl 60
end
```
This will make all the instances of the People class will have a Time To Live of `60` seconds in the database.

Creating a single record with a given TTL:

```Ruby
class Person < Cassandro::Model
  table :people
  attribute :first_name, :text
  attribute :last_name, :text
end

Person.create_with_ttl(20, :first_name => "Eddie", :last_name => "Vedder")
```

This will create a record in the `people` table with a TTL of `20` seconds. It doesn't matter if the model has a different TTL set, this will override that TTL for _this record only_


__A complete example__

```ruby
class User < Cassandro::Model

  table 'users'

  attribute :email, :text
  attribute :first_name, :text
  attribute :age, :integer
  attribute :created_at, :datetime
  
  primary_key [:email, :created_at]

  unique :email
end
```

### Interacting

Creating a new row:

```ruby
user = User.create(email: '<EMAIL>', first_name: 'Test', age: 30, created_at: DateTime.now)
{'fraction_non_alphanumeric': 0.11199460916442049, 'fraction_numerical': 0.007816711590296496, 'mean_word_length': 2.5270912547528517, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '847166', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2092, 'n_words': 642}