All files / web/src/db/schema auth-accounts.ts

100% Statements 47/47
100% Branches 4/4
100% Functions 1/1
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 482x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 39x 39x 39x 39x 39x 39x 2x 2x 2x 2x  
import { createId } from '@paralleldrive/cuid2'
import { integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core'
import { users } from './users'
 
/**
 * Auth accounts table - links OAuth providers to users
 *
 * A user can have multiple linked providers (Google + email).
 * Used by the custom NextAuth adapter to look up users by provider.
 */
export const authAccounts = sqliteTable(
  'auth_accounts',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => createId()),
 
    /** Foreign key to users table - cascades on delete */
    userId: text('user_id')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
 
    /** Provider name: 'google', 'email' */
    provider: text('provider').notNull(),
 
    /** Provider-specific account ID (Google sub ID, or email address for magic links) */
    providerAccountId: text('provider_account_id').notNull(),
 
    /** Account type: 'oauth', 'email' */
    type: text('type').notNull(),
 
    /** When this provider link was created */
    createdAt: integer('created_at', { mode: 'timestamp' })
      .notNull()
      .$defaultFn(() => new Date()),
  },
  (table) => ({
    /** Unique constraint: one account per provider */
    providerIdx: uniqueIndex('auth_accounts_provider_idx').on(
      table.provider,
      table.providerAccountId
    ),
  })
)
 
export type AuthAccount = typeof authAccounts.$inferSelect
export type NewAuthAccount = typeof authAccounts.$inferInsert