loading...

Seeding Mongo with Realistic Records using Faker

cameronblandford profile image Cameron ・2 min read

So you have your app basically up and running, with a working API, but the only data you have are the manual entries that you've added to the database yourself. You don't want to have to manually sign up as ten thousand different users each with their own several hundred related items (blog posts, todo lists, etc) to get an idea of how your app looks and works when it's operating at moderate scale.

Enter faker. Faker is a library designed to, in its own words, generate massive amounts of fake data. By connecting to our mongo database, we can upload tons of unique users and items.

Seeding a Mongo Database

First, install the two dependencies we'll be working with npm i -D faker mongodb lodash. Next, make a new file in your server folder, preferably in a subdirectory for scripts, called something like seed.js.

/* mySeedScript.js */
// require the necessary libraries
const faker = require("faker");
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const _ = require("lodash");
// Connection URL
const url = "mongodb://localhost:27017";

// Database Name
const dbName = "my_dev_database";

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);

  const db = client.db(dbName);

  // get access to the relevant collections
  const usersCollection = db.collection("users");
  const postsCollection = db.collection("posts");
  // make a bunch of users
  let users = [];
  for (let i = 0; i < 5000; i += 1) {
    const firstName = faker.name.firstName();
    const lastName = faker.name.lastName();
    let newUser = {
      email: faker.internet.email(firstName, lastName),
      firstName,
      lastName,
      password: "password123"
    };
    users.push(newUser);

    // visual feedback always feels nice!
    console.log(newUser.email);
  }
  usersCollection.insertMany(users);

  // make a bunch of posts
  let posts = [];
  for (let i = 0; i < 5000; i += 1) {
    let newPost = {
      title: faker.lorem.words(7),
      body: faker.lorem.words(500),

      // use lodash to pick a random user as the author of this post
      author: _.sample(users),

      // use lodash to add a random subset of the users to this post
      likes: _.sampleSize(users, Math.round(Math.random * users.length)).map(
        user => user._id
      )
    };
    posts.push(newPost);

    // visual feedback again!
    console.log(newPost.title);
  }
  postsCollection.insertMany(posts);
  console.log("Database seeded! :)");
  client.close();
});

Other Databases

The best way to do this with other databases is to require the ORM or database connection you're using into the seed script and then use a combination of faker.js and the ORM to do the above. If there's interest, I'd be happy to write up an example!


What are your favorite ways to generate fake/testing data for your API? Let me know below!

Posted on by:

cameronblandford profile

Cameron

@cameronblandford

Growing all kinds of gardens.

Discussion

pic
Editor guide
 

Why do we need the MongoClient method here? Where does that come from? Is there a package.json file somewhere you can share?

Also what does lodash give you here? Isn't it just a functional programming library?

 

random selection of an existing item previously pushed to the an array