anki-reader
Version:
A library for reading Anki apkg and collection files.
166 lines (165 loc) • 4.7 kB
JavaScript
import { Deck } from './Deck.js';
export class AnkiCollection {
db;
crt;
mod;
scm;
decks;
version;
config;
models;
dconf;
tags;
constructor(db) {
this.db = db;
}
getDecks() {
if (this.decks != null) {
return {
...this.decks
};
}
const result = this.db.exec('SELECT decks FROM col');
if (result == null || result.length === 0) {
this.decks = {};
return {};
}
const deckJsons = JSON.parse(result[0].values[0][0]?.toString() ?? '{}');
const decks = {};
for (const [deckId, deckJson] of Object.entries(deckJsons)) {
decks[deckId] = new Deck(deckId, deckJson, this.db);
}
this.decks = decks;
return {
...this.decks
};
}
getCreationTime() {
if (this.crt != null) {
return this.crt;
}
const result = this.db.exec('SELECT crt FROM col');
if (result == null || result.length === 0) {
this.crt = new Date();
return this.crt;
}
const crt = result[0].values[0][0];
// multiply by 1000 to convert seconds to milliseconds (only for crt)
this.crt = typeof crt === 'number' ? new Date(crt * 1000) : new Date();
return this.crt;
}
getModificationTime() {
if (this.mod != null) {
return this.mod;
}
const result = this.db.exec('SELECT mod FROM col');
if (result == null || result.length === 0) {
this.mod = new Date();
return this.mod;
}
const mod = result[0].values[0][0];
this.mod = typeof mod === 'number' ? new Date(mod) : new Date();
return this.mod;
}
getSchemaModificationTime() {
if (this.scm != null) {
return this.scm;
}
const result = this.db.exec('SELECT scm FROM col');
if (result == null || result.length === 0) {
this.scm = new Date();
return this.scm;
}
const scm = result[0].values[0][0];
this.scm = typeof scm === 'number' ? new Date(scm) : new Date();
return this.scm;
}
getVersion() {
if (this.version != null) {
return this.version;
}
const result = this.db.exec('SELECT ver FROM col');
if (result == null || result.length === 0) {
this.version = 0;
return this.version;
}
const ver = result[0].values[0][0];
this.version = typeof ver === 'number' ? ver : 0;
return this.version;
}
getConfig() {
if (this.config != null) {
return {
...this.config
};
}
const result = this.db.exec('SELECT conf FROM col');
if (result == null || result.length === 0) {
this.config = {};
return {};
}
const config = JSON.parse(result[0].values[0][0]?.toString() ?? '{}');
this.config = config;
return {
...this.config
};
}
getModels() {
if (this.models != null) {
return {
...this.models
};
}
const result = this.db.exec('SELECT models FROM col');
if (result == null || result.length === 0) {
this.models = {};
return {};
}
const models = JSON.parse(result[0].values[0][0]?.toString() ?? '{}');
this.models = models;
return {
...this.models
};
}
getDconf() {
if (this.dconf != null) {
return {
...this.dconf
};
}
const result = this.db.exec('SELECT dconf FROM col');
if (result == null || result.length === 0) {
this.dconf = {};
return {
...this.dconf
};
}
const dconf = JSON.parse(result[0].values[0][0]?.toString() ?? '{}');
this.dconf = dconf;
return {
...this.dconf
};
}
getTags() {
if (this.tags != null) {
return {
...this.tags
};
}
const result = this.db.exec('SELECT tags FROM col');
if (result == null || result.length === 0) {
this.tags = {};
return {
...this.tags
};
}
const tags = JSON.parse(result[0].values[0][0]?.toString() ?? '{}');
this.tags = tags;
return {
...this.tags
};
}
getRawCollection() {
return this.db;
}
}