Skip to content

Commit

Permalink
Integration coming along
Browse files Browse the repository at this point in the history
  • Loading branch information
Damon Oehlman committed Jan 10, 2013
1 parent 977cc50 commit 614150d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
98 changes: 95 additions & 3 deletions index.js
@@ -1,6 +1,13 @@
var async = require('async'),
debug = require('debug')('graphdb-orient'),
orientdb = require('orientdb');
errors = {
NOT_CONNECTED: 'Unable to perform operation on disconnected db'
},
orientdb = require('orientdb'),
commands = {},
_ = require('underscore');

/* define base types handler */

exports.defineBaseTypes = function(types) {
debug('defining core types for orientdb connection');
Expand All @@ -9,6 +16,9 @@ exports.defineBaseTypes = function(types) {
types.define('uuid').alias('string');
};

/**
## connect(graph, opts, callback)
*/
exports.connect = function(graph, opts, callback) {
var db, server;

Expand All @@ -30,7 +40,7 @@ exports.connect = function(graph, opts, callback) {
// connect the server
server.connect(function(err) {
// create the db instance
db = graph._db = new orientdb.Db(opts.db.name, server, opts.db);
db = graph._db = new orientdb.GraphDb(opts.db.name, server, opts.db);

// attempt to open the database
// and if that fails, attempt to create the db and then open it
Expand All @@ -49,6 +59,9 @@ exports.connect = function(graph, opts, callback) {
});
};

/**
## close(graph, callback)
*/
exports.close = function(graph, callback) {
// if we have no db, then return the callback
if (! graph._db) return callback();
Expand All @@ -60,4 +73,83 @@ exports.close = function(graph, callback) {
// pass the callback through
callback.apply(this, arguments);
});
};
};

/* operation definitions */

/**
## activateType(graph, definition, callback)
The activateType operation handler is used to ensure that a type has been
properly defined within orient. Within orient, there is a notion of classes
that provide some aspect of types.
*/
exports.activateType = function(graph, definition, callback) {
var db = graph._db,
className = definition.type;

// if we don't have a db connection, abort the operation
if (! db) return callback(errors.NOT_CONNECTED);

// attempt to create the class
db.createClass(className, 'V', function(err) {
// if we encountered an error, let's flag that type as existing
// so we do not attempt to create it again
if (err) {
definition.active = true;
return callback();
}

// execute the following commands in series
commands.series([
'CREATE PROPERTY ' + className + '.id STRING',
'CREATE INDEX ' + className + '.id UNIQUE'
], db, callback);
});
};

/**
## createNode(data, callback)
*/
exports.createNode = function(graph, data, callback) {
var db = graph._db;

// if we don't have a db connection, abort the operation
if (! db) return callback(errors.NOT_CONNECTED);

// ensure we have data
data = data || {};

// if we don't have a type for the node, it will simply be of type V
data.type = data.type || 'V';

// run the insert statement on the graph
debug('creating a new node in the graph with data: ', data);

// get the field values, without the type field
db.createVertex(_.omit(data, 'type'), { 'class': data.type }, callback);
};

/**
## series(commands, targetDb, callback)
This function is used to execute a series of OrientDB commands in series on
the targetdb.
## parallel(commands, targetDb, callback)
As per the series command, except the commands are executed in parallel
*/
['series', 'parallel'].forEach(function(op) {
commands[op] = function(commands, targetDb, callback) {
debug('running commands: ', commands);

// create the bound command calls
var boundCommands = commands.map(function(command) {
return targetDb.command.bind(targetDb, command);
});

// execute the commands
async[op].call(null, boundCommands, callback);
};
});
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,8 @@
"dependencies": {
"async": "0.1.x",
"debug": "*",
"orientdb": "0.9.x"
"orientdb": "0.9.x",
"underscore": "1.4.x"
},
"devDependencies": {
"mocha": "1.7.x"
Expand Down

0 comments on commit 614150d

Please sign in to comment.