3.3.3: Advanced Sequelize Concepts
- 1.Sequelize model aliases and custom foreign keys can make our app logic clearer, especially when our schemas involve models with multiple associations with any other model
- 2.Sequelize eager loading can make our database querying more efficient by retrieving all data we need from multiple tables in a single query instead of multiple
- 3.Sequelize validations and constraints can make our applications more robust by allowing us to prompt our users when input data is invalid, and preventing us from saving invalid data to our databases
The Sequelize concepts in this module will not be necessary for many applications, but they are commonly used in industry, especially for more complex apps with many features, more related data that is queried often, and greater requirements for data integrity.
These Sequelize concepts do not appear often in coding interviews, although related concepts have come up in interviews, such as how to perform joins in SQL queries and how to ensure data integrity in a database.
Sequelize allows us to define aliases for models in associations. This is especially helpful in situations where a model has multiple associations with another model, for example in our Carousell app where the
Listingmodel has multiple associations with the
Usermodel, 1 for buyer and 1 for seller.
The following official Sequelize guide shares an example of a sports game model associated twice with a team model, once for home team and once for away team. Rocket implemented the listing, buyer and seller associations similarly in our Carousell reference solution.
- 1.Notice the example specifies both aliases and custom foreign keys. In our experience, when using aliases it's better practice to specify a custom foreign key to have clearer control over Sequelize.
The following section in the guide shares all the different ways to define and uses Sequelize aliases and foreign keys.
- 1.There is no good reason for now to specify a custom foreign key unless we are using Sequelize aliases
- 2.Aliases are primarily useful for Sequelize relationship methods, because we will be able to use the relationship methods with alias names to query related data, which should clarify our app logic
Eager loading is an optimisation and optional at Rocket Academy. We will always be able to retrieve the data we need with multiple, simpler queries. If you are keen to optimise the SQL data query times in your app, read on.
The following official Sequelize tutorial on Eager Loading demonstrates the syntax for loading multiple associated tables in a single query.
- 1.Note the
includesyntax used to fetch associated model data with eager loading
- 2.Note the eager-loaded associated model may be an object or array of objects depending on whether the association is singular or plural
- 3.After "Fetching an Aliased association" section, the other sections in the "Basic example" are nice to know and not necessary for now
- 4.Retrieving junction table attributes with eager loading for M-M relationships can be helpful when we have non-FK data in our junction tables, such as
personalitiesexample from the Sequelize M-M Relationships submodule
- 5.Including all nested associated models is discouraged for performance reasons, less we truly need all that data at once
- 6.Everything from "including soft deleted records" to "Using
findAndCountAllwith includes" is optional, we will not need to read it in detail until we need it
Sequelize validations and constraints make our applications more robust by preventing users from inputting invalid data, and preventing our application logic from saving invalid data to our database.
Sequelize validations and constraints are not necessary for our relatively simple applications at Rocket, but will be more important when we work at companies that require high levels of data integrity, especially those that work with financial or healthcare data.
When we use validations and constraints in our code, we can surround our Sequelize model methods with try-catch blocks to catch errors when validations or constraints fail. This allows us to gracefully let our users know what happened and how to fix it.
The following official Sequelize tutorial demonstrates the syntax of defining Sequelize validations and constraints.
- 2.We can define constraints in migrations using the same validations syntax
- 4.Apply validations and constraints to models as well as migration files for the best result.