diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts index d5c86ad..1225787 100644 --- a/src/db/schema/index.ts +++ b/src/db/schema/index.ts @@ -2,3 +2,5 @@ export * from './enums.js'; export * from './users.js'; export * from './sessions.js'; export * from './subscriptions.js'; +export * from './tests.js'; +export * from './testQuestions.js'; diff --git a/src/db/schema/testQuestions.ts b/src/db/schema/testQuestions.ts new file mode 100644 index 0000000..1113796 --- /dev/null +++ b/src/db/schema/testQuestions.ts @@ -0,0 +1,25 @@ +import { pgTable, uuid, text, integer, boolean, timestamp } from 'drizzle-orm/pg-core'; +import { jsonb } from 'drizzle-orm/pg-core'; +import { questionTypeEnum } from './enums.js'; +import { tests } from './tests.js'; +import { questionBank } from './questionBank.js'; + +export const testQuestions = pgTable('test_questions', { + id: uuid('id').primaryKey().defaultRandom(), + testId: uuid('test_id') + .notNull() + .references(() => tests.id, { onDelete: 'cascade' }), + questionBankId: uuid('question_bank_id').references(() => questionBank.id, { onDelete: 'set null' }), + orderNumber: integer('order_number').notNull(), + type: questionTypeEnum('type').notNull(), + questionText: text('question_text').notNull(), + options: jsonb('options').$type>(), + correctAnswer: jsonb('correct_answer').$type().notNull(), + explanation: text('explanation').notNull(), + userAnswer: jsonb('user_answer').$type(), + isCorrect: boolean('is_correct'), + answeredAt: timestamp('answered_at', { withTimezone: true }), +}); + +export type TestQuestion = typeof testQuestions.$inferSelect; +export type NewTestQuestion = typeof testQuestions.$inferInsert; diff --git a/src/db/schema/tests.ts b/src/db/schema/tests.ts new file mode 100644 index 0000000..aa51768 --- /dev/null +++ b/src/db/schema/tests.ts @@ -0,0 +1,22 @@ +import { pgTable, uuid, integer, timestamp } from 'drizzle-orm/pg-core'; +import { stackEnum, levelEnum, testModeEnum, testStatusEnum } from './enums.js'; +import { users } from './users.js'; + +export const tests = pgTable('tests', { + id: uuid('id').primaryKey().defaultRandom(), + userId: uuid('user_id') + .notNull() + .references(() => users.id, { onDelete: 'cascade' }), + stack: stackEnum('stack').notNull(), + level: levelEnum('level').notNull(), + questionCount: integer('question_count').notNull(), + mode: testModeEnum('mode').notNull().default('fixed'), + status: testStatusEnum('status').notNull().default('in_progress'), + score: integer('score'), + startedAt: timestamp('started_at', { withTimezone: true }).notNull().defaultNow(), + finishedAt: timestamp('finished_at', { withTimezone: true }), + timeLimitSeconds: integer('time_limit_seconds'), +}); + +export type Test = typeof tests.$inferSelect; +export type NewTest = typeof tests.$inferInsert;