Active Orient – Introduction & Overview
Tags: ruby, orientdb, sql, graphdb
Published: 2019-07-24 10:46:17

How about a (fast) [ open source database](https://orientdb.com) usable without any schema, query-able with sql-statements and able to perform sophisticated graph operations? Not to forget: Easily accessible via an intuitive ruby interface. The latter is `ActiveOrient`.

## Prerequisites
* Install the database ( [Download the OpenSource Database](https://orientdb.com/download-2/)) and assign a Username
* Make sure that ruby 2.6  is installed (via snap or [rvm](https::/rvm.io)). Check if `bundle` is installed (otherwise `gem install bundle`)
* Clone the [ActiveOrient git archive]( https://github.com/topofocus/active-orient.git )  and bundle ruby libraries: 
```
git clone https://github.com/topofocus/active-orient.git
cd active-orient
bundle install; bundle update
```
Now modify `config/connect.yaml`.
Test, development and production databases are created if they don't exist.

Then change to `/bin` and run
```
./active-orient-console -t
```

## Simple Start
Any database-class is recognized and allocated. Further, any class created in the process is usable immediately. The behavior of any Class can be customized in `model`-files, which are included upon the creation of a class. 

The base-classes `V`)ertex and `E`)dge are always present. Its good practice, to inherent any user-class from the base-classes. 


```ruby
 # create a class
    V.create_class :m   # V is the base »vertex» class. M is the vertex-class created The corresponding database-class: »m«.
 # INFO->CREATE CLASS m EXTENDS 

 # create a record
    M.create name: 'Hugo', age: 46, interests: [ 'swimming', 'biking', 'reading' ]
 # INFO->CREATE VERTEX m CONTENT {"name":"Hugo","age":46,"interests":["swimming","biking","reading"]}
    # query the database
    hugo = M.where( name: 'Hugo' ).first
    hugo.to_human
    =>"<M[177:0]: age: 46, interests: [\"swimming\", \"biking\", \"reading\"], name : Hugo>" 
    # update the dataset
    hugo.update father: M.create( name: "Volker", age: 76 )  # we create an internal link
# INFO->CREATE VERTEX m CONTENT {"name":"Volker","age":76}
# INFO->update #177:0 set father = #178:0
    hugo.to_human
    => "<M[177:0]: age : 46, father : <M[178:0]: age : 76, name : Volker>, interests : [\"swimming\", \"biking\", \"reading\"], name : Hugo>"
    hugo.father.name	
    => volker
    # change array elements
    hugo.interests << "dancing"  # --> [ 'swimming', 'biking', 'reading', 'dancing' ]
#INFO->update #177:0  set interests = interests || ['dancing'] 
 
    M.remove hugo 
    M.delete_class	# removes the class from OrientDB and deletes the ruby-object-definition
```

In this brief introduction, we created a Vertex-Class »M«. Then we created a document (record) with some properties. We queried the database using a syntax similar to `ActiveRecord`. 
Then we created a self-refrencing link which we  followed to access  connected information. 

At last  – using the syntax of ruby-arrays – we added an entry to an embedded list. Anything without any configuration in schema-, model-files or whatsoever. 
  
This ends Part 1 of the ActiveOrient Mini-Series. 
The [next part](https://dev.to/topofocus/activeorient-joins-and-links-j2d) covers unidirectional links and joins, an introduction to sql-queries and their abstraction with Orientquery