Step 11

Open data/notes.js, the module that generates sample data for us:

const faker = require("faker");

const NUM_SAMPLES = 4;

function addSampleNotes(notes) {
  for (let i = 0; i < NUM_SAMPLES; i++) {
    notes.create(faker.lorem.paragraphs(), faker.name.findName());
  }
}

module.exports = { addSampleNotes };

We must update this to account for the fact that notes.create is an async operation now. Moreover, we don't want to make sample data every time we run the application. It made sense before since we did not persisted the data but now we have a database. So instead, we will check if and generate sample data only if the database is empty.

const faker = require("faker");

const NUM_SAMPLES = 4;

async function addSampleNotes(notes) {
  const data = await notes.readAll();

  if (data.length === 0) {
    for (let i = 0; i < NUM_SAMPLES; i++) {
      await notes.create(faker.lorem.paragraphs(), faker.name.findName());
    }
  }
}

module.exports = { addSampleNotes };

Note that our sample data generation strategy is not a great one! If a client deletes all their notes (intentionally) and then we reset the server, they will open their application finding four sample notes which they didn't want to have.

Sample data is useful during development but you should not use it when the application goes to production.