dart-bridge / trestle

Database Gateway and ORM for Dart
MIT License
26 stars 12 forks source link

Strong/dynamic typing in where-conditions #9

Open RdeWilde opened 7 years ago

RdeWilde commented 7 years ago

Is it possible to have strong typing in the query building?

For example for here, where existence of user.age would be checked at compile time:

// Full query
Stream allUsersOfDrinkingAge = gateway.table('users')
  .where((user) => user.age > 18).get(); // At least in Sweden...

And with that I don't mean manually putting in something like this everytime:

Stream allUsersOfDrinkingAge = gateway.table('users')
  .where((User user) => user.age > 18).get(); // At least in Sweden...

And will the result be 'strong/static' typed?

emilniklas commented 7 years ago

If you're using the Gateway directly, you're basically handling Map<String, dynamic>, which isn't very helpful statically. However, if you're using the ORM (Repository<T>), then where should have the signature (bool predicate(T t)) -> Query. If this is not the case today it should be, and since generic methods are now available, maybe this should use that in some way.

To summarize:

gateway.table('users')
  .where((User user) => ...); // <- necessary

usersRepository
  .where((user) => ...); // <- should be inferred
RdeWilde commented 7 years ago

Second option looks awesome. I'll look into adding generics. If I come up with anything interesting, I'll post here