ArangoDB - Using JavaScript in the database

6,630 views

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 basicswww.arangodb.org © j.steemann@triagens.de
  3. 3. ArangoDB.explain(){ "type": "NoSQL database", "openSource": true, "version": 1.0, "builtWith": [ "C", "C++", "js" ], "Javascript": [ "client side", "server side" ], "uses": [ "V8" ]} www.arangodb.org © j.steemann@triagens.de
  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) www.arangodb.org © j.steemann@triagens.de
  5. 5. Data types: JSON!ArangoDB operates on JSON documentsJSON data can be saved as-is:db.users.save({ "name": "Jan", "uses": [ "vi", "js", "C++" ]}); www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  8. 8. Queryingwww.arangodb.org © j.steemann@triagens.de
  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}); www.arangodb.org © j.steemann@triagens.de
  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) www.arangodb.org © j.steemann@triagens.de
  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); www.arangodb.org © j.steemann@triagens.de
  12. 12. ReferencesArangoDB can store references betweendocumentsReferences are documents with two endpoints(the documents they connect)Reference documents can carry own attributes www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  15. 15. Query languageArangoDB can also answer complexSQL-like queriesSuch queries are expressed in ArangoDBstextual query languageThis language allows joins, sub-queries,aggregation etc. www.arangodb.org © j.steemann@triagens.de
  16. 16. Example queryFOR u IN users It er a t or FILTER u.addr.country == "US" && (u.isConfirmed == true || u.age >= 18) F ilt er LET userLogins = ( FOR l IN logins FILTER l.userId == u.id RETURN l ) S u b q u er y RETURN { "user": u, "logins": LENGTH(userLogins) } R es u lt www.arangodb.org © j.steemann@triagens.de
  17. 17. Server side „actions“ www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  19. 19. ActionsActions are bound to URLs and executed whenURLs are called, e.g./users?name=... => function(...)/users/.../friends => function(...) www.arangodb.org © j.steemann@triagens.de
  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": req.urlParameters.name }); // 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);} www.arangodb.org © j.steemann@triagens.de
  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 www.arangodb.org © j.steemann@triagens.de
  22. 22. Summarywww.arangodb.org © j.steemann@triagens.de
  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“ www.arangodb.org © j.steemann@triagens.de
  24. 24. Thanks! Any questions?Спасибо GrazieTeşekkürler ¡Gracias!Merci! ХвалаDank je wel Ευχαριστώありがとう 고마워Tack så mycket Danke www.arangodb.org © j.steemann@triagens.de
  25. 25. Give it a try!ArangoDB source repository:https://www.github.com/triAGENS/ArangoDB(use master branch)Website: http://www.arangodb.org/Builds available for Homebrew andseveral Linux distributionsTwitter: #arangodb, @arangodbGoogle group: ArangoDB www.arangodb.org © j.steemann@triagens.de
  26. 26. RoadmapMVCC / ACID transactionsTriggersSynchronous and asynchronous replication www.arangodb.org © j.steemann@triagens.de
  27. 27. Thank you!Stay in Touch: • Fork me on github • Google Group: ArangoDB • Twitter: @steemann & @arangodb • www.arangodb.org www.arangodb.org © j.steemann@triagens.de

×