TRPC Drizzle ORM Generic Query - issue with types

382 views Asked by At

I'm trying to create a generic method called createAllQuery, which would create an all query for the trpc router. I've tried two approaches: 1.

export function createAllQuery(getTable) {
  return protectedProcedure.query(({ ctx }) => {
    return getTable(ctx.db).findMany({
      where: (table, { eq }) => eq(table.isDeleted, false),
    });
  });
}

//Example usage : 

export const postsRouter = createTRPCRouter({
  all: createAllQuery((db)=>db.posts)),
  //...
});

export function createAllQuery(table) {
  return protectedProcedure.query(({ ctx }) => {
    const tableQuery = ctx.db.query[table._.name ];
    
    if (!tableQuery) {
      throw new TRPCError({
        code: "INTERNAL_SERVER_ERROR",
        message: "database table not found. " + table._.name,
      });
    }

    return tableQuery.findMany({
      where: (table, { eq }) => eq(table.isDeleted, false),
    });
  });
}

//Example usage : 

export const postsRouter = createTRPCRouter({
  all: createAllQuery<typeof posts>(posts),
  //...
});

  • I've tried to use types imported from trpc and drizzle, but I'm struggling to make it right.
  • I've tried to look for the answer online.
  • I searched for information in Drizzle and trpc docs, yet did not find anything that solved my issues.

**so the question is how to align TS types to this method? **

Any suggestions on how to address this issue would be appreciated.

0

There are 0 answers