Intro to Sails.js

49
sails.js realtime MVC framework for Node.js

description

Presented at DevOpsDays Austin 2014 by Mike McNeil (http://www.slideshare.net/michaelrmcneil).

Transcript of Intro to Sails.js

Page 1: Intro to Sails.js

sails.jsrealtime MVC framework for Node.js

Page 2: Intro to Sails.js

who am i?@mikermcneil

Page 3: Intro to Sails.js

We design and develop javascript apps for

enterprise and startup customers.

i have a startup called balderdash

Page 4: Intro to Sails.js

love at !rst sightcirca 2011

Node.js...

Page 5: Intro to Sails.js

node.js translates server concepts into javascript-- the lingua franca for the every-man

our event loop is faster than your thing

native bindings means Node can do pretty much anything

core is purposefully feature-sparse

npm publish, nuff said (lower barrier to entry)

••••

Page 6: Intro to Sails.js

route&first*

philosophy

opt&in*framework*structure

“smalltalk*ethics”

UML

“javascript*ethics”

servlets

mvc

spring,*et.*al.

rails

enterprise*java

ruby

modulesystem(npm)

noSQL(mongo/redis)

async*flow*

control

from*the*front&end:generators(yeoman)

node.js

convention*over*

configuration

event*loop

ORM

Page 7: Intro to Sails.js

Can we use Node.js for, like, everything?

i wondered...

Page 8: Intro to Sails.js

Can we use Node.js for, like, everything?

i wondered...

maybe.

Page 9: Intro to Sails.js

Realtime used to be kind of hard

XMPPComet

Reverse AJAXLong polling

Flash socketsWebSockets

Server-sent events

Page 10: Intro to Sails.js

A lot easier now

Page 11: Intro to Sails.js

Programming realtime apps

was still not trivial.

WebSocket and HTTP messages/requests have to be handled independently, which leads to separate code bases for realtime and traditional server code.

Page 12: Intro to Sails.js

Programming realtime apps

was still not trivial.

WebSocket and HTTP messages/requests have to be handled independently, which leads to separate code bases for realtime and traditional server code.

Socket programming is a new paradigm for many modern web developers

Page 13: Intro to Sails.js

WebSocket and HTTP messages/requests have to be handled independently, which leads to separate code bases for realtime and traditional server code.

Socket programming is a new paradigm for many modern web developers

Questions about scalability

Programming realtime apps

was still not trivial.

Page 14: Intro to Sails.js

Express wasn’t “structured

enough”

Authentication has to be rolled from scratch

Page 15: Intro to Sails.js

Express wasn’t “structured

enough”

Authentication has to be rolled from scratch

No standard, implementation-agnostic method of working with datastores

Page 16: Intro to Sails.js

Express wasn’t “structured

enough”

Authentication has to be rolled from scratch

No standard, implementation-agnostic method of working with datastores

Low level and free-form -- not a lot of guidelines for teams used to convention-over-con!guration frameworks

Page 17: Intro to Sails.js

Lotsof different

kinds of projects

There weren’t really any simple and reproducible patterns for structuring complicated Node.js aplications top to bottom

Page 18: Intro to Sails.js

Lotsof different

kinds of projects

There weren’t really any simple and reproducible patterns for structuring complicated Node.js aplications top to bottom

Could be working with an existing client, or writing a new client for a mobile web browser, an automobile, a toaster, or god knows what

Page 19: Intro to Sails.js

Lotsof different

kinds of projects

There weren’t really any simple and reproducible patterns for structuring complicated Node.js aplications top to bottom

Could be working with an existing client, or writing a new client for a mobile web browser, an automobile, a toaster, or god knows what

Lots of different 3rd party proprietary services to deal with on the back-end -- there was no standard way to integrate new systems

Page 20: Intro to Sails.js

too bad.we went with Node anyway.

Page 21: Intro to Sails.js

so Sails.js was born

Page 22: Intro to Sails.js

MVC structureSails.js is modeled after the same convention-over-con!guration philosophy you’re used to from frameworks like Rails, Grails, Symfony, and Zend.

Controller actions are just Express middleware

Views are ejs by default, but you can use jade, etc. We rarely use them since we’re normally making single page apps with client-side templates.

Page 23: Intro to Sails.js

Lightweight ORM

Kept querying semantics dead-simple and adapter-agnostic whenever possible

Pulled the best of Active Record, Hibernate, and Mongoose

Made it easy to add purpose-built adapters at the app level

Page 24: Intro to Sails.js

PoliciesPolicies are just more Express middleware

They can be chained together to “protect” or preprocess requests for controllers

E.g. access control, storage quotas, or anything else you’d want to use middleware for

Page 25: Intro to Sails.js

Socket.io Request interpreter

Translated incoming socket.io messages into Express requests

Translated res.send(), res.json(), and res.view() to respond via the socket, as well as allowing for streams

Added res.broadcast() and req.join() methods for pubsub

Normalized con!guration

Page 26: Intro to Sails.js

Blueprints

Instead of something like Rails HTML scaffolds, by default, when you generate a model and controller, Sails serves an API

Built-in search, sort, pagination, and complex queries

Authentication/access control can be built on top using policies

Page 27: Intro to Sails.js

Other cool stuff

CLI tool

REPL

Custom adapters

Optional server-side coffee support

Automatic asset bundling (LESS and coffeescript !les are compiled, merged with css and js, and injected into the DOM, mini!ed in production mode)Uses grunt by default, but can be replaced by ant, gulp, maven, etc.

Page 28: Intro to Sails.js

associations• should upgrade not only Waterline

(ORM), but also RESTful blueprintsand model-focused publish/subscribe (pubsub) methods.

• cross-adapter

• cross-connection

• efficient as possible, allowing moreefficient solutions to be implementedat the adapter layer

Sturday, Jauary 25, 4

Page 29: Intro to Sails.js

why mess with this?

• NoSQL

• simplicity

• performance

• traditional SQL databases

• built-in ACID

• reporting queries

Page 30: Intro to Sails.js

cross-adapter

• MySQL tables

• MongoDB collections

• PostgreSQL tables

• Twitter API REST resources

• etc.

Page 31: Intro to Sails.js

cross-connection

• different hosts (servers)

• different database users

• nasty/annoying integrations w/legacy data

e.g. join two different MySQL databases with incompatible schemas

Page 32: Intro to Sails.js

how it works

• when possible, use nativeoptimizations (i.e. joins) at thedatabase layer

• in-memory joins for the rest, justlike you would have to do in code

• maintains multiple connection pools

Page 33: Intro to Sails.js

impact on adapters

• Existing Sails adapters (and anynew ones you write) now supportassociations out of the box,without writing any additionalcode.

• You can add a `join()` methodthat will be used instead of`find()` for running intra-database associations queries.

Page 34: Intro to Sails.js

let’s code things now

OK

Page 35: Intro to Sails.js

CURRENT STATUS (V0.9.13)

Community is flourishing (over 6000 stars / 600 forks on github, active IRC channel and

Google group)

Page 36: Intro to Sails.js

Community is flourishing (over 6000 stars /

600 forks on github, active IRC channel and

Google group)

My company and most of our customers are using Sails in production

CURRENT STATUS (V0.9.13)

Page 37: Intro to Sails.js

My company and most of our customers are using Sails in production

Oldest running production Sails apps havs been up for over 2 years

CURRENT STATUS (V0.9.13)

Community is flourishing (over 6000 stars /

600 forks on github, active IRC channel and

Google group)

Page 38: Intro to Sails.js

extending sails• adapter interface: standards for how our

servers talk to databases and web services

• blueprint interface: standards for how ourservers offer up their own API-- start withconventional setup, then jump into writingcustom code later.

generators: plugins that allow sails new and sails generate output to be completely overridden and/or extended

• hooks: plugins that allow componentsof the Sails runtime to be overridden orextended

Page 39: Intro to Sails.js

adapters• sails-mysql (npm$install$sails*mysql)

• sails-redis (npm$install$sails*redis)

• sails-mongo (npm$install$sails*mongo)

• sails-postgresql (npm$install$sails*postgresql)

• sails-rest (npm$install$sails*rest)

• sails-couch (npm$install$sails*couch)

• etc.

Page 40: Intro to Sails.js

blueprints

• generic impl. for HTTP/ socket APIs

• prioritizes usability / practicality over elegance

• declarative configuration(e.g. ember expects a certain format, orangular prefers a certain JSONP parameter)

• overridable(can replace logic completely withimperative code at a moment’s notice)

Page 41: Intro to Sails.js

generators• sails-generate-new

npm$install$sails*generate*newnpm$install$sails*generate*new*coffeenpm$install$sails*generate*new*but*like*expressnpm$install$sails*generate*new*blog

• sails-generate-backendnpm$install$sails*generate*backend*coffeenpm$install$sails*generate*new*jade

• sails-generate-frontend

• sails-generate-gruntfile

• sails-generate-controllernpm$install$sails*generate*controller*coffee

• sails-generate-auth

• etc.

Page 42: Intro to Sails.js

hooks• http

• sockets

• orm

• cors

• csrf

• spdy

• etc.

Page 43: Intro to Sails.js

Want to get involved?

Contribute to an adapter

Page 44: Intro to Sails.js

Want to get involved?

Contribute to an adapter

improve the documentation

Page 45: Intro to Sails.js

Want to get involved?

Contribute to an adapter

improve the documentation

Write tutorials

Page 46: Intro to Sails.js

Want to get involved?

Contribute to an adapter

Improve the documentation

Write tutorials

Blog about Sails

Page 47: Intro to Sails.js

Contribute to an adapter

improve the documentation

Write tutorials

Blog about Sails

Build an integration with your product or API (it's pretty easy)

Want to get involved?

Page 48: Intro to Sails.js

Questions?

Page 49: Intro to Sails.js

Thanks!@mikermcneil

[email protected]