How do I type a generic crud service?

I am attempting to create a generic Crud service that has methods to CRUD any table. All my attempts thus far has failed to overcome TS errors. I'm a novice at Drizzle and although proficient by no means an expert at TS. All hep is appreciated. Here is my latest attempt so far along with the TS errors:
import { Context } from "hono";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { getDatabase } from "../utils/db.utils";
import { PgTable, TableConfig } from "drizzle-orm/pg-core";
import { InferInsertModel, InferSelectModel, Table } from "drizzle-orm";

export abstract class DrizzleService<
  TTable extends Table,
  SelectModel = InferSelectModel<TTable>,
  InsertModel = InferInsertModel<TTable>
> {
  protected db: PostgresJsDatabase;
  async create(data: InsertModel): Promise<SelectModel> {
    try {
      const [newData] = await this.db
        .insert(this.table)
        .values([data]) // Error: No overload matches this call.  Overload 1 of 2, '(value: { [x: string]: any; }): PgInsertBase<Table<TableConfig<Column<any, object, object>>>, PostgresJsQueryResultHKT, undefined, false, never>', gave the following error.    Argument of type 'InsertModel' is not assignable to parameter of type '{ [x: string]: any; }'.  Overload 2 of 2, '(values: { [x: string]: any; }[]): PgInsertBase<Table<TableConfig<Column<any, object, object>>>, PostgresJsQueryResultHKT, undefined, false, never>', gave the following error.    Argument of type 'InsertModel' is not assignable to parameter of type '{ [x: string]: any; }[]'.ts(2769)generic.service.ts(10, 3): This type parameter might need an `extends { [x: string]: any; }` constraint.

        .returning();
      return newData; // Error: Type '{ [x: string]: any; }' is not assignable to type 'SelectModel'.  'SelectModel' could be instantiated with an arbitrary type which could be unrelated to '{ [x: string]: any; }'.ts(2322)
    } catch (error) {
    }
  }
}
Was this page helpful?