UNPKG

anki-reader

Version:

A library for reading Anki apkg and collection files.

67 lines (66 loc) 1.76 kB
import { Card } from './Card.js'; export class Deck { id; deckJson; db; cards; name; desc; constructor(id, deckJson, db) { this.id = id; this.deckJson = deckJson; this.db = db; } getId() { return this.id; } getCards() { if (this.cards != null) { return { ...this.cards }; } // join cards on notes to get the card data // TODO: cards and notes share some fields, so we need to alias them const result = this.db.exec(` SELECT cards.*, flds, sfld, mid FROM cards JOIN notes ON notes.id = cards.nid WHERE cards.did = ${this.id} `); if (result == null || result.length === 0) { this.cards = {}; return {}; } const cards = {}; for (const card of result[0].values) { // match columns to card data const cardData = {}; for (let i = 0; i < result[0].columns.length; i++) { cardData[result[0].columns[i]] = card[i]; } const id = cardData.id?.toString() ?? ''; cards[id] = new Card(id, cardData, this.id); } this.cards = cards; return { ...this.cards }; } getName() { if (this.name != null) { return this.name; } this.name = this.deckJson.name ?? ''; return this.deckJson.name ?? ''; } getDescription() { if (this.desc != null) { return this.desc; } this.desc = this.deckJson.desc ?? ''; return this.deckJson.desc ?? ''; } getRawDeck() { return this.deckJson; } }