Skip to content

Instantly share code, notes, and snippets.

@nsipplswezey
Last active May 11, 2016 15:25
Show Gist options
  • Save nsipplswezey/c28acf9778287e95834788feeb4c1644 to your computer and use it in GitHub Desktop.
Save nsipplswezey/c28acf9778287e95834788feeb4c1644 to your computer and use it in GitHub Desktop.
Nodal TL;DR: Tweet Model + User Model + TL;DR = tldr-instatweet-api
//TL;DR: Nodal API server, with users joined to their tweets data
//Make sure you have postgres running. If not, I like postgresapp.com as the fastest way to get set up
//I use Postman to test API endpoints, but you can use whatever you want.
$ npm install -g nodal
$ nodal new tldr-instatweet-api
$ cd tldr-api
$ nodal s
$ nodal g:model Tweet user_id:int body:string
$ nodal g:controller v1 --for Tweets
$ nodal db:create
$ nodal db:prepare
$ nodal db:migrate
//Check your /v1/tweets endpoints with some POSTs and GETs
//Request data is expected as x-www-form-urlencoded
$ nodal g:model --user
$ nodal g:controller v1 --for Users
$ nodal db:migrate
//Check your /v1/users/ endpoint by adding some new users.
//After you join your tweets and users, check your tweets endpoint
//Add your joined table into your query composer in your tweet controller.
//Define your tweet response interface!
index() {
Tweet.query()
.join('user') // joined into Composer
.where(this.params)
.end((err, models) => {
this.respond(err || models, ['id','body','created_at', {user: ['username','email','created_at']}]); //set response interface
})
}
//Explicitly require the model you want to join
//Join your users to tweets; always bottom up. The child model joins to the parent.
const User = Nodal.require('app/models/user.js'); //require
Tweet.joinsTo(User, {multiple:true}) //join child to parent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment