Tip: Lorem ipsum Database Generation

So you’re working on a web site, eh? You need some data in your database to do accurate testing!

I’m working on a side project, where all of the content is AJAX’d in. To make this work, I had to create a small script that would fill a collection in my database with misc information.

Previously I was working with MySQL and Postgres, which are a bit of a pain. You need to have some script output a large JSON file, which is often stored in repos, and bloats everything up. These are called “fixtures”. They are then imported into a database using shell scripts or similar, cause lots of problems when the APIs change, etc.

To remedy this, I used a script which connects to the database using the same API as my application would. I have that API defined in a file called database.js.

The code isn’t pretty, but it is two things.

  1. Modular.

    it allows me to add more fields, remove things, change things, and modify things with ease. I want three words in a name, or maybe one? I can just change the parameter for rWords.

  2. Quick

    some would use a weird framework to do stuff like this, but I have functions like pickOne committed to memory. Instead of typing some likely text I found lorem ipsum text (remember this query, and click on the first result).

var db = require(‘./database’);

function pickOne(opts) {
    return opts[Math.floor(Math.random()*opts.length)]
}

function rWords(count) {
    var lorem = "sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium totam rem aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque porro quisquam est qui dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur";
    var words = lorem.split(" ");

    var chosen = [];

    for (var i=0; i<count; i++) {
        chosen.push(pickOne(words));
    }
    console.log(chosen, count);
    console.log(pickOne(words));

    return chosen.join(" ");
}

function rWord() {
    return rWords(1);
}

if (require.main === module) {
    var total = 100, completed = 0, i;

    for ( i = 1; i <= total; i++ ) {
        db.newBlend({
            path: "/files/" + rWord() + ".blend",
            name: rWords(2),
            category: pickOne(["materials", "tools"]),
            description: rWords(14),
            appendType: "Object",
            appendName: rWord(),
            rating: Math.floor( Math.random() * 6 ) * 5,
            ratings: 5,
        }, function () {
            completed++;

            if ( (completed / total).toFixed(2) !==  ((completed - 1) / total).toFixed(2)) {
                console.log(Math.floor(completed / total * 100) + "% done");
            }

            if (completed === total) {
                process.exit();
            }
        });
    }
}

Take it, modify it, or just learn to write your own. I spent about 10 minutes writing that, and it’s saved me hours so far. It takes about 3 seconds to run on my computer, so I usually see a flash of percentages, and then it’s done.