module Orchestrate::Rails::UserGuide::TheSchema

Properties and attributes ↑

Orchestrate.io collections consist of sets of key/value pairs, each referenced by a primary_key and stored in a json document. Property names are the original key names in json documents stored in orchestrate collections - before they are converted to snake_case, rails-style attribute names.

The id attribute ↑

orchestrate-rails models use the id attribute as the primary_key when accessing orchestrate.io collections. The rails application is responsible for initializing the value of the id attribute when adding new data to a collection.

The schema ↑

Since property names cannot be expected to conform to orchestrate-rails convention, a schema is used to ensure that they are correctly mapped to attribute names, and vice-versa.

Here is an example schema definition file, for the films collection from the orchestrate.io tutorial:

Orchestrate::Rails::Schema.define_collection(
  :name        => 'films',
  :classname   => 'Film',
  :properties  => [ :Title, :Year, :Rated, :Released,
                    :Runtime, :Genre, :Director, :Writer,
                    :Actors, :Plot, :Poster, :imdbRating,
                    :imdbVotes, :imdbID, :Type, :Response ],
  :event_types => [ :comments ],
  :graphs      => [ :sequel ],
)

Orchestrate::Rails::Schema.define_event_type(
  :collection => 'films',
  :event_type => :comments,
  :properties => [ :User, :Comment ]
)

Orchestrate::Rails::Schema.define_graph(
  :collection    => 'films',
  :relation_kind => :sequel,
  :to_collection => 'films',
)

orchestrate-rails generates these attribute names from the above schema:

:Title      => 'title'
:Year       => 'year'
:Rated      => 'rated'
:Released   => 'released'
:Runtime    => 'runtime'
:Genre      => 'genre'
:Director   => 'director'
:Writer     => 'writer'
:Actors     => 'actors'
:Plot       => 'plot'
:Poster     => 'poster'
:imdbRating => 'imdb_rating'
:imdbVotes  => 'imdb_votes'
:imdbID     => 'imdb_id'
:Type       => 'type'
:Response   => 'response'

:User       => 'user'
:Comment    => 'comment'

An application may define a new schema at any time before the corresponding model is instantiated.

Schema definition during Rails initialization ↑

This section describes how the schema definition file can be loaded during rails initialization. The schema definition file may reside anywhere that makes sense, for example:

<rails-root>/db/schema.rb

This schema can then be loaded during rails initialization by adding the following line to <rails-root>/config/application.rb:

Orchestrate::Rails::Schema.load "./db/schema.rb"

Next ↑

Defining the model