Name: loopback-jeopardy-mongo-bluemix
Owner: StrongLoop Evangelists
Description: an example application of a Jeopardy question API with Loopback and MongoDB
Created: 2017-05-25 17:55:05.0
Updated: 2018-03-03 22:05:15.0
Pushed: 2017-09-12 20:51:49.0
Homepage: null
Size: 2663
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
You will need:
`npm install -g loopback-cli`
The Jeopardy! dataset (216,930 Jeopardy! questions in JSON format) is available here. Thanks so much to the great Data is Plural tinyletter for finding and sharing so many interesting datasets!
db.jeopardy.find().forEach(function(doc){doc.air_date = new ISODate(doc.air_date);db.jeopardy.save(doc)})
Start Mongo locally and load your data into your local MongoDB. (Your process will vary, but you could use something like mongoimport --db test --collection jeopardy --file JEOPARDY_QUESTIONS.txt
To create your LoopBack application, use
lb app jeopardy-mongo-api
Wait for npm install
to finish, then cd jeopardy-mongo-api
lb datasource
Name your datasource and choose the LoopBack MongoDB connector:
If you use a URL for your connector you don't need to specify the host/port/user/password:
Wait for npm install
of the connector to finish.
lb model
The schema for the Jeopardy! questions is
"_id" : ObjectId("5922063e8f51a16a884263bf"),
"category" : "HISTORY",
"air_date" : "2004-12-31",
"question" : "'For the last 8 years of his life, Galileo was under house arrest for espousing this man's theory'",
"value" : "$200",
"answer" : "Copernicus",
"round" : "Jeopardy!",
"show_number" : "4680"
Name your model and connect it to the datasource you just created. You probably won't be saving new questions to the database but you can just go ahead and select PersistedModel just in case:
Expose your model through the API and make it 'common' (available to both the server and client). We're only creating a server in this example but there's no harm in choosing 'common'.
You don't need to create a property specifically for '_id'; LoopBack will do it for you:
node .
…
…
A. Get a random question:
In the common/models/jeopardy-question.js
file, add the endpoint functionality:
ardyquestion.random = function(cb){
Jeopardyquestion.getDataSource().connector.connect(function(err, db) {
var collection = db.collection('jeopardyQuestion');
collection.aggregate([
{ $sample: { size: 1 } }
], function(err, data) {
if (err) return cb(err);
return cb(null, data);
});
});
}
and the endpoint description:
Jeopardyquestion.remoteMethod(
'random', {
http: {
path: '/random',
verb: 'get'
},
description: 'Gets one random question',
returns: {
arg: 'questions',
type: 'json'
}
});
B. Get the entire list of Jeopardy! categories:
In the common/models/jeopardy-question.js
file, add the endpoint functionality:
ardyquestion.categories = function(cb){
Jeopardyquestion.getDataSource().connector.connect(function(err, db) {
var collection = db.collection('jeopardyQuestion');
collection.distinct('category', function(err, data) {
if (err) return cb(err);
return cb(null, data);
});
});
}
and the endpoint description:
Jeopardyquestion.remoteMethod(
'categories', {
http: {
path: '/categories',
verb: 'get'
},
description: 'Gets list of categories',
returns: {
arg: 'categories',
type: 'json'
}
});
Restart your application, and you should now see these endpoints in your Explorer:
manifest.yml
file in your downloaded starter code and copy that file to your LoopBack application.
MONGODB_CONNECTION_URL
and ca_certificate_base64
cf set-env $YOUR_APP_NAME MONGODB_CONNECTION_URL $YOUR_MONGO_URI
cf set-env $YOUR_APP_NAME ca_certificate_base64 $YOUR_CA_CERTIFICATE
(Replace the $YOUR_APP_NAME and $YOUR_MONGO_URI and $YOUR_CA_CERTIFICATE` with your Bluemix app name, your “ca_certificate_base64” the “uri” values from your credentials.server/datasources.production.json
file, with this content:
go":{
e": "mongo",
": "${MONGODB_CONNECTION_URL}",
nector":"mongodb",
":true,
ver": {
"auto_reconnect": true,
"reconnectTries": 100,
"reconnectInterval": 1000,
"sslValidate":false,
"checkServerIdentity":false,
"sslCA":"${ca_certificate_base64}"
`json "engines": {
"node": ">=6.9.1"
},
and
“cfenv”: “1.0.x”,
to your dependencies in your
package.json`.` cf push $YOUR_APP_NAME
`NOTE: at this point you won't have any data in the Compose for MongoDB instance that is attached to your Bluemix LoopBack app. You can load data into your Compose for MongoDB in a couple of different ways. One quick way is to do a `mongodump
from your local MongoDB, and then ``
mongorestore``` it to your Compose for MongoDB instance, like so: