Query API Returned Structure

Is there a way to change the structure of the result of the query API. Here's an example showing what it currently does, and what I'd like to do.
const result = await db.query.tableGame.findFirst({
with: {
gameCategories: true,
},
});
console.log();

// The current result type is
const result: {
userId: string | null;
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string;
title: string;
publisher: string;
numberOfPlayers: number;
playtime: number;
coverImageUrl: string | null;
gameCategories: {
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string | null;
categoryId: string | null;
gameCategoryId: string;
}[];
} | undefined

// is there an easy way to make it do this
const result: {
game: {
userId: string | null;
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string;
title: string;
publisher: string;
numberOfPlayers: number;
playtime: number;
coverImageUrl: string | null;
}
gameCategories: {
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string | null;
categoryId: string | null;
gameCategoryId: string;
}[];
} | undefined
const result = await db.query.tableGame.findFirst({
with: {
gameCategories: true,
},
});
console.log();

// The current result type is
const result: {
userId: string | null;
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string;
title: string;
publisher: string;
numberOfPlayers: number;
playtime: number;
coverImageUrl: string | null;
gameCategories: {
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string | null;
categoryId: string | null;
gameCategoryId: string;
}[];
} | undefined

// is there an easy way to make it do this
const result: {
game: {
userId: string | null;
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string;
title: string;
publisher: string;
numberOfPlayers: number;
playtime: number;
coverImageUrl: string | null;
}
gameCategories: {
createdAt: number;
updatedAt: number;
deletedAt: number | null;
gameId: string | null;
categoryId: string | null;
gameCategoryId: string;
}[];
} | undefined
1 Reply
Angelelz
Angelelz4mo ago
Not natively in drizzle. But this looks like an easy map function with JS

Did you find this page helpful?