Permalink
Browse files

Refactored save node so it will both create and update vertices

  • Loading branch information...
1 parent 77d2d05 commit ae33126ba7ddc083a4aa3234e43a891c28073e52 @DamonOehlman committed Jan 12, 2013
Showing with 41 additions and 29 deletions.
  1. +41 −29 index.js
View
@@ -4,8 +4,18 @@ var async = require('async'),
NOT_CONNECTED: 'Unable to perform operation on disconnected db'
},
orientdb = require('orientdb'),
+ orientParser = require('orientdb/lib/orientdb/connection/parser'),
commands = {},
- _ = require('underscore');
+ _ = require('underscore'),
+ templates = {
+ update: 'UPDATE <%= type %> <%= sqlsets %> WHERE id = "<%= id %>"',
+ vertexCreate: 'CREATE VERTEX <%= type %> <%= sqlsets %>'
+ };
+
+// compile each of the templates
+_.each(templates, function(value, key) {
+ templates[key] = _.template(value);
+});
/* define base types handler */
@@ -109,34 +119,6 @@ exports.activateType = function(graph, definition, callback) {
};
/**
-## createLink
-*/
-exports.createLink = function(graph, data, callback) {
-};
-
-/**
-## createNode(graph, 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);
-};
-
-/**
## createEdge(graph, sourceId, targetId, data, callback)
*/
exports.createEdge = function(graph, sourceId, targetId, data, callback) {
@@ -162,6 +144,36 @@ exports.getNode = function(graph, id, nodeType, callback) {
};
/**
+## saveNode(graph, node, callback)
+*/
+exports.saveNode = function(graph, node, callback) {
+ var db = graph._db;
+
+ // if we don't have a db connection, abort the operation
+ if (! db) return callback(new Error(errors.NOT_CONNECTED));
+
+ // if we don't have node data, then report an invalid node
+ if (! node.data) return callback(new Error('A node object is require for a save operation'));
+
+ // look for an existing node
+ exports.getNode(graph, node.data.id, node.type, function(err, existing) {
+ var commandTemplate = templates[existing ? 'update' : 'vertexCreate'],
+ data = _.omit(node.data, existing ? ['id'] : []),
+ commandText = commandTemplate({
+ type: node.type,
+ sqlsets: orientParser.hashToSQLSets(data).sqlsets,
+ id: node.data.id
+ });
+
+ if (err) return callback(err);
+
+ // run the command
+ debug('running command: ' + commandText);
+ db.command(commandText, callback);
+ });
+};
+
+/**
## series(commands, targetDb, callback)
This function is used to execute a series of OrientDB commands in series on

0 comments on commit ae33126

Please sign in to comment.