Skip to content
veny edited this page Jul 19, 2012 · 14 revisions

Home

Create Class

client = Orientdb4r.client
client.connect :database => 'temp', :user => 'admin', :password => 'admin'

# create a class
client.create_class 'MyClass'
# or with super-class and cluster
client.create_class 'MyClass', :extends => 'OUser', :cluster => 4
# or force to drop an already existing class before creating
client.create_class 'MyClass', :force => true

# or with properties
client.create_class 'MyClass' do |c|
  c.property 'prop1', :integer, :notnull => true, :min => 1, :max => 99
  c.property 'prop2', :string, :mandatory => true
  c.link     'user',  :linkset, 'OUser'
end

# or in one step
client.create_class('MyClass', :properties => [
  { :property => 'prop1', :type => :integer },
  { :property => 'prop2', :type => :string, :mandatory => true, :notnull => true, :min => 1, :max => 99 },
  { :property => 'user',  :type => :linkset, :linked_class => 'OUser' }
])

Get Class

clazz = client.get_class 'OUser'
 => {"name"=>"OUser", "superClass"=>"", "alias"=>nil, "clusters"=>[4], "defaultCluster"=>4, "records"=>3, "properties"=>[{"name"=>"roles", "linkedClass"=>"ORole", "type"=>"LINKSET", "mandatory"=>false, "notNull"=>false, "min"=>nil, "max"=>nil}, {"name"=>"name", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}, {"name"=>"password", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}]}

clazz.name
 => "OUser"
clazz.properties
 => [{"name"=>"roles", "linkedClass"=>"ORole", "type"=>"LINKSET", "mandatory"=>false, "notNull"=>false, "min"=>nil, "max"=>nil}, {"name"=>"name", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}, {"name"=>"password", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}]
clazz.property :password
 => {"name"=>"password", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}
clazz.super_class
 => "" 
clazz.clusters
 => [4]
clazz.default_cluster
 => 4

prop = clazz.property :password
 => {"name"=>"password", "type"=>"STRING", "mandatory"=>true, "notNull"=>true, "min"=>nil, "max"=>nil}

prop.name
 => "password" 
prop.type
 => "STRING" 
prop.mandatory
 => true 
prop.not_null
 => true 
prop.min
 => nil 
prop.max
 => nil


# or try to get an unknown class
client.get_class 'UnknownClass'
 => Orientdb4r::NotFoundError: class not found, name=UnknownClass
	from /home/veny/projects/orientdb4r/lib/orientdb4r/rest/client.rb:92:in `get_class'
	from ...

Drop Class

client.drop_class 'MyClass'

# :mode=>:strict forbids to drop a class that is a super class for other one
client.create_class 'MyClass', :extends => 'OUser'
client.drop_class 'OUser', :mode=>:strict
 => Orientdb4r::OrientdbError: class is super-class, cannot be deleted, name=OUser
	from ./orientdb4r/lib/orientdb4r/client.rb:93:in `drop_class'
	from ...

Clone this wiki locally