diff options
Diffstat (limited to 'src/lib/db')
-rw-r--r-- | src/lib/db/index.ts | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 6bd417c..a767f70 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -348,11 +348,13 @@ class DatabaseHandler { page?: number; random?: boolean; }): Result<DeckResponse> { + console.time("fetchLesson-total"); const p = page ? page : 1; const size = count ? count : PAGE_SIZE; const offset = getDBOffset(p, size); const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); + console.time("fetchLesson-query"); const queryString = ` SELECT l.name, l.description, ll.lang as llang, cards.text, cards.note, cards.id as cid, @@ -403,10 +405,18 @@ class DatabaseHandler { // LIMIT 10; const query = this.db.query(queryString); const res = query.all(userId, lessonId, tomorrow.getTime(), size, offset); + console.timeEnd("fetchLesson-query"); + // console.log("cards", res.length); - if (res.length === 0) return { error: "Lesson not found" }; + if (res.length === 0) { + console.timeEnd("fetchLesson-total"); + return { error: "Lesson not found" }; + } + const row: any = res[0]; - console.log({ row }); + // console.log({ row }); + + console.time("fetchLesson-process"); const lesson = { id: lessonId, name: row.name, @@ -414,10 +424,11 @@ class DatabaseHandler { language: row.llang, cardCount: row.total_card_count, }; - // TODO IPA, prosody, senses... should we unify the format on the wikisource standard? + + // Process the cards + console.time("fetchLesson-json-processing"); const cards = res.map((row: any) => { - // TODO parse here...? - // console.log({ row }); + // JSON parsing is often expensive const sense_array = JSON.parse(row.senses_array); const senses = sense_array.map((s: any) => { const senses = JSON.parse(s.senses); @@ -457,6 +468,10 @@ class DatabaseHandler { }; return card; }); + console.timeEnd("fetchLesson-json-processing"); + + console.timeEnd("fetchLesson-process"); + console.timeEnd("fetchLesson-total"); return { ok: { lesson, cards } }; } fetchCard(cid: number, userid: number) { @@ -794,4 +809,5 @@ type ExpressionSearchParams = { type ExpressionType = "syllable" | "word" | "expression"; const db = new DatabaseHandler(); +export type { DatabaseHandler }; export default db; |