medromo
medromo
DTDrizzle Team
Created by patochem on 11/18/2023 in #help
How to get the return Type of a query with relations?
I would create an external helper function that makes the db query and then use the return type for the public user variable
const getUser = async (id: number) => {
return await db.query.users.findFirst({
where: .....,
with: {
posts: true
}
});
};

type UserWithPosts = Awaited<ReturnType<typeof getUser>>;

class myStore {
public user: UserWithPosts;

async loadUser(id: number) {
this.user = await getUser(id);
};
};
const getUser = async (id: number) => {
return await db.query.users.findFirst({
where: .....,
with: {
posts: true
}
});
};

type UserWithPosts = Awaited<ReturnType<typeof getUser>>;

class myStore {
public user: UserWithPosts;

async loadUser(id: number) {
this.user = await getUser(id);
};
};
This should do the trick
3 replies