ArangoDB - Using JavaScript in the database


Published on

Jan Steemann had a talk at Javascript Everywhere in Paris 2012 on Javascript in Arangodb an open source NoSQL database. With ArangoDB you can use Javascript and/or Ruby (mruby) as embedded language

Published in: Technology

ArangoDB - Using JavaScript in the database

  1. 1. ArangoDB/* Using Javascript in the database */ Jan Steemann, triAGENS, Cologne
  2. 2. ArangoDB ©
  3. 3. ArangoDB.explain(){ "type": "NoSQL database", "openSource": true, "version": 1.0, "builtWith": [ "C", "C++", "js" ], "Javascript": [ "client side", "server side" ], "uses": [ "V8" ]} ©
  4. 4. Documents and collectionsArangoDB is a database that works withdocumentsDocuments are sets of name / value pairs(think of JSON objects), e.g.{ "name": "Jan", "age": 37 }Documents are organised in collections (liketables in relational databases) ©
  5. 5. Data types: JSON!ArangoDB operates on JSON documentsJSON data can be saved{ "name": "Jan", "uses": [ "vi", "js", "C++" ]}); ©
  6. 6. Schema-freeDocuments can be heterogenous –even in the same collection, e.g.{ "name": "Jan", "age": 37 },{ "name": "Tim", "likes": [ "js" ] },{ "name": { "first": "Patrick", "last": "Star" }}No need to care about schemas ©
  7. 7. API & protocolsArangoDB server functionality is exposed via anHTTP REST APIArangoDB answers HTTP queries,so it can be used from the browserSimple to use from Javascript clients(3rd party) drivers available for node.js, Ruby,PHP, ...Javascript-enabled client shell provided ©
  8. 8. ©
  9. 9. Querying is simpleQuery a document by its unique id / key:db._document("users/9558905");Query by providing an example:db.users.byExample({ "name": "Jan", "age": 37}); ©
  10. 10. IndexesArangoDB allows querying on any documentattributes or sub-attributesSecondary indexes can be created for higherquery performance:• Hash indexes (equality queries)• Skiplists (range queries)• 2d geo indexes (location queries) ©
  11. 11. Geo index examples// get b a r n ea r es t t o coor d in a t edb.bars.near(48.51, 2.21).limit(1);// get a ll s h op s w it h in r a d iu s// a r ou n d coor d in a t edb.shops.within(48.51, 2.21, rad); ©
  12. 12. ReferencesArangoDB can store references betweendocumentsReferences are documents with two endpoints(the documents they connect)Reference documents can carry own attributes ©
  13. 13. References exampleUsers documents: Relations documents: (references){ "name": "Jan", "age": 37 {} "_from": "users/jan", "_to": "users/tim",{ "type": "knows", "name": "Tim", } "city": "Paris"}// q u er y ou t b ou n d r ela t ion s f r om "J a n "db.relations.outEdges(jan); // => tim ©
  14. 14. Graph queriesReferences make it possible to process graphswith ArangoDBSome special functions are provided todetermine paths in a graph etc.Can write own Javascript functions for specialtraversals ©
  15. 15. Query languageArangoDB can also answer complexSQL-like queriesSuch queries are expressed in ArangoDBstextual query languageThis language allows joins, sub-queries,aggregation etc. ©
  16. 16. Example queryFOR u IN users It er a t or FILTER == "US" && (u.isConfirmed == true || u.age >= 18) F ilt er LET userLogins = ( FOR l IN logins FILTER l.userId == RETURN l ) S u b q u er y RETURN { "user": u, "logins": LENGTH(userLogins) } R es u lt ©
  17. 17. Server side „actions“ ©
  18. 18. Application serverArangoDB can answer web requests directlyThis also makes it an application serverUsers can extend its functionality with serverside Javascript „actions“Actions are user-defined functions that containcustom business logic ©
  19. 19. ActionsActions are bound to URLs and executed whenURLs are called, e.g./users?name=... => function(...)/users/.../friends => function(...) ©
  20. 20. Actions examplefunction (req, res) { // get r eq u es t ed u s er f r om d b , // n a m e is r ea d f r om U R L p a r a m et er var u = db.users.byExample({ "username": }); // r em ov e p a s s w or d h a s h delete u.hashedPassword; // b ef or e r et u r n in g t o ca ller actions.resultOk(req, res, 200, u);} ©
  21. 21. Actions – Use casesFilter out sensitive data before respondingValidate inputCheck privilegesCheck and enforce constraintsAggregate data from multiple queries into a singleresponseCarry out data-intensive operations ©
  22. 22. ©
  23. 23. ArangoDB - summaryFlexible in terms of data modelling and queryingAPI is based on web standards:HTTP, REST, JSONEasy to use from Javascript clientsCan be used as application serverFunctionality can be extended with server sideJavascript „actions“ ©
  24. 24. Thanks! Any questions?Спасибо GrazieTeşekkürler ¡Gracias!Merci! ХвалаDank je wel Ευχαριστώありがとう 고마워Tack så mycket Danke ©
  25. 25. Give it a try!ArangoDB source repository: master branch)Website: available for Homebrew andseveral Linux distributionsTwitter: #arangodb, @arangodbGoogle group: ArangoDB ©
  26. 26. RoadmapMVCC / ACID transactionsTriggersSynchronous and asynchronous replication ©
  27. 27. Thank you!Stay in Touch: • Fork me on github • Google Group: ArangoDB • Twitter: @steemann & @arangodb • ©