Seeding Data for a StrongLoop App
Automatically restarting a node.js application when a file changes speeds up development but means lots of restarts. This article shows how to re-seed data into a test application when using StrongLoop.
Join the DZone community and get the full member experience.
Join For Freeplease read! a few hours after posting this, a member of the strongloop team pointed out an alternative that did exactly what i wanted to accomplish in about one second of typing. i still think the core of this blog entry makes sense as is so i’m not editing it, but see the note at the bottom!
this is just a quick post as a followup to something i mentioned in my post the other day on building a blog with strongloop . i mentioned that while working on my application, i kept losing my temporary data as i was using the “in memory” datasource that is the default persistence method for data. that’s not a bug—in memory means exactly that—in memory—and as i restarted the app (using nodemon ), i had to re-enter fake data to test.
while it takes all of three minutes to connect your app to mongo, if you don’t have mongo (or mysql, or a db in general), it would be nice to be able to stick with the simple ram based system while prototyping.
one of the things i realized is that strongloop will run a set of scripts inside the boot directory on startup. in theory, that could be used to set some seed data. jordan kasper (evangelist for strongloop, which sounds like a fun job, ahem) shared this script with me as an example:
var async = require('async');
var mysqldatasourcename = 'mysql_dev';
var mongodatasourcename = 'mongodb_dev';
module.exports = function(app) {
//data sources
var mongods = app.datasources[mongodatasourcename];
var mysqlds = app.datasources[mysqldatasourcename];
//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) {
if (err) throw err;
console.log('> models created sucessfully');
});
});
//create reviewers
function createreviewers(cb) {
mongods.automigrate('reviewer', function(err) {
if (err) return cb(err);
var reviewer = app.models.reviewer;
reviewer.create([
{email: 'foo@bar.com', password: 'foobar'},
{email: 'john@doe.com', password: 'johndoe'},
{email: 'jane@doe.com', password: 'janedoe'}
], cb);
});
}
//create coffee shops
function createcoffeeshops(cb) {
mysqlds.automigrate('coffeeshop', function(err) {
if (err) return cb(err);
var coffeeshop = app.models.coffeeshop;
var shops = [
{name: 'bel cafe',openinghour:10, closinghour:18},
{name: 'three bees coffee house',openinghour:6, closinghour:15},
{name: 'caffe artigiano',openinghour:17, closinghour:24},
];
//add city if it's in the model
if(coffeeshop.definition.properties.hasownproperty('city')){
var cities = ['vancouver', 'san mateo'];
shops.foreach(function(shop, idx){
shop.city = cities[idx%2];
});
}
coffeeshop.create(shops, cb);
});
}
//create reviews
function createreviews(reviewers, coffeeshops, cb) {
mongods.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);
});
}
};
i’m still new to strongloop and loopback in general, but this makes sense. my needs were far simpler, so here is a script i came up with (and again, jordan helped me make it better) that just writes to a model in the in memory datasource.
var chalk = require('chalk');
console.log(chalk.magenta('lets seed this app!'));
/*
this script is based on:
https://github.com/strongloop-training/coffee-time/blob/master/server/boot/create-sample-model-data.js
*/
module.exports = function(app) {
//sample data
var data = [
{
title:'content one',
body:'body one',
posted:new date()
},
{
title:'content two',
body:"body two",
posted:new date()
},
{
title:'content three',
body:'body three',
posted:new date()
}
];
app.models.testcontent.create(data, function(err, records) {
if (err) { return console.log(chalk.red(err.message)); }
console.log(chalk.magenta('done seeding data, '+records.length+' records created.'));
});
}
pretty simple, and it works nicely.
but wait — there’s more!
so, as i said up top, a few hours after posting this, rand mckinney from strongloop shared this link with me: data persistence . in this doc, they mention that you can simply specify a json file for the datasource and the in memory data will persist to it. like, seriously, exactly what i had wanted. here is an example:
{
"db": {
"name": "db",
"connector": "memory",
"file": "mydata.json"
}
}
still—probably—a bad idea in production. but as i said, this would be incredibly useful when prototyping!
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments