Classes

Multi-model support in the OrientDB engine provides a number of ways in approaching and understanding its basic concepts. These concepts are clearest when viewed from the perspective of the Document Database API. Like many database management systems, OrientDB uses the Record as an element of storage. There are many types of records, but with the Document Database API, records always use the Document type. Documents are formed by a set of key/value pairs, referred to as fields and properties, and can belong to a class.

The Class is a concept drawn from the Object-oriented programming paradigm. It is a type of data model that allows you to define certain rules for records that belong to it. In the traditional Document database model, it is comparable to the collection, while in the Relational database model it is comparable to the table.

For more information on classes in general, see Wikipedia.

To list all the configured classes on your system, use the CLASSES command in the console:

  1. orientdb> CLASSES
  2.  
  3. CLASSES:
  4. -------------------+------------+----------+-----------+
  5. NAME | SUPERCLASS |CLUSTERS | RECORDS |
  6. -------------------+------------+----------+-----------+
  7. AbstractPerson | | -1 | 0 |
  8. Account | | 11 | 1126 |
  9. Actor | | 91 | 3 |
  10. Address | | 19 | 166 |
  11. Animal | | 17 | 0 |
  12. .... | .... | .... | .... |
  13. Whiz | | 14 | 1001 |
  14. -------------------+------------+----------+-----------+
  15. TOTAL 22775 |
  16. -------------------------------------------------------+

Working with Classes

In order to start using classes with your own applications, you need to understand how to create and configure them for use. As a concept, the class in OrientDB has the closest relationship with the table in relational databases, but (unlike tables) classes can be schema-less, schema-full or mixed. Classes can inherit from other classes, creating trees of classes. Each class has its own cluster or clusters, (created by default, if none are defined).

For more information on classes in OrientDB, see Class.

To create a new class, use the CREATE CLASS command:

  1. orientdb> CREATE CLASS Student
  2.  
  3. Class created successfully. Total classes in database now: 92

This creates a class called Student. Given that no cluster was defined in the CREATE CLASS command, OrientDB creates a default cluster called student, to contain records assigned to this class. For the moment, the class has no records or properties tied to it. It is now displayed in the CLASSES listings.

Adding Properties to a Class

As mentioned above, OrientDB does allow you to work in a schema-less mode. That is, it allows you to create classes without defining their properties. However, in the event that you would like to define indexes or constraints for your class, properties are mandatory. Following the comparison to relational databases, if classes in OrientDB are similar to tables, properties are the columns on those tables.

To create new properties on Student, use the CREATE PROPERTY command in the console:

  1. orientdb> CREATE PROPERTY Student.name STRING
  2.  
  3. Property created successfully with id=1
  4.  
  5. orientdb> CREATE PROPERTY Student.surname STRING
  6.  
  7. Property created successfully with id=2
  8.  
  9. orientdb> CREATE PROPERTY Student.birthDate DATE
  10.  
  11. Property created successfully with id=3

These commands create three new properties on the Student class to provide you with areas to define the individual student’s name, surname and date of birth.

Displaying Class Information

On occasion, you may need to reference a particular class to see what clusters it belongs to and any properties configured for its use. Using the INFO CLASS command, you can display information on the current configuration and properties of a class.

To display information on the class Student, use the INFO CLASS command:

  1. orientdb> INFO CLASS Student
  2.  
  3. Class................: Student
  4. Default cluster......: student (id=96)
  5. Supported cluster ids: [96]
  6. Properties:
  7. -----------+--------+--------------+-----------+----------+----------+-----+-----+
  8. NAME | TYPE | LINKED TYPE/ | MANDATORY | READONLY | NOT NULL | MIN | MAX |
  9. | | CLASS | | | | | |
  10. -----------+--------+--------------+-----------+----------+----------+-----+-----+
  11. birthDate | DATE | null | false | false | false | | |
  12. name | STRING | null | false | false | false | | |
  13. surname | STRING | null | false | false | false | | |
  14. -----------+--------+--------------+-----------+----------+----------+-----+-----+

Adding Constraints to Properties

Constraints create limits on the data values assigned to properties. For instance, the type, the minimum or maximum size of, whether or not a value is mandatory or if null values are permitted to the property.

To add a constraint, use the ALTER PROPERTY command:

  1. orientdb> ALTER PROPERTY Student.name MIN 3
  2.  
  3. Property updated successfully

This command adds a constraint to Student on the name property. It sets it so that any value given to this class and property must have a minimum of three characters.

Viewing Records in a Class

Classes contain and define records in OrientDB. You can view all records that belong to a class using the BROWSE CLASS command and data belonging to a particular record with the DISPLAY RECORD command.

In the above examples, you created a Student class and defined the schema for records that belong to that class, but you did not create these records or add any data. As a result, running these commands on the Student class returns no results. Instead, for the examples below, consider the OUser class.

  1. orientdb> INFO CLASS OUser
  2.  
  3. CLASS 'OUser'
  4.  
  5. Super classes........: [OIdentity]
  6. Default cluster......: ouser (id=5)
  7. Supported cluster ids: [5]
  8. Cluster selection....: round-robin
  9. Oversize.............: 0.0
  10.  
  11. PROPERTIES
  12. ----------+---------+--------------+-----------+----------+----------+-----+-----+
  13. NAME | TYPE | LINKED TYPE/ | MANDATORY | READONLY | NOT NULL | MIN | MAX |
  14. | | CLASS | | | | | |
  15. ----------+---------+--------------+-----------+----------+----------+-----+-----+
  16. password | STRING | null | true | false | true | | |
  17. roles | LINKSET | ORole | false | false | false | | |
  18. name | STRING | null | true | false | true | | |
  19. status | STRING | null | true | false | true | | |
  20. ----------+---------+--------------+-----------+----------+----------+-----+-----+
  21.  
  22. INDEXES (1 altogether)
  23. -------------------------------+----------------+
  24. NAME | PROPERTIES |
  25. -------------------------------+----------------+
  26. OUser.name | name |
  27. -------------------------------+----------------+
  28.  

OrientDB ships with a number of default classes, which it uses in configuration and in managing data on your system, (the classes with the O prefix shown in the CLASSES command output). The OUser class defines the users on your database.

To see records assigned to the OUser class, run the BROWSE CLASS command:

  1. orientdb> BROWSE CLASS OUser
  2.  
  3. ---+------+-------+--------+-----------------------------------+--------+-------+
  4. # | @RID | @Class| name | password | status | roles |
  5. ---+------+-------+--------+-----------------------------------+--------+-------+
  6. 0 | #5:0 | OUser | admin | {SHA-256}8C6976E5B5410415BDE90... | ACTIVE | [1] |
  7. 1 | #5:1 | OUser | reader | {SHA-256}3D0941964AA3EBDCB00EF... | ACTIVE | [1] |
  8. 2 | #5:2 | OUser | writer | {SHA-256}B93006774CBDD4B299389... | ACTIVE | [1] |
  9. ---+------+-------+--------+-----------------------------------+--------+-------+
Classes - 图1 In the example, you are listing all of the users of the database. While this is fine for your initial setup and as an example, it is not particularly secure. To further improve security in production environments, see Security.

When you run BROWSE CLASS, the first column in the output provides the identifier number, which you can use to display detailed information on that particular record.

To show the first record browsed from the OUser class, run the DISPLAY RECORD command:

  1. orientdb> DISPLAY RECORD 0
  2.  
  3. ------------------------------------------------------------------------------+
  4. Document - @class: OUser @rid: #5:0 @version: 1 |
  5. ----------+-------------------------------------------------------------------+
  6. Name | Value |
  7. ----------+-------------------------------------------------------------------+
  8. name | admin |
  9. password | {SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873F8A81F6F2AB... |
  10. status | ACTIVE |
  11. roles | [#4:0=#4:0] |
  12. ----------+-------------------------------------------------------------------+

Bear in mind that this command references the last call of BROWSE CLASS. You can continue to display other records, but you cannot display records from another class until you browse that particular class.