fluent-kit icon indicating copy to clipboard operation
fluent-kit copied to clipboard

Computed fields

Open tanner0101 opened this issue 6 years ago • 4 comments

A property similar to @Field for computed columns could be useful for things like case-insensitive text search.

tanner0101 avatar Oct 03 '19 20:10 tanner0101

Related: https://github.com/vapor/fluent-postgres-driver/issues/62

tanner0101 avatar Oct 03 '19 20:10 tanner0101

From https://github.com/vapor/fluent-postgres-driver/issues/62:

try User.query(on: db)
    .filter(\User.$email.lowercased() == emailAddress.lowercased())
    .first()

tanner0101 avatar Jun 13 '20 02:06 tanner0101

I'm eager to have this feature: I can't seem to match the the capabilities of Hibernate's Formula using custom sql. Maybe it could take a form similar to Hibernate's...

final class Person : Content, Model {
    static let schema = "person"

    @ID(custom: "id", generatedBy: .database)
    var id: Int?

    @Field(key: "first_name") var firstName: String
    @Field(key: "last_name") var lastName: String
    @Field(key: "date_of_birth") var dateOfBirth: Date
     
    @Computed(value: "year(current_date()) - year(date_of_birth)") var age: Int
    @Computed(value: "first_name || ' ' || last_name") var fullName: String
    
    init() {}
}

fwgreen avatar Oct 28 '21 04:10 fwgreen

I'll assign this to @gwynne - this isn't something that will likely get implemented for Fluent 4 but could be something to consider for Fluent 5

0xTim avatar Jan 18 '22 11:01 0xTim