27
Developing REST services with SailsJS

JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Embed Size (px)

Citation preview

Page 1: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Developing REST services with SailsJS

Page 2: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

What is SailsJS?

✘ MVC backend framework for Node.js✘ Built on top of Express✘ Inspired by Ruby on Rails / Zend✘ Convention over configuration ✘ Ideal for real time applications, games, REST

services

Page 3: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Core features

✘ Database agnostic (Waterline ORM) ✘ Front end agnostic✘ Auto generated REST API✘ Auto generated model methods✘ DB migration support ✘ Easy web socket support ✘ Flexible configuration✘ Multiple environments support ✘ Localization support✘ Still allows to use Express

Page 4: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Installation and first application

✘ npm install -g sails✘ cd to your project folder✘ sails new [path]//path is optional✘ npm install //optional depends on version✘ sails lift

Page 5: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Running application

Page 6: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Project structure

✘ api/controller – controllers✘ api/model – models✘ api/policies – policies✘ api/responses – application responses✘ api/services – reusable services✘ assets – static assets✘ config – application configuration settings✘ views – application views✘ tasks – grunt cli tasks

Page 7: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Generate API Sample

✘ Sails generate api Category ✘ What just has happened:

✗ CategoryController is created✗ Category model is created✗ Sails created blueprint for route /category

✘ Let’s run the app✘ Note: Model and controller can be generated same

way

Page 8: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Migration options

Page 9: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Models

✘ Located at: Api/models✘ Describes:

✗ DB mapping✗ Validation rules✗ Custom functions✗ And other settings like: migration strategy,

schema, primary key properties✘ Please refer to Waterline ORM documentation ✘ Note: SailsJS supports polyglot persistence layer by

allowing models to be stored in different databases

Page 10: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Supported databases✘ PostgreSQL - 0.9+ compatible✘ MySQL - 0.9+ compatible✘ MongoDB - 0.9+ compatible✘ Memory - 0.9+ compatible✘ Disk - 0.9+ compatible✘ Microsoft SQL Server✘ Redis✘ Riak✘ Neo4j✘ OrientDB✘ ArangoDB✘ Apache Cassandra✘ GraphQL✘ Solr✘ Apache Derby✘ And many others

Page 11: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

How does it work with multiple databases?

✘ It uses Waterline adapters concept✘ Waterline defines query language✘ Then each adapter knows how to:

✗ Connect to each specific DB✗ Turn query object into specific DB query✗ Read/Write data from the source

Page 12: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Model sample

Page 13: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Model blueprints

✘ following methods are available for each property:✗ findOneByPropertyName✗ findOneByPropertyNameIn✗ findOneByPropertyNameLike✗ findByPropertyName✗ findByPropertyNameIn✗ findByPropertyNameLike✗ countByPropertyName✗ countByPropertyNameIn✗ countByPropertyNameLike✗ propertyNameStartsWith✗ propertyNameEndsWith✗ propertyNameContains

Page 14: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Model blueprints

✘ find() – accepts object with query definition like {where: {name:{startsWith:”A”}}}

✘ Or it can an be called as chain: Category.find().skip(10).limit(10)

✘ Supports: ✗ Filtering✗ Paging✗ Sorting

Page 15: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Controller

✘ Full support for RESTful routes✘ Shortcut routes✘ Can have custom actions✘ Can be put in subdirs and subdir becomes part of

the route✘ Has standard methods for CRUD support which can

be overridden✘ Policies can be applied either per action or for entire

controller

Page 16: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Controller: custom action sample

Page 17: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Policies

✘ Used for authentication/authorization or like simple handlers

✘ Can be applied:✗ Globally✗ On controllers✗ On controller actions

✘ Can be integrated with Passport

Page 18: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Policy sample

Page 19: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Services

✘ Contains reusable logic across application✘ Automatically registered by Sails like Model or

Controller ✘ Sample: https://github.com/balderdashy/sails-docs/blob/0.9/services.md

Page 20: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Assets

✘ This folder is just for static content✘ Note: No need to add “/assets” in URL

Page 21: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Configuration

✘ Allows to configure: ✗ Environments✗ Locales✗ Blueprints – routing rules✗ Connection strings✗ Application bootstrap✗ Sessions✗ Sockets✗ View engine✗ Logging

Page 22: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Views

✘ View is simple HTML page✘ Multiple view engines are supported✘ Default is EJB

Page 23: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

What about realtime?

✘ Based on socket.io✘ By default notifies of any changes in any model✘ Can notify of data requested✘ Allows to write custom endpoints✘ Does have scalability option by using redis as a

pub/sub storage✘ Has transport fallback (long polling or web sockets)

Page 24: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Demo

Page 25: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Sum Up

✘ Runs on any platform✘ Front end agnostic✘ DB agnostic✘ Good for prototyping✘ Speeds up implementation✘ Does not require sails to be installed in production✘ Provides fallback to express when needed

Page 26: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

Cons

✘ Is not as fast as Express could be✘ Some adapters do not support native and effective

DB capabilities ✘ In theory switching from one DB adapter to another

go seamless in practice it’s a PAIN!✘ Mixing DB can be an issue when you do have

associations✘ Does not suit well for complex data structures✘ No transaction support

Page 27: JS Lab`16. Андрей Колодницкий: "Разработка REST сервисов на SailsJS"

THANKS!Any questions?