diff --git a/backend/src/database/schemas/api_keys.ts b/backend/src/database/schemas/api_keys.ts new file mode 100644 index 0000000..ea58bbe --- /dev/null +++ b/backend/src/database/schemas/api_keys.ts @@ -0,0 +1,18 @@ +import { pgTable, varchar, timestamp, uuid, index, boolean } from 'drizzle-orm/pg-core'; +import { users } from './users'; + +export const apiKeys = pgTable('api_keys', { + id: uuid('id').primaryKey().defaultRandom(), + userId: uuid('user_id').notNull().references(() => users.uuid, { onDelete: 'cascade' }), + keyHash: varchar('key_hash', { length: 128 }).notNull().unique(), // Haché pour la sécurité (SHA-256) + name: varchar('name', { length: 128 }).notNull(), // Nom donné par l'utilisateur (ex: "My App") + prefix: varchar('prefix', { length: 8 }).notNull(), // Pour identification visuelle (ex: "mg_...") + isActive: boolean('is_active').notNull().default(true), + lastUsedAt: timestamp('last_used_at', { withTimezone: true }), + expiresAt: timestamp('expires_at', { withTimezone: true }), + createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), + updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(), +}, (table) => ({ + userIdIdx: index('api_keys_user_id_idx').on(table.userId), + keyHashIdx: index('api_keys_key_hash_idx').on(table.keyHash), +}));