Created
May 14, 2016 22:15
-
-
Save nsipplswezey/aeb08c8d696a496d8eb4be2f20a0c7c2 to your computer and use it in GitHub Desktop.
TL;DR: nodal-react-splash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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