Cannot reference KeyPath correctly

Hi

I'm having a little difficulty trying to use this key path. So wondering if someone can help point me in the right direction. I'm basically using a protocol to see whether the resource requested is able to be accessed by the user in a REST API.

OrganizationRepresentable

protocol OrganizationRepresentable: Model {
    typealias OrganizationKey = ReferenceWritableKeyPath<Self, UUID>
    static var organizationKey: OrganizationKey { get } // basically a reference to the organization FK on another model
}

extension OrganizationRepresentable where Self: Model {
    static func isBelongingToOrganization(user: User, on connection: DatabaseConnectable) throws -> EventLoopFuture<Int>
    {
        let userId = try user.requireID()
// checks a user can access this resource 
        return UserOrganization
                .query(on: connection)
                .filter(\.userId == userId)
                .filter(\UserOrganization.organizationId == Self.organizationKey) --> Cannot convert value of type 'Bool' to expected argument type 'FilterOperator<_, _>'
                .count()
    }
}

So, the filter() above, I feel like I've tried almost every possible way to reference the keyPath, but I'm obviously missing something. For completeness, this is how I conform the model Campaign and the extension on the request.

final class Campaign: Codable {
    var id: UUID?
    var organizationId: Organization.ID
    ...
}
extension Campaign: PostgreSQLUUIDModel {}
extension Campaign: Content {}
extension Campaign: Parameter {}

extension Campaign: OrganizationRepresentable {
    static var organizationKey: OrganizationKey = \Campaign.organizationId
}

// extension on request to 
extension Request {
    func isOrganizationRepresentable<O>(resource: O.Type, user: User, on connection: Request) throws -> EventLoopFuture<Bool> where O: OrganizationRepresentable
    {
        return try resource.isBelongingToOrganization(user: user, on: connection).map { total in
            return total > 0
        }
    }
}

This is not supported as of now (even after SE-0249).

This line works fine, it's Vapor's filter() for its DB layer Fluent.
It's the line below that I can't get to compile, which also uses Fluent

Oh, I thought it was the default filter from stdlib!

Then if I had to guess the issue is that you do Self.. You're in a static context, accessing a static var.

Terms of Service

Privacy Policy

Cookie Policy