I have three tables with a many to many relationship, Games
, GamesToPlatforms
, Platforms
. How do I query all Games
so that it has a key of platforms
that is an array of platform objects associated to it threw the join table.
const GamesTable = pgTable(
'games',
{
id: uuid('id').primaryKey().defaultRandom().notNull(),
name: varchar('name', { length: 255 }).notNull(),
backgroundImage: text('background_image').notNull(),
})
const GamesRelations = relations(GamesTable, ({ one, many }) => ({
platforms: many(GamesToPlatformsTable)
})
const GamesToPlatformsTable = pgTable(
'games_to_platforms',
{
gameId: uuid('game_id').notNull(),
platformId: smallint('platform_id').notNull(),
},
(t) => {
return {
uniqueIdx: uniqueIndex(`unique_idx`).on(t.gameId, t.platformId),
}
}
)
const GamesToPlatformsRelations = relations(
GamesToPlatformsTable,
({ one }) => {
return {
platform: one(PlatformsTable, {
fields: [GamesToPlatformsTable.platformId],
references: [PlatformsTable.id],
}),
})
const PlatformsTable = pgTable(
'platforms',
{
id: smallint('id').primaryKey().notNull(),
name: varchar('name', { length: 255 }).notNull(),
imageBackground: text('image_background').notNull(),
},
(platforms) => {
return {
uniqueIdx: uniqueIndex(`unique_idx`).on(platforms.slug),
}
}
)
const PlatformsRelations = relations(PlatformsTable, ({ many }) => {
return {
games: many(GamesToPlatformsTable),
}
})
You can do it like this:
You will get an array of this shape:
You will still need to access the junction table and then the
platform
property inside each object because, for now, there is no other way to do it in Drizzle ORM's Relational Queries API. You can always map it to any shape you need, but I would recommend using it as it is