Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide table name at runtime when building relations #57

Open
bssstudio opened this issue Feb 21, 2018 · 1 comment
Open

Provide table name at runtime when building relations #57

bssstudio opened this issue Feb 21, 2018 · 1 comment

Comments

@bssstudio
Copy link
Contributor

bssstudio commented Feb 21, 2018

Hi!
In my use case, I have multiple tables with schema that is exactly the same. What I would like to do is to provide the table name at runtime. I would use one table to generate the definitions with template haskell, but when building a relation, I would provide a different name.

nRel :: String -> Relation () MyData
nRel name = relationName name $ do
  query $ MyData.myData
  ...

Is this currently possible, or might this be a feature request?

@igrep
Copy link
Contributor

igrep commented Mar 20, 2018

Providing a table name as a String can break the type safety of this library.
So you should not do that.

I have multiple tables with schema that is exactly the same.

I guess you should make use of HasProjection type class.
Given tables with columns foo :: String and bar :: Int, you can write the query like this.

(Updated due to a type error)

nRel
  :: (HasProjection "foo" a String, HasProjection "bar" a Int)
  => Relation () a -> Relation () a
nRel tbl = relation $ do
  t <- query tbl
  wheres $ t ! #bar .=. value "some bar value"
  ...

(You may have to enable DataKinds, OverloadedLabels, and the other related extensions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants