Database Functions

In previous exmaples, such as factorial(), the function is relatively self-contained. It takes an argument from the query, operates on it, and returns the result. This is useful in cases where you need to perform some routine arithmetic operation or manipulate strings in a consistent way, but you can also use functions to perform more complex database operations. That is, the function can receive arguments, interact with the database, then return the results of that interaction.

Database Variable

When you create a function, OrientDB always binds itself to the orient variable. Using this variable you can call methods to access, query and operate on the database from the function. The specific method called to access the database depends on the type of database you're using:

Function Description
orient.getGraph() Retrieves the current Transactional Graph Database instance
orient.getGraphNoTx Retrieves the current Non-transactional Graph Database instance
orient.getDatabase() Retrieves the current Document Database instance

For instance, say you wanted a function to create the given user on the database. You might create one that takes three arguments: userName, passwd and roleName.

// Fetch Database
var db = orient.getDatabase();
var role = db.query("SELECT FROM ORole WHERE name = ?", roleName);

if (role == null){
    response.send(404, "Role name not found", "text/plain",
        "Error: Role name not found");
} else {
    try {
        var result ={"@class", name: userName, password: passwd,
            status: "ACTIVE", roles: role});
            return result;
    } catch(err){
        response.send(500, "Error creating new user", "text/plain",


As demonstrated in the example above, once you've retrieved the database interface, you can begin to call a series of additional methods to operate on the database from within the function.

Method Description
addEdge() Adds edges to the graph
addVertex() Adds vertices to the graph
command() Issues SQL command
delete() Removes records
getEdge() Retrieves edges
getVertex() Retrieves vertices
isUseLightweightEdges() Check if database uses Lightweight Edges
load() Retrieves records
query() Queries the database
removeEdge() Removes edges from a graph
removeVertex() Removes vertices from a graph
setUseLightweightEdges() Enable or disable the use of Lightweight Edges

Database Methods

Method Description
open() Opens the database
close() Closes the database

Class Methods

Method Description
browseClass() Returns all records in a class
countClass() Counts records in given class
createEdgeType() Creates a new class for edges
createVertexType() Creates a new class for vertices
dropEdgeType() Removes an edge class
dropVertexType() Removes a vertex class
getEdgeBaseType() Retrieves the base class for edges, which is E by default
getEdgeType() Retrieves the given edge class
getVertexBaseType() Retrieves the base class for vertices, which is V by default
getVertexType() Retrieves the given edge class

Cluster Methods

Method Description
browseCluster() Returns all records in a cluster
dropCluster() Removes cluster
getClusterIdByName() Retrieves the Cluster ID for the given cluster
getClusterNameById() Retrieves logical cluster name
getClusterNames() Retrieve cluster names
getClusterRecordSizeById() Retrieves the number of records in a cluster
getClusterRecordSizeByName() Retrieves the number of records in a cluster
getClusters() Retrieves clusters

Transaction Methods

Method Description
begin() Initiates a transaction
commit() Commits a transaction
isAutoStartTx() Checks whether transaction auto-start is enabled
rollback() Reverts a transaction
setAutoStartTx() Enables transaction auto-start

User Methods

Method Description
getUser() Retrieves the current user
setUser() Sets the user

Size Methods

Method Description
countClass() Counts records in given class
countEdges() Counts edge records
countVertices() Counts vertex records

results matching ""

    No results matching ""