Javascript

OrientDB supports server-side scripting. All the JVM languages are supported. By default JavaScript is installed.

Scripts can be executed on the client and on the server-side. On the client-side, the user must have READ privilege against the database.command resource. On the server-side, the scripting interpreter must be enabled. It is disabled by default for security reasons.

In order to return the result of a variable, put the variable name as last statement. Example:

  1. var r = db.query('select from ouser');
  2. print(r);
  3. r

Will return the resultset.

See also

Usage

Via Java API

Executes a command like SQL but uses the class OCommandScript passing in the language to use. JavaScript is installed by default. Example:

  1. db.command( new OCommandScript("Javascript", "print('hello world')") ).execute();

Via console

JavaScript code can be executed on the client-side, the console, or server-side:

  • Use js to execute the script on the client-side running it in the console
  • use jss to execute the script on the server-side. This feature is disabled by default. To enable it look at Enable Server side scripting.

Since the semi-colon ; character is used in both console and JavaScript languages to separate statements, how can we execute multiple commands on the console and with JavaScript?

The OrientDB console uses a reserved keyword end to switch from JavaScript mode to console mode.

Example:

  1. orientdb> connect remote:localhost/demo admin admin; js for( i = 0; i < 10; i++ ){ db.query('select from MapPoint') };end; exit

This line connects to the remote server and executes 10 queries on the console. The end command switches the mode back to the OrientDB console and then executes the console exit command.

Below is an example to display the results of a query on the server and on the client.

  1. connects to the remote server as admin
  2. executes a query and assigns the result to the variable r, then displays it server-side and returns it to be displayed on the client side too
  3. exits the console

Interactive mode

  1. $ ./console.sh
  2. OrientDB console v.1.5 www.orientechnologies.com
  3. Type 'help' to display all the commands supported.
  4. orientdb> connect remote:localhost/demo admin admin
  5. Connecting to database [remote:localhost/demo] with user 'admin'...OK
  6. orientdb> jss var r = db.query('select from ouser');print(r);r
  7. ---+---------+--------------------+--------------------+--------------------+--------------------
  8. #| RID |name |password |status |roles
  9. ---+---------+--------------------+--------------------+--------------------+--------------------
  10. 0| #4:0|admin |{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918|ACTIVE |[1]
  11. 1| #4:1|reader |{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30|ACTIVE |[1]
  12. 2| #4:2|writer |{SHA-256}B93006774CBDD4B299389A03AC3D88C3A76B460D538795BC12718011A909FBA5|ACTIVE |[1]
  13. ---+---------+--------------------+--------------------+--------------------+--------------------
  14. Script executed in 0,073000 sec(s). Returned 3 records
  15. orientdb> exit

Batch mode

The same example above is executed in batch mode:

  1. $ ./console.sh "connect remote:localhost/demo admin admin;jss var r = db.query('select from ouser');print(r);r;exit"
  2. OrientDB console v.1.0-SNAPSHOT (build 11761) www.orientechnologies.com
  3. Type 'help' to display all the commands supported.
  4. Connecting to database [remote:localhost/demo] with user 'admin'...OK
  5. ---+---------+--------------------+--------------------+--------------------+--------------------
  6. #| RID |name |password |status |roles
  7. ---+---------+--------------------+--------------------+--------------------+--------------------
  8. 0| #4:0|admin |{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918|ACTIVE |[1]
  9. 1| #4:1|reader |{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30|ACTIVE |[1]
  10. 2| #4:2|writer |{SHA-256}B93006774CBDD4B299389A03AC3D88C3A76B460D538795BC12718011A909FBA5|ACTIVE |[1]
  11. ---+---------+--------------------+--------------------+--------------------+--------------------
  12. Script executed in 0,099000 sec(s). Returned 3 records

Examples of usage

Insert 1000 records

  1. orientdb> js for( i = 0; i < 1000; i++ ){ db.query( 'insert into jstest (label) values ("test'+i+'")' ); }

Create documents using wrapped Java API

  1. orientdb> js new com.orientechnologies.orient.core.record.impl.ODocument('Profile').field('name', 'Luca').save()
  2. Client side script executed in 0,426000 sec(s). Value returned is: Profile#11:52{name:Luca} v3

Enable Server-side scripting

For security reasons server-side scripting is disabled by default on the server. To enable it change the enable field to true in the orientdb-server-config.xml file:

  1. <!-- SERVER SIDE SCRIPT INTERPRETER. WARNING! THIS CAN BE A SECURITY HOLE: ENABLE IT ONLY IF CLIENTS ARE TRUSTED, TO TURN ON SET THE 'ENABLED' PARAMETER TO 'true' -->
  2. <handler class="com.orientechnologies.orient.server.handler.OServerSideScriptInterpreter">
  3. <parameters>
  4. <parameter name="enabled" value="true" />
  5. </parameters>
  6. </handler>

NOTE: this will allow clients to execute any code inside the server. Enable it only if clients are trusted.