Seeding Data for a StrongLoop App

DZone's Guide to

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.

Please 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:

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.

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: