Dynamically adding joins based on relation?

I have this setup:

export const circuitTable = sqliteTable('circuit', {
  id: text('id').notNull().primaryKey(),
  name: text('name').notNull(),
  placeName: text('place_name').notNull(),
  countryId: text('country_id')
    .notNull()
    .references(() => countryTable.id),
});

export const circuitRelations = relations(circuitTable, ({ one }) => ({
  country: one(countryTable, {
    fields: [circuitTable.countryId],
    references: [countryTable.id],
  }),
}));


I can easily refer to columns within the circuit table like circuitTable.name or circuitTable.placeName.

Can I also refer to relations to dynamically add a join?

E.g.:

const baseQuery = ctx.mydb
  .select({
    ...getTableColumns(circuitTable),
  })
  .from(circuitTable)
  .$dynamic();


and then based on user input I want to add some joins dynamically.
E.g. the user input could be sort on placeName or country.name, and in the latter case I want to add the country join.
As I have this in many places I would like to do something smart like:

const sortFields = {
  'id': circuitTable.id,
  'name': circuitTable.name,
  'placeName': circuitTable.placeName,
  'countryId': circuitTable.countryId,
  'countryName': {
    column: countryTable.name,
    join: circuitRelations.country, // not possible
  }
};


based on such configs I wanted to apply the sort based on user input sort field.

Would something like that be possible?
Was this page helpful?