$30 off During Our Annual Pro Sale. View Details »

Customizing Loopback - Integrating More Modules

David Okun
February 21, 2017

Customizing Loopback - Integrating More Modules

Loopback is a great tool that allows you to generate an Express.js API from the command line. In this talk, I show you how you can take advantage of the Node.js ecosystem and integrate other modules into a Loopback API as business logic.

David Okun

February 21, 2017
Tweet

More Decks by David Okun

Other Decks in Programming

Transcript

  1. Customizing Loopback
    – Integrating More
    Modules
    David Okun, Developer Evangelist, Strongloop & Swift@IBM
    @dokun24
    February 21, 2017

    View Slide

  2. What is a Loopback?
    • THE easiest & fastest
    way to set up a
    RESTful web API.
    • Uses Express, Node,
    and Yeoman.
    • Get an API up with
    writing zero code.

    View Slide

  3. Extending Your API – The Scenario
    • We have two immutable microservices.
    • We want to combine their output and cache it
    locally.

    View Slide

  4. Aren’t Microservices Adorable?

    View Slide

  5. Today’s Modules
    • request
    • https://npmjs.com/package/request

    View Slide

  6. Today’s Modules
    • request-promise & bluebird
    • https://npmjs.com/package/request-
    promise
    • https://npmjs.com/package/bluebird

    View Slide

  7. Setup
    • Node.js & npm (I’m running 7.4.0 and 4.0.5)
    • npm install –g loopback
    • npm install –g strongloop

    View Slide

  8. Our Microservices

    View Slide

  9. Today’s Demo
    • The finished product can be found at:
    • https://github.com/dokun1/LoopbackModulesDemo

    View Slide

  10. Our Microservices – Up And Running

    View Slide

  11. Loopback!

    View Slide

  12. In-Memory Datasource

    View Slide

  13. Setting up our Animal object

    View Slide

  14. Model…

    View Slide

  15. Model…

    View Slide

  16. Model…

    View Slide

  17. Model…

    View Slide

  18. Sanity Check

    View Slide

  19. Sanity Check

    View Slide

  20. Remote Methods
    • A remote method is a method of a model.
    • Can be made with lb remote-method.

    View Slide

  21. Animal.js

    View Slide

  22. Remote Hooks
    • A remote hook is a function that can be
    executed before or after a remote method is
    called.
    • beforeRemote(), afterRemote(),
    afterRemoteError().

    View Slide

  23. Animal.js

    View Slide

  24. Operation Hooks
    • An operation hook is a function triggered on a
    higher-order function of a PersistedModel
    object.

    View Slide

  25. Operation Hooks (cont.)

    View Slide

  26. Animal.js

    View Slide

  27. Animal.js

    View Slide

  28. Animal.js

    View Slide

  29. And we’re clear!!

    View Slide

  30. But actually…
    • We aren’t validating the entries we get, so we
    always get duplicates.

    View Slide

  31. Animal.js

    View Slide

  32. Animal.js

    View Slide

  33. Animal.js

    View Slide

  34. Animal.js

    View Slide

  35. And we’re clear!!

    View Slide

  36. Bonus Points: Validators
    • Model objects can use a set of common
    validators for convenience.
    • Only supported with in-memory, Oracle, and
    MongoDB.

    View Slide

  37. Animal.js

    View Slide

  38. Wrapping up
    • Loopback isn’t just for POCs – rather than
    limiting you to generated code, it supports
    what you want to build.
    • It gets out of the way when you need it to.

    View Slide

  39. Resources
    • https://loopback.io
    • Adding logic: https://loopback.io/doc/en/lb3/Adding-application-logic.html
    • Demo Repo: https://github.com/dokun1/LoopbackModulesDemo
    • Stack Overflow: http://stackoverflow.com/questions/tagged/loopback
    • Me on Twitter: @dokun24

    View Slide

  40. Thank You!!!

    View Slide