lookup-user.ts 846 B

123456789101112131415161718192021222324252627282930313233
  1. import { Database, eq } from "../src/drizzle/index.js"
  2. import { AuthTable } from "../src/schema/auth.sql"
  3. // get input from command line
  4. const email = process.argv[2]
  5. if (!email) {
  6. console.error("Usage: bun lookup-user.ts <email>")
  7. process.exit(1)
  8. }
  9. const authData = await printTable("Auth", (tx) =>
  10. tx.select().from(AuthTable).where(eq(AuthTable.subject, email)),
  11. )
  12. if (authData.length === 0) {
  13. console.error("User not found")
  14. process.exit(1)
  15. }
  16. await printTable("Auth", (tx) =>
  17. tx.select().from(AuthTable).where(eq(AuthTable.accountID, authData[0].accountID)),
  18. )
  19. function printTable(
  20. title: string,
  21. callback: (tx: Database.TxOrDb) => Promise<any[]>,
  22. ): Promise<any[]> {
  23. return Database.use(async (tx) => {
  24. const data = await callback(tx)
  25. console.log(`== ${title} ==`)
  26. console.table(data)
  27. return data
  28. })
  29. }