Chapter 8. The server database
This chapter covers
- The role of the database in an SPA
- Using JavaScript as the database language with MongoDB
- Understanding the Node.js MongoDB driver
- Implementing CRUD operations
- Using JSV for data validation
- Pushing data changes to the client with Socket.IO
This chapter builds on code we’ve written in chapter 7. We recommend copying the entire directory structure for that chapter into a new “chapter_8” directory and updating the files there.
In this chapter, we add the database to our SPA for persistent data storage. This completes our vision of using JavaScript end-to-end—on the database, the server, and the browser. When we’re finished, we’ll be able to start our Node.js server application and invite our friends to sign in to the SPA with their computer or touch device. They can then chat with each other or make changes to avatars that everyone can see in near-real time. Let’s get started by looking more closely at the role of the database.
We use the database server to provide reliable, persistent storage of data. We rely on the server for this role because data stored on the client is transitory and prone to application errors, user error, and user tampering. Client-side data is also difficult to share peer-to-peer and is available only when the client is online.