-
Notifications
You must be signed in to change notification settings - Fork 56
/
relationship-many-to-many.js
51 lines (47 loc) · 1.5 KB
/
relationship-many-to-many.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
});
});
});
});
});
});
});