Skip to content

Instantly share code, notes, and snippets.

@sogko
Created August 1, 2014 11:47
Show Gist options
  • Save sogko/ed481db90ae566b61817 to your computer and use it in GitHub Desktop.
Save sogko/ed481db90ae566b61817 to your computer and use it in GitHub Desktop.
var Arango = require('arangojs');
var async = require('async');
var db = Arango.Connection();
var testDate = 1406881769977; // Fri Aug 01 2014 08:29:29 GMT+0000
var dateDelta = 500000;
var data = [];
var edgesData = [];
var createdDocuments = [];
async.series([
function createNormalData(next) {
var i = 0;
async.whilst(
function () { return i < 10; },
function (callback) {
data.push({ name: 'test data ' + i, created: new Date(testDate).toISOString() });
testDate += dateDelta; // increase the date timestamp so we get a different incremental date for each document
i++;
callback();
},
function (err, results) {
next(err, results);
}
);
},
function createEdgesData(next) {
var i = 0;
async.whilst(
function () { return i < 5; },
function (callback) {
edgesData.push({ name: 'edge data ' + i, created: new Date(testDate).toISOString() });
testDate += dateDelta; // increase the date timestamp so we get a different incremental date for each document
i++;
callback();
},
function (err, results) {
next(err, results);
}
);
},
function deleteCollection(next) {
db.collection.delete('normalTest', function () { next(); });
},
function deleteCollection(next) {
db.collection.delete('edgesTest', function () { next(); });
},
function deleteGraph(next) {
db.delete('/_api/gharial/graphTest', function () { next(); });
},
function createCollection(next) {
db.collection.create('normalTest', function (err, results) { next(err, results); });
},
function createEdgeCollection(next) {
db.collection.create('edgesTest', { type: 3 }, function (err, results) { next(err, results); });
},
function createGraph(next) {
var data = {
name: 'graphTest',
edgeDefinitions : [
{
collection : 'edgesTest',
from : ['normalTest'],
to : ['normalTest']
}
]
};
db.post('/_api/gharial', data, function (err, results) { next(err, results); });
},
function addDocumentsToNormalCollection(next) {
async.forEachSeries(data, function (d, next) {
db.document.create('normalTest', d, function (err, results) {
createdDocuments.push(results._id);
next(err, results);
})
}, function (err, results) {
next(err, results);
});
},
function addDocumentsToEdgesCollection(next) {
async.forEachSeries(edgesData, function (d, next) {
var from = createdDocuments.pop();
var to = createdDocuments.pop();
db.edge.create('edgesTest', from, to, d, function (err, results) {
next(err, results);
})
}, function (err, results) {
next(err, results);
});
},
function getDocumentsFromNormalCollectionSortedByCreated(next) {
db.query.string = 'FOR d in normalTest SORT d.created DESC RETURN d';
db.query.exec(function (err, results) {
console.log('normalTest', results);
next(err, results);
});
},
function getDocumentsFromEdgesCollectionSortedByCreated(next) {
db.query.string = 'FOR d in edgesTest SORT d.created DESC RETURN d';
db.query.exec(function (err, results) {
console.log('edgesTest', results);
next(err, results);
});
},
function getDocumentsFromGraphSortedByCreated(next) {
db.query.string = 'FOR d in GRAPH_EDGES(\'graphTest\', {}, {}) SORT d.created ASC RETURN d';
db.query.exec(function (err, results) {
console.log('graphTest', results);
next(err, results);
});
}
], function (err, results) {
console.log('DONE', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment