I am trying to use Kysely as a query builder and prisma for migration
Prisma Model
model User {
id Int @id @default(autoincrement())
name String?
email String @unique
username String @unique
password String
role String @default("USER")
isActive Boolean @default(true)
profile Profile?
}
The generated types are
export type User = {
id: Generated<number>;
name: string | null;
email: string;
username: string;
password: string;
role: Generated<string>;
isActive: Generated<boolean>;
};
and for this query
export const findUserByEmail = async (email: string, username: string) => {
const user = await db
.selectFrom("User")
.selectAll()
.where((eb) => eb("email", "=", email).or("username", "=", username))
.execute();
return user[0];
};
user is of type
`const user: {
password: string;
id: number;
name: string | null;
email: string;
username: string;
role: string;
isActive: boolean;
}[]`
So when I use the USER type form generated one
import { User } from "../db/types";
const createAuthToken = (user: User): string => {
invariant(process.env.JWT_SECRET, "JWT_SECRET not set");
return jwt.sign({ userId: user.id }, process.env.JWT_SECRET, {
expiresIn: "90d",
});
};
Here type mismatch is happening the returned user has no generated but the generated one has and I am not able to understand how to solve this
router.post("/signin", async (req, res) => {
const { name, password, email, username } = req.body;
const user = await findUserByEmail(email, username);
if (!user) {
return res.send("Email/password combination is invalid");
}
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) return res.json("Email/password combination is invalid");
return res.send({ authToken: Auth.createAuthToken(user) });
});
Error message :
``
Argument of type '{ password: string; id: number; name: string | null; email: string; username: string; role: string; isActive: boolean; }' is not assignable to parameter of type 'User'.
Types of property 'id' are incompatible.
Type 'number' is not assignable to type 'ColumnType<number, number | undefined, number>'.ts(2345)
const user: {
password: string;
id: number;
name: string | null;
email: string;
username: string;
role: string;
isActive: boolean;
}
``
I am new to the codegen world any help would be really appreciated do I need to define types my self in this case?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too