Generic update component using table schema – how to type the table argument?

SOLVED IN BOTTOM COMMENT

I'm attempting a generic update checkbox component where I can pass a type of MySQL (PlanetScale) table, and an associated keyName, and use those params in the db.update() query.

Can anyone guide me on how to type the table argument?

import * as schema from '@/db/schema'
import { AnyMySqlTable } from 'drizzle-orm/mysql-core' // just found this, testing now
import { eq } from 'drizzle-orm'

export function CheckboxUpdate<T extends AnyMySqlTable>({ ...args }: {
  model: T
  keyName: keyof T & string
  id: number
}) {
  return (
    <input
      type='checkbox'
      onChange={async (e) => {
        'use server' // pseudo code
        await db
          .update(args.model)
          .set({ [args.keyName]: e.target.checked })
          .where(eq(args.model.id, args.id))
      }}
    />
  )
}

await CheckboxUpdate<typeof users>({
  model: users,
  keyName: 'isSubscriber',
  id: 1,
})
Was this page helpful?