conditional select return wrong type

const registeree = (getCount?: boolean) => {
return tx
.select({
...(getCount ? { total: count() } : {}),
})
.from(eventHasParticipant)
.innerJoin(events, eq(eventHasParticipant.eventId, events.id))
.innerJoin(
participants,
eq(eventHasParticipant.participantId, participants.id)
)
.where(and(eq(events.id, eventID), eq(events.createdBy, createdBy)));
};

const total = await registeree(true).limit(limit).offset((page - 1) * limit);
const registereeResult = await registeree();

total[0].total = total[0].total || 0;

return { total: total[0].total, registereeResult };
const registeree = (getCount?: boolean) => {
return tx
.select({
...(getCount ? { total: count() } : {}),
})
.from(eventHasParticipant)
.innerJoin(events, eq(eventHasParticipant.eventId, events.id))
.innerJoin(
participants,
eq(eventHasParticipant.participantId, participants.id)
)
.where(and(eq(events.id, eventID), eq(events.createdBy, createdBy)));
};

const total = await registeree(true).limit(limit).offset((page - 1) * limit);
const registereeResult = await registeree();

total[0].total = total[0].total || 0;

return { total: total[0].total, registereeResult };
so I've tried using conditional select query, so that i'm able to reuse my query syntax without repeating to get the rows and the count, I'm not sure where I'm wrong at be when I do this the registereeResult would return this object instead
const registereeResult: {
total?: number | undefined;
}[]
const registereeResult: {
total?: number | undefined;
}[]
2 Replies
Mykhailo
Mykhailo8mo ago
Hello, @Vigne! Am I right, that you use this function for getting rows & count, so you want to have type like this:
const registereeResult: {
// your columns
total?: number | undefined;
}[]
const registereeResult: {
// your columns
total?: number | undefined;
}[]
Mykhailo
Mykhailo8mo ago
If so, try do smth like this:
const getUsers = async (getCount?: boolean) => {
return await db.select(getCount ? { total: count() } : getTableColumns(users)).from(users);
};


const total = await getUsers(true);
const rows = await getUsers();
const getUsers = async (getCount?: boolean) => {
return await db.select(getCount ? { total: count() } : getTableColumns(users)).from(users);
};


const total = await getUsers(true);
const rows = await getUsers();
No description
Want results from more Discord servers?
Add your server