Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[examples] Added many-to-many code example.

  • Loading branch information...
commit 509239b91435de6061499c9f372c2f30aed204da 1 parent 93f541c
@Marak Marak authored
Showing with 51 additions and 0 deletions.
  1. +51 −0 examples/relationship-many-to-many.js
View
51 examples/relationship-many-to-many.js
@@ -0,0 +1,51 @@
+var resourceful = require('../lib/resourceful');
+resourceful.use('memory', { database: "many" });
+
+//
+// Define the resources
+//
+var Artist = resourceful.define('artist'),
+ Album = resourceful.define('album'),
+ Song = resourceful.define('song');
+
+//
+// Now, define the relationships of every resource
+//
+Album.parent('artist'); // Artists can have many Albums
+Artist.parent('album'); // Albums can have many featured Artists
+Artist.parent('song'); // Songs can have many Artists
+Song.parent('artist'); // Artists can have many Songs
+Song.parent('album'); // Albums can have many Songs
+
+//
+// Now create a few new resources that are related
+//
+// Remark: There are several ways you can now use the resourceful API
+// to interact with your relational resources.
+// The following code is just an example
+//
+
+Artist.create({
+ id: 'Beastie Boys'
+}, function(err, result){
+ Artist.create({
+ id: 'Q-Tip'
+ }, function(err, result){
+ Album.create({
+ id: 'Ill Communication'
+ }, function(err, album){
+ album.createArtist({ id: "Q-Tip"}, function(err, result){
+ console.log(err, result);
+ album.createSong({ id: "Get it together"}, function(err, song){
+ console.log(err, result);
+ album.createArtist({ id: "Beastie Boys"}, function(err, result){
+ console.log(err, result);
+ Album.get({ id: 'Ill Communication' }, function(err, result){
+ console.log(err, result);
+ });
+ });
+ });
+ });
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.