Last active
May 25, 2016 02:38
-
-
Save nsipplswezey/6bcbf537d4ee2ac9104bd7531b9c10df to your computer and use it in GitHub Desktop.
Nodal Conditional Joins TL;DR
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
nodal new conditional-joins | |
nodal g:model Tweet user_id:int body:string | |
nodal g:controller --for Tweets | |
nodal g:model --user | |
nodal g:controller --for Users | |
nodal db:create | |
nodal db:prepare | |
nodal db:migrate | |
nodal s |
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
// users_controller.js | |
index() { | |
User.query() | |
.where({tweets__body__icontains: 'Minecraft'}) // add me | |
.join('tweets', {body__icontains: 'Minecraft'}) // add me | |
.end((err, models) => { | |
this.respond(err || models, ['tweets']); // add me | |
}); | |
} |
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
// models/user.js | |
'use strict'; | |
const Nodal = require('nodal'); | |
const Tweet = Nodal.require('app/models/tweet.js'); // import me | |
User.setDatabase(Nodal.require('db/main.js')); | |
User.setSchema(Nodal.my.Schema.models.User); | |
User.joinedBy(Tweet); // add me |
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
// models/tweet.js | |
'use strict'; | |
const Nodal = require('nodal'); | |
const User = Nodal.require('app/models/user.js'); // import me | |
class Tweet extends Nodal.Model {} | |
Tweet.setDatabase(Nodal.require('db/main.js')); | |
Tweet.setSchema(Nodal.my.Schema.models.Tweet); | |
Tweet.joinsTo(User, {multiple: true}) // add me | |
return Tweet; |
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
POST /users username:nodalisawesome password:password email:nodal@isawesome.com | |
POST /tweets user_id:1 body:some other text | |
POST /tweets user_id:1 body:minecraft | |
POST /tweets user_id:1 body:Minecraft | |
GET /users |
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
/** | |
* | |
* You should be receiving all the tweets that contain Minecraft | |
* | |
* */ | |
{ | |
"meta": { | |
"total": 1, | |
"count": 1, | |
"offset": 0, | |
"error": null | |
}, | |
"data": [ | |
{ | |
"tweets": [ | |
{ | |
"id": 2, | |
"user_id": 1, | |
"body": "minecraft", | |
"created_at": "2016-05-22T02:25:47.979Z", | |
"updated_at": "2016-05-22T02:25:47.979Z" | |
}, | |
{ | |
"id": 3, | |
"user_id": 1, | |
"body": "Minecraft", | |
"created_at": "2016-05-22T02:25:52.454Z", | |
"updated_at": "2016-05-22T02:25:52.454Z" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment