feat: add API keys schema with Drizzle ORM integration
This commit is contained in:
18
backend/src/database/schemas/api_keys.ts
Normal file
18
backend/src/database/schemas/api_keys.ts
Normal file
@@ -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),
|
||||
}));
|
||||
Reference in New Issue
Block a user