Sequelize Assocations

Agenda
- Association Types
- Creating Associations
- Eager Loading
Association Types
Key Terms
Phone.belongsTo(Person)
Source
Target
Alternatively, we can think of the two models as owner and possession
Possession
(in this case)
Owner
(in this case)
One-to-One (1:1)
Phone.belongsTo(Person) | Person.hasOne(Phone) |
Foreign Key appears on the source model | Foreign Key appears on the target model |
There are two methods to create 1:1 associations:
In both methods, the possession model (Phone) holds the foreign key
These methods always assign association methods (i.e. get, set, create) to the source
One-to-Many (1:m)
.hasMany creates a 1:m relationship where:
- A source can have multiple targets
- Targets can each only have one source (can be supplemented with .belongsTo method)
- Target names will be pluralized automatically by Sequelize; consider assigning foreign keys manually
Many-to-Many (m:m)
.belongsToMany creates a m:m relationship where:
- Sources can have multiple targets, and vice versa
- A "through" option must be specified, creating a join table tracking each association
- Possible to use a predefined table
- m:m associations must typically be defined two-ways
- Can be self-referencing
Creating Associations
Get, Set, Add, & Create
- These methods are used to retrieve and create associations
- Sources will automatically gain these methods once an association is defined in the model
Aliasing
Aliasing provides context behind associations, for example:
- Self-referencing models
- i.e. Person.belongsTo(Person, {as: 'father'})
- Aliasing changes naming conventions for methods and foreignKeys
- In the above example, the Person model would now have a 'fatherId' column
- To add a Person as another Person's father, we would use Person.addFather(Person)
Foreign Keys
At times, foreign keys may be necessary to preserve context and prevent repetition
- Asymmetrical aliasing
- Pet.belongsTo(Person, {as: 'owner'})
Person.hasMany(Pet, {foreignKey: 'ownerId'}) - Without the Foreign Key definition, Pet would have two columns: ownerId and personId
- Pet.belongsTo(Person, {as: 'owner'})
Eager Loading
Scopes
- Sequelize allows users to define scopes in model definitions as well as in finder requests (i.e. .findOne and .findAll);
- A particularly useful scope is include, allowing us to eagerly load information from associated models
- This is particularly advantageous as associations, and ultimately promise-chaining, become more complex
Eager Loading Methods
Person.findById(123, {
include: [{model: Pet}]
})
.then(function(response) {
return response.data
});
.catch(console.error);
Person.addScope('defaultScope', {
include: [{model: Pet}]
}, {
override: true
});
In Finder | In Model |
Both methods will add a Pet object as a property on relevant instances of Person
Conclusion
Resources
- Sequelize Documentation:
- Sequelize Model API
http://docs.sequelizejs.com/en/latest/api/model/ - Sequelize Association API
http://docs.sequelizejs.com/en/latest/api/associations/ - Sequelize Association Documentation
http://docs.sequelizejs.com/en/latest/docs/associations/ - Eager Loading
http://docs.sequelizejs.com/en/latest/docs/models-usage/#eager-loading
- Sequelize Model API
Copy of Copy of Copy of Tech Talk: Sequelize Assocations
By beelai88
Copy of Copy of Copy of Tech Talk: Sequelize Assocations
- 670