K
Kysely8mo ago
tzezar

Select by id?

With this code:
export const getProductById: RequestHandler = async (req, res, next) => {
const { id } = req.params
try {
const result = await db.selectFrom("product").where('id', '=', id).selectAll().execute();
res.send(result);
} catch (err) {
next(err);
}
};
export const getProductById: RequestHandler = async (req, res, next) => {
const { id } = req.params
try {
const result = await db.selectFrom("product").where('id', '=', id).selectAll().execute();
res.send(result);
} catch (err) {
next(err);
}
};
I get type script error on in in query:
Argument of type 'string' is not assignable to parameter of type 'OperandValueExpressionOrList<PublicSchema, "product", "id">'.ts(2345)
const id: string
Argument of type 'string' is not assignable to parameter of type 'OperandValueExpressionOrList<PublicSchema, "product", "id">'.ts(2345)
const id: string
any clue how to fix it?
Solution:
That is because Kanel create a "special" type for the id
Jump to solution
5 Replies
tzezar
tzezar8mo ago
actually, I can't pass either a string or an int there, I used kenel to introspect database
Solution
Meithz (Razioner)
That is because Kanel create a "special" type for the id
Meithz (Razioner)
The type is
export type [TableName]Id = string || number & {
__flavor: 'TableNameId'
}
export type [TableName]Id = string || number & {
__flavor: 'TableNameId'
}
Something like that So when wanting to find something by Id you has to cast it to the appropiate type They have it explained in the kanel docs
tzezar
tzezar8mo ago
Thank you very much for fast response. I missed that part in docs.
koskimas
koskimas8mo ago
It's not in kysely docs since we are not the authors of kanel-kysely. It's a third party library