Presentation of the backend features provided by CompoundJS a full-featured web framework for Node.js
http://compoundjs.com/
https://github.com/1602/compound/
Talk performed @ LyonJS Meetup, April 2013
16. models and JugglingDB
ORM/ODM for Node.js
Integrated with Compound
adaptable to any data source
validators
relations
17. generator
compound g model post title content published:boolean
renders:
# db/schema.coffee
Post = describe 'Post', ->
property 'title', String
property 'content', String
property 'published', Boolean
# app/models/post.coffee
module.exports = (compound, Post) →
let's add this:
Post.published = (callback) ->
Post.all published: true, callback
18. controllers
# app/controllers/posts_controller.coffee
action 'create', ->
Post.create body (err, post) ->
if err then send 500
else send post, 201
action 'all', ->
Post.all (err, posts) ->
if err then send 500
else send posts
action 'published', ->
Post.published (err, posts) ->
if err then send 500
else send posts
19. pre-processors and post-processors
before ->
Post.find params.id, (err, post) =>
if err then send 500
else if not post then send 404
else
@post = post
next()
, only: ['show', 'update', 'destroy']
after ->
console.log 'request processed'
next()
20. That makes simpler controllers!
action 'show', ->
send @post
action 'update', ->
@post.updateAttributes body, (err) ->
if err then send 500
else send 200
action 'destroy', ->
@post.destroy ->
if err then send 500
else send 204
21. routing helpers
basic
# config/routes.coffee
map.get 'posts', 'posts#published'
map.get 'admin/posts', 'posts#all'
map.post 'admin/posts', 'posts#create'
map.get 'admin/posts/:id', 'posts#show'
map.put 'admin/posts/:id', 'posts#modify'
map.del 'admin/posts/:id', 'posts#destroy'
evolved
# generate standard CRUD routes
map.resources 'posts'
# Link directly to controller with the right name
map.all ':controller/:action'
map.all ':controller/:action/:id'