Domain (@cards/domain):
- zod-Schemas SSOT für Deck, Card, Review, StudySession, FsrsSettings,
Tools (cards.create + cards.search Input/Output)
- CardType-Discriminated-Union: MVP basic+basic-reverse, Future-Set
(cloze, type-in, image-occlusion, audio, multiple-choice) für
Schema-stable-Migration vorbereitet
- validateFieldsForType() Pure-Function pro CardType
- FSRS-Adapter über ts-fsrs v5.3.2: newReview, gradeReview,
subIndexCount, toFsrsCard/fromFsrsCard ISO↔Date-Roundtrip
- Encryption-Hinweis: reviews bleiben PLAINTEXT (Scheduler quert
täglich `due <= now`, siehe Lessons §3)
Drizzle-Schemas (apps/api/src/db/schema, alles in pgSchema('cards')):
- decks, cards, card_tags, reviews (PK card_id+sub_index), study_sessions,
tags (deck-skopiert), media_refs (verweist auf mana-media), import_jobs
- _schema.ts-Pattern um Zirkular-Imports zu vermeiden (Lesson aus
mana-share/-events während F-0)
- Hot-Path-Index reviews_user_due_idx für Scheduler-Queries
Routes (apps/api/src/routes):
- POST/GET/PATCH/DELETE /api/v1/decks (Deck-CRUD)
- POST/GET/PATCH/DELETE /api/v1/cards (Card-CRUD mit Auto-Reviews-Init:
beim Card-Insert werden N Reviews via subIndexCount(type) angelegt,
in einer Transaktion)
- GET /api/v1/reviews/due (Hot-Path, optional deck_id-Filter, Limit 500)
- POST /api/v1/reviews/:cardId/:subIndex/grade (FSRS-State-Transition,
per-Deck FSRS-Settings)
Auth: Stub-Middleware liest X-User-Id-Header (Phase 2 ersetzt durch
@mana/shared-hono authMiddleware mit JWKS-Cache).
Tests (vitest, Hono app.request()):
- @cards/domain: fsrs.test.ts (newReview, gradeReview Roundtrip,
Rating-Mapping), schemas.test.ts (zod-strict-Variants, Field-Type-
Validation, hex-Color)
- apps/api: decks.test.ts + cards.test.ts + reviews.test.ts —
Auth-Gate + Input-Validation. Volle DB-Integrationstests folgen mit
pg-mem oder testcontainers in späterer Phase.
Cleanup: types.ts entfernt, zod-Schemas sind SSOT (z.infer für Types).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { index, jsonb, primaryKey, text, timestamp } from 'drizzle-orm/pg-core';
|
|
|
|
import { cardsSchema } from './_schema.ts';
|
|
import { decks } from './decks.ts';
|
|
|
|
/**
|
|
* Karten. `fields` ist ein generischer JSONB-Slot, der je nach `type`
|
|
* unterschiedliche Felder enthält:
|
|
*
|
|
* - basic / basic-reverse: { front, back }
|
|
* - cloze: { text, extra? }
|
|
* - type-in: { question, expected }
|
|
* - image-occlusion: { image_ref, mask_regions: [...] }
|
|
*
|
|
* MVP unterstützt nur `basic` und `basic-reverse`.
|
|
*
|
|
* `tags` ist ein eigener Tag-Mapping-Layer (siehe `tags.ts` + `cardTags`),
|
|
* NICHT in dieser Tabelle gespeichert.
|
|
*/
|
|
export const cards = cardsSchema.table(
|
|
'cards',
|
|
{
|
|
id: text('id').primaryKey(),
|
|
deckId: text('deck_id')
|
|
.notNull()
|
|
.references(() => decks.id, { onDelete: 'cascade' }),
|
|
userId: text('user_id').notNull(),
|
|
type: text('type').notNull(),
|
|
fields: jsonb('fields').notNull(),
|
|
mediaRefs: jsonb('media_refs').notNull().$type<string[]>().default([]),
|
|
contentHash: text('content_hash'),
|
|
createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' })
|
|
.notNull()
|
|
.defaultNow(),
|
|
updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' })
|
|
.notNull()
|
|
.defaultNow(),
|
|
},
|
|
(t) => ({
|
|
deckIdx: index('cards_deck_idx').on(t.deckId),
|
|
userIdx: index('cards_user_idx').on(t.userId),
|
|
})
|
|
);
|
|
|
|
export type CardRow = typeof cards.$inferSelect;
|
|
export type CardInsert = typeof cards.$inferInsert;
|
|
|
|
/**
|
|
* Card↔Tag-Mapping. PK ist (card_id, tag_id) — kein eigenes id-Feld nötig.
|
|
*/
|
|
export const cardTags = cardsSchema.table(
|
|
'card_tags',
|
|
{
|
|
cardId: text('card_id')
|
|
.notNull()
|
|
.references(() => cards.id, { onDelete: 'cascade' }),
|
|
tagId: text('tag_id').notNull(),
|
|
},
|
|
(t) => ({
|
|
pk: primaryKey({ columns: [t.cardId, t.tagId] }),
|
|
})
|
|
);
|
|
|
|
export type CardTagRow = typeof cardTags.$inferSelect;
|
|
export type CardTagInsert = typeof cardTags.$inferInsert;
|