Skip to content

Relationships

Eager load related data in a single query batch.

When your table has a foreign key like userId, include will fetch the parent:

const posts = await db.posts.include({ user: true }).many();
// Each post will have a `user` object attached

When a related table has a foreign key pointing to your table:

const users = await db.users.include({ posts: true }).many();
// Each user will have a `posts` array attached
db.posts.include({
author: {
table: 'users', // Table to query
foreignKey: 'authorId', // FK column name
as: 'author' // Property name on result
}
}).many()
OptionDescription
tableTable name to query (defaults to key name)
foreignKeyForeign key column name
localKeyLocal key column (defaults to ‘id’)
asProperty name on result (defaults to key name)

Include uses batched queries for efficiency:

  1. Execute main query
  2. Collect all foreign key values
  3. Execute single WHERE id IN (...) query for related data
  4. Map results back to parent rows

This avoids N+1 query problems while keeping the API simple.