...

/

Coding the Add Books Functionality

Coding the Add Books Functionality

Learn and build the functionality that adds books to the bookshop.

Adding books

Books are added to the books collection through Meteor Methods called from the client. The functionality that adds books is an admin-only function. The admin enters details about the book through the user interface. The authors of the entered books are searched using the firstname of an author. A list of authors is published to the client from the server.

The added book details are saved into the book collection. The saved book’s _id is returned from the server. This _id is passed to the component used to upload files. The upload files component uploads the pdf and cover image of the book to the server, where they’re handled.

Creating an index for the author’s first name

In the user interface, we want to present the admin with the ability to search for the author of a book using the first name of the author. When searching for a piece of data, MongoDB performs a complete scan of all records in a collection, if the collection isn’t indexed.

An index is like a lookup page that the database can query to retrieve the information instead of scanning all the records one at a time. It functions like the index page of a book, where it’s easy to look up where specific information in the book is located.

Scroll down and open the imports/api/authors/authors.js file in the coding playground at the end of this section. On line 20, an index is created for the firstname property of the author collection. Creating an index is done on the server, hence the method is wrapped in a Meteor.isServer check. The 1 ...