Slide 1

Slide 1 text

Basejump Build a REST API on RethinkDB

Slide 2

Slide 2 text

Ryan Paul RethinkDB Evangelist @segphault

Slide 3

Slide 3 text

API Anatomy Dissecting an API backend

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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}; });

Slide 9

Slide 9 text

Can we abstract away the boilerplate?

Slide 10

Slide 10 text

Project Basejump Attach queries to endpoints

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Basejump

Slide 13

Slide 13 text

{A} 1ST

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Swagger

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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);

Slide 20

Slide 20 text

Project Basejump Status & Future Plans

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

How does it compare to Fusion?

Slide 25

Slide 25 text

How does it compare to Fusion? Horizon?

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

• RethinkDB website:
 http://rethinkdb.com • Basejump Router:
 http://github.com/segphault/ basejump-router • Follow me on Twitter:
 @segphault Resources