fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
580 stars 146 forks source link

[Question, Postgres, Schemas] Is there a way to select a schema at runtime? #828

Closed aggieben closed 2 months ago

aggieben commented 3 months ago

I'm using Postgres schemas to implement multitenancy; I've got a couple of meta-tables in the public schema, and then N other schemas (1 per tenant). The context types produced by the provider have static members for each schema provided at compile-time, but is there a way to switch which schema would be queried at runtime?

Thorium commented 3 months ago

The compile-time type-checking is very intentional. I don't think there is easy way around that. Maybe:

1) If you know the schema is exactly same, the .GetDataContext takes runtime connection string as parameter. That helps if they are totally separate identical databases, but maybe not with schemas?

2) You could do conditional compilation. Not runtime but to avoid duplicate code.

3) To share queries... Trying to write generic LINQ queries is not very easy in .NET because there is no ducktyping, Expression trees are typed strongly on compile time. There are some options listed in https://fsprojects.github.io/SQLProvider/core/composable.html#Composable-Queries but they may not suite for your need.

aggieben commented 2 months ago

Got it. I read this as "for now there is no way". Thanks!