Javascript API

This driver wraps the most common use cases in database usage. All parameters required by methods or constructor are Strings. This library works on top of HTTP RESTful protocol.

Note: Due to cross-domain XMLHttpRequest restriction this API works, for now, only placed in the server deployment. To use it with cross-site look at Cross-site scripting.

The full source code is available here: oriendb-api.js.

See also

Example

  1. var database = new ODatabase('http://localhost:2480/demo');
  2. databaseInfo = database.open();
  3. queryResult = database.query('select from Address where city.country.name = \'Italy\'');
  4. if (queryResult["result"].length == 0){
  5. commandResult = database.executeCommand('insert into Address (street,type) values (\'Via test 1\',\'Tipo test\')');
  6. } else {
  7. commandResult = database.executeCommand('update Address set street = \'Via test 1\' where city.country.name = \'Italy\'');
  8. }
  9. database.close();

API

ODatabase object

ODatabase object requires server URL and database name:

Syntax: new ODatabase(http://:/)

Example:

  1. var orientServer = new ODatabase('http://localhost:2480/demo');

Once created database instance is ready to be used. Every method return the operation result when it succeeded, null elsewhere.
In case of null result the database instance will have the error message obtainable by the getErrorMessage() method.

Open

Method that connects to the server, it returns database information in JSON format.

Browser Authentication

Syntax: <databaseInstance>.open()
Note: This implementation asks to the browser to provide user and password.

Example:

  1. orientServer = new ODatabase('http://localhost:2480/demo');
  2. databaseInfo = orientServer.open();

Javascript Authentication

Syntax: <databaseInstance>.open(username,userpassword)

Example:

  1. orientServer = new ODatabase('http://localhost:2480/demo');
  2. databaseInfo = orientServer.open('admin','admin');

Return Example:

  1. {"classes": [
  2. {
  3. "id": 0,
  4. "name": "ORole",
  5. "clusters": [3],
  6. "defaultCluster": 3, "records": 3,
  7. "properties": [
  8. {
  9. "id": 0,
  10. "name": "mode",
  11. "type": "BYTE",
  12. "mandatory": false,
  13. "notNull": false,
  14. "min": null,
  15. "max": null,
  16. "indexed": false
  17. },
  18. {
  19. "id": 1,
  20. "name": "rules",
  21. "linkedType": "BYTE",
  22. "type": "EMBEDDEDMAP",
  23. "mandatory": false,
  24. "notNull": false,
  25. "min": null,
  26. "max": null,
  27. "indexed": false
  28. }
  29. ]},
  30. ],
  31. "dataSegments": [
  32. {"id": -1, "name": "default", "size": 10485760, "filled": 1380391, "maxSize": "0", "files": "[${STORAGE_PATH}/default.0.oda]"}
  33. ],
  34. "clusters": [
  35. {"id": 0, "name": "internal", "type": "PHYSICAL", "records": 4, "size": 1048576, "filled": 60, "maxSize": "0", "files": "[${STORAGE_PATH}/internal.0.ocl]"},
  36. ],
  37. "txSegment": [
  38. {"totalLogs": 0, "size": 1000000, "filled": 0, "maxSize": "50mb", "file": "${STORAGE_PATH}/txlog.otx"}
  39. ], "users": [
  40. {"name": "admin", "roles": "[admin]"},
  41. {"name": "reader", "roles": "[reader]"},
  42. {"name": "writer", "roles": "[writer]"}
  43. ],
  44. "roles": [
  45. {"name": "admin", "mode": "ALLOW_ALL_BUT",
  46. "rules": []
  47. },
  48. {"name": "reader", "mode": "DENY_ALL_BUT",
  49. "rules": [{
  50. "name": "database", "create": false, "read": true, "update": false, "delete": false
  51. }, {
  52. "name": "database.cluster.internal", "create": false, "read": true, "update": false, "delete": false
  53. }, {
  54. "name": "database.cluster.orole", "create": false, "read": true, "update": false, "delete": false
  55. }, {
  56. "name": "database.cluster.ouser", "create": false, "read": true, "update": false, "delete": false
  57. }, {
  58. "name": "database.class.*", "create": false, "read": true, "update": false, "delete": false
  59. }, {
  60. "name": "database.cluster.*", "create": false, "read": true, "update": false, "delete": false
  61. }, {
  62. "name": "database.query", "create": false, "read": true, "update": false, "delete": false
  63. }, {
  64. "name": "database.command", "create": false, "read": true, "update": false, "delete": false
  65. }, {
  66. "name": "database.hook.record", "create": false, "read": true, "update": false, "delete": false
  67. }]
  68. },
  69. ],
  70. "config":{
  71. "values": [
  72. {"name": "dateFormat", "value": "yyyy-MM-dd"},
  73. {"name": "dateTimeFormat", "value": "yyyy-MM-dd hh:mm:ss"},
  74. {"name": "localeCountry", "value": ""},
  75. {"name": "localeLanguage", "value": "en"},
  76. {"name": "definitionVersion", "value": 0}
  77. ],
  78. "properties": [
  79. ]
  80. }
  81. }

Query

Method that executes the query, it returns query results in JSON format.

Syntax: <databaseInstance>.query(<queryText>, [limit], [fetchPlan])

Limit and fetchPlan are optional.

Simple Example:

  1. queryResult = orientServer.query('select from Address where city.country.name = \'Italy\'');

Return Example:

  1. { "result": [{
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "type": "Residence",
  5. "city": "#13:0"
  6. }, {
  7. "@rid": "12:1", "@class": "Address",
  8. "street": "Piazza di Spagna, 111",
  9. "type": "Residence",
  10. "city": "#13:0"
  11. }
  12. ]
  13. }

Fetched Example: fetching of all fields except “type”

  1. queryResult = orientServer.query('select from Address where city.country.name = \'Italy\'',null,'*:-1 type:0');

Return Example 1:

  1. { "result": [{
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "city":{
  5. "@rid": "13:0", "@class": "City",
  6. "name": "Rome",
  7. "country":{
  8. "@rid": "14:0", "@class": "Country",
  9. "name": "Italy"
  10. }
  11. }
  12. }, {
  13. "@rid": "12:1", "@version": 1, "@class": "Address",
  14. "street": "Piazza di Spagna, 111",
  15. "city":{
  16. "@rid": "13:0", "@class": "City",
  17. "name": "Rome",
  18. "country":{
  19. "@rid": "14:0", "@class": "Country",
  20. "name": "Italy"
  21. }
  22. }
  23. }
  24. ]
  25. }

Fetched Example: fetching of all fields except “city” (Class)

  1. queryResult = orientServer.query('select from Address where city.country.name = \'Italy\'',null,'*:-1 city:0');

Return Example 2:

  1. { "result": [{
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "type": "Residence"
  5. }, {
  6. "@rid": "12:1", "@version": 1, "@class": "Address",
  7. "street": "Piazza di Spagna, 111",
  8. "type": "Residence"
  9. }
  10. ]
  11. }

Fetched Example: fetching of all fields except “country” of City class

  1. queryResult = orientServer.query('select from Address where city.country.name = \'Italy\'',null,'*:-1 City.country:0');

Return Example 3:

  1. { "result": [{
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "type": "Residence",
  5. "city":{
  6. "@rid": "13:0", "@class": "City",
  7. "name": "Rome"
  8. }
  9. }
  10. ]
  11. }

Execute Command

Method that executes arbitrary commands, it returns command result in text format.

Syntax: <databaseInstance>.executeCommand(<commandText>)

Example 1 (insert):

  1. commandResult = orientServer.executeCommand('insert into Address (street,type) values (\'Via test 1\',\'Tipo test\')');

Return Example 1 (created record):

  1. Address@14:177{street:Via test 1,type:Tipo test}


Example 2 (delete):

  1. commandResult = orientServer.executeCommand('delete from Address where street = \'Via test 1\' and type = \'Tipo test\'');

Return Example 2 (records deleted):

  1. { "value" : 5 }

Note: Delete example works also with update command

Load

Method that loads a record from the record ID, it returns the record informations in JSON format.

Syntax: `.load(, [fetchPlan]);

Simple Example:

  1. queryResult = orientServer.load('12:0');

Return Example:

  1. {
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "type": "Residence",
  5. "city": "#13:0"
  6. }

Fetched Example: all fields fetched except “type”

  1. queryResult = orientServer.load('12:0', '*:-1 type:0');

Return Example 1:

  1. {
  2. "@rid": "12:0", "@class": "Address",
  3. "street": "Piazza Navona, 1",
  4. "city":{
  5. "@rid": "13:0",
  6. "name": "Rome",
  7. "country":{
  8. "@rid": "14:0",
  9. "name": "Italy"
  10. }
  11. }
  12. }

Class Info

Method that retrieves information of a class, it returns the class informations in JSON format.

Syntax: <databaseInstance>.classInfo(<className>)

Example:

  1. addressInfo = orientServer.classInfo('Address');

Return Example:

  1. { "result": [{
  2. "@rid": "14:0", "@class": "Address",
  3. "street": "WA 98073-9717",
  4. "type": "Headquarter",
  5. "city": "#12:1"
  6. }, {
  7. "@rid": "14:1", "@class": "Address",
  8. "street": "WA 98073-9717",
  9. "type": "Headquarter",
  10. "city": "#12:1"
  11. }
  12. ]
  13. }

Browse Cluster

Method that retrieves information of a cluster, it returns the class informations in JSON format.

Syntax: <databaseInstance>.browseCluster(<className>)

Example:

  1. addressInfo = orientServer.browseCluster('Address');

Return Example:

  1. { "result": [{
  2. "@rid": "14:0", "@class": "Address",
  3. "street": "WA 98073-9717",
  4. "type": "Headquarter",
  5. "city": "#12:1"
  6. }, {
  7. "@rid": "14:1", "@class": "Address",
  8. "street": "WA 98073-9717",
  9. "type": "Headquarter",
  10. "city": "#12:1"
  11. }
  12. ]
  13. }

Server Information

Method that retrieves server informations, it returns the server informations in JSON format.
Note: Server information needs root username and password.

Syntax: <databaseInstance>.serverInfo()

Example:

  1. serverInfo = orientServer.serverInfo();

Return Example:

  1. {
  2. "connections": [{
  3. "id": "64",
  4. "id": "64",
  5. "remoteAddress": "127.0.0.1:51459",
  6. "db": "-",
  7. "user": "-",
  8. "protocol": "HTTP-DB",
  9. "totalRequests": "1",
  10. "commandInfo": "Server status",
  11. "commandDetail": "-",
  12. "lastCommandOn": "2010-12-23 12:53:38",
  13. "lastCommandInfo": "-",
  14. "lastCommandDetail": "-",
  15. "lastExecutionTime": "0",
  16. "totalWorkingTime": "0",
  17. "connectedOn": "2010-12-23 12:53:38"
  18. }],
  19. "dbs": [{
  20. "db": "demo",
  21. "user": "admin",
  22. "open": "open",
  23. "storage": "OStorageLocal"
  24. }],
  25. "storages": [{
  26. "name": "temp",
  27. "type": "OStorageMemory",
  28. "path": "",
  29. "activeUsers": "0"
  30. }, {
  31. "name": "demo",
  32. "type": "OStorageLocal",
  33. "path": "/home/molino/Projects/Orient/releases/0.9.25-SNAPSHOT/db/databases/demo",
  34. "activeUsers": "1"
  35. }],
  36. "properties": [
  37. {"name": "server.cache.staticResources", "value": "false"
  38. },
  39. {"name": "log.console.level", "value": "info"
  40. },
  41. {"name": "log.file.level", "value": "fine"
  42. }
  43. ]
  44. }

Schema

Method that retrieves database Schema, it returns an array of classes (JSON parsed Object).

Syntax: <databaseInstance>.schema()

Example:

  1. schemaInfo = orientServer.schema();

Return Example:

  1. {"classes": [
  2. {
  3. "id": 0,
  4. "name": "ORole",
  5. "clusters": [3],
  6. "defaultCluster": 3, "records": 3,
  7. "properties": [
  8. {
  9. "id": 0,
  10. "name": "mode",
  11. "type": "BYTE",
  12. "mandatory": false,
  13. "notNull": false,
  14. "min": null,
  15. "max": null,
  16. "indexed": false
  17. },
  18. {
  19. "id": 1,
  20. "name": "rules",
  21. "linkedType": "BYTE",
  22. "type": "EMBEDDEDMAP",
  23. "mandatory": false,
  24. "notNull": false,
  25. "min": null,
  26. "max": null,
  27. "indexed": false
  28. }
  29. ]},
  30. ]
  31. }

getClass()

Return a schema class from the schema.

Syntax: <databaseInstance>.getClass(<className>)

Example:

  1. var customerClass = orientServer.getClass('Customer');

Return Example:

  1. {
  2. "id": 0,
  3. "name": "Customer",
  4. "clusters": [3],
  5. "defaultCluster": 3, "records": 3,
  6. "properties": [
  7. {
  8. "id": 0,
  9. "name": "name",
  10. "type": "STRING",
  11. },
  12. {
  13. "id": 1,
  14. "name": "surname",
  15. "type": "STRING",
  16. }
  17. ]
  18. }

Security

Roles

Method that retrieves database Security Roles, it returns an array of Roles (JSON parsed Object).

Syntax: <databaseInstance>.securityRoles()

Example:

  1. roles = orientServer.securityRoles();

Return Example:

  1. { "roles": [
  2. {"name": "admin", "mode": "ALLOW_ALL_BUT",
  3. "rules": []
  4. },
  5. {"name": "reader", "mode": "DENY_ALL_BUT",
  6. "rules": [{
  7. "name": "database", "create": false, "read": true, "update": false, "delete": false
  8. }, {
  9. "name": "database.cluster.internal", "create": false, "read": true, "update": false, "delete": false
  10. }, {
  11. "name": "database.cluster.orole", "create": false, "read": true, "update": false, "delete": false
  12. }, {
  13. "name": "database.cluster.ouser", "create": false, "read": true, "update": false, "delete": false
  14. }, {
  15. "name": "database.class.*", "create": false, "read": true, "update": false, "delete": false
  16. }, {
  17. "name": "database.cluster.*", "create": false, "read": true, "update": false, "delete": false
  18. }, {
  19. "name": "database.query", "create": false, "read": true, "update": false, "delete": false
  20. }, {
  21. "name": "database.command", "create": false, "read": true, "update": false, "delete": false
  22. }, {
  23. "name": "database.hook.record", "create": false, "read": true, "update": false, "delete": false
  24. }]
  25. }
  26. ]
  27. }

Users

Method that retrieves database Security Users, it returns an array of Users (JSON parsed Object).

Syntax: <databaseInstance>.securityUsers()

Example:

  1. users = orientServer.securityUsers();

Return Example:

  1. { "users": [
  2. {"name": "admin", "roles": "[admin]"},
  3. {"name": "reader", "roles": "[reader]"},
  4. {"name": "writer", "roles": "[writer]"}
  5. ]
  6. }

close()

Method that disconnects from the server.

Syntax: <databaseInstance>.close()

Example:

  1. orientServer.close();

Change server URL

Method that changes server URL in the database instance.
You’ll need to call the open method to reconnect to the new server.

Syntax: <databaseInstance>.setDatabaseUrl(<newDatabaseUrl>)

Example:

  1. orientServer.setDatabaseUrl('http://localhost:3040')

Change database name

Method that changes database name in the database instance.
You’ll need to call the open method to reconnect to the new database.

Syntax: <databaseInstance>.setDatabaseName(<newDatabaseName>)

Example:

  1. orientServer.setDatabaseName('demo2');

Setting return type

This API allows you to chose the return type, Javascript Object or JSON plain text. Default return is Javascript Object.

Important: the javascript object is not always the evaluation of JSON plain text: for each document (identified by its Record ID) the JSON file contains only one expanded object, all other references are just its Record ID as String, so the API will reconstruct the real structure by re-linking all references to the matching javascript object.

Syntax: orientServer.setEvalResponse(<boolean>)

Examples:

  1. orientServer.setEvalResponse(true);

Return types will be Javascript Objects.

  1. orientServer.setEvalResponse(false);

Return types will be JSON plain text.

Cross-site scripting

To invoke OrientDB cross-site you can use the query command in GET and the JSONP protocol. Example:

  1. <script type="text/javascript" src='http://127.0.0.1:2480/query/database/sql/select+from+XXXX?jsoncallback=var datajson='></script>

This will put the result of the query select from XXXX</code> into the <code>datajson variable.

Errors

In case of errors the error message will be stored inside the database instance, retrievable by getErrorMessage() method.

Syntax: <databaseInstance>.getErrorMessage()

Example:

  1. if (orientServer.getErrorMessage() != null){
  2. //write error message
  3. }