Faust(Thuen)
Faust(Thuen)
DTDrizzle Team
Created by Faust(Thuen) on 12/15/2023 in #help
unionAll nullable typing error
I have something like this
const blackPlayerOneEloChanges = db
.select({
player_id: matches.blackPlayerOne,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerOne));
const blackPlayerTwoEloChanges = db
.select({
player_id: matches.blackPlayerTwo,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));;
const blackPlayerOneEloChanges = db
.select({
player_id: matches.blackPlayerOne,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerOne));
const blackPlayerTwoEloChanges = db
.select({
player_id: matches.blackPlayerTwo,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));;
then I do a unionAll(blackPlayerOneEloChanges, blackPlayerTwoEloChanges and get the following error, because blackPlayerTwo is nullable, it's optional, the question is if there is anyway I can override the type of playerid in the blackPlayerTwoEloChanges query, since I'm only selecting the ones where it is not null. `The types of '.result' are incompatible between these types. Type '{ player_id: string | null; elo_change: number; }[]' is not assignable to type { player_id: string; elo_change: number; }[]'.`
4 replies
DTDrizzle Team
Created by Faust(Thuen) on 10/16/2023 in #help
Reuse with in multiple queries
This is what I currently do, but I would like to be able to reuse the with clause in multiple queries. Is there a way to do this?
export const matchesWithPlayers = () =>
db.query.matches.findMany({
with: {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
},
});

export const playerMatches = (userId: string) =>
db.query.matches.findMany({
where: or(eq(matches.playerOne, userId), eq(matches.playerTwo, userId)),
with: {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
},
});
export const matchesWithPlayers = () =>
db.query.matches.findMany({
with: {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
},
});

export const playerMatches = (userId: string) =>
db.query.matches.findMany({
where: or(eq(matches.playerOne, userId), eq(matches.playerTwo, userId)),
with: {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
},
});
I would like something like this, but when I do it this way I end up with wrong types
const withPlayers = {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
};

export const matchesWithPlayers = () =>
db.query.matches.findMany({
with: withPlayers,
});

export const playerMatches = (userId: string) =>
db.query.matches.findMany({
where: or(eq(matches.playerOne, userId), eq(matches.playerTwo, userId)),
with: withPlayers,
});
const withPlayers = {
playerOne: {
columns: {
id: true,
name: true,
elo: true,
},
},
playerTwo: {
columns: {
id: true,
name: true,
elo: true,
},
},
};

export const matchesWithPlayers = () =>
db.query.matches.findMany({
with: withPlayers,
});

export const playerMatches = (userId: string) =>
db.query.matches.findMany({
where: or(eq(matches.playerOne, userId), eq(matches.playerTwo, userId)),
with: withPlayers,
});
3 replies