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: lookups with composite uniques #1254

Conversation

davenewza
Copy link
Contributor

@davenewza davenewza commented Oct 24, 2023

Overview

  • Support for composite unique lookup on the get, update and delete actions
  • Support for unique lookups down relationship paths (on inputs and in expressions)
  • Support for reliably checking uniqueness across multiple expression conditions
  • If update or delete somehow act on more than one row, then transaction is rolled back
  • Missing validations and fixes for unique lookups in general
  • Improved validation messaging
  • Moved to visitor pattern validation

Composite unique lookups

Composite uniques can now be used to identity a row in the get, update and delete actions in either action inputs or in expressions.

model Product {
    fields {
        name Text
        sku Text @unique
        supplierCode Text
        supplierSku Text
    }

    @unique([supplierSku, supplierCode])

    actions {
        get getProduct(supplierSku, supplierCode)

        get getAppleProduct(supplierSku) {
            @where(product.supplierCode == "APPLE")
        }
    }
}

Unique lookups with relationships

Unique lookups (composite included) can be inferred when travelling down related models. Compare this schema with the one above:

model Product {
    fields {
        name Text
        sku Text @unique
        supplier Supplier
        supplierSku Text
    }

    @unique([supplierSku, supplier])

    actions {
        get getProduct(supplierSku, supplier.code)
    }
}

model Supplier {
    fields {
       code Text @unique
       products Products[]
    }

   actions {
     get getSupplierByProduct(products.sku)
   }
}

Improved validation message

image

@davenewza davenewza requested a review from a team October 24, 2023 10:23
@linear
Copy link

linear bot commented Oct 24, 2023

KE-1138 Get action 'not a unique field' input error with nested fields in @where

model WeeklyPlan {
    fields {
        weekNumber Number
        user User unique
    }

    actions {
        get getMyWeeklyPlan(weekNumber) {
            where(weeklyPlan.user.identity == ctx.identity)
        }
    }
}

model User {
    fields {
        identity Identity unique
    }
}

@davenewza
Copy link
Contributor Author

Copy link
Member

@jonbretman jonbretman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, some gnarly validation logic there but looks like it's well tested.

@davenewza davenewza merged commit 41be0a4 into main Oct 24, 2023
9 checks passed
@davenewza davenewza deleted the ke-1138-get-action-not-a-unique-field-input-error-with-nested-fields branch October 24, 2023 11:09
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