How to get schema name from table name?
I use the table as input for a function, and we can get the table name with
getTableName
which is no issue. However for using db.query.<schema name>
we need the schema name (i.e. the js variable name), not the table name, but the table itself doesn't know it's variable name.
Is there an easy way to retrieve it?
Context:
I want to make a function that takes a table and some columns and returns the query only taking those columns. I've tried it with db.select()
, however i've found it quite cumbersome to make such a function for it:
- The select syntax is more bloated, and more annoying to convert to. e.g. why enter:
instead of the db.query
which would need:
And we can even easily convert ['id', 'name']
to that format internally. Hence we thought the db.query
would be a better choice here. However the issue is that using db.query.<schema name>
needs the schema name, not the table name. And that schema name... is more difficult to get (especially in a generic way).
We could make a mapping once based on the schema of the application (here appSchema
), however then we hardcode the schema in there, which makes it more difficult to use in generic functionality (see first comment with code, otherwise post it to long).
If anyone know a good solution, we're trying to make generic crud endpoints, and would like the dev's to specify which columns should be returned. This while all being 100% typed.1 Reply