adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.08k stars 195 forks source link

feat(orm): add loadOnce #1052

Closed RomainLanz closed 1 month ago

RomainLanz commented 2 months ago

Hey there! 👋🏻

This PR adds the loadOnce helper.

This helper is particularly useful when you're unsure whether a relation is already loaded and want to avoid unnecessary database calls if the data is already in memory.

Currently, loadOnce does not accept a callback like load does, as it's designed to avoid altering the query.

If you need to load nested relationships, you can do so like this:

await user.loadOnce('profile')
await user.profile.loadOnce('country')
// ...