4

I would like to do ACID Transactions with ArangoDB. And I would like to send the transaction Code (nodejs-code) to the ArangoDB server where it will then be executed and hopefully commited or rolled back if it fails. But at the ArangoDB serverside I need the NPM Decimal module or called package installed.

How do install it, and how do I access that particular module from the transaction code within?

Greetings and thanks.

europeanguy
  • 235
  • 2
  • 10
  • Are you referring to the package "decimal" (https://www.npmjs.org/package/decimal) or "deci-mal" (https://www.npmjs.org/package/Deci-mal)? – fceller May 28 '14 at 20:35
  • I am referring to the first one. "decimal". (published by shinuza). (npmjs.org/package/decimal) – europeanguy May 28 '14 at 21:12
  • I tried to copy the "decimal" directory of that NPM package into /usr/share/arangodb/js/server/modules . And then I startet arangosh: $ arangosh --javascript.modules-path /usr/share/arangodb/js/server/modules. Inside of arangosh I did this command: var deci = require("decimal"); But its not working. do you have any ideas? Decimal seems to be a very small handy npm package. Only few lines of code. I think it does not have dependencies. – europeanguy May 28 '14 at 21:26
  • Though there might be better places to put the module, the following seems to work fine: if you want to access it from arangosh and arangod, you can copy it in /usr/share/arangodb/js/common/modules (instead of .../js/server/modules). If you then start arangosh regularly (without extra modules-path), it should be able to pick up the module and allows using it via require("decimal"). The same should work from inside arangod. Inside a transaction, you could use the module via require("decimal"), too. – stj May 28 '14 at 22:01
  • I tried it, but its not perfectly working. When I copy the npm module "decimal" and its complete file structure into "/usr/share/arangodb/js/common/modules" then its not working. Also starting arangosh with various paths does not work. But, when I only copy the "decimal.js"-file into "/usr/share/arangodb/js/common/modules" then it works fine. Then in Arangosh I can acces it via: "var deci = require("decimal");" and then "console.log(deci('4').add('5').toString());" so now I am happy it works as I want it. But we could not add another larger NPM module. – europeanguy May 29 '14 at 07:24

2 Answers2

3

Once you have the decimal module at the proper location and you can require it properly inside arangod, you should be able to use it inside a transaction like this:

db._executeTransaction({ 
  collections: { }, 
  action: function (params) { 
    var Decimal = require("decimal"); 
    return Decimal(params.foo).add(params.bar).toNumber(); 
  }, 
  params: { 
    foo: '1.1', 
    bar: '2.2' 
  } 
});

If your transactions need to access collection, you obviously need to specify their names in the "collections" attribute, e.g.

db._executeTransaction({ 
  collections: { 
    write: [ "test" ]
  }, 
  action: function (params) { 
    var Decimal = require("decimal");
    var collection = require("org/arangodb").db.test;
    var amount = Decimal(params.foo).add(params.bar).toNumber(); 

    return collection.save({ _key: params.key, amount: amount }); 
  }, 
  params: { 
    key: "mykey",
    foo: '1.1', 
    bar: '2.2' 
  } 
});
stj
  • 9,037
  • 19
  • 33
3

stj has already answered the transaction part of the question. Regarding installing NPM modules.

Switch into the folder

/usr/share/arangodb/js/common/node

and execute

npm install decimal

This should install the NODE module for both the server (arangod) and the shell (arangosh).

fceller
  • 2,734
  • 15
  • 19
  • 1
    thanks to both of you. Now I opened a new question. ArangoDB Transactions - How prevent from throwing exceptions http://stackoverflow.com/questions/23934058/arangodb-transactions-how-prevent-from-throwing-exceptions – europeanguy May 29 '14 at 13:06
  • @fceller Are your instructions still valid as of Arango 2.8.9? My `common` directory has `boostrap` and `modules` subdirectories but no `node` subdirectory. – ropeladder Jun 06 '16 at 15:33