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:
{
field1: users.id,
field2: users.name,
}
{
field1: users.id,
field2: users.name,
}
instead of the db.query which would need:
{
id: true,
name: true,
}
{
id: true,
name: true,
}
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
iamgengar.
iamgengar.OP4d ago
// Type definition for the mapping from table names (string literals) to schema keys (string literals)
// This uses a mapped type to invert the relationship: TableName -> SchemaKey
type TableNameSchemaKeyMap = {
// Iterate through each possible table name derived from appSchema
[TableName in {
// Helper mapped type to extract the union of all table names (TConfig['name'])
[K in keyof typeof appSchema]: (typeof appSchema)[K] extends Table<
infer TConfig
>
? TConfig['name'] // Get the literal table name
: never // Ignore non-table entries
}[keyof typeof appSchema]]: {
// The result is a union of table name literals
// For the current TableName, find the original SchemaKey in appSchema
[SchemaKey in keyof typeof appSchema]: (typeof appSchema)[SchemaKey] extends Table<
infer TConfig
>
? TConfig['name'] extends TableName // Check if the table's name matches the current TableName
? SchemaKey // If it matches, the value is the original schema key literal
: never // Otherwise, this key is not the one we're looking for
: never // Ignore non-table entries
}[keyof typeof appSchema] // Extract the specific SchemaKey literal that matched (non-never type)
}

export const tableNameSchemaKeyMap = Object.fromEntries(
Object.entries(appSchema).map(([key, table]) => [
getTableName(table as Table),
key,
]),
) as TableNameSchemaKeyMap
// Type definition for the mapping from table names (string literals) to schema keys (string literals)
// This uses a mapped type to invert the relationship: TableName -> SchemaKey
type TableNameSchemaKeyMap = {
// Iterate through each possible table name derived from appSchema
[TableName in {
// Helper mapped type to extract the union of all table names (TConfig['name'])
[K in keyof typeof appSchema]: (typeof appSchema)[K] extends Table<
infer TConfig
>
? TConfig['name'] // Get the literal table name
: never // Ignore non-table entries
}[keyof typeof appSchema]]: {
// The result is a union of table name literals
// For the current TableName, find the original SchemaKey in appSchema
[SchemaKey in keyof typeof appSchema]: (typeof appSchema)[SchemaKey] extends Table<
infer TConfig
>
? TConfig['name'] extends TableName // Check if the table's name matches the current TableName
? SchemaKey // If it matches, the value is the original schema key literal
: never // Otherwise, this key is not the one we're looking for
: never // Ignore non-table entries
}[keyof typeof appSchema] // Extract the specific SchemaKey literal that matched (non-never type)
}

export const tableNameSchemaKeyMap = Object.fromEntries(
Object.entries(appSchema).map(([key, table]) => [
getTableName(table as Table),
key,
]),
) as TableNameSchemaKeyMap

Did you find this page helpful?