Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preloader): add preloadOnce to preload a relationship only once #1078

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

zaosoula
Copy link
Contributor

πŸ”— Linked issue

Similar to PR #1052 by @RomainLanz

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Added a preloadOnce method to the query building using the same logic as the loadOnce

In our use case we allow the client to preload field using a query parameter $populate: string[]
Screenshot 2024-12-14 at 23 30 25

this method is in a https://github.com/lookinlab/adonis-lucid-filter file

While doing so we sometimes also use preload in our services
Screenshot 2024-12-14 at 23 32 53

If the client request find with the following payload

{
	"$populate": ["tenant.settings"],
	"id": 1
}

Without .preloadOnce

  1. The filter will call query.preload("tenant", (subquery) => { subquery.preload('settings') })
  2. The service will call query.preload("tenant") and override the callback
  3. find will return the data with the tenant populated but without the tenant > settings

With .preloadOnce

  1. The filter will call query.preload("tenant", (subquery) => { subquery.preload('settings') })
  2. The service will call query.preloadOnce("tenant") and nothing will happen as tenant is already queued for preloading
  3. find will return the data with the tenant populated and also tenant > settings populated

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@zaosoula zaosoula marked this pull request as ready for review December 14, 2024 22:41
@thetutlage thetutlage merged commit fbfadbe into adonisjs:21.x Dec 18, 2024
16 checks passed
@thetutlage
Copy link
Member

Makes sense πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants