haxetink / tink_sql

SQL embedded into Haxe
MIT License
53 stars 16 forks source link

Typing of orderBy with joining #125

Open kevinresol opened 3 years ago

kevinresol commented 3 years ago
class Main {
    static function main() {
        final db:Db = null;
        db.User.join(db.Post).on(User.id == Post.id).orderBy(f -> [{field: f.User.id, order: Desc}]);
    }
}

@:tables(User, Post)
class Db extends tink.sql.Database {}

typedef User = {
    final id:tink.sql.Types.Id<User>;
}
typedef Post = {
    final id:tink.sql.Types.Id<Post>;
}

errors with

src/Main.hx:12: characters 13-22 : error: { User : Table0_Result, Post : Table1_Result } has no field id
src/Main.hx:12: characters 13-22 :  have: tink.sql.Field<..., { id }>
src/Main.hx:12: characters 13-22 :  want: tink.sql.Field<..., { User, Post }>
src/Main.hx:12: characters 13-22 : For function argument 'orderBy'

This is basically just a typing issue caused by the wrong Owner in Field. With cast f.User.id it runs fine at runtime because it is merely an Expr

kevinresol commented 3 years ago

Same for groupBy:

db.User.join(db.Post).on(User.id == Post.id).groupBy(f -> [f.User.id]);

benmerckx commented 3 years ago

Not completely related, but I had this api idea:

abstract Field(...) {
  public function asc()
    return {field: this, order: Asc}
  public function desc()
    return {field: this, order: Desc}
}
...orderBy(f -> [f.User.id.desc()])