Skip to content
Giraldo Rosales edited this page Feb 12, 2014 · 11 revisions

The GraphDB functionality extends the document functionality. For basic document database functions, take a look at the document methods. The only difference is calling orientdb.GraphDb instead of orientdb.Db. Where GraphDb extends Db. See the example in the open method below. The default type of database is also set to graph.

Table of Contents


###Open

This is the first operation the client should call. It opens the default database specified in the node-orientdb configuration or you can specify another database to access. Returns the session ID to be reused for all the next calls and the list of configured clusters.

db.open();

//or

db.open(databaseName, databaseType);
  • databaseName - (string) Name of the database. Optional.

  • databaseType - (string) Type of database. Either document or graph. Optional.

  • result - (array) An array of databases on the server.

    • obj - (object) Database object.
      • sessionId - (string) Database name.
      • clusters - (array) Path to database.
        • id - (numeric) Id.
        • name - (string) Name of cluster.
        • type - (string) Cluster type.
        • dataSegmentId - (numeric) Data segment id.
      • config - (string) Is always null unless you're running in a server clustered configuration.
      • release - (string) Version of OrientDB release deployed on server and optionally build number. Example: "1.4.0-SNAPSHOT (build 13)"

Example

var orientdb = require("node-orientdb"),
var Db       = orientdb.GraphDb;

var dbConfig = {
    //Server
    server_host:'localhost',
    server_port:2424,
    server_username:'admin',
    server_password:'admin',

    //Database
    database_name:'social',
    database_username:'admin',
    database_password:'admin',
    database_type: "document", //Optional. Default: document.
    database_storage: "local" //Optional. Default: local.
};

var db = new Db(dbConfig);

db.open()
    .then(function(results) {
        //Details
        console.log("Database '" + db.databaseName + "' has " + db.clusters.length + " clusters");

        //Traverse
        db.from("#9:5", "isFollowing").in('following').then(
            function(results) {
                db.close();
                console.log(results);
            },
            function(error) {
                db.close();
                console.log(error);
            }
        );
    })
    .error(function(error) {
        console.log(error);
    });

***

##Vertex More on Vertices....

###Create a New Vertex Shortcut for: "CREATE VERTEX [@class] SET [field=value, ...]"

db.vertexCreate(vertex);
  • vertex - (object) Data to create a new vertex.
    • @class - (string) Class to create new record. Optional. Default: "V".
    • [custom] - (*) custom fields within record.
  • result - (object) Vertex object
    • @rid - (string) Formatted record ID of the vertex.
    • @class - (string) Class of the vertex record.
    • @version - (numeric) The version of the new vertex.
    • @type - (string) Type of record.
    • [custom] - (*) custom fields within record.

Example

db.open()
    .then(function(){
        var vertex = {
            '@class':'People',
            'id':'123',
            'name':'Giraldo'
        };

        db.vertexCreate(vertex)
            .then(function(results){
                console.log(results);
            })
            .error(function(error){
                console.log('Error creating a vertex:', error);
            });
    })
    .error(function(error){
        console.log('Database connection error:', error);
    });

###Delete a Vertex Shortcut for: "DELETE VERTEX [@rid | @class] WHERE [condition] LIMIT [limit]"

db.vertexDelete(vertex, condition, limit);
  • vertex - (string | object) May be a formatted record ID as a string (ie. "#9:5") or as a document object. Required.
  • condition - (string) The condition for the WHERE statement. Optional. Default: "".
  • limit - (numeric) A limit on how many items to delete. -1 removes the limit. Optional. Default: -1.
  • result - (boolean) 1 if deletion was successful. 0 if not.

***

Edges

Edges in the graph database. Edges, together with Vertices, are the main components of a Graph. OrientDB supports polymorphism on edges. More on Edges...


###Create Edge Shortcut for:
"CREATE EDGE [@class] FROM [fromRID] TO [toRID] SET [field=value, ...]"

If no properties are set on an edge, an edge record will NOT be added to the schema, only a link is added between two vertices.

db.edgeCreate(fromRID, toRID, edge);
  • fromRID - (string | object) The Record ID from the source. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. If using an object, must contain the recordId property or both, the clusterId and clusterPosition properties. Required.
    • recordId - (numeric) Formatted record ID.
    • clusterId - (numeric) The cluster the record belongs to.
    • clusterPosition - (numeric) Cluster position.depending on record type.
  • toRID - (string | object) The Record ID to the destination. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
    • recordId - (numeric) Formatted record ID.
    • clusterId - (numeric) The cluster the record belongs to.
    • clusterPosition - (numeric) Cluster position.depending on record type.
  • edge - (object) Data to create a new vertex.
    • @class - (string) Class to create new record. Required (unless clusterId is specified).
    • [custom] - (*) custom fields within record.
  • result - (object) Edge object
    • @rid - (string) Edge ID.
    • @class - (string) Class of the edge record.
    • @version - (numeric) The version of the new edge.
    • @type - (string) Type of record.
    • in - (string) This is the record ID of the destination record.
    • out - (string) This is the record ID of the source record.
    • [custom] - (*) custom fields within record.

Example

//Example of adding an edge
var edge = {
    '@class':'isFollowing',
    'date':'2014-01-10'
};

db.edgeCreate('#9:5', '#9:6', edge)
    .then(function(results) {
        console.log(results);
    })
    .error(function(error) {
        console.log(error);
    });

###Get Out Vertices Get the vertices going out of a vertex. (vertex)->(OUT). This is a shortcut for:
SELECT expand(in()) FROM (SELECT expand(out([edgeClass])) FROM [vertex])

Note: If using the SQL command in db.query instead of this shortcut, It is recommended to refrain from using raw properties when you work with edge/vertex relations use graphdb functions outE(), out(), in() and so on. ie. Use "in(followed_by)" instead of "in_following"

db.from(vertex).out(edgeClass);
  • vertex - (string | object) The Record ID from the source. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
  • edgeClass - (string) The class name of the edges to follow. Required.
  • results - (array)

Example

db.from('#9:5').out('following')
    .then(function(results) {
        console.log(results);
    })
    .error(function(error) {
        console.log(error);
    });

###Get In Vertices Get the vertices going into a vertex. (IN)->(vertex). This is a shortcut for:
SELECT expand(out()) FROM (SELECT expand(in([edgeClass])) FROM [vertex])

Note: If using the SQL command in db.query instead of this shortcut, It is recommended to refrain from using raw properties when you work with edge/vertex relations use graphdb functions outE(), out(), in() and so on. ie. Use "in(followed_by)" instead of "in_following"

db.from(vertex).in(edgeClass);
  • vertex - (string | object) The Record ID from the source. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
  • edgeClass - (string) The class name of the edges to follow. Required.
  • results - (array)
db.from('#9:5').in('following')
    .then(function(results) {
        console.log(results);
    })
    .error(function(error) {
        console.log(error);
    });

Clone this wiki locally