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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# Note Feature Implementation Roadmap
This document outlines the implementation plan for adding Anki-compatible "Note" concept to Kioku.
## Overview
Currently, Kioku uses a simple Card model with `front` and `back` text fields. To improve Anki interoperability, we will introduce:
1. **Note** - A container for field values (similar to Anki's Note)
2. **NoteType** - Defines the structure of notes (fields and card templates)
3. **Card** - Generated from Notes, holds FSRS scheduling state
### Key Concepts
- **One Note → Multiple Cards**: A "Basic (and reversed)" note type creates 2 cards (front→back, back→front)
- **One Note → One Card**: A "Basic" note type creates 1 card
- **Shared Content**: When note content is edited, all generated cards reflect the change
- **Independent Scheduling**: Each card has its own FSRS state (due date, stability, etc.)
## Data Model Design
### New Entities
```
NoteType
├── id: UUID
├── user_id: UUID (FK → users)
├── name: string
├── front_template: string (mustache template, e.g., "{{Front}}")
├── back_template: string (mustache template, e.g., "{{Back}}")
├── is_reversible: boolean (if true, creates reversed card too)
├── created_at: timestamp
├── updated_at: timestamp
├── deleted_at: timestamp (soft delete)
└── sync_version: number
NoteFieldType
├── id: UUID
├── note_type_id: UUID (FK → note_types)
├── name: string (e.g., "Front", "Back")
├── order: number (display order)
├── field_type: enum ("text") // Fixed to "text" for now
├── created_at: timestamp
├── updated_at: timestamp
├── deleted_at: timestamp
└── sync_version: number
Note
├── id: UUID
├── deck_id: UUID (FK → decks)
├── note_type_id: UUID (FK → note_types)
├── created_at: timestamp
├── updated_at: timestamp
├── deleted_at: timestamp
└── sync_version: number
NoteFieldValue
├── id: UUID
├── note_id: UUID (FK → notes)
├── note_field_type_id: UUID (FK → note_field_types)
├── value: text
├── created_at: timestamp
├── updated_at: timestamp
└── sync_version: number
Card (modified)
├── id: UUID
├── deck_id: UUID (FK → decks)
├── note_id: UUID (FK → notes) [REQUIRED]
├── is_reversed: boolean [REQUIRED] (false=normal, true=reversed)
├── front: text [CACHED - rendered from template for performance]
├── back: text [CACHED - rendered from template for performance]
├── (FSRS fields unchanged)
├── created_at: timestamp
├── updated_at: timestamp
├── deleted_at: timestamp
└── sync_version: number
```
### Card Display Logic
Template rendering uses a custom mustache-like renderer (no external dependencies).
Syntax: `{{FieldName}}` is replaced with the field value.
When displaying a card:
- **Normal card** (`is_reversed = false`): Render `front_template` on front, `back_template` on back
- **Reversed card** (`is_reversed = true`): Render `back_template` on front, `front_template` on back
Example templates:
- Simple: `{{Front}}`
- With text: `Q: {{Front}}`
- Multiple fields: `{{Word}} - {{Reading}}`
### Built-in Note Types
Create these as default note types for each user:
1. **Basic**
- Fields: Front, Back
- front_template: `{{Front}}`, back_template: `{{Back}}`
- is_reversible: false
2. **Basic (and reversed card)**
- Fields: Front, Back
- front_template: `{{Front}}`, back_template: `{{Back}}`
- is_reversible: true
### Behavior Rules
**Updating `is_reversible`:**
- Changing `is_reversible` does NOT affect existing cards
- Only affects new note creation (whether to create 1 or 2 cards)
- Existing reversed cards remain even if `is_reversible` is set to `false`
**Deletion Constraints (enforced by FK constraints + application logic):**
- **NoteType**: Cannot delete if any Notes reference it
- **NoteFieldType**: Cannot delete if any NoteFieldValues reference it
- **Note**: Deleting a Note cascades soft-delete to all its Cards
- **Card**: Deleting a Card also deletes its Note (and all sibling Cards via cascade)
**Required Tests for Deletion:**
- [ ] Attempt to delete NoteType with existing Notes → should fail
- [ ] Attempt to delete NoteFieldType with existing NoteFieldValues → should fail
- [ ] Delete Note → verify all related Cards are soft-deleted
- [ ] Delete Card → verify Note and all sibling Cards are soft-deleted
## Implementation Phases
### Phase 1: Database Schema
**Tasks:**
- [x] Add `note_types` table schema (Drizzle)
- [x] Add `note_field_types` table schema
- [x] Add `notes` table schema
- [x] Add `note_field_values` table schema
- [x] Modify `cards` table: add `note_id`, `is_reversed` columns (nullable initially)
- [x] Create migration file
- [x] Add Zod validation schemas
**Files to modify:**
- `src/server/db/schema.ts`
- `src/server/schemas/index.ts`
- `drizzle/` (new migration)
### Phase 2: Server Repositories
**Tasks:**
- [x] Create `NoteTypeRepository`
- CRUD operations
- Include fields when fetching
- [x] Create `NoteTypeFieldRepository`
- CRUD operations
- Reorder fields
- [x] Create `NoteRepository`
- Create note with field values (auto-generate cards based on `is_reversible`)
- Update note (updates field values)
- Delete note (cascade soft-delete to cards)
- [x] Modify `CardRepository`
- Fetch card with note data for display
- Support note-based card creation
**Files to create/modify:**
- `src/server/repositories/noteType.ts` (new)
- `src/server/repositories/note.ts` (new)
- `src/server/repositories/card.ts` (modify)
- `src/server/repositories/types.ts` (modify)
### Phase 3: Server API Routes
**Tasks:**
- [x] Add NoteType routes
- `GET /api/note-types` - List user's note types
- `POST /api/note-types` - Create note type
- `GET /api/note-types/:id` - Get note type with fields
- `PUT /api/note-types/:id` - Update note type (name, front_template, back_template, is_reversible)
- `DELETE /api/note-types/:id` - Soft delete
- [x] Add NoteFieldType routes (nested under note-types)
- `POST /api/note-types/:id/fields` - Add field
- `PUT /api/note-types/:id/fields/:fieldId` - Update field
- `DELETE /api/note-types/:id/fields/:fieldId` - Remove field
- `PUT /api/note-types/:id/fields/reorder` - Reorder fields
- [x] Add Note routes
- `GET /api/decks/:deckId/notes` - List notes in deck
- `POST /api/decks/:deckId/notes` - Create note (auto-generates cards)
- `GET /api/decks/:deckId/notes/:noteId` - Get note with field values
- `PUT /api/decks/:deckId/notes/:noteId` - Update note field values
- `DELETE /api/decks/:deckId/notes/:noteId` - Delete note and its cards
- [x] Modify Card routes
- Update GET to include note data when available
- [x] Modify Study routes
- Fetch note/field data for card display
**Files to create/modify:**
- `src/server/routes/noteTypes.ts` (new)
- `src/server/routes/notes.ts` (new)
- `src/server/routes/cards.ts` (modify)
- `src/server/routes/study.ts` (modify)
- `src/server/index.ts` (register new routes)
### Phase 4: Client Database (Dexie)
**Tasks:**
- [x] Add `LocalNoteType` interface and table
- [x] Add `LocalNoteFieldType` interface and table
- [x] Add `LocalNote` interface and table
- [x] Add `LocalNoteFieldValue` interface and table
- [x] Modify `LocalCard` interface: add `noteId`, `isReversed`
- [x] Update Dexie schema version and upgrade handler
- [x] Create client repositories for new entities
**Files to modify:**
- `src/client/db/index.ts`
- `src/client/db/repositories.ts`
### Phase 5: Sync Logic
**Tasks:**
- [x] Add sync for NoteType, NoteFieldType
- [x] Add sync for Note, NoteFieldValue
- [x] Update Card sync to include `noteId`, `isReversed`
- [x] Define sync order (NoteTypes → Notes → Cards)
- [x] Update pull/push sync handlers
**Files to modify:**
- `src/server/routes/sync.ts`
- `src/server/repositories/sync.ts`
- `src/client/sync/push.ts`
- `src/client/sync/pull.ts`
### Phase 6: Frontend - Note Type Management
**Tasks:**
- [x] Create NoteType list page (`/note-types`)
- [x] Create NoteType editor component
- Edit name
- Manage fields (add/remove/reorder)
- Edit front/back templates (mustache syntax)
- Toggle `is_reversible` option
- [x] Add navigation to note type management
**Files to create:**
- `src/client/pages/NoteTypesPage.tsx`
- `src/client/components/NoteTypeEditor.tsx`
- `src/client/components/FieldEditor.tsx`
### Phase 7: Frontend - Note CRUD
**Tasks:**
- [x] Update CreateCardModal → CreateNoteModal
- Select note type
- Dynamic field inputs based on note type
- Preview generated cards
- [x] Update EditCardModal → EditNoteModal
- Load note and field values
- Update all generated cards on save
- [x] Update DeckDetailPage
- Group cards by note
- Show note-level actions (edit note, delete note)
- Display whether card is normal or reversed
**Files to modify:**
- `src/client/components/CreateCardModal.tsx` → `CreateNoteModal.tsx`
- `src/client/components/EditCardModal.tsx` → `EditNoteModal.tsx`
- `src/client/pages/DeckDetailPage.tsx`
### Phase 8: Frontend - Study Page
**Tasks:**
- [x] Create custom template renderer utility
- [x] Update StudyPage to render cards based on note data
- Fetch note field values
- Use `isReversed` to determine which template to use for front/back
- Render templates with custom renderer
- Maintain front/back flip behavior
- [x] Handle both legacy cards (direct front/back) and new note-based cards
**Files to modify/create:**
- `src/client/utils/templateRenderer.ts` (new)
- `src/client/pages/StudyPage.tsx`
### Phase 9: Cleanup & Documentation
**Tasks:**
- [x] Make `note_id` and `is_reversed` NOT NULL (schema migration)
- [x] Update architecture.md with Note-based data model
- [ ] E2E tests for study flow with note-based cards
- [ ] Performance testing with multiple cards per note
**Note:** Data migration is not needed since production has no legacy data.
## Design Notes
### Card front/back Fields
Cards retain `front` and `back` text fields as **cached rendered content**. When a card is created from a note:
1. Templates are rendered with field values
2. Rendered content is stored in `front`/`back` for quick display
3. When note content is updated, all related cards are re-rendered
This approach avoids rendering templates on every card display while maintaining the Note as the source of truth.
## API Examples
### Create Note
```http
POST /api/decks/:deckId/notes
Content-Type: application/json
{
"noteTypeId": "uuid-of-basic-reversed",
"fields": {
"field-uuid-front": "What is the capital of Japan?",
"field-uuid-back": "Tokyo"
}
}
```
Response:
```json
{
"note": {
"id": "note-uuid",
"noteTypeId": "...",
"deckId": "...",
"fieldValues": [...]
},
"cards": [
{ "id": "card-1-uuid", "isReversed": false },
{ "id": "card-2-uuid", "isReversed": true }
]
}
```
### Get Cards for Study
```http
GET /api/decks/:deckId/study
```
Response:
```json
{
"cards": [
{
"id": "card-uuid",
"noteId": "note-uuid",
"isReversed": false,
"fieldValues": {
"Front": "What is the capital of Japan?",
"Back": "Tokyo"
},
"state": 0,
"due": "2024-01-15T00:00:00Z",
...
}
]
}
```
Note: Templates (`front_template`, `back_template`) are fetched separately via NoteType API and cached on the client. The client renders the card display using the template and field values.
## Testing Checklist
- [x] Unit tests for all new repositories
- [x] Integration tests for note CRUD
- [x] Test card generation from different note types
- [x] Test sync with note data
- [ ] E2E tests for study flow with note-based cards
|