Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Basejump

Ryan Paul
March 14, 2016

Introduction to Basejump

Build REST APIs on RethinkDB without writing code

Ryan Paul

March 14, 2016
Tweet

More Decks by Ryan Paul

Other Decks in Programming

Transcript

  1. Basejump
    Build a REST API
    on RethinkDB

    View Slide

  2. Ryan Paul
    RethinkDB
    Evangelist
    @segphault

    View Slide

  3. API Anatomy
    Dissecting an API backend

    View Slide

  4. API Anatomy
    • Server to parse & handle requests
    • Routing to manage endpoints
    • Validation to sanitize user input
    • Persistence to store & retrieve data

    View Slide

  5. API Anatomy
    RethinkDB NodeJS
    JOI
    Koa Vue
    Browser
    REST API
    Koa Router W3C Fetch
    Real-world RethinkDB stack

    View Slide

  6. API Anatomy
    Persistence Runtime
    Validation
    Server Frontend
    Framework
    Browser
    REST API
    Router REST Client
    Real-world RethinkDB stack

    View Slide

  7. API Anatomy
    • Lots of layers between persistence
    and requests
    • More boilerplate than business
    logic
    • Repetitive code that is difficult to
    reuse

    View Slide

  8. API Anatomy
    router.post("/api/event/:id/messages", function*(id) {
    this.checkBody("message").notEmpty();
    if (this.errors)
    this.throw(400, {success: false, errors: this.errors});
    yield r.table("messages").insert({
    created: r.now(),
    text: this.request.body.message,
    sender: sender,
    event: id});
    this.body = {success: true};
    });

    View Slide

  9. Can we abstract away
    the boilerplate?

    View Slide

  10. Project Basejump
    Attach queries to endpoints

    View Slide

  11. Basejump
    • Visual tool for prototyping API
    backends
    • Dynamic routing middleware for
    attaching queries to endpoints
    • Powered by declarative JSON
    description format

    View Slide

  12. Basejump

    View Slide

  13. {A} 1ST

    View Slide

  14. Basejump Router
    • Database agnostic routing middleware
    • Uses Node’s VM module to execute code
    • Integrates seamlessly with Koa and
    Socket.io
    • Swagger-based format to describe
    routes and actions

    View Slide

  15. Swagger

    View Slide

  16. Swagger
    • Standard format for describing
    APIs — can use YAML or JSON
    • Uses JSON Schema for validation
    • Supported by many tools &
    frameworks
    • Extensible via vendor properties

    View Slide

  17. Swagger
    paths:
    /test:
    get:
    parameters:
    - {in: query, name: count, type: number}
    x-action: >
    r.table("test")
    .orderBy(r.desc("time"))
    .limit(params.count)
    post:
    parameters:
    - in: body
    name: body
    schema:
    $ref: "#/definitions/test"
    x-action: >
    r.table("test").insert(params.body)

    View Slide

  18. Embed Basejump
    • Embed in your own Node app
    • Pass custom objects into the execution
    context
    • Inherit and override the request handler
    • Custom pre & post processing on input
    and output

    View Slide

  19. Embed Basejump
    const app = require("koa")();
    const router = require("basejump-router");
    const r = require("rethinkdbdash")();
    app.use(require("kcors")());
    app.use(require("koa-bodyparser")());
    app.use(router.middleware.koa({
    swagger: "routes.yaml",
    context: {r: r}
    }));
    app.listen(8000);

    View Slide

  20. Project Basejump
    Status & Future Plans

    View Slide

  21. Basejump
    • Not ready for adoption quite yet
    • API and data format still subject
    to change
    • New features might necessitate
    additional format churn

    View Slide

  22. Feature Roadmap
    • Support for embedding in Express
    • Interactive Schema editing
    • Pluggable view system
    • Route collections and blueprints
    • Authentication and user management
    • Live API debugging

    View Slide

  23. Feature Ideas
    • Generate client libraries for mobile & web
    • Use schemas to generate input forms &
    data dashboard
    • Support for file uploads & multipart
    mime
    • Automatically generate tables and
    indexes from schemas

    View Slide

  24. How does it
    compare to Fusion?

    View Slide

  25. How does it
    compare to Fusion?
    Horizon?

    View Slide

  26. Basejump vs Horizon
    • Basejump: backend-first
    development & REST APIs
    • Horizon: frontend-first
    development & WebSockets

    View Slide

  27. Basejump & Horizon
    • Embed Basejump and Horizon
    middleware in the same Node app
    • Expose Basejump routes through
    Horizon custom endpoint
    • Converge authentication and
    schema validation?

    View Slide

  28. • RethinkDB website:

    http://rethinkdb.com
    • Basejump Router:

    http://github.com/segphault/
    basejump-router
    • Follow me on Twitter:

    @segphault
    Resources

    View Slide