-
Hi everyone If Drizzle is returned, the type is displayed as any. Version
There is an API whyany that uses Drizzle to return post. const post = new Hono<{ Bindings: Bindings }>()
.get("/whyany", dbCon, async (c) => {
const post = await c.var.db().select({title: schema.posts.title}).from(schema.posts).limit(1);
return c.json(post);
}) Request whyany root with hc will show any. import type { AppType } from "@acme/api";
import { hc } from "hono/client";
const rpc = hc<AppType>("http://localhost:3100");
// any
const whyany = await rpc.api.post.whyany.$get(); This works, but it is hard to make type with zod’s pick. (Relation, etc.) .get("/drizzleZod", dbCon, async (c) => {
const posts = await c.var.db().query.posts.findMany();
// generate type from schema(drizzle-zod)
const selectPostsSchema = createSelectSchema(schema.posts);
const type = selectPostsSchema.array();
// const drizzleZod: ClientResponse<{
// data: {
// id: number;
// slug: string;
// ......
// }[];
// }>
return c.json(type.parse(posts));
}) This also works, but create the type from the schema. .get("/typeofInferSelect", dbCon, async (c) => {
const posts = await c.var.db().query.posts.findMany();
// generate type from schema(typeof inferSelect)
type type = typeof schema.posts.$inferSelect;
// const typeofInferSelect: ClientResponse<{
// id: number;
// slug: string;
// ......
// }[]>
return c.json<type[]>(posts);
}) This one creates a type from Query, but it is displayed as [x: string]: never; .get("/typeofQuery", dbCon, async (c) => {
// Mettya Long Select
const sameCategoryPosts = c.var.db()
.select({
title: schema.posts.title,
slug: schema.posts.slug,
thumbnailUrl: schema.posts.thumbnailUrl,
})
.from(schema.posts)
.where(
and(
eq(schema.posts.isPublish, true),
eq(schema.posts.categorySlug, "slug1"),
),
)
.orderBy(desc(schema.posts.id))
.limit(10);
const recentPosts = c.var.db()
.select({
title: schema.posts.title,
slug: schema.posts.slug,
thumbnailUrl: schema.posts.thumbnailUrl,
})
.from(schema.posts)
.where(
and(
eq(schema.posts.isPublish, true),
ne(schema.posts.categorySlug, "slug1"),
),
)
.orderBy(desc(schema.posts.id))
.limit(10);
const resultQuery = await union(sameCategoryPosts, recentPosts).limit(10);
// generate type from schema(typeof resultQuery)
type type = typeof resultQuery;
// const typeofQuery: ClientResponse<({
// [x: string]: never;
// } | {
// [x: string]: never;
// } | {
// [x: string]: never;
// } | {})[]>
return c.json<type>(resultQuery);
}); test repository |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @sori883 I have tried to clone your project, but I am not sure where to look to find the problem. If you think it is a bug, please create a minimal project that reproduces it. |
Beta Was this translation helpful? Give feedback.
Hi @sori883
I have tried to clone your project, but I am not sure where to look to find the problem. If you think it is a bug, please create a minimal project that reproduces it.