Step 5

Add the following method to NoteDao class:

readAll() {
  return this.notes;
}

The readAll method is like a getter method that returns all notes.

Let's update the index.js where we created sample notes previously to instead use NoteDao as follows:

const notes = new NoteDao();
notes.create("Sample 1", "Author 1");
notes.create("Sample 2", "Author 2");
notes.create("Sample 3", "Author 2");
notes.create("Sample 4", "Author 1");
console.log(notes.readAll());

As you save the changes to your file, you must see the sample notes printed to the terminal.

Expected output
[
  Note { content: 'Sample 1', author: 'Author 1', _id: 0 },
  Note { content: 'Sample 2', author: 'Author 2', _id: 1 },
  Note { content: 'Sample 3', author: 'Author 2', _id: 2 },
  Note { content: 'Sample 4', author: 'Author 1', _id: 3 }
]

It is common to search a database for a specific record/object using its unique identifier. We can add the following read method for that:

read(id) {
  return this.notes.find((note) => note._id === id);
}

It is also common to search for database records given one or more attributes. We can allow clients to search for notes given their author or content attributes.

Let's update the readAll method to have an optional parameter author:

readAll(author = "") {
  if (author !== "") {
    return this.notes.filter((note) => note.author === author);
  }
  return this.notes;
}

To test our application, update the index.js where we created sample notes previously to read (and return) notes given an id or their author:

const notes = new NoteDao();
notes.create("Sample 1", "Author 1");
notes.create("Sample 2", "Author 2");
notes.create("Sample 3", "Author 2");
notes.create("Sample 4", "Author 1");
console.log(notes.read(2));
console.log(notes.readAll("Author 1"));
console.log(notes.readAll());

As you save the changes to your file, you must see the sample notes printed to the terminal.

Expected output
Note { content: 'Sample 3', author: 'Author 2', _id: 2 }
[
  Note { content: 'Sample 1', author: 'Author 1', _id: 0 },
  Note { content: 'Sample 4', author: 'Author 1', _id: 3 }
]
[
  Note { content: 'Sample 1', author: 'Author 1', _id: 0 },
  Note { content: 'Sample 2', author: 'Author 2', _id: 1 },
  Note { content: 'Sample 3', author: 'Author 2', _id: 2 },
  Note { content: 'Sample 4', author: 'Author 1', _id: 3 }
]