Ruby Adventures with ArcadeDB
Tags: ruby, nosql, orm, arcadedb
Published: 2023-10-25 14:39:01

[ArcadeDB](https://arcadedb.com) is a modern, multi purpose Not-Only-SQL database. The [ArcadeDB-Gem](https://github.com/topofocus/arcadedb) opens the database for ruby-projects. 

The database is organized with `types`. A database-type is either a `document`, a `vertex` or an `edge`.

Similar to other database-adapters in ruby, `types` are declared in the model directory of the project. 

A typical `Account`-model is similar to a rudimentary `ActiveRecord`-based Model-setup. 
```ruby
module Arcade
  class  Account  < Arcade::Vertex
    attribute :name, Types::Nominal::String
    attribute :mail, Types::Nominal::String
   
    def self.db_init
      File.read(__FILE__).gsub(/.*__END__/m, '')
    end
  end
end
__END__
CREATE PROPERTY account.name STRING
CREATE INDEX  ON account (name) UNIQUE
## /model/arcade/account.rb
```
* Account is a `Vertex`
* Two attributes are predefined and type-checked via DRY::Types. 
* One Index is defined. Its needed for filtering.
* The code at the bottom of the model-file creates the corresponding database-properties through -> `Account.create_type`.


## First steps
It is assumed, that an [arcadedb-server](https://docs.arcadedb.com/#_run-arcadedb) is setup and running.
Initialize a ruby project
```
mkdir test-project && cd test-project
mkdir bin
mkdir model; mkdir model/arcade
bundle init
bundle add arcadedb
```

**Clone [dev-adventures](https://github.com/topofocus/dev-adventures) and modify the provides framework to your needs.** Modify the provided arcade.yml!

Open the console `cd bin && ./console` and create the Account-Vertex
```ruby
3.2.0 :002 > Account
 => Arcade::Account 
3.2.0 :003 > Account.create_type
25.10.(15:51:50) INFO->Q: create vertex type account    
25.10.(15:51:50) INFO->Q: CREATE PROPERTY account.name STRING
25.10.(15:51:50) INFO->Q: CREATE INDEX  ON account (name) UNIQ
3.2.0 :004 > Account.insert name: 'hugo', mail: 'hugo@test.com', age: 37, home: '0098 565 4433' 
 => <account[#1:0]:{0->}{->0}, age: 37, home: 0098 565 4433, mail: hugo@test.com, name: hugo> 
```
Congratulations! You just created the first record. Only essential attributes are predefined. Other attributes are added as needed. 

## Basic commands

At that point, any [database query](https://docs.arcadedb.com/#SQL) can be submitted via `DB.query "query"` or `DB.execute { "query" }`, eg.
```ruby
 DB.query 'select from account'
 =>                                                
[{:@rid=>"#1:0",                                  
  :@type=>"account",                               
  :@cat=>"v",                                      
  :mail=>"hugo@test.com",                          
  :name=>"hugo",                                   
  :age=>37,                                        
  :home=>"0098 565 4433"}]  
```
Most common tasks are present via ruby, eg.
```ruby
a =  Account.find name: 'hugo' # find one record
a.update name: 'danzig'        # does not modify a
a = a.refresh                  # mutate a
=> <account[#1:0]:{0->}{->0}, age: 37, home: 0098 565 4433, mail: hugo@test.com, name: danzig> 
```


This is the first part of an introduction series on ArcadeDB.

Next Part: [Ruby Adventures with ArcadeDB 2](https://dev.to/topofocus/ruby-adventures-with-arcadedb-2-534k)