DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Top React Libraries for Data-Driven Dashboard App Development
  • The Technology Stack Needed To Build a Web3 Application
  • Allow Users to Track Fitness Status in Your App
  • Turn Your App into a Handy Health Assistant

Trending

  • Smart BDD vs. Cucumber Using Java and JUnit5
  • Next.js vs. Gatsby: A Comprehensive Comparison
  • Is OpenJDK Just a Drop-In Replacement?
  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  1. DZone
  2. Data Engineering
  3. Data
  4. Seeding Data for a StrongLoop App

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.

Raymond Camden user avatar by
Raymond Camden
·
Jan. 12, 16 · Tutorial
Like (1)
Save
Tweet
Share
5.25K Views

Join the DZone community and get the full member experience.

Join For Free

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:

https://github.com/strongloop-training/coffee-time/blob/master/server/boot/create-sample-model-data.js

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.

image title

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!

Data (computing) app

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Top React Libraries for Data-Driven Dashboard App Development
  • The Technology Stack Needed To Build a Web3 Application
  • Allow Users to Track Fitness Status in Your App
  • Turn Your App into a Handy Health Assistant

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: