Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Rapid API Development ArangoDB Foxx

Weitere Verwandte Inhalte

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Rapid API Development ArangoDB Foxx

  1. 1. by Michael Hackstein @mchacki Rapid API Development ArangoDB Foxx
  2. 2. Michael Hackstein @mchacki • Member of ArangoDB Core Team • web front-end • graph features • Organizer of cologne.js • Master’s Degree
 (spec. Databases and
 Information Systems)
  3. 3. Classical Web Applications Application Logic
  4. 4. Single Page Web Applications Application Logic
  5. 5. The Idea • What if we could talk to the database directly? • It would only need an API • What if we could define this API in JavaScript?
  6. 6. Single Page Web Applications This doesn‘t mean its a Rails/… Killer
  7. 7. Client Server DB
  8. 8. Client Server (optional) DB with Foxx
  9. 9. What is ? • a multi-model database (document store & graph database) • is open source and free (Apache 2 license) • offers convenient queries (via HTTP/REST and AQL) • offers high performance and is memory efficient • uses JavaScript throughout (V8 built into server) • doubles as a web and application server (Foxx) • offers many drivers for a wide range of languages • is easy to use with web frontend and good documentation • enjoys good professional as well as community support • and recently added sharding in Version 2.0.
  10. 10. / (~( ) ) /_/ ( _-----_(@ @) ( / /|/--| V " " " "
  11. 11. • … a feature of ArangoDB since 1.4 • … an easy way to define REST APIs on top of ArangoDB • … a toolset for developing your single page web application • … not requiring any special code on the client side – use it with Angular, Backbone, Ember,… Foxx is…
  12. 12. Why another solution? • ArangoDB Foxx is streamlined for API creation – not a Jack of all trades • There‘s no communication overhead between (serverside) application and database • It is designed for front end developers: Use JavaScript, you already know that
  13. 13. Foxx.Controller
  14. 14. Foxx = require("org/arangodb/foxx");
  15. 15. Foxx = require("org/arangodb/foxx"); controller = new Foxx.Controller(appContext);
  16. 16. Foxx = require("org/arangodb/foxx"); controller = new Foxx.Controller(appContext); controller.get("/users", function(req, res) { });
  17. 17. Foxx = require("org/arangodb/foxx"); controller = new Foxx.Controller(appContext); controller.get("/users", function(req, res) { res.json({ hello: "world" }); });
  18. 18. Parameterize the routes • You may want a route like `users/:name`… • …and then access the value of `name` easily
  19. 19. Foxx = require("org/arangodb/foxx"); ! controller = new Foxx.Controller(appContext); ! controller.get("/users", function(req, res) { res.json({ hello: "world" }); });
  20. 20. Foxx = require("org/arangodb/foxx"); ! controller = new Foxx.Controller(appContext); ! controller.get("/users ", function(req, res) { res.json({ hello: "world" }); }); /:name
  21. 21. Foxx = require("org/arangodb/foxx"); ! controller = new Foxx.Controller(appContext); ! controller.get("/users ", function(req, res) { res.json({ hello: "world" }); }); req.params("name"); /:name
  22. 22. • In your Foxx.Controller you describe your routes • But your application can consist of multiple Foxx.Controllers • … and you also want to deliver assets and files Manifest.json
  23. 23. { "name": "my_website", "version": "1.2.1", "description": "My Website with a blog and a shop", "thumbnail": "images/website-logo.png", ! "controllers": { "/blog": "apps/blog.js", "/shop": "apps/shop.js" }, ! "assets": { "application.js": { "files": [ "vendor/jquery.js", "assets/javascripts/*" ] } } }
  24. 24. { "name": "my_website", "version": "1.2.1", "description": "My Website with a blog and a shop", "thumbnail": "images/website-logo.png", ! "controllers": { "/blog": "apps/blog.js", "/shop": "apps/shop.js" }, ! "assets": { "application.js": { "files": [ "vendor/jquery.js", "assets/javascripts/*" ] } } }
  25. 25. { "name": "my_website", "version": "1.2.1", "description": "My Website with a blog and a shop", "thumbnail": "images/website-logo.png", ! "controllers": { "/blog": "apps/blog.js", "/shop": "apps/shop.js" }, ! "assets": { "application.js": { "files": [ "vendor/jquery.js", "assets/javascripts/*" ] } } }
  26. 26. More • Define a setup and teardown function to create and delete collections • Define lib to set a base path for your require statements • Define files to deliver binary data unaltered
  27. 27. Documentation as a first class citizen
  28. 28. Annotate your Routes • For Documentation • But it is also used for validation
  29. 29. controller.get("/users/:name", function(req, res) { res.json({ hello: req.params("name"); }); });
  30. 30. controller.get("/users/:name", function(req, res) { res.json({ hello: req.params("name"); }); }).pathParam("name", { description: "Name of the User", dataType: "string" }); ;
  31. 31. controller.get("/users/:name", function(req, res) { res.json({ hello: req.params("name"); }); }).pathParam("name", { description: "Name of the User", dataType: "string" }); /** What's my name? * * This route knows it. */
  32. 32. Automatically generate Swagger Docs
  33. 33. Foxx.Repository and Foxx.Model
  34. 34. Domain Models Persistence Foxx.Model Foxx.Repository
  35. 35. Foxx.Model Foxx.Repository • Representation of the data • Convenience Methods • Validation • Save and Retrieve Data • Simple Queries • Define your own queries
  36. 36. Why this separation? • It doesn‘t violate the SRP like ActiveRecord • In a lot of cases you can use the standard Repository or Model and don‘t need your own • It‘s great for testing • You can mock the collection and the model prototype to test your Repository • You don‘t need to mock anything to test your model
  37. 37. Foxx.Model • The constructor takes a hash of attributes • Access the attributes with get, set and has • The forDB will be used to write the data into the database • Use forClient to prepare the data for delivery
  38. 38. Foxx.Repository • The constructor takes an arangodb- collection and the prototype of the model • save for example expects an instance of the model • firstExample finds a suitable dataset and returns it as an instance of the model • Other methods: remove, replace, update…
  39. 39. You need more? • Use Foxx.Repository.extend and Foxx.Model.extend to inherit from the prototype • Add your own methods • Your extensions live in separate files
  40. 40. Foxx = require("org/arangodb/foxx");! ! MyRepository = Foxx.Repository.extend({ });! ! exports.Repository = MyRepository;
  41. 41. byName: function (name) {! return this.byExample({! name: name! });! } Foxx = require("org/arangodb/foxx");! ! MyRepository = Foxx.Repository.extend({ });! ! exports.Repository = MyRepository;
  42. 42. Thanks • Please try ArangoDB Foxx • www.arangodb.org • We to get feedback
  43. 43. Contact • I am mchacki everywhere: • mchacki@arangodb.org • @mchacki on Twitter • mchacki on Github
  44. 44. Thanks • First Slide version by Lucas Dohmen • Database icon designed by Romeo Barreto from The Noun Project • Logos from Node.js, Ruby on Rails, Django, AngularJS, Backbone, Ember and Symfony from the respective projects • All other icons are from Font Awesome

×