sadmann7 / shadcn-table

Shadcn table with server-side sorting, filtering, and pagination.
https://table.sadmn.com
MIT License
3.05k stars 251 forks source link

[feat]: Relations / references #265

Closed t1nky closed 2 weeks ago

t1nky commented 6 months ago

Feature description

I have a table with userId field, but it is not helpful to display it as is, instead It would be nice to have the ability to easily add columns from the referenced table (user.name):

export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").default(sql`current_timestamp`),
})

export const tasks = pgTable("tasks", {
  id: varchar("id", { length: 30 })
    .$defaultFn(() => generateId())
    .primaryKey(),
  // ...
  userId: integer("user_id").notNull().references(() => users.id),
  createdAt: timestamp("created_at").defaultNow().notNull(),
  updatedAt: timestamp("updated_at").default(sql`current_timestamp`),
})

export const tasksRelations = relations(tasks, ({ one }) => ({
  user: one(users, {
    fields: [tasks.userId],
    references: [users.id],
  }),
}));

p.s. I've tried it myself, it did work, but it's very hacky and also type for DataTableFilterField.value had to be changed to string, because the id of my column was user_name as a result of referencing

Additional Context

Also, maybe we can utilize dynamic query building for better types and more reusability: https://orm.drizzle.team/docs/dynamic-query-building

Before submitting

sadmann7 commented 6 months ago

you can just do a left join. to join the tasks table with the user table.

you can show whatever that way

sadmann7 commented 2 weeks ago

you can use the accessor function in the column definition to transform the key