Skip to content

Instantly share code, notes, and snippets.

View bmitchinson's full-sized avatar
🏠
Working from home

Ben Mitchinson bmitchinson

🏠
Working from home
View GitHub Profile
@alordiel
alordiel / markdown-link.js
Created January 31, 2018 12:42
[JS] to replace a markdown for link with actual HTML link element
@zcaceres
zcaceres / Include-in-Sequelize.md
Last active January 8, 2024 07:14
using Include in sequelize

'Include' in Sequelize: The One Confusing Query That You Should Memorize

When querying your database in Sequelize, you'll often want data associated with a particular model which isn't in the model's table directly. This data is usually typically associated through join tables (e.g. a 'hasMany' or 'belongsToMany' association), or a foreign key (e.g. a 'hasOne' or 'belongsTo' association).

When you query, you'll receive just the rows you've looked for. With eager loading, you'll also get any associated data. For some reason, I can never remember the proper way to do eager loading when writing my Sequelize queries. I've seen others struggle with the same thing.

Eager loading is confusing because the 'include' that is uses has unfamiliar fields is set in an array rather than just an object.

So let's go through the one query that's worth memorizing to handle your eager loading.

The Basic Query

FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.


Effective Engineer - Notes

What's an Effective Engineer?

@armandofox
armandofox / oo_1.rb
Created February 16, 2016 01:16
oo_1.rb
class Movie
def initialize(title, year)
@title = title
@year = year
end
def title
@title
end
def title=(new_title)
@title = new_title