Created
September 8, 2016 22:33
-
-
Save dearfrankg/ef551bf482593e3c8f798f20185aa0c1 to your computer and use it in GitHub Desktop.
loopback-getting-started-intermediate create sample data
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
| var async = require('async'); | |
| module.exports = function(app) { | |
| //data sources | |
| var dsMongoDB = app.dataSources.dsMongoDB; | |
| //create all models | |
| async.parallel({ | |
| reviewers: async.apply(createReviewers), | |
| coffeeShops: async.apply(createCoffeeShops), | |
| }, function(err, results) { | |
| if (err) throw err; | |
| createReviews(results.reviewers, results.coffeeShops, function(err) { | |
| console.log('> models created sucessfully'); | |
| }); | |
| }); | |
| //create reviewers | |
| function createReviewers(cb) { | |
| dsMongoDB.automigrate('Reviewer', function(err) { | |
| if (err) return cb(err); | |
| var Reviewer = app.models.Reviewer; | |
| Reviewer.create([ | |
| {email: '[email protected]', password: 'foobar'}, | |
| {email: '[email protected]', password: 'johndoe'}, | |
| {email: '[email protected]', password: 'janedoe'} | |
| ], cb); | |
| }); | |
| } | |
| //create coffee shops | |
| function createCoffeeShops(cb) { | |
| dsMongoDB.automigrate('CoffeeShop', function(err) { | |
| if (err) return cb(err); | |
| var CoffeeShop = app.models.CoffeeShop; | |
| CoffeeShop.create([ | |
| {name: 'Bel Cafe', city: 'Vancouver'}, | |
| {name: 'Three Bees Coffee House', city: 'San Mateo'}, | |
| {name: 'Caffe Artigiano', city: 'Vancouver'}, | |
| ], cb); | |
| }); | |
| } | |
| //create reviews | |
| function createReviews(reviewers, coffeeShops, cb) { | |
| dsMongoDB.automigrate('Review', function(err) { | |
| if (err) return cb(err); | |
| var Review = app.models.Review; | |
| var DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24; | |
| Review.create([ | |
| { | |
| date: Date.now() - (DAY_IN_MILLISECONDS * 4), | |
| rating: 5, | |
| comments: 'A very good coffee shop.', | |
| publisherId: reviewers[0].id, | |
| coffeeShopId: coffeeShops[0].id, | |
| }, | |
| { | |
| date: Date.now() - (DAY_IN_MILLISECONDS * 3), | |
| rating: 5, | |
| comments: 'Quite pleasant.', | |
| publisherId: reviewers[1].id, | |
| coffeeShopId: coffeeShops[0].id, | |
| }, | |
| { | |
| date: Date.now() - (DAY_IN_MILLISECONDS * 2), | |
| rating: 4, | |
| comments: 'It was ok.', | |
| publisherId: reviewers[1].id, | |
| coffeeShopId: coffeeShops[1].id, | |
| }, | |
| { | |
| date: Date.now() - (DAY_IN_MILLISECONDS), | |
| rating: 4, | |
| comments: 'I go here everyday.', | |
| publisherId: reviewers[2].id, | |
| coffeeShopId: coffeeShops[2].id, | |
| } | |
| ], cb); | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment