3

I am coming from MongoDB, and now I would like to use ArangoDb for use of ACID Transactions. I am using NodeJS only for programming.

The problem I encounter is a following, I can not bring the NPM code example to life!

There are 3 NPM packages with Drivers for NodeJS. None of theses code examples is working!

Then, when I try to use instead the HTTP REST API, they do at least partially work. Here is one HTTP REST API approach to access the ArangoDB.

But, how can I specify in what Database the Document should get inserted into? It always inserts it in the _System-Database. Which I dont want.

Here you see my code..

var restify = require('restify');

// Client
var client = restify.createJsonClient({
  url: 'http://192.168.142.139:8529',
  version: '~1.0'
});

// Now call this insert function to insert a new Document into the ArangoDB Database
insert (function(e,o){

});

function insert ( callback) {
  var data = {
        user : 'Harryblabla',
        nicenumber : 8675,
        nicestring : 'des04950'};

  client.post('/_api/document?collection=testcollection1&createCollection=true&waitForSync=true', 
    data, function (err, req, res, obj) {

    if (obj.error==false) {
        console.log('ok - saved');
        console.log('result '+JSON.stringify(obj));
        callback (err, 1);
    } else {
        console.log('not saved');
        callback ('error', null);
    }
  });

}

It does insert the Document in 'testcollection1' but how can I specify into wich Database?

Also, could you give me examples on how to 'ensureIndex' with Unique Index on a field, 'update $set for setting a field, and findOne, like in MongoDB?

You could also write the code with using one of these NPM packages. But the only approach I brought to life is via HTTP REST Api..

please, thanks. I like that ArangoDb because its open source, offers ACID transactions. But major disadvantage is lack of Tutorials, and lack of example code...

* Also I need: UPDATE of a field in Collection, IF NOT EXIST create it:*

Here you can see a NodeJS + MongoDB example on how to update only a specific field. And if it not exists, it will be created. Thats exactly what I also need to ArangoDB in Nodejs. Can you write me an example? please

// Mongo DB connection already opened.
// this is the function to update or create if not exists
mycollection.update({txid: tx.txid},
      {$set: {txid: tx.txid, nice: tx.nice, confvalidated: true}}, {upsert: true}, function(err, records1){
    if (records1) {
      console.log('insertreceived: Amount '+tx.nice+' C: '+tx.confirmations+ 'INSERT RECEIVED: '+tx.txid);
      callback(null, records1);
    } else {
      //callback('error');
      callback('error', null);
    }
   });

3 Answers 3

6

there is also a Javascript/Node.js driver that is continuously developed available for ArangoDB (working with 2.0 is confirmed). In the readme of this driver there are examples how to install and use it, documentation is available here. If this is not working for you, could please send us an error report for your problems, then we will fix the issues.

As a code example for your updated question (i have just taken the same object you did use in your MongoDB example, tx.txid has to be the _id attribute of your document.) you can use:

var arango = require('arangojs');
var db = arango.Connection('http://127.0.0.1:8529');
mydb = db.use("yourDatabaseName");
mydb.document.patch(tx.txid,  {
  nice: tx.nice,
  confvalidated: true
});
Sign up to request clarification or add additional context in comments.

1 Comment

First link is broken
4

The reason your collections all end up in the _system database is because you're not using a database in the URL. If you want to use e.g. my_database instead of the default database (i.e. _system), you should use /db/my_database/_api/ instead of simply /_api/. You can find more on working with multiple databases in [the HTTP API for databases].

I'm not very familiar with MongoDB, but I think the equivalent of ensureIndex would be POST'ing to the /_api/index collection with the same arguments and data. According to the API documentation for indices the server will answer with a 200 if the index already existed or 201 if it was created.

The equivalent of $set is probably PATCH'ing to the document, which will update the document instead of overwriting it. From the documentation on HTTP PATCH for documents:

All attributes from the patch document will be added to the existing document if they do not yet exist, and overwritten in the existing document if they do exist there.

The closest equivalent of findOne is probably the firstExample simple query. It only supports exact matches, however, for actual queries you should probably use it's query language, which also has an HTTP API.

I'm the author of one of the node.js "drivers" for ArangoDB, which I discontinued. The reason I decided against continuing my development of it is that using the HTTP API directly is probably not the most useful approach for integrating ArangoDB in your project.

ArangoDB comes with its own JavaScript environment called Foxx. Instead of sending your query strings over the wire and calling multiple REST endpoints in your node code it's a much better idea to create your own domain-specific HTTP API using Foxx, which lets you abstract the underlying database-specific calls away. In addition to providing you with a pure JS API that covers the full functionality of the HTTP API, Foxx also lets you perform transactions with automatic rollback on failure.

If you can write node.js code, you can write Foxx code. The Foxx documentation includes a tutorial that should get you started. If you've used arangosh, ArangoDB's interactive shell, you'll be happy to know that you can use the same API in Foxx code if you're having trouble with Foxx's model logic.

The only drawback of using Foxx is that you need to deploy your Foxx app to the database (in addition to deploying your node.js code to your node.js server), but in practice this shouldn't make your project's deployment any more difficult.

2 Comments

thanks too, I will try the drivers mentioned above. Also I will look into FOXX. Both approaches seem to be interesting. I definitly need ACID transactions. Thats way I prefer arangoDB over mongoDB
There is now an official JS/TS driver making most of this information irrelevant: arangodb.github.io/arangojs
3
var db = new Database();
db.database('nodetest1')
.then(function (mydb) {return mydb.query('FOR x IN User RETURN x');})
.then(function (cursor) {return cursor.all();})
.then(
  function (list) {console.log(list);},
  function (err) {console.error('Something went wrong:', err);}
);

more info blog http://www.ashishblog.com/getting-start-with-arangodb-using-nodejs-nodejs-ejs-arangojs/ source code https://github.com/A5hpat/nodeArangoWebApp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.