Overview
- The local library website will store information about books and borrowers.
- We will use Mongoose to provide database access for the locallbrary website.
- Explains how object schema and models are declared, main field types, basic validation.
- Shows a few ways in which you can access model data
- Express apps – can use many different databases
- Several approaches you can use for performing CRUD – create, read, update, delete operations.
- Brief overview of some options.
What databases can I use?
- Express apps – many different database supported by node.
- PostgreSQL, MySQL, Redis, SQLite and MongoDB
What is the best way to interact with a database
- Two common approaches – use the native query language, use an ODM/ ORM – this represents the websites data as JS objects, which are mapped to the underlying database.
- ODMs slower because of translation code, but ca n continue to think in terms of JS objects.
What ORM/ODM should I use?
- Many ODMs/ ORMs available on the NPM package manager site.
- Mongoose, Waterline, Bookshelf, Sequelize, Node ORM, GraphQL
Using Mongoose and MongoDb for the LocalLibrary
- We’re going to use Mongoose ODM.
- Mongoose acts as a front end to MongoDB, open source NoSQL database.
- Uses a document-oriented data model
- ‘Collection of documents’ = ‘table of rows’ in a relational database.
Designing the Local Library Models
- Start thinking about the kinds of data we need to store and the relationships between the different objects.
- Need to store information about books, might have multiple copies.
- Store information about the author, multiple authors.
- Sort information base don book title, author, genre and category.
- Separate models for every object.
- Models for selection-list options – e.g. genre.
- UML association diagram – show the models we will define in this case.