go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

Should return value with return type #343

Closed gxanshu closed 1 month ago

gxanshu commented 2 months ago

I'm coming from TypeScript environment, and Kysely is by far the best SQL builder in TypeScript. The only 2 things that make it great is is Type safety in SQL query building and return type. Jet done really great job in SQL query building but still i'm struggling to get the Return Type.

What i mean is in Jet we have to first write the query and then have to write a destination variable with proper return type this takes time to develop.

var dest []struct {
    model.Actor

    Films []struct {
        model.Film

        Language    model.Language
        Categories  []model.Category
    }
}
err := stmt.Query(db, &dest)
handleError(err)

I think if Jet return that variable with proper type then it would be very nice, it can increase developer experience and productivity.

go-jet commented 2 months ago

Hi @gxanshu, I agree that it would be nice, but I don't think it's technically feasible in Go or probably in any other type safe language. It's probably doable for a single table select, but for multiple joined table its much harder. Especially because Go doesn't have such a powerful generics support. Are you saying that Kysely can produce automatically deeply nested destination struct using generics? If that's the case I would love the see an example.

Event if it is technically possible, I don't think it would be desirable. There can be different valid destinations for the same query. Also, for some of the tables(like N:M), you don't want them to appear in the destination. What to do with calculated projections(like COUNT(*))? Where those values will go in the automatic destination? There are probably many more edge cases, where automatic destination would not work.