Last active
February 11, 2017 06:43
-
-
Save nuklehed/76dbe7eafb1fd47eb1d1 to your computer and use it in GitHub Desktop.
Sails.js/MySQL example using the sails-mysql adapter
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
/** | |
* Location: /config/connections.js | |
**/ | |
module.exports.connections = { | |
mysql: { | |
adapter: 'sails-mysql', | |
host: 'localhost', | |
user: 'myuser', | |
password: 'xxxxxxxx', | |
database: 'sandbox', | |
port: 3306 | |
} | |
}; |
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
/** | |
* Location: /api/models/Game.js | |
* | |
* Virtual fields: | |
* - sponsor: Games have a one to one relationship with Sponsors | |
* - guesses: Games have a one to many relationship with Guesses | |
* | |
**/ | |
module.exports = { | |
attributes: { | |
gameId: { | |
type: 'integer', | |
primaryKey: true, | |
unique: true | |
}, | |
gameTitle: { | |
type: 'string', | |
required: true | |
}, | |
gameDesc: { | |
type: 'string', | |
required: true | |
}, | |
isActive: { | |
type: 'boolean' | |
}, | |
isPremium: { | |
type: 'boolean' | |
}, | |
gameImage: { | |
type: 'string' | |
}, | |
thumbImage: { | |
type: 'string' | |
}, | |
startDate: { | |
type: 'datetime' | |
}, | |
endDate: { | |
type: 'datetime' | |
}, | |
createdAt: { | |
type: 'datetime' | |
}, | |
sponsor: { | |
model: 'Sponsor', | |
via: 'sponsorId', | |
columnName: 'sponsorId' | |
}, | |
guesses: { | |
collection: 'Guess', | |
via: 'gameId' | |
} | |
} | |
}; |
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
/** | |
* Location: /api/models/Guess.js | |
* | |
* Virtual fields: | |
* - gameId: each game has 0 to many guesses | |
* | |
**/ | |
module.exports = { | |
attributes: { | |
guessId: { | |
type: 'integer', | |
primaryKey: true, | |
unique: true | |
}, | |
guessDesc: { | |
type: 'string', | |
required: true | |
}, | |
gameId: { | |
model: 'Game' | |
} | |
} | |
}; |
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
/** | |
* Location: /api/controllers/HomeController.js | |
* | |
* Ignore the route since it's merely here as a means to an end. We just want to get | |
* MySQL calls working and browsing to http://localhost:1337/hi will test it. | |
* | |
* Output: | |
* [ { guesses: | |
* [ { guessId: 1, gameId: 1, guessDesc: 'dog' }, | |
* { guessId: 2, gameId: 1, guessDesc: 'cat' }, | |
* { guessId: 3, gameId: 1, guessDesc: 'bird' } ], | |
* sponsor: { sponsorId: 1, sponsorDesc: 'Costco' }, | |
* gameId: 1, | |
* gameTitle: '$25 Costco Gift Card', | |
* gameDesc: 'Some fine words about Costco go here.', | |
* isActive: true, | |
* isPremium: false, | |
* gameImage: '/img/game/0001/game.png', | |
* thumbImage: '/img/game/0001/thumb.png', | |
* startDate: null, | |
* endDate: null, | |
* createdAt: null, | |
* updatedAt: null } ] | |
**/ | |
module.exports = { | |
hi: function (req, res) { | |
Game.find().populate('sponsor').populate('guesses').exec(function (err, games) { | |
if (err) console.log(err); | |
console.log(games); | |
return res.send("Hi there!"); | |
}); | |
}, | |
bye: function (req, res) { | |
return res.redirect("http://www.sayonara.com"); | |
} | |
}; |
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
/** | |
* Location: /api/models/Sponsor.js | |
* | |
* Virtual fields: | |
* - gameId: each game has 0 to many guesses | |
* | |
**/ | |
module.exports = { | |
autoCreatedAt: false, | |
autoUpdatedAt: false, | |
attributes: { | |
sponsorId: { | |
type: 'integer', | |
primaryKey: true, | |
unique: true | |
}, | |
sponsorDesc: { | |
type: 'string', | |
required: true | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found it difficult to find a good example that included some complications like a table with multiple foreign keys. This is part a sandbox project I created to explore how it would all work.