dbos-inc / dbos-transact

The TypeScript framework for backends that scale
https://docs.dbos.dev
MIT License
291 stars 20 forks source link

Consider changing the middleware api #457

Open demetris-manikas opened 1 month ago

demetris-manikas commented 1 month ago

This is a suggestion for v2 The following type definition in httpServer/middleware.ts looks a bit odd (somewhat golang like).

query<C extends UserDatabaseClient, R, T extends unknown[]>(qry: (dbclient: C, ...args: T) => Promise<R>, ...args: T): Promise<R>;

The following definition is a lot cleaner.

  query<C extends UserDatabaseClient, R>(qry: (dbclient: C) => Promise<R>): 

This is how I would write my code (I can do it already) No need for any extra args defined but just the dbclient,

let username = getItFromSomewhere();
const u = await ctx.query(
    (dbClient: Knex) => {
      return dbClient<UserTable>(userTableName).select("username").where({ username: username })
    }
);

Instead of

let user = getItFromSomewhere();
const u = await ctx.query(
    (dbClient: Knex, user: string) => {
      return dbClient<UserTable>(userTableName).select("username").where({ username: user })
    },
    user
);