Created
August 1, 2014 11:51
-
-
Save sogko/16676f6391ba409c7911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Arango = require('arangojs'); | |
var async = require('async'); | |
var db = Arango.Connection(); | |
var testDate = 1406881769977; // Fri Aug 01 2014 16:29:29 GMT+0800 (SGT) | |
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) }); | |
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) }); | |
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) { | |
var data = { | |
sort: 'd.created DESC' | |
}; | |
db.query.string = 'FOR d in normalTest SORT @sort DESC RETURN d.created'; | |
db.query.exec(data, function (err, results) { | |
console.log('normalTest', results); | |
next(err, results); | |
}); | |
}, | |
function getDocumentsFromEdgesCollectionSortedByCreated(next) { | |
var data = { | |
sort: 'd.created DESC' | |
}; | |
db.query.string = 'FOR d in edgesTest SORT @sort RETURN d.created'; | |
db.query.exec(data, function (err, results) { | |
console.log('edgesTest', results); | |
next(err, results); | |
}); | |
}, | |
function getDocumentsFromGraphSortedByCreated(next) { | |
var data = { | |
sort: 'd.created DESC' | |
}; | |
db.query.string = 'FOR d in GRAPH_EDGES(\'graphTest\', {}, {}) SORT @sort RETURN d.created'; | |
db.query.exec(data, 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