Skip to content

Instantly share code, notes, and snippets.

@nsipplswezey
Last active May 25, 2016 02:38
Show Gist options
  • Save nsipplswezey/6bcbf537d4ee2ac9104bd7531b9c10df to your computer and use it in GitHub Desktop.
Save nsipplswezey/6bcbf537d4ee2ac9104bd7531b9c10df to your computer and use it in GitHub Desktop.
Nodal Conditional Joins TL;DR
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
// 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
});
}
// 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
// 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;
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
/**
*
* 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