Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // require dependencies
  2. var Oriento = require('oriento');
  3. var async = require('async');
  4.  
  5. // constructor for Class OrientDB
  6. function OrientDB(){
  7. // configure orientdb client - ensure you change the username and password here
  8. this.server = Oriento({
  9. host: 'localhost',
  10. port: 2424,
  11. username: 'username',
  12. password: 'password'
  13. });
  14.  
  15. // we will connect to a database named 'yelp-test'
  16. try {
  17. this.db = this.server.use('yelp-test');
  18. } catch (e) {
  19. console.log('ERROR : database error --> '+ e);
  20. process.exit(1);
  21. }
  22. console.log('Using database: ' + this.db.name);
  23. }
  24.  
  25. //----------------- class methods ------------------
  26.  
  27.  
  28. //--------------------------------------------------
  29. // function createNode -- creates a node (or vertex)
  30. // inputs:
  31. // orientClass --> name of orientDb Vertex class to use
  32. // data --> a JSON formatted record
  33.  
  34. OrientDB.prototype.createNode = function(orientClass, data){
  35. this.db.insert().into(orientClass).set(data).one()
  36. .then(function(data) {
  37. console.log('Stored record --> ', data.name);
  38. }, function(error){
  39. console.log('ERROR : record ' + error);
  40. });
  41. }
  42.  
  43. //--------------------------------------------------
  44. // function lookupIndex -- lookup an index
  45. // inputs:
  46. // indexName --> the name of the index to lookup
  47. // key --> the key to be looked up
  48. // callback --> the callback function
  49. // outputs:
  50. // returns @rid (orientdb record_id) to callback
  51.  
  52. OrientDB.prototype.lookupIndex = function(indexName, key, callback) {
  53. this.db.index.get(indexName)
  54. .then(function (index){
  55. index.get(key)
  56. .then(function (result){
  57. var record_id = '#' + result.rid.cluster + ':' + result.rid.position;
  58. return record_id;
  59. })
  60. .then(function (res){
  61. callback(res);
  62. });
  63. })
  64. .catch(function (err){
  65. console.error('error : ', err.message);
  66. process.exit(1);
  67. });
  68. }
  69.  
  70. //--------------------------------------------------
  71. // function createEdge -- creates an edge (or relationship)
  72. // this function leverages index LookupIndex for speedy lookups
  73. // inputs:
  74. // fromIndex --> name of orientDb From index
  75. // fromKey --> From Vertex key to be lookedup
  76. // toIndex --> name of orientDb To index
  77. // toKey --> To Vertex key to be lookedup
  78. // data --> a JSON formatted record
  79.  
  80. OrientDB.prototype.createEdge = function(fromIndex, fromKey, toIndex, toKey, data){
  81. var fromRID = 'a';
  82. var toRID = 'b';
  83.  
  84. var adb = this.db; // because db is undefined in async
  85.  
  86. async.waterfall([
  87. function(callback){
  88. adb.index.get(fromIndex)
  89. .then(function (index){
  90. index.get(fromKey)
  91. .then(function (result){
  92. var record_id = '#' + result.rid.cluster + ':' + result.rid.position;
  93. callback(null, record_id);
  94. })
  95. })
  96. }
  97. ,
  98.  
  99. function(fromRID, callback){
  100. adb.index.get(toIndex)
  101. .then(function (index){
  102. index.get(toKey)
  103. .then(function (result){
  104. var record_id = '#' + result.rid.cluster + ':' + result.rid.position;
  105. callback(null, fromRID, record_id);
  106. })
  107. })
  108. }
  109. ,
  110.  
  111. function(fromRID, toRID, callback){
  112. adb.create('edge', 'reviewed_by')
  113. .from(fromRID)
  114. .to(toRID)
  115. .set(data)
  116. .commit()
  117. .one()
  118. .then(function (result){
  119. callback(null, 'edge created --> ' + JSON.stringify(result['@rid']));
  120. });
  121.  
  122. }
  123. ],
  124. function (err, result) {
  125. if (err){console.log(err)}
  126. if (result){console.log(result)}
  127. }
  128. );
  129. }
  130. module.exports = OrientDB;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement