Created
June 17, 2016 03:41
-
-
Save nsipplswezey/2258710f3e4e7ba089ce55728987057c to your computer and use it in GitHub Desktop.
Nodal TL;DR Many-to-Many Relationship
This file contains hidden or 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
npm install -g nodal | |
nodal new many-to-many | |
cd many-to-many | |
nodal g:model User name:string | |
nodal g:model Group name:string | |
nodal g:controller v1 --for Users | |
nodal g:controller v1 --for Groups | |
nodal g:model UserGroup user_id:int group_id:int | |
nodal g:controller v1 --for UserGroup | |
nodal db:create | |
nodal db:prepare | |
nodal db:migrate | |
nodal db:seed | |
This file contains hidden or 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
/* config/seed.json */ | |
{ | |
"development": { | |
"User": [ | |
{"name": "Kevin"}, | |
{"name": "Michelle"}, | |
{"name": "Connie"}, | |
{"name": "Liam"} | |
], | |
"Group": [ | |
{"name": "deluxe-pug" }, | |
{"name": "savory-penguin"}, | |
{"name": "sabine-sardine"} | |
] | |
}, | |
"test": {}, | |
"production": {} | |
} |
This file contains hidden or 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
module.exports = (function() { | |
'use strict'; | |
const Nodal = require('nodal'); | |
const UserGroup = Nodal.require('app/models/user_group.js'); | |
const //import models | |
class V1UserGroupsController extends Nodal.Controller { | |
index() { | |
UserGroup.query() | |
.where(this.params.query) | |
.end((err, models) => { | |
this.respond(err || models); | |
}); | |
} | |
show() { | |
UserGroup.find(this.params.route.id, (err, model) => { | |
this.respond(err || model); | |
}); | |
} | |
create() { | |
let user_id; | |
let group_id; | |
let user_group; | |
const userName = { name: this.params.body.userName }; | |
const groupName = { name: this.params.body.groupName }; | |
User.findOrCreateBy("name", userName, (err, user) => { | |
if ( err ) { return this.respond(err); } // make sure your handle error | |
user_id = user.get('id'); | |
Group.findOrCreateBy("name", groupName, (err, group) => { | |
if ( err ) { return this.respond(err); } // every step!!! | |
group_id = group.get('id'); | |
user_group = { user_id, group_id }; | |
UserGroup.create(user_group, (err, userGroup) => { | |
this.respond(err || userGroup); | |
}); | |
}); | |
}); | |
} | |
update() { | |
UserGroup.update(this.params.route.id, this.params.body, (err, model) => { | |
this.respond(err || model); | |
}); | |
} | |
destroy() { | |
UserGroup.destroy(this.params.route.id, (err, model) => { | |
this.respond(err || model); | |
}); | |
} | |
} | |
return V1UserGroupsController; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment