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

fix: update action to support relationship lookups #1255

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

davenewza
Copy link
Contributor

Previously we could only call update and delete using inputs targeting fields on the direct model, but with recent changes we can now support relationships to lookup and filter by.

For example, the following action performs

  • a lookup by composite unique fields,
  • a lookup using fields in a nested model, and
  • filtering by related model fields.
update deactivateProduct(productCode, brand.code) {
  @set(product.isActive = false)
  @where(product.isActive)
  @where(product.supplier.isRegistered)
  @permission(expression: ctx.identity.user in product.brand.editors)
}

The following SQL will be generated for such a query:

UPDATE "product" 
SET is_active = ? 
FROM "brand" AS "product$brand" 
LEFT JOIN "supplier" AS "product$supplier" ON "product$supplier"."id" = "product"."supplier_id" 
WHERE 
  "product$brand"."id" = "product"."brand_id" AND 
  "product"."product_code" IS NOT DISTINCT FROM ? AND 
  "product$brand"."code" IS NOT DISTINCT FROM ? AND 
  "product"."is_active" IS NOT DISTINCT FROM ? AND 
  "product$supplier"."is_registered" IS NOT DISTINCT FROM ? 
RETURNING "product".*

@davenewza davenewza requested a review from a team October 25, 2023 11:49
@davenewza davenewza merged commit c22a534 into main Oct 25, 2023
9 checks passed
@davenewza davenewza deleted the fix/update-filtering branch October 25, 2023 14:20
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